source: examples/skeleton/event.c @ 0eef5d4

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

Upgrade the skeletons to the new API

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 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
51void per_packet(libtrace_packet_t *packet) {
52
53        /* Your code goes here */
54}
55
56uint32_t event_read_packet(libtrace_t *trace, libtrace_packet_t *packet) {
57        libtrace_eventobj_t obj;
58        fd_set rfds;
59        struct timeval sleep_tv;
60       
61        FD_ZERO(&rfds);
62       
63        for (;;) {
64                obj = trace_event(trace, packet);
65
66                switch(obj.type) {
67                       
68                        /* Device has no packets at present - lets wait until
69                         * it does get something */
70                        case TRACE_EVENT_IOWAIT:
71                                FD_ZERO(&rfds);
72                                FD_SET(obj.fd, &rfds);
73                                select(obj.fd + 1, &rfds, NULL, NULL, 0);
74                                continue;
75                               
76                        /* Replaying a trace in tracetime and the next packet
77                         * is not due yet */
78                        case TRACE_EVENT_SLEEP:
79                                /* select offers good precision for sleeping */
80                                sleep_tv.tv_sec = (int)obj.seconds;
81                                sleep_tv.tv_usec = (int) ((obj.seconds - sleep_tv.tv_sec) * 1000000.0);
82                                select(0, NULL, NULL, NULL, &sleep_tv);
83                                continue;
84                               
85                        /* We've got a packet! */
86                        case TRACE_EVENT_PACKET:
87                                /* Check for error first */
88                                if (obj.size == -1)
89                                        return -1;
90                                return packet->size;
91                               
92                        /* End of trace has been reached */
93                        case TRACE_EVENT_TERMINATE:
94                                return -1;
95                               
96                        /* An event we don't know about has occured */
97                        default:
98                                fprintf(stderr, "Unknown event type occured\n");
99                                return -1;
100                }
101        }
102}
103               
104int main(int argc, char *argv[]) {
105       
106        libtrace_t *trace;
107        libtrace_packet_t *packet;
108        int psize = 0;
109        char *uri = 0;
110       
111        if (argc >= 2) {
112                uri = strdup(argv[1]);
113        } else {
114                fprintf(stderr, "Usage: event_example <uri>\n");
115                return -1;
116        }
117
118        /* Create the trace */
119        trace = trace_create(uri);
120        if (trace_is_err(trace)) {
121                trace_perror(trace, "trace_create");
122                return 0;
123        }
124       
125        /* Starting the trace */
126        if (trace_start(trace) != 0) {
127                trace_perror(trace, "trace_start");
128                return 0;
129        }
130
131        packet = trace_create_packet();
132
133        for (;;) {
134                if ((psize = event_read_packet(trace, packet)) <= 0) {
135                        break;
136                }
137
138                /* Got a packet - let's do something with it */
139                per_packet(packet);
140               
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.