nmxptool_ew.c 17.9 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 106 107 108 109 110 111 112 113 114 115 116 117
    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 +
		((tbuf.trh2.nsamp - 1) / tbuf.trh2.samprate));

	strcpy(tbuf.trh2.net, pd->network);
	strcpy(tbuf.trh2.sta, pd->station);
	strcpy(tbuf.trh2.chan, pd->channel);

	strncpy(tbuf.trh2.loc, LOC_NULL_STRING, 2);

	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 140 141 142 143 144 145 146

    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 +
		((tbuf.trh.nsamp - 1) / tbuf.trh.samprate));

	strcpy(tbuf.trh.net, pd->network);
	strcpy(tbuf.trh.sta, pd->station);
	strcpy(tbuf.trh.chan, pd->channel);

	/* 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
 * nmxptoo_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 ) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
214 215 216 217
	fprintf (stderr, "%s: configure() failed \n", argvec[0]);
	exit (EW_FAILURE);
    }

218 219
    params->stc=-1;

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

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

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

    if (GetType ("TYPE_TRACEBUF", &typeWaveform) != 0) {
	fprintf (stderr, 
247
		"%s: Invalid message type <TYPE_TRACEBUF>; exitting!\n", PACKAGE_NAME);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	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
271
	    NMXP_LOG_STR(argvec[0]), NMXP_LOG_STR(myModName), NMXP_LOG_STR(argvec[1]));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
272 273 274 275 276 277 278 279 280 281 282 283

    /* 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.
 *
 ***************************************************************************/
284
int nmxptool_ew_proc_configfile (char * configfile, NMXPTOOL_PARAMS *params) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
285 286 287 288 289
    char    		*com;
    char    		*str;
    int      		nfiles;
    int      		success;

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 301
    /* 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) {
	fprintf (stderr,
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 323
		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) {
		    fprintf (stderr, 
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 335 336 337 338 339 340 341 342
			    &com[1]);
		    return EW_FAILURE;
		}
		continue;
	    }

	    /* Process anything else as a command */
	    if (k_its ("MyModuleId")) {
		if ( (str = k_str ()) ) {
		    if (strlen(str) >= MAXMODNAMELEN) {
			fprintf(stderr, "MyModId too long; max is %d\n", MAXMODNAMELEN -1);
			return EW_FAILURE;
		    }

		    strcpy (myModName, str);

		    /* Lookup module ID */
		    if ( GetModId( myModName, &myModId) != 0 ) {
343
			fprintf( stderr, "%s: Error getting myModId.\n", PACKAGE_NAME );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			exit (EW_FAILURE);
		    }
		}
	    }

	    else if (k_its ("RingName")) {
		if ( (str = k_str ()) ) {
		    if (strlen(str) >= MAXRINGNAMELEN) {
			fprintf(stderr, "OutRing name too long; max is %d\n", 
				MAXRINGNAMELEN - 1);
			return EW_FAILURE;
		    }

		    strcpy (ringName, str);
		}
	    }

	    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
			fprintf(stderr, "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 414 415
	    else if (k_its ("MaxTolerableLatency")) {
		params->max_tolerable_latency = k_int();
	    }

416 417 418
	    else if (k_its ("MaxDataToRetrieve")) {
		params->max_data_to_retrieve = k_int();
	    }
419

420 421 422 423 424 425
	    else if (k_its ("DefaultNetworkCode")) {
		if ( (str = k_str ()) ) {
		    if(params->network) {
			fprintf(stderr, "DefaultNetworkCode has been replicated!\n");
			return EW_FAILURE;
		    } else {
426
			params->network = NMXP_MEM_STRDUP(str);
427 428 429 430
		    }
		}
	    }

431
	    else if (k_its ("Channel")) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
432
		if ( (str = k_str ()) ) {
433
		    if(params->statefile) {
434 435
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY,
				"Channels have been already defined inside channel state file!\n");
436 437
			return EW_FAILURE;
		    }
438 439
		    if(!params->channels) {
#define MAXSIZECHANNELSTRING 8000
440
			params->channels = (char *) NMXP_MEM_MALLOC(MAXSIZECHANNELSTRING);
441 442 443 444
			strncpy(params->channels, str, MAXSIZECHANNELSTRING);
		    } else {
			strncat(params->channels, ",", MAXSIZECHANNELSTRING);
			strncat(params->channels, str, MAXSIZECHANNELSTRING);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
445 446 447 448
		    }
		}
	    }

Matteo Quintiliani's avatar
Matteo Quintiliani committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	    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
466

467 468 469
	    else if (k_its ("ChannelFile")) {
		if ( (str = k_str ()) ) {
		    params->flag_buffered = 1;
470
		    params->statefile = (char *) NMXP_MEM_MALLOC(512 * sizeof(char));
471 472 473 474 475 476
		    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 {
477 478
			    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));
479 480 481
			    return EW_FAILURE;
			}
		    } else {
482 483
			nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY,
				"Channels have been already defined inside main configuration file!\n");
