2
* Copyright (c) 2008 Jiri Svoboda
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
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
15
* derived from this software without specific prior written permission.
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
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
#include <adt/hash_table.h>
43
#define SRV_PROTO_TABLE_CHAINS 32
44
#define METHOD_OPER_TABLE_CHAINS 32
46
hash_table_t srv_proto;
60
static hash_index_t srv_proto_hash(unsigned long key[]);
61
static int srv_proto_compare(unsigned long key[], hash_count_t keys,
63
static void srv_proto_remove_callback(link_t *item);
65
hash_table_operations_t srv_proto_ops = {
66
.hash = srv_proto_hash,
67
.compare = srv_proto_compare,
68
.remove_callback = srv_proto_remove_callback
71
static hash_index_t method_oper_hash(unsigned long key[]);
72
static int method_oper_compare(unsigned long key[], hash_count_t keys,
74
static void method_oper_remove_callback(link_t *item);
76
hash_table_operations_t method_oper_ops = {
77
.hash = method_oper_hash,
78
.compare = method_oper_compare,
79
.remove_callback = method_oper_remove_callback
82
static hash_index_t srv_proto_hash(unsigned long key[])
84
return key[0] % SRV_PROTO_TABLE_CHAINS;
87
static int srv_proto_compare(unsigned long key[], hash_count_t keys,
92
sp = hash_table_get_instance(item, srv_proto_t, link);
94
return key[0] == sp->srv;
97
static void srv_proto_remove_callback(link_t *item)
101
static hash_index_t method_oper_hash(unsigned long key[])
103
return key[0] % METHOD_OPER_TABLE_CHAINS;
106
static int method_oper_compare(unsigned long key[], hash_count_t keys,
111
mo = hash_table_get_instance(item, method_oper_t, link);
113
return key[0] == mo->method;
116
static void method_oper_remove_callback(link_t *item)
121
void proto_init(void)
123
hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1,
127
void proto_cleanup(void)
129
hash_table_destroy(&srv_proto);
132
void proto_register(int srv, proto_t *proto)
137
sp = malloc(sizeof(srv_proto_t));
142
hash_table_insert(&srv_proto, &key, &sp->link);
145
proto_t *proto_get_by_srv(int srv)
152
item = hash_table_find(&srv_proto, &key);
153
if (item == NULL) return NULL;
155
sp = hash_table_get_instance(item, srv_proto_t, link);
159
static void proto_struct_init(proto_t *proto, char *name)
162
hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1,
166
proto_t *proto_new(char *name)
170
p = malloc(sizeof(proto_t));
171
proto_struct_init(p, name);
176
void proto_delete(proto_t *proto)
181
void proto_add_oper(proto_t *proto, int method, oper_t *oper)
186
mo = malloc(sizeof(method_oper_t));
191
hash_table_insert(&proto->method_oper, &key, &mo->link);
194
oper_t *proto_get_oper(proto_t *proto, int method)
201
item = hash_table_find(&proto->method_oper, &key);
202
if (item == NULL) return NULL;
204
mo = hash_table_get_instance(item, method_oper_t, link);
208
static void oper_struct_init(oper_t *oper, char *name)
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)
219
o = malloc(sizeof(oper_t));
220
oper_struct_init(o, name);
223
for (i = 0; i < argc; i++)
224
o->arg_type[i] = arg_types[i];
226
o->rv_type = rv_type;
229
for (i = 0; i < respc; i++)
230
o->resp_type[i] = resp_types[i];