Rev 4509 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4509 | Rev 4681 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup trace |
29 | /** @addtogroup trace |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <stdio.h> |
35 | #include <stdio.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
37 | #include <ipc/ipc.h> |
37 | #include <ipc/ipc.h> |
38 | #include <adt/hash_table.h> |
38 | #include <adt/hash_table.h> |
39 | 39 | ||
40 | #include "trace.h" |
40 | #include "trace.h" |
41 | #include "proto.h" |
41 | #include "proto.h" |
42 | 42 | ||
43 | #define SRV_PROTO_TABLE_CHAINS 32 |
43 | #define SRV_PROTO_TABLE_CHAINS 32 |
44 | #define METHOD_OPER_TABLE_CHAINS 32 |
44 | #define METHOD_OPER_TABLE_CHAINS 32 |
45 | 45 | ||
46 | hash_table_t srv_proto; |
46 | hash_table_t srv_proto; |
47 | 47 | ||
48 | typedef struct { |
48 | typedef struct { |
49 | int srv; |
49 | unsigned srv; |
50 | proto_t *proto; |
50 | proto_t *proto; |
51 | link_t link; |
51 | link_t link; |
52 | } srv_proto_t; |
52 | } srv_proto_t; |
53 | 53 | ||
54 | typedef struct { |
54 | typedef struct { |
55 | ipcarg_t method; |
55 | ipcarg_t method; |
56 | oper_t *oper; |
56 | oper_t *oper; |
57 | link_t link; |
57 | link_t link; |
58 | } method_oper_t; |
58 | } method_oper_t; |
59 | 59 | ||
60 | static hash_index_t srv_proto_hash(unsigned long key[]); |
60 | static hash_index_t srv_proto_hash(unsigned long key[]); |
61 | static int srv_proto_compare(unsigned long key[], hash_count_t keys, |
61 | static int srv_proto_compare(unsigned long key[], hash_count_t keys, |
62 | link_t *item); |
62 | link_t *item); |
63 | static void srv_proto_remove_callback(link_t *item); |
63 | static void srv_proto_remove_callback(link_t *item); |
64 | 64 | ||
65 | hash_table_operations_t srv_proto_ops = { |
65 | hash_table_operations_t srv_proto_ops = { |
66 | .hash = srv_proto_hash, |
66 | .hash = srv_proto_hash, |
67 | .compare = srv_proto_compare, |
67 | .compare = srv_proto_compare, |
68 | .remove_callback = srv_proto_remove_callback |
68 | .remove_callback = srv_proto_remove_callback |
69 | }; |
69 | }; |
70 | 70 | ||
71 | static hash_index_t method_oper_hash(unsigned long key[]); |
71 | static hash_index_t method_oper_hash(unsigned long key[]); |
72 | static int method_oper_compare(unsigned long key[], hash_count_t keys, |
72 | static int method_oper_compare(unsigned long key[], hash_count_t keys, |
73 | link_t *item); |
73 | link_t *item); |
74 | static void method_oper_remove_callback(link_t *item); |
74 | static void method_oper_remove_callback(link_t *item); |
75 | 75 | ||
76 | hash_table_operations_t method_oper_ops = { |
76 | hash_table_operations_t method_oper_ops = { |
77 | .hash = method_oper_hash, |
77 | .hash = method_oper_hash, |
78 | .compare = method_oper_compare, |
78 | .compare = method_oper_compare, |
79 | .remove_callback = method_oper_remove_callback |
79 | .remove_callback = method_oper_remove_callback |
80 | }; |
80 | }; |
81 | 81 | ||
82 | static hash_index_t srv_proto_hash(unsigned long key[]) |
82 | static hash_index_t srv_proto_hash(unsigned long key[]) |
83 | { |
83 | { |
84 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
84 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
85 | } |
85 | } |
86 | 86 | ||
87 | static int srv_proto_compare(unsigned long key[], hash_count_t keys, |
87 | static int srv_proto_compare(unsigned long key[], hash_count_t keys, |
88 | link_t *item) |
88 | link_t *item) |
89 | { |
89 | { |
90 | srv_proto_t *sp; |
90 | srv_proto_t *sp; |
91 | 91 | ||
92 | sp = hash_table_get_instance(item, srv_proto_t, link); |
92 | sp = hash_table_get_instance(item, srv_proto_t, link); |
93 | 93 | ||
94 | return key[0] == sp->srv; |
94 | return key[0] == sp->srv; |
95 | } |
95 | } |
96 | 96 | ||
97 | static void srv_proto_remove_callback(link_t *item) |
97 | static void srv_proto_remove_callback(link_t *item) |
98 | { |
98 | { |
99 | } |
99 | } |
100 | 100 | ||
101 | static hash_index_t method_oper_hash(unsigned long key[]) |
101 | static hash_index_t method_oper_hash(unsigned long key[]) |
102 | { |
102 | { |
103 | return key[0] % METHOD_OPER_TABLE_CHAINS; |
103 | return key[0] % METHOD_OPER_TABLE_CHAINS; |
104 | } |
104 | } |
105 | 105 | ||
106 | static int method_oper_compare(unsigned long key[], hash_count_t keys, |
106 | static int method_oper_compare(unsigned long key[], hash_count_t keys, |
107 | link_t *item) |
107 | link_t *item) |
108 | { |
108 | { |
109 | method_oper_t *mo; |
109 | method_oper_t *mo; |
110 | 110 | ||
111 | mo = hash_table_get_instance(item, method_oper_t, link); |
111 | mo = hash_table_get_instance(item, method_oper_t, link); |
112 | 112 | ||
113 | return key[0] == mo->method; |
113 | return key[0] == mo->method; |
114 | } |
114 | } |
115 | 115 | ||
116 | static void method_oper_remove_callback(link_t *item) |
116 | static void method_oper_remove_callback(link_t *item) |
117 | { |
117 | { |
118 | } |
118 | } |
119 | 119 | ||
120 | 120 | ||
121 | void proto_init(void) |
121 | void proto_init(void) |
122 | { |
122 | { |
123 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
123 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
124 | &srv_proto_ops); |
124 | &srv_proto_ops); |
125 | } |
125 | } |
126 | 126 | ||
127 | void proto_cleanup(void) |
127 | void proto_cleanup(void) |
128 | { |
128 | { |
129 | hash_table_destroy(&srv_proto); |
129 | hash_table_destroy(&srv_proto); |
130 | } |
130 | } |
131 | 131 | ||
132 | void proto_register(int srv, proto_t *proto) |
132 | void proto_register(int srv, proto_t *proto) |
133 | { |
133 | { |
134 | srv_proto_t *sp; |
134 | srv_proto_t *sp; |
135 | unsigned long key; |
135 | unsigned long key; |
136 | 136 | ||
137 | sp = malloc(sizeof(srv_proto_t)); |
137 | sp = malloc(sizeof(srv_proto_t)); |
138 | sp->srv = srv; |
138 | sp->srv = srv; |
139 | sp->proto = proto; |
139 | sp->proto = proto; |
140 | key = srv; |
140 | key = srv; |
141 | 141 | ||
142 | hash_table_insert(&srv_proto, &key, &sp->link); |
142 | hash_table_insert(&srv_proto, &key, &sp->link); |
143 | } |
143 | } |
144 | 144 | ||
145 | proto_t *proto_get_by_srv(int srv) |
145 | proto_t *proto_get_by_srv(int srv) |
146 | { |
146 | { |
147 | unsigned long key; |
147 | unsigned long key; |
148 | link_t *item; |
148 | link_t *item; |
149 | srv_proto_t *sp; |
149 | srv_proto_t *sp; |
150 | 150 | ||
151 | key = srv; |
151 | key = srv; |
152 | item = hash_table_find(&srv_proto, &key); |
152 | item = hash_table_find(&srv_proto, &key); |
153 | if (item == NULL) return NULL; |
153 | if (item == NULL) return NULL; |
154 | 154 | ||
155 | sp = hash_table_get_instance(item, srv_proto_t, link); |
155 | sp = hash_table_get_instance(item, srv_proto_t, link); |
156 | return sp->proto; |
156 | return sp->proto; |
157 | } |
157 | } |
158 | 158 | ||
159 | static void proto_struct_init(proto_t *proto, char *name) |
159 | static void proto_struct_init(proto_t *proto, char *name) |
160 | { |
160 | { |
161 | proto->name = name; |
161 | proto->name = name; |
162 | hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1, |
162 | hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1, |
163 | &method_oper_ops); |
163 | &method_oper_ops); |
164 | } |
164 | } |
165 | 165 | ||
166 | proto_t *proto_new(char *name) |
166 | proto_t *proto_new(char *name) |
167 | { |
167 | { |
168 | proto_t *p; |
168 | proto_t *p; |
169 | 169 | ||
170 | p = malloc(sizeof(proto_t)); |
170 | p = malloc(sizeof(proto_t)); |
171 | proto_struct_init(p, name); |
171 | proto_struct_init(p, name); |
172 | 172 | ||
173 | return p; |
173 | return p; |
174 | } |
174 | } |
175 | 175 | ||
176 | void proto_delete(proto_t *proto) |
176 | void proto_delete(proto_t *proto) |
177 | { |
177 | { |
178 | free(proto); |
178 | free(proto); |
179 | } |
179 | } |
180 | 180 | ||
181 | void proto_add_oper(proto_t *proto, int method, oper_t *oper) |
181 | void proto_add_oper(proto_t *proto, int method, oper_t *oper) |
182 | { |
182 | { |
183 | method_oper_t *mo; |
183 | method_oper_t *mo; |
184 | unsigned long key; |
184 | unsigned long key; |
185 | 185 | ||
186 | mo = malloc(sizeof(method_oper_t)); |
186 | mo = malloc(sizeof(method_oper_t)); |
187 | mo->method = method; |
187 | mo->method = method; |
188 | mo->oper = oper; |
188 | mo->oper = oper; |
189 | key = method; |
189 | key = method; |
190 | 190 | ||
191 | hash_table_insert(&proto->method_oper, &key, &mo->link); |
191 | hash_table_insert(&proto->method_oper, &key, &mo->link); |
192 | } |
192 | } |
193 | 193 | ||
194 | oper_t *proto_get_oper(proto_t *proto, int method) |
194 | oper_t *proto_get_oper(proto_t *proto, int method) |
195 | { |
195 | { |
196 | unsigned long key; |
196 | unsigned long key; |
197 | link_t *item; |
197 | link_t *item; |
198 | method_oper_t *mo; |
198 | method_oper_t *mo; |
199 | 199 | ||
200 | key = method; |
200 | key = method; |
201 | item = hash_table_find(&proto->method_oper, &key); |
201 | item = hash_table_find(&proto->method_oper, &key); |
202 | if (item == NULL) return NULL; |
202 | if (item == NULL) return NULL; |
203 | 203 | ||
204 | mo = hash_table_get_instance(item, method_oper_t, link); |
204 | mo = hash_table_get_instance(item, method_oper_t, link); |
205 | return mo->oper; |
205 | return mo->oper; |
206 | } |
206 | } |
207 | 207 | ||
208 | static void oper_struct_init(oper_t *oper, char *name) |
208 | static void oper_struct_init(oper_t *oper, char *name) |
209 | { |
209 | { |
210 | oper->name = name; |
210 | oper->name = name; |
211 | } |
211 | } |
212 | 212 | ||
213 | oper_t *oper_new(char *name, int argc, val_type_t *arg_types, |
213 | oper_t *oper_new(char *name, int argc, val_type_t *arg_types, |
214 | val_type_t rv_type, int respc, val_type_t *resp_types) |
214 | val_type_t rv_type, int respc, val_type_t *resp_types) |
215 | { |
215 | { |
216 | oper_t *o; |
216 | oper_t *o; |
217 | int i; |
217 | int i; |
218 | 218 | ||
219 | o = malloc(sizeof(oper_t)); |
219 | o = malloc(sizeof(oper_t)); |
220 | oper_struct_init(o, name); |
220 | oper_struct_init(o, name); |
221 | 221 | ||
222 | o->argc = argc; |
222 | o->argc = argc; |
223 | for (i = 0; i < argc; i++) |
223 | for (i = 0; i < argc; i++) |
224 | o->arg_type[i] = arg_types[i]; |
224 | o->arg_type[i] = arg_types[i]; |
225 | 225 | ||
226 | o->rv_type = rv_type; |
226 | o->rv_type = rv_type; |
227 | 227 | ||
228 | o->respc = respc; |
228 | o->respc = respc; |
229 | for (i = 0; i < respc; i++) |
229 | for (i = 0; i < respc; i++) |
230 | o->resp_type[i] = resp_types[i]; |
230 | o->resp_type[i] = resp_types[i]; |
231 | 231 | ||
232 | return o; |
232 | return o; |
233 | } |
233 | } |
234 | 234 | ||
235 | /** @} |
235 | /** @} |
236 | */ |
236 | */ |
237 | 237 |