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/server.cc

106 lines
2.0 KiB

#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>
#include "modelbahn.h"
#include "server.h"
int Server::Start() {
int err;
pthread_attr_t attr;
mtx = { 0 };
mtx = PTHREAD_MUTEX_INITIALIZER;
thread_running = 1;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
err = pthread_create (&thread, &attr, ThreadEntry, this);
if (err != 0) {
debug (DEBUG_ERROR, (char*)"%s(%s:%d) pthread_create errror: %s", __FUNCTION__, __FILE__, __LINE__, strerror (errno));
running = 0;
return 0;
}
return 1;
};
Server::Server() {
thread = 0;
thread_running = 0;
railways.SetSize(200, 200);
Load ();
};
Server::~Server() {
if (IsChanged()) Save();
};
void Server::ThreadProcess() {
int i = 0;
while (running) {
interfaces.Loop();
turnouts.Loop();
usleep (25000);
}
debug (0, "Server::ThreadProcess Finished");
thread_running = 0;
};
void Server::LockThread() {
debug(DEBUG_INFO, "%s:%d Server::LockThread", __FILE__, __LINE__);
pthread_mutex_lock (&mtx);
};
void Server::UnLockThread() {
debug (DEBUG_INFO, "%s:%d Server::UnLockThreads", __FILE__, __LINE__);
pthread_mutex_unlock (&mtx);
};
//
// return JSONObject with all data
void Server::GetJSONAll(JSONParse *json) {
debug (DEBUG_INFO, "* Track::GetJSONAll data");
if (json == NULL) return;
railways.GetJSONAll(json);
interfaces.GetJSONAll(json);
sensors.GetJSONAll(json);
locomotives.GetJSONAll(json);
turnouts.GetJSONAll(json);
}
bool Server::IsChanged() {
if (railways.IsChanged() ||
interfaces.IsChanged() ||
locomotives.IsChanged() ||
sensors.IsChanged() ||
turnouts.IsChanged()) return true;
else return false;
}