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.
Modelbahn/server/network.h

137 lines
3.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 <pthread.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"
#include "debug.h"
extern int next_sessionID;
class Session {
private:
int sessionID;
int randomID;
list<string> 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<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;
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_