nmxptool_ew.c 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*! \file
 *
 * \brief Earthworm support for Nanometrics Protocol Tool
 *
 * Author:
 * 	Matteo Quintiliani
 * 	Istituto Nazionale di Geofisica e Vulcanologia - Italy
 *	quintiliani@ingv.it
 *
 * $Id $
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "config.h"
#include "nmxp.h"
20
#include "nmxptool_getoptlong.h"
21

Matteo Quintiliani's avatar
Matteo Quintiliani committed
22 23 24 25 26 27 28
#ifdef HAVE_EARTHWORMOBJS
/* Earthworm includes */
#include <earthworm.h>
#include <kom.h>
#include <transport.h>
#include <trace_buf.h>

29 30
#include "nmxptool_ew.h"

31 32
NMXPTOOL_EW_ERR_MSG nmxptool_ew_err_msg[NMXPTOOL_EW_ERR_MAXVALUE] = {
    { NMXPTOOL_EW_ERR_NULL, ""} ,
33 34
    { NMXPTOOL_EW_ERR_RECVDATA, "Error receiving data." },
    { NMXPTOOL_EW_ERR_TERMREQ, "Terminating on request." }
35 36
};

Matteo Quintiliani's avatar
Matteo Quintiliani committed
37

38
#define MAXMESSAGELEN   160     /* Maximum length of a status or error  */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
39
/*   message.                           */
40
#define MAXRINGNAMELEN  28      /* Maximum length of a ring name.       */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
41
/* Should be defined by kom.h           */
42
#define MAXMODNAMELEN   30      /* Maximum length of a module name      */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
43
/* Should be defined by kom.h           */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
44
#define MAXADDRLEN      80      /* Length of NaqsServer hostname/address  */
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

SHM_INFO      regionOut;         /* Shared memory region                 */
pid_t         myPid;             /* Process ID                           */

char  ringName[MAXRINGNAMELEN];  /* Name of destination ring for data    */
long  ringKey;                   /* Key to output shared memory region   */
char  myModName[MAXMODNAMELEN];  /* Name of module instance              */
int   forcetracebuf = 0;         /* Switch to force TRACEBUF             */

unsigned char myModId;           /* ID of this module                    */
unsigned char myInstId;          /* Installation running this module     */
unsigned char typeError;         /* Error message type                   */
unsigned char typeHeartbeat;     /* Heartbeat message type               */
unsigned char typeWaveform;      /* Waveform message type TRACEBUF       */
unsigned char typeWaveform2 = 0; /* Waveform message type TRACEBUF2      */

MSG_LOGO      hrtLogo;           /* Heartbeat message logo               */
MSG_LOGO      waveLogo;          /* Waveform message logo                */
MSG_LOGO      errLogo;           /* Error message logo                   */

int           heartbeatInt;      /* Heartbeat interval (seconds)         */
int           logSwitch;         /* 1 -> write log, 0 -> no log          */
67 68 69 70
				/* 2 -> write module log but not stderr/stdout */

time_t timeNow;
time_t timeLastBeat = 0;
71

Matteo Quintiliani's avatar
Matteo Quintiliani committed
72 73 74
void nmxptool_ew_attach() {
    /* Attach to Output transport ring */
    tport_attach (&regionOut, ringKey);
75 76
    logit ("t", "%s version %s\n",
	    NMXP_LOG_STR(PACKAGE_NAME), NMXP_LOG_STR(PACKAGE_VERSION));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
77 78 79 80
}

