00001
00002
00003 #include "nmlwatch.h"
00004 #include "rcs.hh"
00005
00006 extern "C"
00007 {
00008 #include <stdio.h>
00009 #include <string.h>
00010 #include <stdlib.h>
00011 #include <signal.h>
00012 }
00013
00014 static unsigned char stop = 1;
00015
00016 #if (!defined(VXWORKS) && !defined(_Windows)) || defined(__CONSOLE__) || defined(gnuwin32)
00017 int
00018 main (int argc, char **argv)
00019 {
00020 static char config_file_buf[256];
00021 static char buffer_name_buf[256];
00022 static char sample_period_buf[40];
00023 static char *config_file = NULL;
00024 static char *buffer_name = NULL;
00025 double sample_period = -1;
00026
00027 if (argc > 1)
00028 {
00029 config_file = argv[1];
00030 }
00031 if (argc > 2)
00032 {
00033 buffer_name = argv[2];
00034 }
00035 if (argc > 3)
00036 {
00037 sample_period = strtod (argv[3], NULL);
00038 }
00039
00040 if (NULL == config_file)
00041 {
00042 config_file = config_file_buf;
00043 printf ("Configuration File?\n");
00044 fgets (config_file_buf, 256, stdin);
00045 }
00046 if (NULL == buffer_name)
00047 {
00048 buffer_name = buffer_name_buf;
00049 printf ("Buffer Name?\n");
00050 fgets (buffer_name_buf, 256, stdin);
00051 }
00052 if (sample_period < 0)
00053 {
00054 printf ("Sample Period?\n");
00055 fgets (sample_period_buf, 256, stdin);
00056 sample_period = strtod (sample_period_buf, NULL);
00057 }
00058 nml_watch (config_file, buffer_name, sample_period);
00059 }
00060 #endif
00061
00062 void
00063 stop_watching_nml (int sig)
00064 {
00065 stop = 1;
00066 }
00067
00068 int
00069 format_stub (NMLTYPE type, void *buffer, CMS * cms)
00070 {
00071 return (0);
00072 }
00073
00074 int
00075 nml_watch (char *config_file, char *buffer, double sample_period)
00076 {
00077 NML *nml;
00078 RCS_TIMER *timer;
00079 NMLmsg *nmlmsg;
00080 NMLTYPE peek_ret;
00081 double start_time;
00082 rcs_print ("NMLWATCH(%s,%s,%lf) called.\n",
00083 config_file, buffer, sample_period);
00084 timer = new RCS_TIMER (sample_period);
00085 stop = 0;
00086 nml = new NML (format_stub, buffer, "nmlwatch", config_file);
00087 if (NULL == nml)
00088 {
00089 rcs_print_error ("NMLWATCH: Can't create nml channel.\n");
00090 delete timer;
00091 return -1;
00092 }
00093 if (!nml->valid ())
00094 {
00095 rcs_print_error ("NMLWATCH: Can't create nml channel.\n");
00096 delete nml;
00097 delete timer;
00098 return -1;
00099 }
00100 #ifdef sparcworks_sun4
00101 signal (SIGINT, (void (*)(int,...)) stop_watching_nml);
00102 #else
00103 signal (SIGINT, stop_watching_nml);
00104 #endif
00105 rcs_print ("Waiting for messages.\n");
00106 start_time = etime ();
00107 while (!stop)
00108 {
00109 peek_ret = nml->peek ();
00110 switch (peek_ret)
00111 {
00112 case 0:
00113 break;
00114
00115 case -1:
00116 rcs_print_error ("NMLWATCH: Can`t read nml channel.\n");
00117 delete nml;
00118 delete timer;
00119 return (-1);
00120
00121 default:
00122 nmlmsg = nml->get_address ();
00123 rcs_print ("NML message %ld of size %ld recieved at t=%lf.\n",
00124 nmlmsg->type, nmlmsg->size, etime () - start_time);
00125 break;
00126 }
00127 timer->wait ();
00128 }
00129 rcs_print ("NMLWATCH is quitting.\n");
00130 delete nml;
00131 delete timer;
00132 return (0);
00133 }