Rev 2894 | Rev 3435 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2894 | Rev 2946 | ||
---|---|---|---|
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 sctrace |
29 | /** @addtogroup sctrace |
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 <libadt/hash_table.h> |
38 | #include <libadt/hash_table.h> |
39 | 39 | ||
40 | #include "proto.h" |
40 | #include "proto.h" |
41 | 41 | ||
42 | #define SRV_PROTO_TABLE_CHAINS 32 |
42 | #define SRV_PROTO_TABLE_CHAINS 32 |
43 | #define METHOD_OPER_TABLE_CHAINS 32 |
43 | #define METHOD_OPER_TABLE_CHAINS 32 |
44 | 44 | ||
45 | hash_table_t srv_proto; |
45 | hash_table_t srv_proto; |
46 | 46 | ||
47 | typedef struct { |
47 | typedef struct { |
48 | int srv; |
48 | int srv; |
49 | proto_t *proto; |
49 | proto_t *proto; |
50 | link_t link; |
50 | link_t link; |
51 | } srv_proto_t; |
51 | } srv_proto_t; |
52 | 52 | ||
53 | typedef struct { |
53 | typedef struct { |
54 | ipcarg_t method; |
54 | ipcarg_t method; |
55 | oper_t *oper; |
55 | oper_t *oper; |
56 | link_t link; |
56 | link_t link; |
57 | } method_oper_t; |
57 | } method_oper_t; |
58 | 58 | ||
- | 59 | static hash_index_t srv_proto_hash(unsigned long key[]); |
|
- | 60 | static int srv_proto_compare(unsigned long key[], hash_count_t keys, |
|
- | 61 | link_t *item); |
|
- | 62 | static void srv_proto_remove_callback(link_t *item); |
|
- | 63 | ||
- | 64 | hash_table_operations_t srv_proto_ops = { |
|
- | 65 | .hash = srv_proto_hash, |
|
- | 66 | .compare = srv_proto_compare, |
|
- | 67 | .remove_callback = srv_proto_remove_callback |
|
- | 68 | }; |
|
- | 69 | ||
- | 70 | static hash_index_t method_oper_hash(unsigned long key[]); |
|
- | 71 | static int method_oper_compare(unsigned long key[], hash_count_t keys, |
|
- | 72 | link_t *item); |
|
- | 73 | static void method_oper_remove_callback(link_t *item); |
|
- | 74 | ||
- | 75 | hash_table_operations_t method_oper_ops = { |
|
- | 76 | .hash = method_oper_hash, |
|
- | 77 | .compare = method_oper_compare, |
|
- | 78 | .remove_callback = method_oper_remove_callback |
|
- | 79 | }; |
|
- | 80 | ||
59 | hash_index_t srv_proto_hash(unsigned long key[]) |
81 | static hash_index_t srv_proto_hash(unsigned long key[]) |
60 | { |
82 | { |
61 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
83 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
62 | } |
84 | } |
63 | 85 | ||
64 | int srv_proto_compare(unsigned long key[], hash_count_t keys, |
86 | static int srv_proto_compare(unsigned long key[], hash_count_t keys, |
65 | link_t *item) |
87 | link_t *item) |
66 | { |
88 | { |
67 | srv_proto_t *sp; |
89 | srv_proto_t *sp; |
68 | 90 | ||
69 | sp = hash_table_get_instance(item, srv_proto_t, link); |
91 | sp = hash_table_get_instance(item, srv_proto_t, link); |
70 | 92 | ||
71 | return key[0] == sp->srv; |
93 | return key[0] == sp->srv; |
72 | } |
94 | } |
73 | 95 | ||
74 | void srv_proto_remove_callback(link_t *item) |
96 | static void srv_proto_remove_callback(link_t *item) |
75 | { |
97 | { |
76 | } |
98 | } |
77 | 99 | ||
78 | hash_table_operations_t srv_proto_ops = { |
- | |
79 | .hash = srv_proto_hash, |
- | |
80 | .compare = srv_proto_compare, |
- | |
81 | .remove_callback = srv_proto_remove_callback |
- | |
82 | }; |
- | |
83 | - | ||
84 | hash_index_t method_oper_hash(unsigned long key[]) |
100 | static hash_index_t method_oper_hash(unsigned long key[]) |
85 | { |
101 | { |
86 | return key[0] % METHOD_OPER_TABLE_CHAINS; |
102 | return key[0] % METHOD_OPER_TABLE_CHAINS; |
87 | } |
103 | } |
88 | 104 | ||
89 | int method_oper_compare(unsigned long key[], hash_count_t keys, |
105 | static int method_oper_compare(unsigned long key[], hash_count_t keys, |
90 | link_t *item) |
106 | link_t *item) |
91 | { |
107 | { |
92 | method_oper_t *mo; |
108 | method_oper_t *mo; |
93 | 109 | ||
94 | mo = hash_table_get_instance(item, method_oper_t, link); |
110 | mo = hash_table_get_instance(item, method_oper_t, link); |
95 | 111 | ||
96 | return key[0] == mo->method; |
112 | return key[0] == mo->method; |
97 | } |
113 | } |
98 | 114 | ||
99 | void method_oper_remove_callback(link_t *item) |
115 | static void method_oper_remove_callback(link_t *item) |
100 | { |
116 | { |
101 | } |
117 | } |
102 | 118 | ||
103 | hash_table_operations_t method_oper_ops = { |
- | |
104 | .hash = method_oper_hash, |
- | |
105 | .compare = method_oper_compare, |
- | |
106 | .remove_callback = method_oper_remove_callback |
- | |
107 | }; |
- | |
108 | - | ||
109 | 119 | ||
110 | void proto_init(void) |
120 | void proto_init(void) |
111 | { |
121 | { |
112 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
122 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
113 | &srv_proto_ops); |
123 | &srv_proto_ops); |
114 | } |
124 | } |
115 | 125 | ||
116 | void proto_cleanup(void) |
126 | void proto_cleanup(void) |
117 | { |
127 | { |
118 | hash_table_destroy(&srv_proto); |
128 | hash_table_destroy(&srv_proto); |
119 | } |
129 | } |
120 | 130 | ||
121 | void proto_register(int srv, proto_t *proto) |
131 | void proto_register(int srv, proto_t *proto) |
122 | { |
132 | { |
123 | srv_proto_t *sp; |
133 | srv_proto_t *sp; |
124 | unsigned long key; |
134 | unsigned long key; |
125 | 135 | ||
126 | sp = malloc(sizeof(srv_proto_t)); |
136 | sp = malloc(sizeof(srv_proto_t)); |
127 | sp->srv = srv; |
137 | sp->srv = srv; |
128 | sp->proto = proto; |
138 | sp->proto = proto; |
129 | key = srv; |
139 | key = srv; |
130 | 140 | ||
131 | hash_table_insert(&srv_proto, &key, &sp->link); |
141 | hash_table_insert(&srv_proto, &key, &sp->link); |
132 | } |
142 | } |
133 | 143 | ||
134 | proto_t *proto_get_by_srv(int srv) |
144 | proto_t *proto_get_by_srv(int srv) |
135 | { |
145 | { |
136 | unsigned long key; |
146 | unsigned long key; |
137 | link_t *item; |
147 | link_t *item; |
138 | srv_proto_t *sp; |
148 | srv_proto_t *sp; |
139 | 149 | ||
140 | key = srv; |
150 | key = srv; |
141 | item = hash_table_find(&srv_proto, &key); |
151 | item = hash_table_find(&srv_proto, &key); |
142 | if (item == NULL) return NULL; |
152 | if (item == NULL) return NULL; |
143 | 153 | ||
144 | sp = hash_table_get_instance(item, srv_proto_t, link); |
154 | sp = hash_table_get_instance(item, srv_proto_t, link); |
145 | return sp->proto; |
155 | return sp->proto; |
146 | } |
156 | } |
147 | 157 | ||
148 | static void proto_struct_init(proto_t *proto, char *name) |
158 | static void proto_struct_init(proto_t *proto, char *name) |
149 | { |
159 | { |
150 | proto->name = name; |
160 | proto->name = name; |
151 | hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1, |
161 | hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1, |
152 | &method_oper_ops); |
162 | &method_oper_ops); |
153 | } |
163 | } |
154 | 164 | ||
155 | proto_t *proto_new(char *name) |
165 | proto_t *proto_new(char *name) |
156 | { |
166 | { |
157 | proto_t *p; |
167 | proto_t *p; |
158 | 168 | ||
159 | p = malloc(sizeof(proto_t)); |
169 | p = malloc(sizeof(proto_t)); |
160 | proto_struct_init(p, name); |
170 | proto_struct_init(p, name); |
161 | 171 | ||
162 | return p; |
172 | return p; |
163 | } |
173 | } |
164 | 174 | ||
165 | void proto_add_oper(proto_t *proto, int method, oper_t *oper) |
175 | void proto_add_oper(proto_t *proto, int method, oper_t *oper) |
166 | { |
176 | { |
167 | method_oper_t *mo; |
177 | method_oper_t *mo; |
168 | unsigned long key; |
178 | unsigned long key; |
169 | 179 | ||
170 | mo = malloc(sizeof(method_oper_t)); |
180 | mo = malloc(sizeof(method_oper_t)); |
171 | mo->method = method; |
181 | mo->method = method; |
172 | mo->oper = oper; |
182 | mo->oper = oper; |
173 | key = method; |
183 | key = method; |
174 | 184 | ||
175 | hash_table_insert(&proto->method_oper, &key, &mo->link); |
185 | hash_table_insert(&proto->method_oper, &key, &mo->link); |
176 | } |
186 | } |
177 | 187 | ||
178 | oper_t *proto_get_oper(proto_t *proto, int method) |
188 | oper_t *proto_get_oper(proto_t *proto, int method) |
179 | { |
189 | { |
180 | unsigned long key; |
190 | unsigned long key; |
181 | link_t *item; |
191 | link_t *item; |
182 | method_oper_t *mo; |
192 | method_oper_t *mo; |
183 | 193 | ||
184 | key = method; |
194 | key = method; |
185 | item = hash_table_find(&proto->method_oper, &key); |
195 | item = hash_table_find(&proto->method_oper, &key); |
186 | if (item == NULL) return NULL; |
196 | if (item == NULL) return NULL; |
187 | 197 | ||
188 | mo = hash_table_get_instance(item, method_oper_t, link); |
198 | mo = hash_table_get_instance(item, method_oper_t, link); |
189 | return mo->oper; |
199 | return mo->oper; |
190 | } |
200 | } |
191 | 201 | ||
192 | static void oper_struct_init(oper_t *oper, char *name) |
202 | static void oper_struct_init(oper_t *oper, char *name) |
193 | { |
203 | { |
194 | oper->name = name; |
204 | oper->name = name; |
195 | } |
205 | } |
196 | 206 | ||
197 | oper_t *oper_new(char *name) |
207 | oper_t *oper_new(char *name) |
198 | { |
208 | { |
199 | oper_t *o; |
209 | oper_t *o; |
200 | 210 | ||
201 | o = malloc(sizeof(oper_t)); |
211 | o = malloc(sizeof(oper_t)); |
202 | oper_struct_init(o, name); |
212 | oper_struct_init(o, name); |
203 | 213 | ||
204 | return o; |
214 | return o; |
205 | } |
215 | } |
206 | 216 | ||
207 | /** @} |
217 | /** @} |
208 | */ |
218 | */ |
209 | 219 |