void nmxptool_ew_detach() {
    tport_detach(&regionOut);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
81
    logit("t","%s terminated\n", NMXP_LOG_STR(PACKAGE_NAME));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
82 83
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
84

Matteo Quintiliani's avatar
Matteo Quintiliani committed
85
int nmxptool_ew_pd2ewring (NMXP_DATA_PROCESS *pd, SHM_INFO *pregionOut, MSG_LOGO *pwaveLogo) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
86 87 88 89 90 91 92 93 94 95
    TracePacket tbuf;
    int tracebuf2 = 0;   /* TRACEBUF2 => 0: none, 1: available, 2: populated */
    int len;
    int32_t *samples;
    int i;

    /* TRACE_HEADER and TRACE2_HEADER are the same size */
    memset (&tbuf, 0, sizeof(TRACE_HEADER));

    /* Create a TRACEBUF2 message if supported */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
96
#ifdef TRACE2_STA_LEN
Matteo Quintiliani's avatar
Matteo Quintiliani committed
97 98 99 100 101 102 103 104 105
    tracebuf2 = 1;

    if ( ! forcetracebuf ) {
	tbuf.trh2.pinno = 0;
	tbuf.trh2.nsamp = pd->nSamp;

	tbuf.trh2.starttime = pd->time;
	tbuf.trh2.samprate = pd->sampRate;
	tbuf.trh2.endtime = (tbuf.trh2.starttime +
106
		(((double) tbuf.trh2.nsamp - 1.0) / (double) tbuf.trh2.samprate));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
107

108 109 110
	strncpy(tbuf.trh2.net, pd->network, TRACE2_NET_LEN);
	strncpy(tbuf.trh2.sta, pd->station, TRACE2_STA_LEN);
	strncpy(tbuf.trh2.chan, pd->channel, TRACE2_CHAN_LEN);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
111

112
	strncpy(tbuf.trh2.loc, (pd->location[0]==0)?  LOC_NULL_STRING : ( strcmp(pd->location, DEFAULT_NULL_LOCATION)==0? LOC_NULL_STRING : pd->location ), 2);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
113 114 115 116 117

	tbuf.trh2.version[0] = TRACE2_VERSION0;
	tbuf.trh2.version[1] = TRACE2_VERSION1;

	/* The decoding always produces 32-bit integers in host byte order */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
118
#ifdef _INTEL
Matteo Quintiliani's avatar
Matteo Quintiliani committed
119
	strcpy(tbuf.trh2.datatype, "i4");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
120 121
#endif
#ifdef _SPARC
Matteo Quintiliani's avatar
Matteo Quintiliani committed
122
	strcpy(tbuf.trh2.datatype, "s4");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
123
#endif
Matteo Quintiliani's avatar
Matteo Quintiliani committed
124 125 126 127 128

	tbuf.trh2.quality[0] = 100; /* TODO */
	tbuf.trh2.quality[1] = 0;

	tracebuf2 = 2;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
129 130
    }
#endif
Matteo Quintiliani's avatar
Matteo Quintiliani committed
131 132 133 134 135 136 137 138 139

    if ( tracebuf2 != 2 ) {
	/* Create a TRACEBUF message otherwise */
	tbuf.trh.pinno = 0;
	tbuf.trh.nsamp = pd->nSamp;

	tbuf.trh.starttime = pd->time;
	tbuf.trh.samprate = pd->sampRate;
	tbuf.trh.endtime = (tbuf.trh.starttime +
140
		(((double) tbuf.trh.nsamp - 1.0) / (double) tbuf.trh.samprate));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
141

142 143 144
	strncpy(tbuf.trh.net, pd->network, TRACE_NET_LEN);
	strncpy(tbuf.trh.sta, pd->station, TRACE_STA_LEN);
	strncpy(tbuf.trh.chan, pd->channel, TRACE_CHAN_LEN);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
145 146

	/* The decoding always produces 32-bit integers in host byte order */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
147
#ifdef _INTEL
Matteo Quintiliani's avatar
Matteo Quintiliani committed
148
	strcpy(tbuf.trh.datatype, "i4");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
149 150
#endif
#ifdef _SPARC
Matteo Quintiliani's avatar
Matteo Quintiliani committed
151
	strcpy(tbuf.trh.datatype, "s4");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
152
#endif
Matteo Quintiliani's avatar
Matteo Quintiliani committed
153 154 155

	tbuf.trh.quality[0] = 100; /* TODO */
	tbuf.trh.quality[1] = 0;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
156
    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
157 158


Matteo Quintiliani's avatar
Matteo Quintiliani committed
159
    /* TODO : all of the samples
Matteo Quintiliani's avatar
Matteo Quintiliani committed
160
       should always fit into a single TracePacket if MAX_TRACEBUF_SIZ
Matteo Quintiliani's avatar
Matteo Quintiliani committed
161 162 163 164
       remains defined in Trace_buf.h as 4096 or greater
       17 * 59 = 1003 samples = 4012 bytes
       4012 + 64 = 4076 < 4096
     */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
165 166 167 168 169 170 171 172 173 174 175

    samples = (int32_t *) ((char *)&tbuf + sizeof(TRACE_HEADER));
    for(i=0; i < pd->nSamp; i++) {
	samples[i] = pd->pDataPtr[i];
    }

    len = (pd->nSamp * sizeof(int32_t)) + sizeof(TRACE_HEADER);

    /* Set the approriate TRACE type in the logo */
    if ( tracebuf2 == 2 ) {
	if ( typeWaveform2 == 0 ) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
176
	    logit("et", "%s: Error - created TRACE2_HEADER but TYPE_TRACEBUF2 is unknown\n", NMXP_LOG_STR(PACKAGE_NAME));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
177 178 179 180 181 182
	    return EW_FAILURE;
	} else {
	    pwaveLogo->type = typeWaveform2;
	}
    } else {
	pwaveLogo->type = typeWaveform;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
183
    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
184 185

    if ( tport_putmsg( pregionOut, pwaveLogo, len, (char*)&tbuf ) != PUT_OK ) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
186
	logit("et", "%s: Error sending message via transport.\n", NMXP_LOG_STR(PACKAGE_NAME));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
187
	return EW_FAILURE;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
188
    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
189 190

    return EW_SUCCESS;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
191
}				/* End of nmxptool_ew_pd2ewring() */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
192

193 194 195

int nmxptool_ew_nmx2ew(NMXP_DATA_PROCESS *pd) {
    int ret;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
196
    ret = nmxptool_ew_pd2ewring (pd, &regionOut, &waveLogo);
197 198
    return ret;
}
Matteo Quintiliani's avatar
Matteo Quintiliani committed
199

Matteo Quintiliani's avatar
Matteo Quintiliani committed
200 201 202


/***************************************************************************
203
 * nmxptool_ew_configure():
Matteo Quintiliani's avatar
Matteo Quintiliani committed
204 205 206
 * Process configuration parameters.
 *
 ***************************************************************************/
207
void nmxptool_ew_configure (char ** argvec, NMXPTOOL_PARAMS *params) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
208 209 210 211 212

    /* Initialize name of log-file & open it */
    logit_init (argvec[1], 0, 512, 1);

    /* Read module config file */
213
    if ( nmxptool_ew_proc_configfile (argvec[1], params) == EW_FAILURE ) {
214
	logit("et", "%s: configure() failed \n", argvec[0]);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
215 216 217 218 219
	exit (EW_FAILURE);
    }

    /* Read node configuration info */
    if ( GetLocalInst( &myInstId) != 0 ) {
220
	logit("et", "%s: Error getting myInstId.\n", PACKAGE_NAME );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
221 222 223 224 225
	exit (EW_FAILURE);
    }

    /* Lookup the ring key */
    if ((ringKey = GetKey (ringName) ) == -1) {
226
	logit("et",
227
		"%s:  Invalid ring name <%s>; exitting!\n", PACKAGE_NAME, ringName);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
228 229 230 231 232
	exit (EW_FAILURE);
    }

    /* Look up message types of interest */
    if (GetType ("TYPE_HEARTBEAT", &typeHeartbeat) != 0) {
233
	logit("et", 
234
		"%s: Invalid message type <TYPE_HEARTBEAT>; exitting!\n", PACKAGE_NAME);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
235 236 237
	exit (EW_FAILURE);
    }
    if (GetType ("TYPE_ERROR", &typeError) != 0) {
238
	logit("et", 
239
		"%s: Invalid message type <TYPE_ERROR>; exitting!\n", PACKAGE_NAME);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
240 241 242 243
	exit (EW_FAILURE);
    }

    if (GetType ("TYPE_TRACEBUF", &typeWaveform) != 0) {
244
	logit("et", 
245
		"%s: Invalid message type <TYPE_TRACEBUF>; exitting!\n", PACKAGE_NAME);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	exit (EW_FAILURE);
    }

    /* No GetType error checking as this type will not exist in all versions */
    GetType ("TYPE_TRACEBUF2", &typeWaveform2);

    /* Set up logos for outgoing messages */
    hrtLogo.instid = myInstId;
    hrtLogo.mod    = myModId;
    hrtLogo.type   = typeHeartbeat;

    errLogo.instid = myInstId;
    errLogo.mod    = myModId;
    errLogo.type   = typeError;

    waveLogo.instid = myInstId;
    waveLogo.mod    = myModId;
    waveLogo.type   = 0;  /* This gets set to the appropriate type later */

    /* Get my process ID so I can let statmgr restart me */
    myPid = getpid();

    logit ("et" , "%s(%s): Read command file <%s>\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
269
	    NMXP_LOG_STR(argvec[0]), NMXP_LOG_STR(myModName), NMXP_LOG_STR(argvec[1]));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
270 271 272 273 274 275 276 277 278 279 280 281

    /* Reinitialize the logging level */
    logit_init (argvec[1], 0, 512, logSwitch);

}				/* End of nmxptool_ew_configure() */


