Rev 2878 | Rev 2883 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2878 | Rev 2880 | ||
---|---|---|---|
Line 4... | Line 4... | ||
4 | /** @file |
4 | /** @file |
5 | */ |
5 | */ |
6 | 6 | ||
7 | #include <stdio.h> |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
8 | #include <stdlib.h> |
- | 9 | #include <ipc/ipc.h> |
|
9 | #include <libadt/hash_table.h> |
10 | #include <libadt/hash_table.h> |
10 | 11 | ||
11 | #include "proto.h" |
12 | #include "proto.h" |
12 | 13 | ||
13 | #define SRV_PROTO_TABLE_CHAINS 32 |
14 | #define SRV_PROTO_TABLE_CHAINS 32 |
- | 15 | #define METHOD_OPER_TABLE_CHAINS 32 |
|
14 | 16 | ||
15 | hash_table_t srv_proto; |
17 | hash_table_t srv_proto; |
16 | 18 | ||
17 | typedef struct { |
19 | typedef struct { |
18 | int srv; |
20 | int srv; |
19 | proto_t *proto; |
21 | proto_t *proto; |
20 | link_t link; |
22 | link_t link; |
21 | } srv_proto_t; |
23 | } srv_proto_t; |
22 | 24 | ||
- | 25 | typedef struct { |
|
- | 26 | ipcarg_t method; |
|
- | 27 | oper_t *oper; |
|
- | 28 | link_t link; |
|
- | 29 | } method_oper_t; |
|
- | 30 | ||
23 | hash_index_t srv_proto_hash(unsigned long key[]) |
31 | hash_index_t srv_proto_hash(unsigned long key[]) |
24 | { |
32 | { |
25 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
33 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
26 | } |
34 | } |
27 | 35 | ||
Line 43... | Line 51... | ||
43 | .hash = srv_proto_hash, |
51 | .hash = srv_proto_hash, |
44 | .compare = srv_proto_compare, |
52 | .compare = srv_proto_compare, |
45 | .remove_callback = srv_proto_remove_callback |
53 | .remove_callback = srv_proto_remove_callback |
46 | }; |
54 | }; |
47 | 55 | ||
- | 56 | hash_index_t method_oper_hash(unsigned long key[]) |
|
- | 57 | { |
|
- | 58 | return key[0] % METHOD_OPER_TABLE_CHAINS; |
|
- | 59 | } |
|
- | 60 | ||
- | 61 | int method_oper_compare(unsigned long key[], hash_count_t keys, |
|
- | 62 | link_t *item) |
|
- | 63 | { |
|
- | 64 | method_oper_t *mo; |
|
- | 65 | ||
- | 66 | mo = hash_table_get_instance(item, method_oper_t, link); |
|
- | 67 | ||
- | 68 | return key[0] == mo->method; |
|
- | 69 | } |
|
- | 70 | ||
- | 71 | void method_oper_remove_callback(link_t *item) |
|
- | 72 | { |
|
- | 73 | } |
|
- | 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 | ||
- | 81 | ||
48 | void proto_init(void) |
82 | void proto_init(void) |
49 | { |
83 | { |
50 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
84 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
51 | &srv_proto_ops); |
85 | &srv_proto_ops); |
52 | } |
86 | } |
Line 81... | Line 115... | ||
81 | 115 | ||
82 | sp = hash_table_get_instance(item, srv_proto_t, link); |
116 | sp = hash_table_get_instance(item, srv_proto_t, link); |
83 | return sp->proto; |
117 | return sp->proto; |
84 | } |
118 | } |
85 | 119 | ||
- | 120 | static void proto_struct_init(proto_t *proto, char *name) |
|
- | 121 | { |
|
- | 122 | proto->name = name; |
|
- | 123 | hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1, |
|
- | 124 | &method_oper_ops); |
|
- | 125 | } |
|
- | 126 | ||
- | 127 | proto_t *proto_new(char *name) |
|
- | 128 | { |
|
- | 129 | proto_t *p; |
|
- | 130 | ||
- | 131 | p = malloc(sizeof(proto_t)); |
|
- | 132 | proto_struct_init(p, name); |
|
- | 133 | ||
- | 134 | return p; |
|
- | 135 | } |
|
- | 136 | ||
- | 137 | void proto_add_oper(proto_t *proto, int method, oper_t *oper) |
|
- | 138 | { |
|
- | 139 | method_oper_t *mo; |
|
- | 140 | unsigned long key; |
|
- | 141 | ||
- | 142 | mo = malloc(sizeof(method_oper_t)); |
|
- | 143 | mo->method = method; |
|
- | 144 | mo->oper = oper; |
|
- | 145 | key = method; |
|
- | 146 | ||
- | 147 | hash_table_insert(&proto->method_oper, &key, &mo->link); |
|
- | 148 | } |
|
- | 149 | ||
- | 150 | oper_t *proto_get_oper(proto_t *proto, int method) |
|
- | 151 | { |
|
- | 152 | unsigned long key; |
|
- | 153 | link_t *item; |
|
- | 154 | method_oper_t *mo; |
|
- | 155 | ||
- | 156 | key = method; |
|
- | 157 | item = hash_table_find(&proto->method_oper, &key); |
|
- | 158 | if (item == NULL) return NULL; |
|
- | 159 | ||
- | 160 | mo = hash_table_get_instance(item, method_oper_t, link); |
|
- | 161 | return mo->oper; |
|
- | 162 | } |
|
- | 163 | ||
86 | /** @} |
164 | /** @} |
87 | */ |
165 | */ |