Пример #1
1
int
main (int argc, char **argv)
{

    int             c,
                    errflg = 0;

    char           *in,
                   *out;
    int             orbin,
                    orbout;
    double          maxpkts = VERY_LARGE_NUMBER ;
    int             quit;
    char           *pktmatch = strdup(".*/pf/evtinfo"),
                   *reject = 0;
    int             nmatch;
    int             specified_after = 0;
    double          after = 0.0,
                    until = VERY_LARGE_NUMBER ;
    double          start_time,
                    end_time,
                    delta_t ;
    double          totpkts = 0,
                    totbytes = 0;
    Flags           flags;
    static int      last_pktid = -1;
    static double   last_pkttime = 0.0;
    char           *statefile = 0;
    double          last_burial = 0.0;
    double          decent_interval = 300.0;
    int             mode = PKT_NOSAMPLES;
    int             rcode;
    char            srcname[ORBSRCNAME_SIZE];
    double          pkttime = 0.0 ;
    int             pktid;
    int             nbytes;
    char           *packet = 0;
    int             packetsz = 0;
    Packet         *unstuffed = 0;
	Pf				*pf;
	Tbl				*tbl;
	Arr				*arr;
	char			*arrkey;
	char			*pffilename;

	Tbl				*channels;
	char			*net;
	char			*sta;
	char			*chan;
	char			netstachan[ORBSRCNAME_SIZE];
	Srcname			parts;
	double			maxtime,mintime,starttime,triggertime,duration,min_g,max_g,snr;
	char			*datalogger;
	int				evtfilesize,channelno,maxcts,mincts;
	char			*errors,*evtfilename,*ft,*filter,*srcid;
	char			*line;
	int				i;
	Pf				*pfnew;
	Dbptr			db;
	int 			put_tests=0;
	char 			*chanmatch=0;
	Hook			*hook=0;
	char			*tdummy=0;
	char 			*match=malloc(100);

	


    memset (&flags, 0, sizeof (flags));
    elog_init (argc, argv);
    elog_notify (0, "%s $Revision: 1.5 $ $Date: 2005/05/10 07:34:43 $\n",
		 Program_Name);

    while ((c = getopt (argc, argv, "m:n:r:S:c:tvV")) != -1) {
	switch (c) {
	  case 'm':
	    match = optarg;
		sprintf(pktmatch,"%s/pf/evtinfo",match);
	    break;
	  case 'c':
	 	chanmatch = optarg;
		break;
	  case 't':
	  	put_tests = 1;
		break;
	  case 'n':
	    maxpkts = atoi (optarg);
	    break;

	  case 'r':
	    reject = optarg;
	    break;

	  case 'S':
	    statefile = optarg;
	    break;

	  case 'v':
	    flags.verbose++;
	    break;

	  case 'V':
		flags.verbose++;
		flags.verbose++;
	    break;

	  case '?':
	    errflg++;
	}
    }

    if (errflg || argc - optind < 2 || argc - optind > 4)
	usage ();

    in = argv[optind++];
    out = argv[optind++];

    if (argc > optind) {
	after = str2epoch (argv[optind++]);
	specified_after = 1;
	if (argc > optind) {
	    until = str2epoch (argv[optind++]);
	    if (until < after) {
		until += after ;
	    }
	}
    }
    if ((orbin = orbopen (in, "r&")) < 0)
	die (0, "Can't open input '%s'\n", in);

    if (statefile != 0) {
		char           *s;
		if (exhume (statefile, &quit, RT_MAX_DIE_SECS, 0) != 0) {
	    elog_notify (0, "read old state file\n");
		}
		if (orbresurrect (orbin, &last_pktid, &last_pkttime) == 0) {
	    	elog_notify (0, "resurrection successful: repositioned to pktid #%d @ %s\n",
			 last_pktid, s = strtime (last_pkttime));
	    	free (s);
		} else {
	    	complain (0, "resurrection unsuccessful\n");
		}
    }
    if ((orbout = orbopen (out, "w&")) < 0) {
	die (0, "Can't open output '%s'\n", out);
    }
    if (pktmatch) {
	nmatch = orbselect (orbin, pktmatch);
    }
    if (nmatch < 0) {
	die (1, "select '%s' returned %d\n", pktmatch, nmatch);
    }
    if (reject) {
	nmatch = orbreject (orbin, reject);
    }
    if (nmatch < 0) {
		elog_die (1, "reject '%s' returned %d\n", reject, nmatch);
    } else {
		if (flags.verbose) {
			elog_notify (1,"%d sources selected\n", nmatch);
		}	
    }

    if (specified_after) {
	pktid = orbafter (orbin, after);
	if (pktid < 0) {
	    char           *s;
	    elog_complain (1, "seek to %s failed\n", s = strtime (after));
	    free (s);
	    pktid = forbtell (orbin);
	    elog_complain (1,"pktid is still #%d\n", pktid);
	} else {
	    if (flags.verbose) elog_notify (1,"new starting pktid is #%d\n", pktid);
	}
    }
    start_time = now ();

	db = dbtmp("rt1.0");

    while (!quit && pkttime < until && totpkts < maxpkts) {
	rcode = orbreap (orbin,
		    &pktid, srcname, &pkttime, &packet, &nbytes, &packetsz);

	switch (rcode) {
	  case 0:
	    totpkts++;
	    totbytes += nbytes;

	    if (flags.verbose>2) {
		showPkt (pktid, srcname, pkttime, packet, nbytes, stdout, mode);
	    }
	    if (statefile != 0
		    && last_pkttime - last_burial > decent_interval) {
		bury ();
		last_burial = pkttime;
	    }
	    if ((unstuffPkt (srcname, pkttime, packet, nbytes, &unstuffed))==Pkt_pf) {
		  	pf = 			unstuffed->pf;
			tbl=			pfkeys(pf);
			arrkey=			gettbl(tbl,0);
			pfget(pf,arrkey,&pfnew);
			datalogger=		pfget_string(pfnew,"datalogger");
			duration=		pfget_double(pfnew,"duration");
			errors=			pfget_string(pfnew,"errors");
			evtfilename=	pfget_string(pfnew,"evtfilename");
			evtfilesize=	pfget_int(pfnew,"evtfilesize");
			ft=				pfget_string(pfnew,"ft");
			starttime=		pfget_double(pfnew,"time");
			triggertime=	pfget_double(pfnew,"triggertime");

			channels=pfget_tbl(pfnew,"channels");
			if (strcasecmp(ft,"no")==0 || put_tests == 1) {
				for (i=0; i<maxtbl(channels);i++) {
					line=gettbl(channels,i);
					sscanf(line,"%s %d %lf %d %lf %lf %d %lf",
						netstachan,&channelno,&maxtime,&maxcts,&max_g,&mintime,&mincts,&min_g);
					split_srcname(netstachan,&parts);	
					strcpy(parts.src_suffix,"GENC");
					join_srcname(&parts,netstachan);
					if (flags.verbose>1) printf("%s,%s\n", netstachan,tdummy=strtime(triggertime));	
					if (chanmatch == 0 || 
							strmatches(parts.src_chan,chanmatch,&hook)) {
						if (flags.verbose) elog_notify(1,"putting detev for %s,%s\n", netstachan,tdummy=strtime(triggertime));	
						
						db=dblookup(db,0,"detev",0,"dbSCRATCH");
						dbputv(db,0,"sta",parts.src_sta,"chan",parts.src_chan,
							"filter",K2_FILTER,
							"time",	triggertime,
							"tron",  starttime - triggertime,
							"troff", starttime - triggertime + duration,
							"iphase","K2",
							"snr",(max_g * 1000.0  - min_g * 1000.0)/2.0,
							0);
						db2orbpkt(db,orbout);
					} else {
						if (flags.verbose>1) elog_notify(1,"ignoring %s,%s\n", netstachan,tdummy=strtime(triggertime));	

					}
				}
			}
			
	    }

	    last_pktid = pktid;
	    last_pkttime = pkttime;
	}

    }

    if (statefile != 0)
	bury ();

    end_time = now ();
    delta_t = end_time - start_time;
	if (flags.verbose>1) {
		if (totpkts > 0) {
		elog_notify (1,"\n%.0f %.2f byte packets (%.1f kbytes) in %.3f seconds\n\t%10.3f kbytes/s\n\t%10.3f kbaud\n\t%10.3f pkts/s\n",
			totpkts, totbytes / totpkts, totbytes / 1024,
			delta_t,
			totbytes / delta_t / 1024,
			totbytes / delta_t / 1024 * 8,
			totpkts / delta_t);
		} else {

		elog_notify (1,"\nno packets read\n");
		}
	}

    if (orbclose (orbin)) {
	elog_complain (1, "error closing read orb\n");
    }
    if (orbclose (orbout)) {
	elog_complain (1, "error closing write orb\n");
    }
    return 0;
}
Пример #2
0
void WriteToOrb(SERVER *orb, UINT8 *das, int verbose, BOOL UseLoc)
{
static int count = 0;
double samprate, tstamp;
char buf[256];
struct reftek_dt dt;
int nbytes;
char *packet;
static int packetsz = 0;
static char srcname[ORBSRCNAME_SIZE];
static char *fid = "WriteToOrb";

/* Only deal with DT packets for which we can get sample rate */

    if (reftek_type(das) != REFTEK_DT) return;

    if (!reftek_dt(&dt, das, TRUE)) {
        rtp_log(RTP_ERR, "%s: DAS pkt dropped (can't decode)\n", fid);
        return;
    }

    if (!DeriveSampleRate(&dt, &samprate)) {
        rtp_log(RTP_ERR, "%s: DAS pkt dropped (unknown sample rate): %s\n",
            fid, reftek_str(das, buf)
        );
        return;
    }

/* Note decompression errors, if any */

    if (dt.dcerr) rtp_log(RTP_ERR, "%s: decompression error %d: %s\n",
        fid, dt.dcerr, reftek_str(das, buf)
    );

/* Convert to GENC format and send to ORB */

    ConvertPacket(&dt, samprate, UseLoc);

    if (stuffPkt(Pkt, srcname, &tstamp, &packet, &nbytes, &packetsz) < 0) {
        rtp_log(RTP_ERR, "stuffPkt routine failed for %s\n", Pkt->pkttype->name);
        return;
    }
    ++count;

    if (verbose) {
        showPkt(0, srcname, tstamp, packet, nbytes, stdout, PKT_TERSE);
    }

    if (orbput(orb->fd, srcname, dt.tstamp, packet, nbytes) != 0) {
        rtp_log(RTP_ERR, "orbput fails\n");
    }
}
Пример #3
0
/*
 * Main program loop
 */