/***************************************************************************
 * nmxptool_ew_proc_configfile():
 * Process the module configuration parameters.
 *
 ***************************************************************************/
282
int nmxptool_ew_proc_configfile (char * configfile, NMXPTOOL_PARAMS *params) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
283 284 285 286
    char    		*com;
    char    		*str;
    int      		nfiles;
    int      		success;
287 288
    int                 flag_MaxTolerableLatency = 0;
    int                 flag_ShortTermCompletion = 0;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
289

Matteo Quintiliani's avatar
Matteo Quintiliani committed
290 291
    char *sep = NULL;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
292 293 294 295 296 297 298 299 300
    /* Some important initial values or defaults */
    ringName[0]   = '\0';
    myModName[0] = '\0';
    heartbeatInt = -1;
    logSwitch    = -1;

    /* Open the main configuration file */
    nfiles = k_open (configfile);
    if (nfiles == 0) {
301
	logit("et",
302
		"%s: Error opening command file <%s>; exiting!\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		configfile);
	return EW_FAILURE;
    }

    /* Process all command files */
    while (nfiles > 0) {   /* While there are command files open */
	while (k_rd ()) {       /* Read next line from active file  */
	    com = k_str ();         /* Get the first token from line */

	    /* Ignore blank lines & comments */
	    if (!com)
		continue;
	    if (com[0] == '#')
		continue;

	    /* Open a nested configuration file */
	    if (com[0] == '@') {
		success = nfiles + 1;
		nfiles  = k_open (&com[1]);
		if (nfiles != success) {
323
		    logit("et", 
324
			    "%s: Error opening command file <%s>; exiting!\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
325 326 327 328 329 330 331 332 333 334
			    &com[1]);
		    return EW_FAILURE;
		}
		continue;
	    }

	    /* Process anything else as a command */
	    if (k_its ("MyModuleId")) {
		if ( (str = k_str ()) ) {
		    if (strlen(str) >= MAXMODNAMELEN) {
335
			logit("et", "MyModId too long; max is %d\n", MAXMODNAMELEN -1);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
336 337 338
			return EW_FAILURE;
		    }

339
		    strncpy (myModName, str, MAXMODNAMELEN);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
340 341 342

		    /* Lookup module ID */
		    if ( GetModId( myModName, &myModId) != 0 ) {
343
			logit("et", "%s: Error getting myModId.\n", PACKAGE_NAME );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
344 345 346 347 348 349 350 351
			exit (EW_FAILURE);
		    }
		}
	    }

	    else if (k_its ("RingName")) {
		if ( (str = k_str ()) ) {
		    if (strlen(str) >= MAXRINGNAMELEN) {
352
			logit("et", "OutRing name too long; max is %d\n", 
Matteo Quintiliani's avatar
Matteo Quintiliani committed
353 354 355 356
				MAXRINGNAMELEN - 1);
			return EW_FAILURE;
		    }

357
		    strncpy (ringName, str, MAXRINGNAMELEN);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
358 359 360 361 362 363 364 365 366 367 368
		}
	    }

	    else if (k_its ("HeartBeatInterval")) {
		heartbeatInt = k_long ();
	    }

	    else if (k_its ("LogFile")) {
		logSwitch = k_int();
	    }

369
	    else if (k_its ("Verbosity")) {
370
		params->verbose_level = k_int();
Matteo Quintiliani's avatar
Matteo Quintiliani committed
371 372
	    }

373
	    else if (k_its ("NmxpHost")) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
374 375
		if ( (str = k_str ()) ) {
		    if (strlen(str) >= MAXADDRLEN) {
376
			logit("et", "nmxphost too long; max is %d characters\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
377 378 379
				MAXADDRLEN);
			return EW_FAILURE;
		    }
380
		    params->hostname = NMXP_MEM_STRDUP(str);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
381 382 383
		}
	    }

384
	    else if ( k_its ("NmxpPortPDS")) {
385
		params->portnumberpds = k_int();
Matteo Quintiliani's avatar
Matteo Quintiliani committed
386 387
	    }

388 389
	    else if ( k_its ("NmxpPortDAP")) {
		params->portnumberdap = k_int();
Matteo Quintiliani's avatar
Matteo Quintiliani committed
390 391
	    }

392 393
	    else if (k_its ("UserDAP")) {
		if ( (str = k_str ()) ) {
394
		    params->datas_username = NMXP_MEM_STRDUP(str);
395 396 397 398 399
		}
	    }

	    else if (k_its ("PassDAP")) {
		if ( (str = k_str ()) ) {
400
		    params->datas_password = NMXP_MEM_STRDUP(str);
401 402 403
		}
	    }

Matteo Quintiliani's avatar
Matteo Quintiliani committed
404 405 406 407
	    else if (k_its ("ForceTraceBuf1")) {
		forcetracebuf = k_int();
	    }

408 409 410 411
	    else if (k_its ("TimeoutRecv")) {
		params->timeoutrecv = k_int();
	    }

412 413
	    else if (k_its ("MaxTolerableLatency")) {
		params->max_tolerable_latency = k_int();
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
		flag_MaxTolerableLatency = 1;
		if(flag_ShortTermCompletion) {
		    logit("et", "You can use either MaxTolerableLatency or ShortTermCompletion\n");
		    return EW_FAILURE;
		}
	    }

	    else if (k_its ("ShortTermCompletion")) {
		params->stc = k_int();
		params->rate = 0; // original sample rate
		flag_ShortTermCompletion = 1;
		if(flag_MaxTolerableLatency) {
		    logit("et", "You can use either MaxTolerableLatency or ShortTermCompletion\n");
		    return EW_FAILURE;
		}
429 430
	    }

431 432 433
	    else if (k_its ("MaxDataToRetrieve")) {
		params->max_data_to_retrieve = k_int();
	    }
434

435 436 437
	    else if (k_its ("DefaultNetworkCode")) {
		if ( (str = k_str ()) ) {
		    if(params->network) {
438
			logit("et", "DefaultNetworkCode has been replicated!\n");
439 440
			return EW_FAILURE;
		    } else {
441
			params->network = NMXP_MEM_STRDUP(str);
442 443 444 445
		    }
		}
	    }

446 447 448 449 450 451 452 453 454 455 456
	    else if (k_its ("DefaultLocationCode")) {
		if ( (str = k_str ()) ) {
		    if(params->location) {
			logit("et", "DefaultLocationCode has been replicated!\n");
			return EW_FAILURE;
		    } else {
			params->location = NMXP_MEM_STRDUP(str);
		    }
		}
	    }

457
	    else if (k_its ("Channel")) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
458
		if ( (str = k_str ()) ) {
459
		    if(params->statefile) {
460 461
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY,
				"Channels have been already defined inside channel state file!\n");
462 463
			return EW_FAILURE;
		    }
464 465
		    if(!params->channels) {
#define MAXSIZECHANNELSTRING 8000
466
			params->channels = (char *) NMXP_MEM_MALLOC(MAXSIZECHANNELSTRING);
467 468
			strncpy(params->channels, str, MAXSIZECHANNELSTRING);
		    } else {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
469 470
			strncat(params->channels, ",", MAXSIZECHANNELSTRING - strlen(params->channels));
			strncat(params->channels, str, MAXSIZECHANNELSTRING - strlen(params->channels));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
471 472 473 474
		    }
		}
	    }

