Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / apps / libevent-test.c @ master

History | View | Annotate | Download (2.73 KB)

1 055d3009 Eric Allman
/*
2
**        ----- BEGIN LICENSE BLOCK -----
3
**        Applications for the Global Data Plane
4
**        From the Ubiquitous Swarm Lab, 490 Cory Hall, U.C. Berkeley.
5
**
6 c87dd166 Eric Allman
**        Copyright (c) 2015-2019, Regents of the University of California.
7 6bd5476b Eric Allman
**        All rights reserved.
8 055d3009 Eric Allman
**
9 6bd5476b Eric Allman
**        Permission is hereby granted, without written agreement and without
10
**        license or royalty fees, to use, copy, modify, and distribute this
11
**        software and its documentation for any purpose, provided that the above
12
**        copyright notice and the following two paragraphs appear in all copies
13
**        of this software.
14 055d3009 Eric Allman
**
15 6bd5476b Eric Allman
**        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
16
**        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
17
**        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
18
**        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 055d3009 Eric Allman
**
20 6bd5476b Eric Allman
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
21 055d3009 Eric Allman
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 6bd5476b Eric Allman
**        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
23
**        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
24
**        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25
**        OR MODIFICATIONS.
26 055d3009 Eric Allman
**        ----- END LICENSE BLOCK -----
27
*/
28
29 47c6ea64 Eric Allman
#include <stdlib.h>
30
#include <fcntl.h>
31
#include <sys/types.h>
32
#include <sys/uio.h>
33
#include <unistd.h>
34
#include <stdio.h>
35
#include <event2/event.h>
36
37
#define RENDEZVOUS        "./r"
38
39
void
40
read_cb(evutil_socket_t fd, short what, void *arg)
41
{
42
        int i;
43
        char buf[4];
44
45
        printf("Data to read on fd = %d\n", fd);
46
        i = read(fd, buf, sizeof buf);
47
        if (i < 0)
48
                perror("read");
49
        else
50
                printf("%*s\n", i, buf);
51
}
52
53
int
54
main(int argc, char **argv)
55
{
56
        struct event_base *eb;
57
        struct event *ev;
58
        int fd;
59
        int i;
60
61
        // set up the rendezvous file (create if necessary, but we will read)
62
        fd = open(RENDEZVOUS, O_RDONLY | O_CREAT, 0777);
63
        if (fd < 0)
64
        {
65
                perror(RENDEZVOUS);
66
                exit(1);
67
        }
68
69
        // we need arbitrary fd access
70
        {
71
                struct event_config *ev_cfg = event_config_new();
72
73
                event_config_require_features(ev_cfg, EV_FEATURE_FDS);
74
75
                // set up the event to read the rendezvous file
76
                eb = event_base_new_with_config(ev_cfg);
77
                if (eb == NULL)
78
                {
79
                        perror("Cannot create event_base");
80
                        exit(1);
81
                }
82
                event_config_free(ev_cfg);
83
        }
84
85
        // just for yucks, let's see what it can do
86
        printf("Base method: %s\n", event_base_get_method(eb));
87
        i = event_base_get_features(eb);
88
        if (i & EV_FEATURE_ET)
89
                printf("  Edge-triggered events\n");
90
        if (i & EV_FEATURE_O1)
91
                printf("  O(1) notification\n");
92
        if (i & EV_FEATURE_FDS)
93
                printf("  All fd types\n");
94
95
        // set up an event
96
        ev = event_new(eb, fd, EV_READ | EV_PERSIST, read_cb, NULL);
97
        event_add(ev, NULL);
98
99
        // now run the actual loop
100
        i = event_base_loop(eb, 0);
101
        printf("event_base_loop => %d\n", i);
102
103
        exit(i);
104
}