#ifndef _NETWORK_H_ #define _NETWORK_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #include "modelbahn.h" #include "json.h" #include "debug.h" extern int next_sessionID; class Session { private: int sessionID; int randomID; list changes; pthread_mutex_t mtxchanges; // void LockChanges() { debug (0," ****** %s ", __FUNCTION__); pthread_mutex_lock(&mtxchanges); }; // void UnLockChanges() { pthread_mutex_unlock(&mtxchanges); debug (0," ****** %s ", __FUNCTION__); }; void LockChanges() { pthread_mutex_lock(&mtxchanges); }; void UnLockChanges() { pthread_mutex_unlock(&mtxchanges); }; void AddJSONRailway(JSONParse *jp); void DelJSONRailway(JSONParse *jp); void AddJSONTurnout(JSONParse *jp); void DelJSONTurnout(JSONParse *jp); void SetJSONTurnout(JSONParse *jp); void AddJSONSensor(JSONParse *jp); void DelJSONSensor(JSONParse *jp); void SetJSONSensorActive(JSONParse *jp); void AddJSONInterface(JSONParse *jp); void DelJSONInterface(JSONParse *jp); void AddJSONLocomotive(JSONParse *jp); void DelJSONLocomotive(JSONParse *jp); void SetJSONLocomotive(JSONParse *jp); void SetJSONLocoDest(JSONParse *jp); void SetJSONLocoAssign(JSONParse *jp); void SetJSONLocoReset(JSONParse *jp); void SetJSONLocoMan(JSONParse *jp); void SetJSONLocoAutoMan(JSONParse *jp); void SetJSONLocoAuto(JSONParse *jp); void SetJSONLocoAutoRand(JSONParse *jp); void SetJSONLocoAutoShed(JSONParse *jp); void BlockJSONOff(JSONParse *jp); void BlockJSONClear(JSONParse *jp); void AddJSONBlock(JSONParse *jp); void DelJSONBlock(JSONParse *jp); public: Session(int rid); ~Session(); // changed which need to be send to the client // void ChangeListPush(string chng); // push ChangeList (String must be JSON format) JSONElement ChangeListGet(); // get ChangeList as JSON and clear list void ChangesListClear(); // clear out Change list int GetSessionID() { return sessionID; }; int GetRandomID() { return randomID; }; int SendData(UNIX *u, string data); int ProcessData(JSONParse *jin, JSONParse *jout); }; class Network { private: void ThreadProcess(); pthread_mutex_t mtx; pthread_mutex_t mtxsessions; pthread_t thread; int thread_running; list sessions; list clients; Session *GetSession(int Sid); int ServerLoop(); // loop for network connection and call clientloop if needed int ClientLoop(UNIX *u); UNIX sockserver; friend class Session; public: Network(); ~Network(); void Lock() { pthread_mutex_lock(&mtx); }; void UnLock() { pthread_mutex_unlock(&mtx); }; void LockSessions() { pthread_mutex_lock(&mtxsessions); }; void UnLockSessions() { pthread_mutex_unlock(&mtxsessions); }; int Start(); void Stop(); void ChangeListPushToAll (string changes); // adds JSON compat. change string too all clients int isRunning() { return thread_running; } protected: static void *ThreadEntry (void *This) { ((Network*)This)->ThreadProcess(); return NULL;}; }; #endif // _NETWORK_H_