Matteo Quintiliani's avatar
Matteo Quintiliani committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	    else if (k_its ("mschan")) {
		if ( (str = k_str ()) ) {
		    sep = strstr(str, "/");
		    if(sep) {
			sep[0] = 0;
			sep++;
			params->usec = atoi(str) * 1000;
			params->n_channel = atoi(sep);
			nmxp_log(NMXP_LOG_WARN, NMXP_LOG_D_ANY,
				"Channels %d usec %d!\n", params->n_channel, params->usec);
		    } else {
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY,
				"Syntax error in parameter 'mschan' %s!\n", NMXP_LOG_STR(str));
			return EW_FAILURE;
		    }
		}
	    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
492

493 494
	    else if (k_its ("ChannelFile")) {
		if ( (str = k_str ()) ) {
495
		    params->statefile = (char *) NMXP_MEM_MALLOC(512 * sizeof(char));
496 497 498 499 500 501
		    strncpy(params->statefile, str, 512);
		    if(params->channels == NULL) {
			params->channels = get_channel_list_argument_from_state_file(params->statefile);
			if(params->channels) {
			    /* Do nothing */
			} else {
502 503
			    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY,
				    "State file %s not found or unable to read!\n", NMXP_LOG_STR(params->statefile));
504 505 506
			    return EW_FAILURE;
			}
		    } else {
507 508
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY,
				"Channels have been already defined inside main configuration file!\n");
509 510 511 512 513 514
			return EW_FAILURE;
		    }
		}
	    }


