source: examples/skeleton/event.c @ c988da9

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since c988da9 was 0f42351, checked in by Perry Lorier <perry@…>, 14 years ago

Lots more warnings squashed

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