Commit 6621e4e2 authored by Matteo Quintiliani's avatar Matteo Quintiliani

Major version


git-svn-id: file:///home/quintiliani/svncopy/nmxptool/trunk@127 3cd66e75-5955-46cb-a940-c26e5fc5497d
parent cf824aea
...@@ -12,11 +12,31 @@ ...@@ -12,11 +12,31 @@
#include "seedlink_plugin.h" #include "seedlink_plugin.h"
#endif #endif
typedef struct {
int significant;
double last_time;
int prev_xn;
} NMXP_CHAN_SUPPORT;
static void clientShutdown(int sig);
static void clientDummyHandler(int sig);
/* Global variable for main program and handling terminitation program */
NMXPTOOL_PARAMS params;
int naqssock = 0;
FILE *outfile = NULL;
NMXP_CHAN_LIST *channelList = NULL;
NMXP_CHAN_LIST *channelList_subset = NULL;
#ifdef HAVE_LIBMSEED
/* Mini-SEED variables */
NMXP_DATA_SEED data_seed;
NMXP_CHAN_SUPPORT *channelListSupport = NULL;
#endif
int main (int argc, char **argv) { int main (int argc, char **argv) {
int naqssock;
NMXP_CHAN_LIST *channelList;
NMXP_CHAN_LIST *channelList_subset;
uint32_t connection_time; uint32_t connection_time;
int request_SOCKET_OK; int request_SOCKET_OK;
int i_chan; int i_chan;
...@@ -26,23 +46,37 @@ int main (int argc, char **argv) { ...@@ -26,23 +46,37 @@ int main (int argc, char **argv) {
uint32_t length; uint32_t length;
int ret; int ret;
FILE *outfile = NULL;
char filename[500]; char filename[500];
char *station_code = NULL, *channel_code = NULL; char *station_code = NULL, *channel_code = NULL;
NMXP_DATA_PROCESS *pd; NMXP_DATA_PROCESS *pd;
NMXPTOOL_PARAMS params;
#ifdef HAVE_LIBMSEED #ifdef HAVE_LIBMSEED
/* Mini-SEED variables */
NMXP_DATA_SEED data_seed;
/* Init mini-SEED variables */ /* Init mini-SEED variables */
nmxp_data_seed_init(&data_seed); nmxp_data_seed_init(&data_seed);
#endif #endif
#ifndef WIN32
/* Signal handling, use POSIX calls with standardized semantics */
struct sigaction sa;
sa.sa_handler = clientDummyHandler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
sa.sa_handler = clientShutdown;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
#endif
/* Default is normal output */
nmxp_log(-1, 0); nmxp_log(-1, 0);
/* Initialize params from argument values */ /* Initialize params from argument values */
...@@ -78,6 +112,16 @@ int main (int argc, char **argv) { ...@@ -78,6 +112,16 @@ int main (int argc, char **argv) {
return 1; return 1;
} else { } else {
nmxp_chan_print_channelList(channelList_subset); nmxp_chan_print_channelList(channelList_subset);
#ifdef HAVE_LIBMSEED
/* init channelListSupport */
channelListSupport = (NMXP_CHAN_SUPPORT *) malloc(sizeof(NMXP_CHAN_SUPPORT) * channelList_subset->number);
for(i_chan = 0; i_chan < channelList_subset->number; i_chan++) {
channelListSupport[i_chan].significant = 0;
channelListSupport[i_chan].last_time = 0.0;
channelListSupport[i_chan].prev_xn = 0;
}
#endif
} }
/* Free the complete channel list */ /* Free the complete channel list */
...@@ -86,6 +130,8 @@ int main (int argc, char **argv) { ...@@ -86,6 +130,8 @@ int main (int argc, char **argv) {
channelList = NULL; channelList = NULL;
} }
nmxp_log(0, 1, "Starting comunication.\n");
/* TODO condition starting DAP */ /* TODO condition starting DAP */
if(params.start_time != 0 && params.end_time != 0) { if(params.start_time != 0 && params.end_time != 0) {
...@@ -131,10 +177,13 @@ int main (int argc, char **argv) { ...@@ -131,10 +177,13 @@ int main (int argc, char **argv) {
if(params.flag_writefile) { if(params.flag_writefile) {
/* Open output file */ /* Open output file */
sprintf(filename, "%s.%s.%d.%d.%d.nmx", (params.network)? params.network : DEFAULT_NETWORK, channelList_subset->channel[i_chan].name, channelList_subset->channel[i_chan].key, params.start_time, params.end_time); sprintf(filename, "%s.%s.%d.%d.%d.nmx",
(params.network)? params.network : DEFAULT_NETWORK,
channelList_subset->channel[i_chan].name,
channelList_subset->channel[i_chan].key,
params.start_time, params.end_time);
outfile = fopen(filename, "w"); outfile = fopen(filename, "w");
if(!outfile) { if(!outfile) {
nmxp_log(1, 0, "Can not to open file %s!", filename); nmxp_log(1, 0, "Can not to open file %s!", filename);
} }
...@@ -143,10 +192,14 @@ int main (int argc, char **argv) { ...@@ -143,10 +192,14 @@ int main (int argc, char **argv) {
#ifdef HAVE_LIBMSEED #ifdef HAVE_LIBMSEED
if(params.flag_writeseed) { if(params.flag_writeseed) {
/* Open output Mini-SEED file */ /* Open output Mini-SEED file */
sprintf(data_seed.filename_mseed, "%s.%s.%d.%d.%d.miniseed", (params.network)? params.network : DEFAULT_NETWORK, channelList_subset->channel[i_chan].name, channelList_subset->channel[i_chan].key, params.start_time, params.end_time); sprintf(data_seed.filename_mseed, "%s.%s.%d.%d.%d.miniseed",
(params.network)? params.network : DEFAULT_NETWORK,
channelList_subset->channel[i_chan].name,
channelList_subset->channel[i_chan].key,
params.start_time,
params.end_time);
data_seed.outfile_mseed = fopen(data_seed.filename_mseed, "w"); data_seed.outfile_mseed = fopen(data_seed.filename_mseed, "w");
if(!data_seed.outfile_mseed) { if(!data_seed.outfile_mseed) {
nmxp_log(1, 0, "Can not to open file %s!", data_seed.filename_mseed); nmxp_log(1, 0, "Can not to open file %s!", data_seed.filename_mseed);
} }
...@@ -183,34 +236,30 @@ int main (int argc, char **argv) { ...@@ -183,34 +236,30 @@ int main (int argc, char **argv) {
while(ret == NMXP_SOCKET_OK && type != NMXP_MSG_READY) { while(ret == NMXP_SOCKET_OK && type != NMXP_MSG_READY) {
/* Process a packet and return value in NMXP_DATA_PROCESS structure */ /* Process a packet and return value in NMXP_DATA_PROCESS structure */
pd = nmxp_processCompressedDataFunc(buffer, length, channelList_subset); pd = nmxp_processCompressedData(buffer, length, channelList_subset);
/* Log contents of last packet */
nmxp_data_log(pd);
#ifdef HAVE_LIBMSEED #ifdef HAVE_LIBMSEED
/* Write Mini-SEED record */ /* Write Mini-SEED record */
if(params.flag_writeseed) { if(params.flag_writeseed) {
nmxp_data_msr_pack(pd, &data_seed, NULL);
nmxp_data_msr_pack(pd, &data_seed);
} }
#endif #endif
#ifdef HAVE___SRC_SEEDLINK_PLUGIN_C #ifdef HAVE___SRC_SEEDLINK_PLUGIN_C
/* Send data to SeedLink Server */ /* Send data to SeedLink Server */
if(params.flag_writeseedlink) { if(params.flag_writeseedlink) {
/* TODO Set values */ /* TODO Set values */
const int usec_correction = 0; const int usec_correction = 0;
const int timing_quality = 100; const int timing_quality = 100;
send_raw_depoch(pd->station, pd->channel, pd->time, usec_correction, timing_quality, send_raw_depoch(pd->station, pd->channel, pd->time, usec_correction, timing_quality,
pd->pDataPtr, pd->nSamp); pd->pDataPtr, pd->nSamp);
} }
#endif #endif
/* Log contents of last packet */
nmxp_data_log(pd);
if(params.flag_writefile && outfile) { if(params.flag_writefile && outfile) {
/* Write buffer to the output file */ /* Write buffer to the output file */
if(outfile && buffer && length > 0) { if(outfile && buffer && length > 0) {
...@@ -221,8 +270,9 @@ int main (int argc, char **argv) { ...@@ -221,8 +270,9 @@ int main (int argc, char **argv) {
} }
} }
if(buffer) { if(pd->buffer) {
free(buffer); free(pd->buffer);
pd->buffer = NULL;
} }
/* Receive Data */ /* Receive Data */
...@@ -233,12 +283,14 @@ int main (int argc, char **argv) { ...@@ -233,12 +283,14 @@ int main (int argc, char **argv) {
if(params.flag_writefile && outfile) { if(params.flag_writefile && outfile) {
/* Close output file */ /* Close output file */
fclose(outfile); fclose(outfile);
outfile = NULL;
} }
#ifdef HAVE_LIBMSEED #ifdef HAVE_LIBMSEED
if(params.flag_writeseed && data_seed.outfile_mseed) { if(params.flag_writeseed && data_seed.outfile_mseed) {
/* Close output Mini-SEED file */ /* Close output Mini-SEED file */
fclose(data_seed.outfile_mseed); fclose(data_seed.outfile_mseed);
data_seed.outfile_mseed = NULL;
} }
#endif #endif
...@@ -295,9 +347,73 @@ int main (int argc, char **argv) { ...@@ -295,9 +347,73 @@ int main (int argc, char **argv) {
/* PDS Step 6: Repeat until finished: receive and handle packets */ /* PDS Step 6: Repeat until finished: receive and handle packets */
#ifdef HAVE_LIBMSEED
if(params.flag_writeseed) {
/* Open output Mini-SEED file */
sprintf(data_seed.filename_mseed, "%s.realtime.miniseed",
(params.network)? params.network : DEFAULT_NETWORK);
data_seed.outfile_mseed = fopen(data_seed.filename_mseed, "w");
if(!data_seed.outfile_mseed) {
nmxp_log(1, 0, "Can not to open file %s!", data_seed.filename_mseed);
} else {
nmxp_log(0, 1, "Opened file %s!\n", data_seed.filename_mseed);
}
}
#endif
while(1) { while(1) {
/* Process Compressed or Decompressed Data */ /* Process Compressed or Decompressed Data */
nmxp_receiveData(naqssock, channelList_subset, &nmxp_data_log); pd = nmxp_receiveData(naqssock, channelList_subset);
/* Log contents of last packet */
nmxp_data_log(pd);
#ifdef HAVE_LIBMSEED
/* Write Mini-SEED record */
if(params.flag_writeseed) {
int *pprev_xn = NULL;
if( (i_chan = nmxp_chan_lookupKeyIndex(pd->key, channelList_subset)) != -1) {
if(channelListSupport[i_chan].significant) {
pprev_xn = &(channelListSupport[i_chan].prev_xn);
}
nmxp_data_msr_pack(pd, &data_seed, NULL);
/* First time */
if(!channelListSupport[i_chan].significant) {
channelListSupport[i_chan].significant = 1;
channelListSupport[i_chan].prev_xn = pd->pDataPtr[pd->nSamp-1];
}
channelListSupport[i_chan].last_time = pd->time + ((double) pd->nSamp / (double) pd->sampRate);
} else {
nmxp_log(1, 0, "Key %d not found in channelList_subset!\n", pd->key);
}
}
#endif
if(pd->buffer) {
free(pd->buffer);
pd->buffer = NULL;
}
}
#ifdef HAVE_LIBMSEED
if(params.flag_writeseed && data_seed.outfile_mseed) {
/* Close output Mini-SEED file */
fclose(data_seed.outfile_mseed);
}
if(channelListSupport) {
free(channelListSupport);
}
#endif
if(channelList_subset) {
free(channelList_subset);
} }
/* PDS Step 7: Send Terminate Subscription */ /* PDS Step 7: Send Terminate Subscription */
...@@ -320,4 +436,42 @@ int main (int argc, char **argv) { ...@@ -320,4 +436,42 @@ int main (int argc, char **argv) {
/* Do any needed cleanup and exit */
static void clientShutdown(int sig) {
nmxp_log(0, 0, "Program interrupted!\n");
if(params.flag_writefile && outfile) {
/* Close output file */
fclose(outfile);
}
#ifdef HAVE_LIBMSEED
if(params.flag_writeseed && data_seed.outfile_mseed) {
/* Close output Mini-SEED file */
fclose(data_seed.outfile_mseed);
}
#endif
/* PDS Step 7: Send Terminate Subscription */
nmxp_sendTerminateSubscription(naqssock, NMXP_SHUTDOWN_NORMAL, "Good Bye!");
/* PDS Step 8: Close the socket */
nmxp_closeSocket(naqssock);
if(channelList == NULL) {
free(channelList);
}
if(channelList_subset == NULL) {
free(channelList_subset);
}
if(channelListSupport == NULL) {
free(channelListSupport);
}
exit( sig );
} /* End of clientShutdown() */
/* Empty signal handler routine */
static void clientDummyHandler(int sig) {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment