#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "rcs.hh"
#include "emc.hh"
#include "emc_nml.hh"
#include "emcglb.h"
#include "inifile.hh"
#include "rcs_print.hh"
#include "nml_oi.hh"
#include "timer.hh"
#include "nml_srv.hh"
static int tool_channels = 1;
static int iniLoad(const char *filename)
{
IniFile inifile;
const char *inistring;
if (inifile.Open(filename) == false) {
return -1;
}
if (NULL != (inistring = inifile.Find("DEBUG", "EMC"))) {
if (1 != sscanf(inistring, "%x", &emc_debug)) {
emc_debug = 0;
}
} else {
emc_debug = 0;
}
if (emc_debug & EMC_DEBUG_RCS) {
set_rcs_print_flag(PRINT_EVERYTHING);
max_rcs_errors_to_print = -1;
}
if (NULL != (inistring = inifile.Find("NML_FILE", "EMC"))) {
strcpy(emc_nmlfile, inistring);
} else {
}
inifile.Find(&tool_channels,"TOOL_CHANNELS","EMC");
inifile.Close();
return 0;
}
static void daemonize()
{
pid_t pid = fork();
if (pid < 0) {
perror("daemonize: fork()");
} else if (pid) {
_exit(0);
}
if(setsid() < 0)
perror("daemonize: setsid()");
signal(SIGHUP,SIG_IGN);
pid=fork();
if (pid < 0) {
perror("daemonize: fork() 2");
} else if (pid) {
_exit(0);
}
}
static RCS_CMD_CHANNEL *emcCommandChannel = NULL;
static RCS_STAT_CHANNEL *emcStatusChannel = NULL;
static NML *emcErrorChannel = NULL;
static RCS_CMD_CHANNEL *toolCommandChannel = NULL;
static RCS_STAT_CHANNEL *toolStatusChannel = NULL;
int main(int argc, char *argv[])
{
double start_time;
if (0 != emcGetArgs(argc, argv)) {
rcs_print_error("Error in argument list\n");
exit(1);
}
iniLoad(emc_inifile);
set_rcs_print_destination(RCS_PRINT_TO_NULL);
rcs_print("after iniLoad()\n");
start_time = etime();
while (fabs(etime() - start_time) < 10.0 &&
(emcCommandChannel == NULL || emcStatusChannel == NULL
|| (tool_channels && (toolCommandChannel == NULL || toolStatusChannel == NULL))
|| emcErrorChannel == NULL)
) {
if (NULL == emcCommandChannel) {
rcs_print("emcCommandChannel==NULL, attempt to create\n");
emcCommandChannel =
new RCS_CMD_CHANNEL(emcFormat, "emcCommand", "emcsvr",
emc_nmlfile);
}
if (NULL == emcStatusChannel) {
rcs_print("emcStatusChannel==NULL, attempt to create\n");
emcStatusChannel =
new RCS_STAT_CHANNEL(emcFormat, "emcStatus", "emcsvr",
emc_nmlfile);
}
if (NULL == emcErrorChannel) {
emcErrorChannel =
new NML(nmlErrorFormat, "emcError", "emcsvr", emc_nmlfile);
}
if (tool_channels) {
if (NULL == toolCommandChannel) {
toolCommandChannel =
new RCS_CMD_CHANNEL(emcFormat, "toolCmd", "emcsvr",
emc_nmlfile);
}
if (NULL == toolStatusChannel) {
toolStatusChannel =
new RCS_STAT_CHANNEL(emcFormat, "toolSts", "emcsvr",
emc_nmlfile);
}
}
if (!emcCommandChannel->valid()) {
delete emcCommandChannel;
emcCommandChannel = NULL;
}
if (!emcStatusChannel->valid()) {
delete emcStatusChannel;
emcStatusChannel = NULL;
}
if (!emcErrorChannel->valid()) {
delete emcErrorChannel;
emcErrorChannel = NULL;
}
if (tool_channels) {
if (!toolCommandChannel->valid()) {
delete toolCommandChannel;
toolCommandChannel = NULL;
}
if (!toolStatusChannel->valid()) {
delete toolStatusChannel;
toolStatusChannel = NULL;
}
}
esleep(0.200);
}
set_rcs_print_destination(RCS_PRINT_TO_STDERR);
if (NULL == emcCommandChannel) {
emcCommandChannel =
new RCS_CMD_CHANNEL(emcFormat, "emcCommand", "emcsvr",
emc_nmlfile);
}
if (NULL == emcStatusChannel) {
emcStatusChannel =
new RCS_STAT_CHANNEL(emcFormat, "emcStatus", "emcsvr",
emc_nmlfile);
}
if (NULL == emcErrorChannel) {
emcErrorChannel =
new NML(nmlErrorFormat, "emcError", "emcsvr", emc_nmlfile);
}
if (tool_channels) {
if (NULL == toolCommandChannel) {
toolCommandChannel =
new RCS_CMD_CHANNEL(emcFormat, "toolCmd", "emcsvr",
emc_nmlfile);
}
if (NULL == toolStatusChannel) {
toolStatusChannel =
new RCS_STAT_CHANNEL(emcFormat, "toolSts", "emcsvr",
emc_nmlfile);
}
}
daemonize();
run_nml_servers();
return 0;
}