Matteo Quintiliani's avatar
Matteo Quintiliani committed
515 516
	    /* Unknown command */ 
	    else {
517
		logit("et", "%s: <%s> Unknown command in <%s>.\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
518 519 520 521 522 523
			com, configfile);
		continue;
	    }

	    /* See if there were any errors processing the command */
	    if (k_err ()) {
524
		logit("et", 
525
			"%s: Bad command in <%s>; exiting!\n\t%s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
526 527 528 529 530 531 532 533 534 535 536 537
			configfile, k_com());
		return EW_FAILURE;
	    }

	} /** while k_rd() **/

	nfiles = k_close ();

    } /** while nfiles **/

    /* Check for required parameters */
    if ( myModName[0] == '\0' ) {
538
	logit("et", "%s: No MyModId parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
539 540 541 542
		configfile);
	return EW_FAILURE;
    }
    if ( ringName[0] == '\0' ) {
543
	logit("et", "%s: No OutRing parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
544 545 546 547
		configfile);
	return EW_FAILURE;
    }
    if ( heartbeatInt == -1 ) {
548
	logit("et", "%s: No HeartBeatInterval parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
549 550 551 552
		configfile);
	return EW_FAILURE;
    }
    if ( logSwitch == -1 ) {
553
	logit("et", "%s: No LogFile parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
554 555 556 557
		configfile);
	return EW_FAILURE;
    }

