#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 using namespace std; #include "modelbahn.h" #include "json.h" extern int next_sessionID; class Session { private: int sessionID; int randomID; list changes; 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 AddJSONInterface(JSONParse *jp); void DelJSONInterface(JSONParse *jp); void AddJSONLocomotive(JSONParse *jp); void DelJSONLocomotive(JSONParse *jp); void SetJSONLocomotive(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_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; void _ChangeListPushToAll (string changes); // not thread save friend class Session; public: Network(); ~Network(); int LockThread(); int UnLockThread(); 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_