summaryrefslogtreecommitdiffstats
path: root/server/tests/basic_event_loop.c
blob: 14dec512fec62c8dcc4a5ce77536bf535f37370d (plain)
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
#include <stdlib.h>
#include <stdio.h>

#include "basic_event_loop.h"

#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);

static SpiceCoreInterface core;

static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
{
    NOT_IMPLEMENTED
}

static void timer_start(SpiceTimer *timer, uint32_t ms)
{
    NOT_IMPLEMENTED
}

static void timer_cancel(SpiceTimer *timer)
{
    NOT_IMPLEMENTED
}

static void timer_remove(SpiceTimer *timer)
{
    NOT_IMPLEMENTED
}

struct SpiceWatch {
    int id;
};

typedef struct Watch {
    SpiceWatch id;
    int fd;
    int event_mask;
    SpiceWatchFunc func;
    void *opaque;
} Watch;

Watch watches[100];
int watch_count = 0;
int next_id = 1;

static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    watches[watch_count].fd = fd;
    watches[watch_count].event_mask = event_mask;
    watches[watch_count].func = func;
    watches[watch_count].opaque = opaque;
    watches[watch_count].id.id = next_id++;
    return &watches[watch_count++].id;
}

static void watch_update_mask(SpiceWatch *watch, int event_mask)
{
    int i;
    Watch *my_watch;

    for (i = 0 ; i < watch_count; ++i) {
        if (watches[i].id.id == watch->id) {
            my_watch = &watches[i];
            if (my_watch->event_mask != event_mask) {
                my_watch->event_mask = event_mask;
            }
            return;
        }
    }
}

static void watch_remove(SpiceWatch *watch)
{
    int i;

    for (i = 0 ; i < watch_count ; ++i) {
        if (watches[i].id.id == watch->id) {
            watches[i] = watches[--watch_count];
            return;
        }
    }
}

static void channel_event(int event, SpiceChannelEventInfo *info)
{
    NOT_IMPLEMENTED
}

void basic_event_loop_mainloop(void)
{
    fd_set rfds, wfds;
    int max_fd = -1;
    int i;
    int retval;

    while (1) {
        FD_ZERO(&rfds);
        FD_ZERO(&wfds);
        for (i = 0 ; i < watch_count; ++i) {
            Watch* watch = &watches[i];
            if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
                FD_SET(watch->fd, &rfds);
                max_fd = watch->fd > max_fd ? watch->fd : max_fd;
            }
            if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
                FD_SET(watch->fd, &wfds);
                max_fd = watch->fd > max_fd ? watch->fd : max_fd;
            }
        }
        retval = select(max_fd + 1, &rfds, &wfds, NULL, NULL);
        if (retval == -1) {
            printf("error in select - exiting\n");
            exit(-1);
        }
        if (retval) {
            for (i = 0 ; i < watch_count; ++i) {
                Watch* watch = &watches[i];
                if ((watch->event_mask & SPICE_WATCH_EVENT_READ) && FD_ISSET(watch->fd, &rfds)) {
                    watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
                }
                if ((watch->event_mask & SPICE_WATCH_EVENT_WRITE) && FD_ISSET(watch->fd, &wfds)) {
                    watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
                }
            }
        }
    }
}

SpiceCoreInterface *basic_event_loop_init(void)
{
    bzero(&core, sizeof(core));
    core.base.major_version = SPICE_INTERFACE_CORE_MAJOR;
    core.base.minor_version = SPICE_INTERFACE_CORE_MINOR; // anything less then 3 and channel_event isn't called
    core.timer_add = timer_add;
    core.timer_start = timer_start;
    core.timer_cancel = timer_cancel;
    core.timer_remove = timer_remove;
    core.watch_add = watch_add;
    core.watch_update_mask = watch_update_mask;
    core.watch_remove = watch_remove;
    core.channel_event = channel_event;
    return &core;
}