#include #include #include #include #include #include #include #include #include #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 simulation = 0; int main (int argc, char **argv) { int i; // // 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; } if (signal(SIGUSR1, 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, ""); for (i = 1; i < argc; i++) { if (strcmp (argv[i], "-simulation") == 0) { simulation = 1; debug (0, "WARINING SIMULATION MODE ACTIVE. ALL COMMANDS WILL BE SEND"); debug (0, "TO THE INTERFACES. TURNOUT AND SENSORDATA WILL BE FORCED."); sleep (5); } } ////////////////////////////////////////////////////////////////////// // // application startup // debug (0, "* application startup"); server = new Server(); network = new Network(); ////////////////////////////////////////////////////////////////////// // // application loop // debug (0, "* application loop"); server->Start(); debug (0, "* server thread started"); network->Start(); debug (0, "* network thread started"); while (running) { usleep (1000000); } ////////////////////////////////////////////////////////////////////// // // 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_get(struct timeval *tv) { struct timeval tvnow; gettimeofday(&tvnow, NULL); return ((tvnow.tv_sec - tv->tv_sec)*1000 + (tvnow.tv_usec - tv->tv_usec)/1000); }; int timevaldiff (struct timeval *tv1, struct timeval *tv2) { return ((tv1->tv_sec - tv2->tv_sec)*1000 + (tv1->tv_usec - tv2->tv_usec)/1000); };