558
    if(params->statefile) {
559 560 561 562
	params->flag_buffered = 1;
	logit("et", "Enable buffer for requesting also recent packets into the past.\n");
    }

Matteo Quintiliani's avatar
Matteo Quintiliani committed
563 564 565
    return EW_SUCCESS;
}				/* End of nmxptool_ew_proc_configfile() */

Matteo Quintiliani's avatar
Matteo Quintiliani committed
566 567 568 569 570
/***************************************************************************
 * nmxptoole_ew_report_status():
 * Send error and hearbeat messages to transport ring.
 *
 ***************************************************************************/
571
void nmxptool_ew_report_status( MSG_LOGO * pLogo, short code, char * message ) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
572 573 574 575 576 577 578 579
    char          outMsg[MAXMESSAGELEN];  /* The outgoing message.        */
    time_t        msgTime;        /* Time of the message.                 */

    /*  Get the time of the message                                       */
    time( &msgTime );

    /* Build & process the message based on the type */
    if ( pLogo->type == typeHeartbeat ) {
580 581
	/* sprintf( outMsg, "%ld %ld\n\0", (long) msgTime, (long) myPid ); */
	sprintf( outMsg, "%ld %ld\n%c", (long) msgTime, (long) myPid, 0 );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
582 583 584 585 586

	/* Write the message to the output region */
	if ( tport_putmsg( &regionOut, &hrtLogo, (long) strlen( outMsg ),
		    outMsg ) != PUT_OK ) {
	    /* Log an error message */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
587
	    logit( "et", "%s: Failed to send a heartbeat message (%d).\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
588
		    NMXP_LOG_STR(PACKAGE_NAME),
Matteo Quintiliani's avatar
Matteo Quintiliani committed
589 590 591 592
		    code );
	}
    } else {
	if ( message ) {
593 594
	    /* sprintf( outMsg, "%ld %hd %s\n\0", (long) msgTime, code, NMXP_LOG_STR(message) ); */
	    sprintf( outMsg, "%ld %hd %s\n%c", (long) msgTime, code, NMXP_LOG_STR(message), 0 );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
595
	    logit("t","Error:%d (%s)\n", code, NMXP_LOG_STR(message) );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
596
	} else {
597 598
	    /* sprintf( outMsg, "%ld %hd\n\0", (long) msgTime, code ); */
	    sprintf( outMsg, "%ld %hd\n%c", (long) msgTime, code, 0 );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
599 600 601 602 603 604 605
	    logit("t","Error:%d (No description)\n", code );
	}

	/* Write the message to the output region  */
	if ( tport_putmsg( &regionOut, &errLogo, (long) strlen( outMsg ),
		    outMsg ) != PUT_OK ) {
	    /*     Log an error message                                    */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
606
	    logit( "et", "%s: Failed to send an error message (%d).\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
607
		    NMXP_LOG_STR(PACKAGE_NAME),
Matteo Quintiliani's avatar
Matteo Quintiliani committed
608 609 610 611 612 613 614
		    code );
	}

    }
}				/* End of nmxptool_ew_report_status() */


