1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* This file is part of libtrace
*
* Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
* Authors: Daniel Lawson
* Perry Lorier
*
* All rights reserved.
*
* This code has been developed by the University of Waikato WAND
* research group. For further information please see http://www.wand.net.nz/
*
* libtrace is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libtrace is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libtrace; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: rate-tracetime.c 551 2005-12-15 01:16:33Z spa1 $
*
*/
/* This is a simple example program that demonstrates how to use the libtrace
* event framework. The event framework is ideal for reading from devices and
* interfaces in a non-blocking manner, and for reading from a trace in
* "tracetime" as opposed to as fast as possible.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <libtrace.h>
static void per_packet(libtrace_packet_t *packet) {
assert(packet);
/* Your code goes here */
}
static uint32_t event_read_packet(libtrace_t *trace, libtrace_packet_t *packet)
{
libtrace_eventobj_t obj;
fd_set rfds;
struct timeval sleep_tv;
FD_ZERO(&rfds);
for (;;) {
obj = trace_event(trace, packet);
switch(obj.type) {
/* Device has no packets at present - lets wait until
* it does get something */
case TRACE_EVENT_IOWAIT:
FD_ZERO(&rfds);
FD_SET(obj.fd, &rfds);
select(obj.fd + 1, &rfds, NULL, NULL, 0);
continue;
/* Replaying a trace in tracetime and the next packet
* is not due yet */
case TRACE_EVENT_SLEEP:
/* select offers good precision for sleeping */
sleep_tv.tv_sec = (int)obj.seconds;
sleep_tv.tv_usec = (int) ((obj.seconds - sleep_tv.tv_sec) * 1000000.0);
select(0, NULL, NULL, NULL, &sleep_tv);
continue;
/* We've got a packet! */
case TRACE_EVENT_PACKET:
/* Check for error first */
if (obj.size == -1)
return -1;
return 1;
/* End of trace has been reached */
case TRACE_EVENT_TERMINATE:
return -1;
/* An event we don't know about has occured */
default:
fprintf(stderr, "Unknown event type occured\n");
return -1;
}
}
}
int main(int argc, char *argv[]) {
libtrace_t *trace;
libtrace_packet_t *packet;
int psize = 0;
char *uri = 0;
if (argc >= 2) {
uri = strdup(argv[1]);
} else {
fprintf(stderr, "Usage: event_example <uri>\n");
return -1;
}
/* Create the trace */
trace = trace_create(uri);
if (trace_is_err(trace)) {
trace_perror(trace, "trace_create");
return 0;
}
/* Starting the trace */
if (trace_start(trace) != 0) {
trace_perror(trace, "trace_start");
return 0;
}
packet = trace_create_packet();
for (;;) {
if ((psize = event_read_packet(trace, packet)) <= 0) {
break;
}
/* Got a packet - let's do something with it */
per_packet(packet);
}
free(uri);
trace_destroy(trace);
trace_destroy_packet(packet);
return 0;
}
|