int main(int iArgCount, char *aArgList[]) {

	uint8_t *aBBAPkt; /* Buffer to hold a bba packet read from the wire */
	struct stBBAPacketInfo oPktInfo; /* Struct to hold information from the packet header */
	double dPrevTime;
	int bOKToSend;
	char *sOutPkt; /* Packet to put onto the orb */
	int iOutPktLen; /* Length of sOutPkt */

	elog_init(iArgCount, aArgList);

	/* Parse out command line options */
	if (parseCommandLineOptions(iArgCount, aArgList) == RESULT_SUCCESS) {

		/* Read in the parameter file */
		if (paramFileRead() == RESULT_FAILURE) {
			elog_complain(1,
					"main(): Error encountered during paramFileRead() operation.");
			dcbbaCleanup(-1);
		}

		/* Exit if bPFValidateFlag is set */
		if (oConfig.bPFValidateFlag == TRUE) {
			elog_notify(
					0,
					"main(): Parameter File %s validated successfully. Exiting.",
					oConfig.sParamFileName);
			dcbbaCleanup(0);
		}

		/* Allocate memory for our packet */
		allot (uint8_t *, aBBAPkt, oConfig.iBBAPktBufSz);

		/* Set up a signal handler to re-read the parameter file on SIGUSR1*/
		signal(SIGUSR1, sig_hdlr);

		/* Connect to the ORB */
		if ((orbfd = orbopen(oConfig.sOrbName, "w&")) < 0) {
			elog_complain(1, "orbopen: unable to connect to ORB \"%s\".",
					oConfig.sOrbName);
			dcbbaCleanup(-1);
		}

		/* Connect to Data Concentrator's Data read port */
		if (dcDataConnect(oConfig.iConnectionType, oConfig.sDCConnectionParams)
				== RESULT_SUCCESS) {
			dPrevTime = now();

			/*** BEGIN MAIN LOOP ***/
			while (readFromDC(&oPktInfo, aBBAPkt) == RESULT_SUCCESS) {
				bOKToSend = TRUE;
				/* Check the packet age */
				/*if (fabs(oPktInfo.dPktTime - dPrevTime) > 86400.0) {
				 dPrevTime = now();
				 if (fabs(oPktInfo.dPktTime - dPrevTime) > 86400.0) {
				 elog_complain(
				 0,
				 "%s packet has bad time - %s (epoch:%lf). Will discard packet.\n",
				 oPktInfo.sSrcname, sTimeStamp = strtime(
				 oPktInfo.dPktTime), oPktInfo.dPktTime);
				 free(sTimeStamp);
				 bOKToSend = FALSE;
				 } else
				 dPrevTime = oPktInfo.dPktTime;
				 } else
				 dPrevTime = oPktInfo.dPktTime;*/

				if (bOKToSend == TRUE) {
					/* Add orb header to Packet */
					iOutPktLen = (int) stuffBBAPkt(&oPktInfo, aBBAPkt, &sOutPkt);
					if (iOutPktLen == 0) {
						/* There was an error stuffing the packet*/
						elog_complain(
								1,
								"An error occurred while adding the ORB header to the raw packet. Not submitting to the orb.");
					} else if (sOutPkt == 0) {
						elog_die(1,
								"Output packet length was non-zero but pointer to Output packet is null");
					} else {

						/* put it into the orb */
						if (oConfig.bVerboseModeFlag == TRUE) {
							showPkt(0, oPktInfo.sSrcname, oPktInfo.dPktTime,
									sOutPkt, iOutPktLen, stderr, PKT_UNSTUFF);
							showPkt(0, oPktInfo.sSrcname, oPktInfo.dPktTime,
									sOutPkt, iOutPktLen, stderr, PKT_DUMP);
						}

						if (orbput(orbfd, oPktInfo.sSrcname, oPktInfo.dPktTime,
								sOutPkt, iOutPktLen)) {
							elog_complain(0, "orbput() failed in main()\n");
							dcbbaCleanup(-1);
						}

						if (oConfig.bVerboseModeFlag == TRUE)
							elog_notify(0, "packet submitted under %s\n",
									oPktInfo.sSrcname);

						/* Free the packet */
						free(sOutPkt);
					}
				}
			}

			/*
			 * If we get here, it means readFromDC failed to get a packet from oDCDataBNS.
			 * This could be either that an EOF was reached if we were reading from a file,
			 * or that the socket died unexpectedly.
			 */
			dcbbaCleanup(-1);
		}

		/* Else unable to connect, cleanup with failure (-1) exit code */
		else
			dcbbaCleanup(-1);

	} else {