You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
2.3 KiB
112 lines
2.3 KiB
|
|
#ifndef _NETWORK_H_
|
|
#define _NETWORK_H_
|
|
|
|
#include <UDPTCPNetwork.h>
|
|
#include <string>
|
|
#include <list>
|
|
#include <string>
|
|
#include <sys/time.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <netdb.h>
|
|
#include <errno.h>
|
|
#include <math.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/wait.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <syslog.h>
|
|
#include <pthread.h>
|
|
#include <sys/syscall.h>
|
|
|
|
using namespace std;
|
|
|
|
#include "modelbahn.h"
|
|
#include "json.h"
|
|
|
|
|
|
extern int next_sessionID;
|
|
|
|
|
|
class Session {
|
|
private:
|
|
int sessionID;
|
|
int randomID;
|
|
list<string> 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<Session*> sessions;
|
|
list<UNIX*> 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_
|