changed to buildin webserver

master
Steffen Pohle 1 month ago
parent bf6df35874
commit dda57ac381

@ -16,7 +16,7 @@ LDFLAGS_CGI= -lm -lc -lpthread -L/usr/local/lib -g -ggdb
DEPENDFILE=.depend DEPENDFILE=.depend
TARGET=modelbahn-server TARGET=modelbahn-server
SERVEROBJ=server.o network.o session.o server-loadsave.o \ SERVEROBJ=server.o network.o webserver.o session.o server-loadsave.o \
main.o sensor.o turnout.o railway.o interfaces.o locomotive.o \ main.o sensor.o turnout.o railway.o interfaces.o locomotive.o \
block.o interface.o interface-z21.o block.o interface.o interface-z21.o

@ -9,6 +9,7 @@
#include <signal.h> #include <signal.h>
#include "modelbahn.h" #include "modelbahn.h"
#include "webserver.h"
void uprintf (UNIX *u, char *fmt,...); void uprintf (UNIX *u, char *fmt,...);
static void sig_int(int); // signal handler static void sig_int(int); // signal handler
@ -16,6 +17,7 @@ int running = 1;
Server *server = NULL; Server *server = NULL;
Network *network = NULL; Network *network = NULL;
int simulation = 0; int simulation = 0;
int main (int argc, char **argv) { int main (int argc, char **argv) {

@ -20,8 +20,9 @@
using namespace std; using namespace std;
#define DEBUG_FILE "/tmp/modelbahn-server.log" #define DEBUG_FILE "/tmp/modelbahn-server.log"
#include "UDPTCPNetwork.h" #include <UDPTCPNetwork.h>
#include "webserver.h"
#include "server.h" #include "server.h"
#include "turnout.h" #include "turnout.h"
#include "sensor.h" #include "sensor.h"

@ -49,8 +49,6 @@ Network::~Network() {
void Network::ThreadProcess() { void Network::ThreadProcess() {
list<UNIX*>::iterator iteru;
UNIX *u;
int i = 0; int i = 0;
int timeout10s; int timeout10s;
@ -65,21 +63,7 @@ void Network::ThreadProcess() {
while (running) { while (running) {
// //
// check network // check network
webserver.Loop();
//
// server socket
ServerLoop();
//
// client socket
for (iteru = clients.begin(); iteru != clients.end(); iteru++) {
if (ClientLoop((UNIX*) *iteru) < 0) {
u = *iteru;
clients.remove(u);
delete u;
iteru = clients.begin();
}
}
gettimeofday (&tv, NULL); gettimeofday (&tv, NULL);
i = (tv.tv_sec-tv_loop.tv_sec) * 1000 + (tv.tv_usec - tv_loop.tv_usec) / 1000; i = (tv.tv_sec-tv_loop.tv_sec) * 1000 + (tv.tv_usec - tv_loop.tv_usec) / 1000;
@ -108,12 +92,11 @@ int Network::Start() {
mtx = PTHREAD_MUTEX_INITIALIZER; mtx = PTHREAD_MUTEX_INITIALIZER;
// //
// start socket server // start the webserver
// //
if (sockserver.Listen(UNIX_SOCKET_FILE) != 1) { webserver.SetupDocumentRoot("webinterface");
return 0; webserver.SetupPorts(1080, 1081);
} webserver.Start();
chmod(UNIX_SOCKET_FILE, 00666);
thread_running = 1; thread_running = 1;
pthread_attr_init (&attr); pthread_attr_init (&attr);
@ -129,8 +112,7 @@ int Network::Start() {
void Network::Stop() { void Network::Stop() {
sockserver.Close(); webserver.Stop();
unlink(UNIX_SOCKET_FILE);
} }
@ -146,17 +128,6 @@ void Network::ChangeListPushToAll(string changes) {
int Network::ServerLoop() {
UNIX *u = NULL;
if (sockserver.IsData(10)) {
u = sockserver.Accept();
clients.push_back(u);
}
return 1;
};
Session *Network::GetSession(int sid) { Session *Network::GetSession(int sid) {
Session *s = NULL; Session *s = NULL;
list<Session*>::iterator iter; list<Session*>::iterator iter;
@ -174,9 +145,8 @@ Session *Network::GetSession(int sid) {
}; };
int Network::ClientLoop(UNIX *client) { int Network::ClientLoop(JSONParse *jpin, JSONParse *jpout) {
char bufferin[BUFFERSIZE]; int rid, sid;
int len, rid, sid;
list<Session*>::iterator siter; list<Session*>::iterator siter;
Session *session = NULL; Session *session = NULL;
string value; string value;
@ -187,101 +157,89 @@ int Network::ClientLoop(UNIX *client) {
struct timeval timer; struct timeval timer;
timer_start(&timer); timer_start(&timer);
JSONParse jelement;
len = client->ReadTimeout(bufferin, BUFFERSIZE, 20); if (jpin == NULL || jpout == NULL) return 0;
if (len > 0 && len < BUFFERSIZE) { jpout->Clear();
JSONParse json;
JSONParse jsonout;
JSONParse jelement;
bufferin[len] = 0; // prevent reading behind the data
json.Set(bufferin); if (!jpin->GetValue("sid", &ssid)) {
if (!json.GetValue("sid", &ssid)) { debug ("jpin->GetValue error --> sid");
debug ("json.GetValue error --> sid"); return 0;
return 0; }
}
if (!json.GetValue("rid", &srid)) { if (!jpin->GetValue("rid", &srid)) {
debug ("json.GetValue error --> rid"); debug ("jpin->GetValue error --> rid");
return 0; return 0;
} }
// //
try { try {
rid = stoi (srid); rid = stoi (srid);
} }
catch (...) { catch (...) {
rid = 0; rid = 0;
debug ("rid error"); debug ("rid error");
} }
try { try {
sid = stoi (ssid); sid = stoi (ssid);
} }
catch (...) { catch (...) {
sid = 0; sid = 0;
debug ("sid error"); debug ("sid error");
} }
if (sid <= 0) { if (sid <= 0) {
debug ("sid not set, gettin new SID"); debug ("sid not set, gettin new SID");
session = new Session(rid); session = new Session(rid);
sid = session->GetSessionID(); sid = session->GetSessionID();
LockSessions(); LockSessions();
sessions.push_back(session); sessions.push_back(session);
UnLockSessions(); UnLockSessions();
} }
else { else {
LockSessions(); LockSessions();
// //
// search for session // search for session
session = NULL; session = NULL;
for (siter = sessions.begin(); siter != sessions.end(); siter++) { for (siter = sessions.begin(); siter != sessions.end(); siter++) {
if ((*siter)->GetRandomID() == rid && ((Session*)(*siter))->GetSessionID() == sid) { if ((*siter)->GetRandomID() == rid && ((Session*)(*siter))->GetSessionID() == sid) {
session = (*siter); session = (*siter);
break; break;
}
} }
UnLockSessions();
} }
UnLockSessions();
}
if (session) { if (session) {
JSONElement je; JSONElement je;
int retval; int retval;
retval = session->ProcessData(&json, &jsonout); retval = session->ProcessData(jpin, jpout);
len = BUFFERSIZE; if (retval) {
if (retval) { je.Clear();
je.Clear(); je.Set("success", 1);
je.Set("success", 1); jpout->AddObject(je);
jsonout.AddObject(je); je.Clear();
je.Clear(); je.Set("serverstatus", server->GetStatus());
je.Set("serverstatus", server->GetStatus()); jpout->AddObject(je);
jsonout.AddObject(je); je.Clear();
je.Clear(); je.Set("simulation", simulation);
je.Set("simulation", simulation); jpout->AddObject(je);
jsonout.AddObject(je);
}
else {
je.Clear();
je.Set("success", 0);
jsonout.AddObject(je);
je.Clear();
je.Set("serverstatus", "ERROR");
jsonout.AddObject(je);
}
s = jsonout.ToString();
client->Write((char*)s.c_str(), strlen(s.c_str()));
result = 1;
} }
else { else {
// no session found je.Clear();
result = 0; je.Set("success", 0);
jpout->AddObject(je);
je.Clear();
je.Set("serverstatus", "ERROR");
jpout->AddObject(je);
} }
result = 1;
} }
else if (len == 0) { else {
// no session found
result = 0; result = 0;
} }

@ -27,6 +27,7 @@
using namespace std; using namespace std;
#include "webserver.h"
#include "modelbahn.h" #include "modelbahn.h"
extern int next_sessionID; extern int next_sessionID;
@ -99,17 +100,13 @@ private:
void ThreadProcess(); void ThreadProcess();
pthread_mutex_t mtx; pthread_mutex_t mtx;
pthread_mutex_t mtxsessions; pthread_mutex_t mtxsessions;
SimpleWebServer webserver;
pthread_t thread; pthread_t thread;
int thread_running; int thread_running;
list<Session*> sessions; list<Session*> sessions;
list<UNIX*> clients;
Session *GetSession(int Sid); Session *GetSession(int Sid);
int ServerLoop(); // loop for network connection and call clientloop if needed
int ClientLoop(UNIX *u);
UNIX sockserver;
friend class Session; friend class Session;
public: public:
@ -126,6 +123,7 @@ public:
void ChangeListPushToAll (string changes); // adds JSON compat. change string too all clients void ChangeListPushToAll (string changes); // adds JSON compat. change string too all clients
int isRunning() { return thread_running; } int isRunning() { return thread_running; }
int ClientLoop(JSONParse *jpin, JSONParse *jpout);
protected: protected:
static void *ThreadEntry (void *This) { ((Network*)This)->ThreadProcess(); return NULL;}; static void *ThreadEntry (void *This) { ((Network*)This)->ThreadProcess(); return NULL;};
}; };

Loading…
Cancel
Save