#include #include #include #include #include #include #include #include #include "common.h" #include "List.h" int RecvMsg(int pipe, msg_t *m) { return read(pipe, m, sizeof(msg_t)); } void Usage(char *program) { fprintf(stderr, "Usage: %s \n", program); exit(1); } // if we are on a sparc/solaris /* double Time_GetSeconds() { struct timespec timeClock; double retVal; clock_gettime(CLOCK_REALTIME, &timeClock); retVal = (double) timeClock.tv_sec; retVal += (double) timeClock.tv_nsec / (double)1e9; return (retVal); } */ // otherwise, just use gettimeofday() double Time_GetSeconds() { struct timeval t; int rc = gettimeofday(&t, NULL); assert(rc == 0); return (double) ((double)t.tv_sec + (double)t.tv_usec / 1e6); } int main(int argc, char *argv[]) { if (argc != 2) { Usage(argv[0]); } char *fifoFile = argv[1]; // check that it's a fifo if (IsFifo(fifoFile) == 0) { fprintf(stderr, "%s is not a FIFO!\n", fifoFile); Usage(argv[0]); } // open up the pipe and check for errors, exiting on a problem int pipe = open(fifoFile, O_RDONLY); if (pipe < 0) { perror("pipe"); Usage(argv[0]); } // main loop - process messages until there is a problem List L; double startTime = -1.0; while (1) { msg_t m; int rc = RecvMsg(pipe, &m); // painful: could got lots of unexpected values here if (rc == 0) { // end of pipe! fprintf(stderr, "Unexpected closure of remote pipe, exiting\n"); exit(0); } else if (rc < (int) sizeof(m)) { // could deal with this in a better way, but oh well fprintf(stderr, "Error: Message of unexpected size (%d), ignoring\n", rc); continue; } else if (rc < 0) { perror("read"); continue; } // got a valid message if (m.type == DATA) { double time; if (startTime < 0.0) { startTime = Time_GetSeconds(); time = 0.0; } else { time = Time_GetSeconds() - startTime; } // insert data into list in time order L.Insert(time, m.data); } else if (m.type == END) { printf("Time of END message: %d\n", (int)((1e3 * (Time_GetSeconds() - startTime)) + 0.5)); L.Print(); exit(0); } else { fprintf(stderr, "Error: message type not understood (%d)\n", (int) m.type); } } return 0; }