Sat, 26 Nov 2022 17:07:08 +0100
refactore http listener creation
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
35
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
29 | #include <cx/hash_map.h> |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
30 | #include "../util/atomic.h" |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include "event.h" |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
34 | CxMap *event_handler_map = NULL; |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | int numevhandlers = 0; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
37 | EVHandler *default_event_handler = NULL; |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
39 | EVHandler *last_handler_c = NULL; |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | int create_event_handler(EventHandlerConfig *cfg) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | if(event_handler_map == NULL) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
43 | event_handler_map = cxHashMapCreate(cxDefaultAllocator, 16); |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
46 | CxHashKey key = cx_hash_key_bytes((const unsigned char*)cfg->name.ptr, cfg->name.length); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
47 | |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | /* if the event handler already exists, we don't modify it */ |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
49 | if(cxMapGet(event_handler_map, key)) { |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | /* TODO: log message */ |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | /* TODO: set reload status */ |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | return 1; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | /* create new handler */ |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
56 | EVHandler *e = evhandler_create(cfg); |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | if(e == NULL) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | return 1; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | if(cfg->isdefault) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | if(default_event_handler) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | /* there can be only one default event handler */ |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | /* TODO: log warning */ |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | default_event_handler = e; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
69 | int ret = cxMapPut(event_handler_map, key, e); |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | if(ret == 0) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | last_handler_c = e; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | numevhandlers++; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | return ret; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | /* |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | * checks if there is at least one event handler and a default handler |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | * if necessary, check_event_handler_cfg() creates a default event handler |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | */ |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | int check_event_handler_cfg() { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | if(numevhandlers > 0 ) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | if(default_event_handler) { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | return 0; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | } else { |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | default_event_handler = last_handler_c; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | return 0; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | EventHandlerConfig cfg; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
93 | cfg.name = cx_str("default"); |
30
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | cfg.nthreads = 1; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | cfg.isdefault = 1; |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | return create_event_handler(&cfg); |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | } |
27c7511c0e34
new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
100 | |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
101 | EVHandler* get_default_event_handler() { |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
102 | return default_event_handler; |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
103 | } |
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
104 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
105 | EVHandler* get_event_handler(const char *name) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
106 | return cxMapGet(event_handler_map, cx_hash_key_str(name)); |
35
4417619a9bbd
using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
30
diff
changeset
|
107 | } |
159
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
108 | |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
109 | EventHandler* ev_instance(EVHandler *ev) { |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
110 | int nev = ev->numins; |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
111 | if(nev == 1) { |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
112 | return ev->instances[0]; |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
113 | } |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
114 | |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
115 | int ins = ev->current & nev; |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
116 | ws_atomic_inc32(&ev->current); |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
117 | return ev->instances[ins]; |
9ba9f8befa80
makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
92
diff
changeset
|
118 | } |