server.C

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #include <archon/x3d/server/server.H>
00021 #include <archon/x3d/server/parse_xml.H>
00022 
00023 //#include <archon/x3d/server/font.H>
00024 
00025 namespace Archon
00026 {
00027   namespace X3D
00028   {
00029     Ref<Server> Server::get(string name, double frameRate, Logger *logger,
00030                             bool loadScene, Uri sceneUrl, bool progressiveLoad)
00031     {
00032       Ref<X3D::Server> server = new X3D::Server(name, frameRate, logger);
00033       Ref<Web::Client> webClient = Web::Client::get(name, "0");
00034 
00035       /*
00036       {
00037         Ref<X3D::FontServer> fontServer = new X3D::FontServer(server, fontDirs);
00038         server->setFontServer(fontServer);
00039       }
00040       */
00041       {
00042         Ref<X3D::Loader> loader = new X3D::Loader(server, webClient);
00043         server->setLoader(loader);
00044       }
00045 
00046       // Load
00047       Uri rootUrl;
00048       if(loadScene) rootUrl = sceneUrl;
00049       Ref<X3D::Scene> rootScene = new X3D::Scene(server, rootUrl);
00050       Ref<X3D::Group> rootGroup = new X3D::Group(Ref<X3D::ExecutionContext>(rootScene));
00051 
00052       /*
00053       if(progressiveLoad)
00054       {
00055         rootScene->setRootGroup(rootGroup);
00056         server->setRootScene(rootScene);
00057         load(rootGroup);
00058       }
00059       */
00060 
00061       if(loadScene)
00062       {
00063         Ref<Web::Client::Response> response =
00064           webClient->request(Web::Client::Request::get(rootUrl));
00065         X3D::XML::parse(rootScene, rootGroup, response, progressiveLoad);
00066       }
00067       rootScene->setRootGroup(rootGroup);
00068       server->setRootScene(rootScene);
00069 
00070       return server;
00071     }
00072 
00073     Server::Server(string name, double frameRate, Logger *logger):
00074       name(name), logger(logger), progressiveLoad(false), frameRate(frameRate)
00075     {
00076     }
00077 
00078     Server::~Server()
00079     {
00080       logger->log("No memory leak this time!");
00081     }
00082 
00083     void Server::refForwardDestroy()
00084     {
00085       //fontServer.reset();
00086       listener.reset();
00087       rootScene.reset();
00088       loader.reset(); // Should be last, since it waits for all jobs to complete
00089     }
00090 
00091     Ref<Loader> Server::getLoader() const
00092     {
00093       Mutex::Lock l(mutex);
00094       return loader;
00095     }
00096 
00097     void Server::setLoader(Ref<Loader> v)
00098     {
00099       Mutex::Lock l(mutex);
00100       loader = v;
00101     }
00102 
00103     /*
00104     Ref<FontServer> Server::getFontServer() const
00105     {
00106       Mutex::Lock l(mutex);
00107       return fontServer;
00108     }
00109 
00110     void Server::setFontServer(Ref<FontServer> v)
00111     {
00112       Mutex::Lock l(mutex);
00113       fontServer = v;
00114     }
00115     */
00116 
00117     Ref<Server::Listener> Server::getListener() const
00118     {
00119       Mutex::Lock l(mutex);
00120       return listener;
00121     }
00122 
00123     void Server::setListener(Ref<Listener> v)
00124     {
00125       Mutex::Lock l(mutex);
00126       listener = v;
00127     }
00128 
00129     Ref<Scene> Server::getRootScene() const
00130     {
00131       Mutex::Lock l(scenegraphMutex);
00132       return rootScene;
00133     }
00134 
00135     void Server::setRootScene(Ref<Scene> c)
00136     {
00137       if(c) c->realize();
00138       Mutex::Lock l(scenegraphMutex);
00139       rootScene = c;
00140     }
00141 
00142     void Server::setProgressiveLoad(bool v)
00143     {
00144       Mutex::Lock l(mutex);
00145       progressiveLoad = v;
00146     }
00147 
00148     bool Server::getProgressiveLoad() const
00149     {
00150       Mutex::Lock l(mutex);
00151       return progressiveLoad;
00152     }
00153 
00154     string Server::getName() const
00155     {
00156       Mutex::Lock l(mutex);
00157       return name;
00158     }
00159 
00160     Logger *Server::getLogger() const
00161     {
00162       Mutex::Lock l(mutex);
00163       return logger;
00164     }
00165 
00166     /*
00167     string Server::dumpScenegraph(ostream &out)
00168     {
00169       Mutex::Lock l(scenegraphMutex);
00170       SceneExporter::exportToVrml(RefArg<Server>(this, RefThisTag()), out);
00171     }
00172     */
00173 
00174     Time Server::getNextTimeStamp()
00175     {
00176       Mutex::Lock l(mutex);
00177       ++previousTimeStamp;
00178       Time t = Time::now();
00179       if(t > previousTimeStamp) previousTimeStamp = t;
00180       return previousTimeStamp;
00181     }
00182 
00183     void Server::add(ExecutionContext *c)
00184     {
00185       Mutex::Lock l(contextsMutex);
00186       contexts.push_back(c);
00187     }
00188 
00189     void Server::remove(ExecutionContext *c)
00190     {
00191       Mutex::Lock l(contextsMutex);
00192       contexts.remove(c);
00193     }
00194 
00195     void Server::tick()
00196     {
00197       list<Ref<ExecutionContext> > t;
00198       {
00199         Mutex::Lock l(contextsMutex);
00200         list<ExecutionContext *>::iterator i = contexts.begin();
00201         while(i != contexts.end())
00202         {
00203           Ref<ExecutionContext> r(*i, RefSafeIncTag());
00204           if(r) t.push_back(r);
00205           ++i;
00206         }
00207       }
00208       {
00209         list<Ref<ExecutionContext> >::iterator i = t.begin();
00210         while(i != t.end())
00211         {
00212           (*i)->tick();
00213           ++i;
00214         }
00215       }
00216     }
00217   }
00218 }

Generated on Sun Jul 30 22:55:50 2006 for Archon by  doxygen 1.4.4