484 485 486 487 488 489
			return EW_FAILURE;
		    }
		}
	    }


Matteo Quintiliani's avatar
Matteo Quintiliani committed
490 491
	    /* Unknown command */ 
	    else {
492
		fprintf (stderr, "%s: <%s> Unknown command in <%s>.\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
493 494 495 496 497 498 499
			com, configfile);
		continue;
	    }

	    /* See if there were any errors processing the command */
	    if (k_err ()) {
		fprintf (stderr, 
500
			"%s: Bad command in <%s>; exiting!\n\t%s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
501 502 503 504 505 506 507 508 509 510 511 512
			configfile, k_com());
		return EW_FAILURE;
	    }

	} /** while k_rd() **/

	nfiles = k_close ();

    } /** while nfiles **/

    /* Check for required parameters */
    if ( myModName[0] == '\0' ) {
513
	fprintf (stderr, "%s: No MyModId parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
514 515 516 517
		configfile);
	return EW_FAILURE;
    }
    if ( ringName[0] == '\0' ) {
518
	fprintf (stderr, "%s: No OutRing parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
519 520 521 522
		configfile);
	return EW_FAILURE;
    }
    if ( heartbeatInt == -1 ) {
523
	fprintf (stderr, "%s: No HeartBeatInterval parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
524 525 526 527
		configfile);
	return EW_FAILURE;
    }
    if ( logSwitch == -1 ) {
528
	fprintf (stderr, "%s: No LogFile parameter found in %s\n", PACKAGE_NAME,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
529 530 531 532 533 534 535
		configfile);
	return EW_FAILURE;
    }

    return EW_SUCCESS;
}				/* End of nmxptool_ew_proc_configfile() */

Matteo Quintiliani's avatar
Matteo Quintiliani committed
536 537 538 539 540
/***************************************************************************
 * nmxptoole_ew_report_status():
 * Send error and hearbeat messages to transport ring.
 *
 ***************************************************************************/
541
void nmxptool_ew_report_status( MSG_LOGO * pLogo, short code, char * message ) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555
    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 ) {
	sprintf( outMsg, "%ld %ld\n\0", (long) msgTime, (long) myPid );

	/* 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
556
	    logit( "et", "%s: Failed to send a heartbeat message (%d).\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
557
		    NMXP_LOG_STR(PACKAGE_NAME),
Matteo Quintiliani's avatar
Matteo Quintiliani committed
558 559 560 561
		    code );
	}
    } else {
	if ( message ) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
562 563
	    sprintf( outMsg, "%ld %hd %s\n\0", (long) msgTime, code, NMXP_LOG_STR(message) );
	    logit("t","Error:%d (%s)\n", code, NMXP_LOG_STR(message) );
Matteo Quintiliani's avatar
Matteo Quintiliani committed
564 565 566 567 568 569 570 571 572
	} else {
	    sprintf( outMsg, "%ld %hd\n\0", (long) msgTime, code );
	    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
573
	    logit( "et", "%s: Failed to send an error message (%d).\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
574
		    NMXP_LOG_STR(PACKAGE_NAME),
Matteo Quintiliani's avatar
Matteo Quintiliani committed
575 576 577 578 579 580 581
		    code );
	}

    }
}				/* End of nmxptool_ew_report_status() */


582 583 584 585 586 587 588 589 590 591 592 593 594 595
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, "" ); 
    }
}

596 597
void nmxptool_ew_send_error(unsigned int ierr, char *message) {
    char complete_message[NMXPTOOL_EW_MAXSZE_MSG];
598 599 600 601 602 603 604
    int i;

    i=0;
    while(i < NMXPTOOL_EW_ERR_MAXVALUE && nmxptool_ew_err_msg[i].error != ierr) {
	i++;
    }
    if(i < NMXPTOOL_EW_ERR_MAXVALUE) {
605
	if(message) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
606
	    snprintf(complete_message, NMXPTOOL_EW_MAXSZE_MSG, "%s %s.", nmxptool_ew_err_msg[i].message, message);
607 608 609
	} else {
	    snprintf(complete_message, NMXPTOOL_EW_MAXSZE_MSG, "%s", nmxptool_ew_err_msg[i].message);
	}
610
	nmxptool_ew_report_status ( &errLogo, nmxptool_ew_err_msg[i].error, complete_message); 
611
    } else {
612
	nmxptool_ew_report_status ( &errLogo, 0, "Unknown error"); 
613 614 615 616
    }

}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
617 618 619 620 621
/***************************************************************************
 * nmxptool_ew_logit_msg() and nmxptool_ew_logit_err():
 * 
 * Hooks for Earthworm logging facility.
 ***************************************************************************/
622
int nmxptool_ew_logit_msg (char *msg) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
623
  logit ("o",  NMXP_LOG_STR(msg));
624
  return 0;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
625 626
}

627
int nmxptool_ew_logit_err (char *msg) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
628
  logit ("e",  NMXP_LOG_STR(msg));
629
  return 0;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
630 631 632
}


633 634
#endif