Functions
warand/main.cpp File Reference
#include <signal.h>
#include <syslog.h>
#include <sys/stat.h>
#include <QGpCoreTools.h>
#include <GeopsyCore.h>
#include "FFTW.h"
#include "ProcessUnit.h"
#include "warandVersion.h"
#include "warandInstallPath.h"

Functions

void crashed (int)
void errorExit ()
ApplicationHelphelp ()
void interrupted (int)
int main (int argc, char **argv)
 PACKAGE_INFO (warand, WARAND)

Function Documentation

void crashed ( int  )

References QGpCoreTools::endl(), and errorExit().

{
  App::stream() << "received SEGV or ABRT signal" << endl;
  errorExit();
}
void errorExit ( )

Referenced by crashed(), and main().

{
  Leds::timer(0, 100, 100);
  exit(2);
}
void interrupted ( int  )

References QGpCoreTools::endl().

{
  App::stream() << "received TERM or INTERRUPT signal" << endl;
  QCoreApplication::instance()->quit();
}
int main ( int  argc,
char **  argv 
)

References ProcessUnit::addCoordinate(), ProcessUnit::clearCoordinates(), crashed(), QGpCoreTools::endl(), errorExit(), help(), interrupted(), ProcessUnit::setServer(), QGpCoreTools::CoreApplicationPrivate::setStream(), QGpCoreTools::CoreApplicationPrivate::setStreamPrefix(), ProcessUnit::setStreams(), and QGpCoreTools::tr().

{
  CoreApplication a(argc, argv, help, false);
  GeopsyCoreEngine gp;
  new FFTW;

  // Options
  bool doFork=true;
  quint16 slPort=18000;
  QString slServer="localhost";
  QString slStreams="WA_WAU[0-9]{2}:...";
  QString basePath("/mnt/usbdrive/waran/process");
  QString stationName("unamed");
  // Check arguments
  int i, j=1;
  for(i=1; i<argc; i++) {
    QByteArray arg=argv[i];
    if(arg[0]=='-') {
      if(arg=="-sl-port") {
        CoreApplication::checkOptionArg(i, argc, argv);
        slPort=atoi(argv[i]);
      } else if(arg=="-sl-server") {
        CoreApplication::checkOptionArg(i, argc, argv);
        slServer=argv[i];
      } else if(arg=="-sl-streams") {
        CoreApplication::checkOptionArg(i, argc, argv);
        slStreams=argv[i];
      } else if(strcmp(arg, "-s")==0) {
        CoreApplication::checkOptionArg(i, argc, argv, true);
        stationName=argv[i];
      } else if(strcmp(arg, "-b")==0) {
        CoreApplication::checkOptionArg(i, argc, argv, true);
        basePath=argv[i];
      } else if(strcmp(arg, "-N")==0) {
        doFork=false;
      } else {
        syslog(LOG_ERR, "warand: bad option %s, see -help\n", argv[i]);
        errorExit();
      }
    } else {
      argv[j++]=argv[i];
    }
  }
  if(j < argc) {
    argv[j]=0;
    argc=j;
  }
  struct stat sInfo;
  int sErr;
  FILE * fpid;

  if(doFork) {
    sErr=stat("/var/run/warand.pid", &sInfo);
    if(sErr==0) {
      syslog(LOG_ERR, "warand is already running (stop it and remove /var/run/warand.pid)\n");
    }

    // Our process ID and Session ID
    pid_t pid, sid;
    // Fork off the parent process
    pid=fork();
    if(pid < 0) {
      syslog(LOG_ERR, "Cannot fork off the parent process\n");
      errorExit();
    }
    // If we got a good PID, then we can exit the parent process.
    if(pid > 0) {
      // Write pid to /var/run/warangpsd.pid
      fpid=fopen("/var/run/warand.pid","wt");
      if(!fpid) {
        syslog(LOG_ERR, "Cannot create /var/run/warand.pid\n");
        errorExit();
      }
      fprintf(fpid, "%u\n", pid);
      fclose(fpid);
      exit(0);
    }
    // Change the file mode mask
    umask(0);
    // Open any logs here
    openlog ("warand", LOG_NDELAY | LOG_PID, LOG_DAEMON);
    // Create a new SID for the child process
    sid=setsid();
    if(sid < 0) {
      syslog(LOG_ERR, "Cannot create a new SID for the child process\n");
      errorExit();
    }
    // Close out the standard file descriptors
    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);
  }

  signal(SIGTERM, interrupted);
  signal(SIGINT, interrupted);
  signal(SIGSEGV, crashed);
  signal(SIGABRT, crashed);

  QString logFile=basePath;
  logFile+="/";
  logFile+=stationName;
  logFile+=".log";
  a.setStream(new FileStream(logFile));
  a.setStreamPrefix("%t: ");
  App::stream() << tr("start new daemon with pid %1").arg(getpid()) << endl;

  ProcessUnit p;
  p.setServer(slServer, slPort);
  p.setStreams(slStreams);
  p.clearCoordinates();
  p.addCoordinate("M2_S01", Point(-48, 9));
  p.addCoordinate("M2_S02", Point(-31, 39));
  p.addCoordinate("M2_S03", Point(1, 50));
  p.addCoordinate("M2_S04", Point(33, 39));
  p.addCoordinate("M2_S05", Point(51, 9));
  p.addCoordinate("M2_S06", Point(45, -25));
  p.addCoordinate("M2_S07", Point(18, -46));
  p.addCoordinate("M2_S08", Point(-16, 47));
  p.addCoordinate("M2_S09", Point(-42, -24));
  p.addCoordinate("M2_S10", Point(0, 0));

  int appReturn=a.exec();
  App::stream() << tr("stop daemon with pid %1").arg(getpid()) << endl;
  return appReturn;
}
PACKAGE_INFO ( warand  ,
WARAND   
)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines