Rev 2878 | Rev 2883 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2878 | svoboda | 1 | /** @addtogroup sctrace |
2 | * @{ |
||
3 | */ |
||
4 | /** @file |
||
5 | */ |
||
6 | |||
7 | #include <stdio.h> |
||
8 | #include <stdlib.h> |
||
2880 | svoboda | 9 | #include <ipc/ipc.h> |
2878 | svoboda | 10 | #include <libadt/hash_table.h> |
11 | |||
12 | #include "proto.h" |
||
13 | |||
14 | #define SRV_PROTO_TABLE_CHAINS 32 |
||
2880 | svoboda | 15 | #define METHOD_OPER_TABLE_CHAINS 32 |
2878 | svoboda | 16 | |
17 | hash_table_t srv_proto; |
||
18 | |||
19 | typedef struct { |
||
20 | int srv; |
||
21 | proto_t *proto; |
||
22 | link_t link; |
||
23 | } srv_proto_t; |
||
24 | |||
2880 | svoboda | 25 | typedef struct { |
26 | ipcarg_t method; |
||
27 | oper_t *oper; |
||
28 | link_t link; |
||
29 | } method_oper_t; |
||
30 | |||
2878 | svoboda | 31 | hash_index_t srv_proto_hash(unsigned long key[]) |
32 | { |
||
33 | return key[0] % SRV_PROTO_TABLE_CHAINS; |
||
34 | } |
||
35 | |||
36 | int srv_proto_compare(unsigned long key[], hash_count_t keys, |
||
37 | link_t *item) |
||
38 | { |
||
39 | srv_proto_t *sp; |
||
40 | |||
41 | sp = hash_table_get_instance(item, srv_proto_t, link); |
||
42 | |||
43 | return key[0] == sp->srv; |
||
44 | } |
||
45 | |||
46 | void srv_proto_remove_callback(link_t *item) |
||
47 | { |
||
48 | } |
||
49 | |||
50 | hash_table_operations_t srv_proto_ops = { |
||
51 | .hash = srv_proto_hash, |
||
52 | .compare = srv_proto_compare, |
||
53 | .remove_callback = srv_proto_remove_callback |
||
54 | }; |
||
55 | |||
2880 | svoboda | 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 | |||
2878 | svoboda | 82 | void proto_init(void) |
83 | { |
||
84 | hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1, |
||
85 | &srv_proto_ops); |
||
86 | } |
||
87 | |||
88 | void proto_cleanup(void) |
||
89 | { |
||
90 | hash_table_destroy(&srv_proto); |
||
91 | } |
||
92 | |||
93 | void proto_register(int srv, proto_t *proto) |
||
94 | { |
||
95 | srv_proto_t *sp; |
||
96 | unsigned long key; |
||
97 | |||
98 | sp = malloc(sizeof(srv_proto_t)); |
||
99 | sp->srv = srv; |
||
100 | sp->proto = proto; |
||
101 | key = srv; |
||
102 | |||
103 | hash_table_insert(&srv_proto, &key, &sp->link); |
||
104 | } |
||
105 | |||
106 | proto_t *proto_get_by_srv(int srv) |
||
107 | { |
||
108 | unsigned long key; |
||
109 | link_t *item; |
||
110 | srv_proto_t *sp; |
||
111 | |||
112 | key = srv; |
||
113 | item = hash_table_find(&srv_proto, &key); |
||
114 | if (item == NULL) return NULL; |
||
115 | |||
116 | sp = hash_table_get_instance(item, srv_proto_t, link); |
||
117 | return sp->proto; |
||
118 | } |
||
119 | |||
2880 | svoboda | 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 | |||
2878 | svoboda | 164 | /** @} |
165 | */ |