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

121 lines
2.6 KiB

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <UDPTCPNetwork.h>
#include "modelbahn.h"
void uprintf (UNIX *u, char *fmt,...);
static void sig_int(int); // signal handler
int running = 1;
Server *server = NULL;
Network *network = NULL;
int main (int argc, char **argv) {
int ret;
//
// setup signals
//
if (signal(SIGINT, sig_int) == SIG_ERR) {
fprintf (stderr, "%s:%d could not set signal for SIGINT\n", __FILE__, __LINE__);
return 0;
}
if (signal(SIGTERM, sig_int) == SIG_ERR) {
fprintf (stderr, "%s:%d could not set signal for SIGTERM\n", __FILE__, __LINE__);
return 0;
}
if (signal(SIGHUP, sig_int) == SIG_ERR) {
fprintf (stderr, "%s:%d could not set signal for SIGHUB\n", __FILE__, __LINE__);
return 0;
}
debug (0, "***************************************************");
debug (0, "* *");
debug (0, "* Modelbahn Server *");
debug (0, "* *");
debug (0, "***************************************************");
debug (0, "");
//////////////////////////////////////////////////////////////////////
//
// application startup
//
debug (0, "* application startup");
server = new Server();
network = new Network();
//////////////////////////////////////////////////////////////////////
//
// application loop
//
debug (0, "* application loop");
server->Start();
network->Start();
while (running) {
sleep (1);
}
//////////////////////////////////////////////////////////////////////
//
// application shutdown
//
while (server->isRunning() || network->isRunning());
debug (0, "* application shutdown");
delete network;
debug (0, "* deleted network");
delete server;
debug (0, "* deleted server");
debug (0, "* application exited");
return 0;
};
void uprintf (UNIX *u, char *fmt,...) {
va_list args;
char buffer[BUFFERSIZE];
va_start (args, fmt);
vsnprintf (buffer, (BUFFERSIZE-1), fmt, args);
va_end (args);
buffer[BUFFERSIZE-1] = 0;
u->Write(buffer, strlen (buffer));
};
static void sig_int(int sig) {
debug (0, "* signal:%d received", sig);
if (signal (SIGINT, sig_int) == SIG_ERR) {
fprintf (stderr, "%s:%d could not set up signal SIGINT\n", __FILE__, __LINE__);
exit (-1);
}
running = 0;
}
void timer_start(struct timeval *tv) {
gettimeofday(tv, NULL);
};
int timer_end(struct timeval *tv) {
struct timeval tvnow;
gettimeofday(&tvnow, NULL);
return ((tvnow.tv_sec - tv->tv_sec)*1000 + (tvnow.tv_usec - tv->tv_usec)/1000);
};