|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "../ucx/map.h" |
|
30 |
|
31 #include "event.h" |
|
32 |
|
33 UcxMap *event_handler_map = NULL; |
|
34 int numevhandlers = 0; |
|
35 |
|
36 event_handler_t *default_event_handler = NULL; |
|
37 |
|
38 event_handler_t *last_handler_c = NULL; |
|
39 |
|
40 int create_event_handler(EventHandlerConfig *cfg) { |
|
41 if(event_handler_map == NULL) { |
|
42 event_handler_map = ucx_map_new(16); |
|
43 } |
|
44 |
|
45 /* if the event handler already exists, we don't modify it */ |
|
46 if(ucx_map_sstr_get(event_handler_map, cfg->name)) { |
|
47 /* TODO: log message */ |
|
48 /* TODO: set reload status */ |
|
49 return 1; |
|
50 } |
|
51 |
|
52 /* create new handler */ |
|
53 event_handler_t *e = evhandler_create(cfg->nthreads); |
|
54 if(e == NULL) { |
|
55 return 1; |
|
56 } |
|
57 |
|
58 if(cfg->isdefault) { |
|
59 if(default_event_handler) { |
|
60 /* there can be only one default event handler */ |
|
61 /* TODO: log warning */ |
|
62 } |
|
63 default_event_handler = e; |
|
64 } |
|
65 |
|
66 int ret = ucx_map_sstr_put(event_handler_map, cfg->name, e); |
|
67 if(ret == 0) { |
|
68 last_handler_c = e; |
|
69 numevhandlers++; |
|
70 } |
|
71 |
|
72 return ret; |
|
73 } |
|
74 |
|
75 /* |
|
76 * checks if there is at least one event handler and a default handler |
|
77 * if necessary, check_event_handler_cfg() creates a default event handler |
|
78 */ |
|
79 int check_event_handler_cfg() { |
|
80 if(numevhandlers > 0 ) { |
|
81 if(default_event_handler) { |
|
82 return 0; |
|
83 } else { |
|
84 default_event_handler = last_handler_c; |
|
85 return 0; |
|
86 } |
|
87 } |
|
88 |
|
89 EventHandlerConfig cfg; |
|
90 cfg.name = sstr("default"); |
|
91 cfg.nthreads = 1; |
|
92 cfg.isdefault = 1; |
|
93 |
|
94 return create_event_handler(&cfg); |
|
95 } |
|
96 |