615 616 617 618 619 620 621 622 623 624 625 626 627 628
int nmxptool_ew_check_flag_terminate() {
    /* Check if we are being asked to terminate */
    return (tport_getflag (&regionOut) == TERMINATE || tport_getflag (&regionOut) == myPid );
}

void nmxptool_ew_send_heartbeat_if_needed() {
    /* Check if we need to send heartbeat message */
    if ( time( &timeNow ) - timeLastBeat >= heartbeatInt )
    {
	timeLastBeat = timeNow;
	nmxptool_ew_report_status ( &hrtLogo, 0, "" ); 
    }
}

629
void nmxptool_ew_send_error(unsigned int ierr, char *message, const char *hostname) {
630
    char complete_message[NMXPTOOL_EW_MAXSZE_MSG];
631 632 633 634 635 636 637
    int i;

    i=0;
    while(i < NMXPTOOL_EW_ERR_MAXVALUE && nmxptool_ew_err_msg[i].error != ierr) {
	i++;
    }
    if(i < NMXPTOOL_EW_ERR_MAXVALUE) {
638
	if(message) {
639
	    snprintf(complete_message, NMXPTOOL_EW_MAXSZE_MSG, "m%u - %s - %s %s.", myModId, hostname, nmxptool_ew_err_msg[i].message, message);
640
	} else {
641
	    snprintf(complete_message, NMXPTOOL_EW_MAXSZE_MSG, "m%u - %s - %s", myModId, hostname, nmxptool_ew_err_msg[i].message);
642
	}
643
	nmxptool_ew_report_status ( &errLogo, nmxptool_ew_err_msg[i].error, complete_message); 
644
    } else {
645
	nmxptool_ew_report_status ( &errLogo, 0, "Unknown error"); 
646 647 648 649
    }

}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
650 651 652 653 654
/***************************************************************************
 * nmxptool_ew_logit_msg() and nmxptool_ew_logit_err():
 * 
 * Hooks for Earthworm logging facility.
 ***************************************************************************/
655
int nmxptool_ew_logit_msg (char *msg) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
656
  logit ("o",  NMXP_LOG_STR(msg));
657
  return 0;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
658 659
}

660
int nmxptool_ew_logit_err (char *msg) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
661
  logit ("e",  NMXP_LOG_STR(msg));
662
  return 0;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
663 664 665
}


666 667
#endif