source: examples/skeleton/event.c @ d420777

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since d420777 was f724640, checked in by Shane Alcock <salcock@…>, 10 years ago
  • Fix warnings in the skeleton code
  • 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        assert(packet);
54        /* Your code goes here */
55}
56
57static uint32_t event_read_packet(libtrace_t *trace, libtrace_packet_t *packet) 
58{
59        libtrace_eventobj_t obj;
60        fd_set rfds;
61        struct timeval sleep_tv;
62       
63        FD_ZERO(&rfds);
64       
65        for (;;) {
66                obj = trace_event(trace, packet);
67
68                switch(obj.type) {
69                       
70                        /* Device has no packets at present - lets wait until
71                         * it does get something */
72                        case TRACE_EVENT_IOWAIT:
73                                FD_ZERO(&rfds);
74                                FD_SET(obj.fd, &rfds);
75                                select(obj.fd + 1, &rfds, NULL, NULL, 0);
76                                continue;
77                               
78                        /* Replaying a trace in tracetime and the next packet
79                         * is not due yet */
80                        case TRACE_EVENT_SLEEP:
81                                /* select offers good precision for sleeping */
82                                sleep_tv.tv_sec = (int)obj.seconds;
83                                sleep_tv.tv_usec = (int) ((obj.seconds - sleep_tv.tv_sec) * 1000000.0);
84                                select(0, NULL, NULL, NULL, &sleep_tv);
85                                continue;
86                               
87                        /* We've got a packet! */
88                        case TRACE_EVENT_PACKET:
89                                /* Check for error first */
90                                if (obj.size == -1)
91                                        return -1;
92                                return 1;
93                               
94                        /* End of trace has been reached */
95                        case TRACE_EVENT_TERMINATE:
96                                return -1;
97                               
98                        /* An event we don't know about has occured */
99                        default:
100                                fprintf(stderr, "Unknown event type occured\n");
101                                return -1;
102                }
103        }
104}
105               
106int main(int argc, char *argv[]) {
107       
108        libtrace_t *trace;
109        libtrace_packet_t *packet;
110        int psize = 0;
111        char *uri = 0;
112       
113        if (argc >= 2) {
114                uri = strdup(argv[1]);
115        } else {
116                fprintf(stderr, "Usage: event_example <uri>\n");
117                return -1;
118        }
119
120        /* Create the trace */
121        trace = trace_create(uri);
122        if (trace_is_err(trace)) {
123                trace_perror(trace, "trace_create");
124                return 0;
125        }
126       
127        /* Starting the trace */
128        if (trace_start(trace) != 0) {
129                trace_perror(trace, "trace_start");
130                return 0;
131        }
132
133        packet = trace_create_packet();
134
135        for (;;) {
136                if ((psize = event_read_packet(trace, packet)) <= 0) {
137                        break;
138                }
139
140                /* Got a packet - let's do something with it */
141                per_packet(packet);
142        }
143        free(uri);
144        trace_destroy(trace);
145        trace_destroy_packet(packet);
146        return 0;
147
148}
Note: See TracBrowser for help on using the repository browser.