Transit Planner  1.0
An experiment on transfer patterns robustness
src/Server.h
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_SERVER_H_
00003 #define SRC_SERVER_H_
00004 
00005 #include <boost/shared_ptr.hpp>
00006 #include <boost/asio.hpp>
00007 #include <boost/thread.hpp>
00008 #include <boost/function.hpp>
00009 #include <string>
00010 #include <vector>
00011 #include <map>
00012 #include <set>
00013 #include "./Logger.h"
00014 #include "./TransferPatternRouter.h"
00015 
00016 using std::string;
00017 using std::set;
00018 using boost::asio::ip::tcp;
00019 using boost::shared_ptr;
00020 using boost::thread;
00021 using boost::function;
00022 
00023 class Server;
00024 class TransitNetwork;
00025 
00026 class Worker {
00027  public:
00028   Worker(const function<void(void)>& func, Server* server);
00029   void operator()();
00030  private:
00031   function<void(void)> _func;
00032   Server* _server;
00033 };
00034 
00035 class Server {
00036  public:
00037   Server(const int port, const string& dataDir,
00038          const string& workDir, const string& logPath);
00039   void loadGtfs(const string& path,
00040                 const int startTime, const int endTime);
00041   void loadGtfs(const vector<string>& paths,
00042                 const int startTime, const int endTime);
00043   void precompute();
00044   void precomputeHubs();
00045   void precomputeTransferPatterns();
00046   void run();
00047   TransitNetwork& network();
00048   TransitNetwork& scenario();
00049   void scenario(const TransitNetwork& scenario);
00050   bool scenarioSet();
00051   void scenarioSet(bool value);
00052   TransferPatternRouter& router();
00053   int maxWorkers() const;
00054   void maxWorkers(const int n);
00055   string dataDir() const;
00056   string workDir() const;
00057   int port() const;
00058   static string retrieveQuery(const string& request);
00059   static string retrieveCommand(const string& query);
00060   static StrStrMap retrieveArgs(const string& query, const string& workDir);
00061 
00062   void reserveWorker();
00063   void joinWorkers(const int numBusy = 0);
00064 
00065  private:
00066   void handleRequest(shared_ptr<tcp::socket> socket, Logger log);
00067   void releaseWorker();
00068 
00069   bool loadHubs(HubSet* hubs);
00070   void saveHubs(const HubSet& hubs);
00071   bool loadTransferPatternsDB(TransferPatternsDB* tpdb);
00072   void saveTransferPatternsDB(const TransferPatternsDB& tpdb);
00073   FRIEND_TEST(ServerTest, hubAndTPDBSerialization);
00074 
00075   friend class Worker;
00076 
00077   TransitNetwork _network;
00078   TransitNetwork _scenario;
00079   TransferPatternRouter _router;
00080   TransferPatternsDB _tpdb;
00081   bool _scenarioSet;
00082 
00083   int _port;
00084   string _dataDir;
00085   string _workDir;
00086   Logger _log;
00087   int _maxWorkers;
00088   int _activeWorkers;
00089   boost::shared_mutex _maxWorkerMutex;
00090   boost::shared_mutex _activeWorkerMutex;
00091 };
00092 
00093 
00094 #endif  // SRC_SERVER_H_
 All Classes