[9a1b20c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 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.
|
---|
| 16 | *
|
---|
| 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.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup trace
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
[d9c8c81] | 37 | #include <adt/hash_table.h>
|
---|
[9a1b20c] | 38 |
|
---|
[abf3564] | 39 | #include "trace.h"
|
---|
[9a1b20c] | 40 | #include "proto.h"
|
---|
| 41 |
|
---|
| 42 |
|
---|
[062d900] | 43 | /* Maps service number to protocol */
|
---|
| 44 | static hash_table_t srv_proto;
|
---|
[9a1b20c] | 45 |
|
---|
| 46 | typedef struct {
|
---|
[062d900] | 47 | int srv;
|
---|
[9a1b20c] | 48 | proto_t *proto;
|
---|
[062d900] | 49 | ht_link_t link;
|
---|
[9a1b20c] | 50 | } srv_proto_t;
|
---|
| 51 |
|
---|
| 52 | typedef struct {
|
---|
[062d900] | 53 | int method;
|
---|
[9a1b20c] | 54 | oper_t *oper;
|
---|
[062d900] | 55 | ht_link_t link;
|
---|
[9a1b20c] | 56 | } method_oper_t;
|
---|
| 57 |
|
---|
[062d900] | 58 | /* Hash table operations. */
|
---|
[9a1b20c] | 59 |
|
---|
[062d900] | 60 | static size_t srv_proto_key_hash(void *key)
|
---|
[9a1b20c] | 61 | {
|
---|
[062d900] | 62 | return *(int *)key;
|
---|
[9a1b20c] | 63 | }
|
---|
| 64 |
|
---|
[062d900] | 65 | static size_t srv_proto_hash(const ht_link_t *item)
|
---|
[9a1b20c] | 66 | {
|
---|
[062d900] | 67 | srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
|
---|
| 68 | return sp->srv;
|
---|
[9a1b20c] | 69 | }
|
---|
| 70 |
|
---|
[062d900] | 71 | static bool srv_proto_key_equal(void *key, const ht_link_t *item)
|
---|
[9a1b20c] | 72 | {
|
---|
[062d900] | 73 | srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
|
---|
| 74 | return sp->srv == *(int *)key;
|
---|
[9a1b20c] | 75 | }
|
---|
| 76 |
|
---|
[062d900] | 77 | static hash_table_ops_t srv_proto_ops = {
|
---|
| 78 | .hash = srv_proto_hash,
|
---|
| 79 | .key_hash = srv_proto_key_hash,
|
---|
| 80 | .key_equal = srv_proto_key_equal,
|
---|
[4e00f87] | 81 | .equal = NULL,
|
---|
| 82 | .remove_callback = NULL
|
---|
[062d900] | 83 | };
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | static size_t method_oper_key_hash(void *key)
|
---|
[9a1b20c] | 87 | {
|
---|
[062d900] | 88 | return *(int *)key;
|
---|
[9a1b20c] | 89 | }
|
---|
| 90 |
|
---|
[062d900] | 91 | static size_t method_oper_hash(const ht_link_t *item)
|
---|
[9a1b20c] | 92 | {
|
---|
[062d900] | 93 | method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
|
---|
| 94 | return mo->method;
|
---|
[9a1b20c] | 95 | }
|
---|
| 96 |
|
---|
[062d900] | 97 | static bool method_oper_key_equal(void *key, const ht_link_t *item)
|
---|
[9a1b20c] | 98 | {
|
---|
[062d900] | 99 | method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
|
---|
| 100 | return mo->method == *(int *)key;
|
---|
[9a1b20c] | 101 | }
|
---|
| 102 |
|
---|
[062d900] | 103 | static hash_table_ops_t method_oper_ops = {
|
---|
| 104 | .hash = method_oper_hash,
|
---|
| 105 | .key_hash = method_oper_key_hash,
|
---|
| 106 | .key_equal = method_oper_key_equal,
|
---|
[4e00f87] | 107 | .equal = NULL,
|
---|
| 108 | .remove_callback = NULL
|
---|
[062d900] | 109 | };
|
---|
| 110 |
|
---|
[9a1b20c] | 111 |
|
---|
| 112 | void proto_init(void)
|
---|
| 113 | {
|
---|
[062d900] | 114 | /* todo: check return value. */
|
---|
| 115 | bool ok = hash_table_create(&srv_proto, 0, 0, &srv_proto_ops);
|
---|
| 116 | assert(ok);
|
---|
[9a1b20c] | 117 | }
|
---|
| 118 |
|
---|
| 119 | void proto_cleanup(void)
|
---|
| 120 | {
|
---|
| 121 | hash_table_destroy(&srv_proto);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | void proto_register(int srv, proto_t *proto)
|
---|
| 125 | {
|
---|
| 126 | srv_proto_t *sp;
|
---|
| 127 |
|
---|
| 128 | sp = malloc(sizeof(srv_proto_t));
|
---|
| 129 | sp->srv = srv;
|
---|
| 130 | sp->proto = proto;
|
---|
| 131 |
|
---|
[062d900] | 132 | hash_table_insert(&srv_proto, &sp->link);
|
---|
[9a1b20c] | 133 | }
|
---|
| 134 |
|
---|
| 135 | proto_t *proto_get_by_srv(int srv)
|
---|
| 136 | {
|
---|
[062d900] | 137 | ht_link_t *item = hash_table_find(&srv_proto, &srv);
|
---|
[9a1b20c] | 138 | if (item == NULL) return NULL;
|
---|
| 139 |
|
---|
[062d900] | 140 | srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
|
---|
[9a1b20c] | 141 | return sp->proto;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[a000878c] | 144 | static void proto_struct_init(proto_t *proto, const char *name)
|
---|
[9a1b20c] | 145 | {
|
---|
| 146 | proto->name = name;
|
---|
[062d900] | 147 | /* todo: check return value. */
|
---|
| 148 | bool ok = hash_table_create(&proto->method_oper, 0, 0, &method_oper_ops);
|
---|
| 149 | assert(ok);
|
---|
[9a1b20c] | 150 | }
|
---|
| 151 |
|
---|
[a000878c] | 152 | proto_t *proto_new(const char *name)
|
---|
[9a1b20c] | 153 | {
|
---|
| 154 | proto_t *p;
|
---|
| 155 |
|
---|
| 156 | p = malloc(sizeof(proto_t));
|
---|
| 157 | proto_struct_init(p, name);
|
---|
| 158 |
|
---|
| 159 | return p;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[8c125ad] | 162 | void proto_delete(proto_t *proto)
|
---|
| 163 | {
|
---|
| 164 | free(proto);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[9a1b20c] | 167 | void proto_add_oper(proto_t *proto, int method, oper_t *oper)
|
---|
| 168 | {
|
---|
| 169 | method_oper_t *mo;
|
---|
| 170 |
|
---|
| 171 | mo = malloc(sizeof(method_oper_t));
|
---|
| 172 | mo->method = method;
|
---|
| 173 | mo->oper = oper;
|
---|
| 174 |
|
---|
[062d900] | 175 | hash_table_insert(&proto->method_oper, &mo->link);
|
---|
[9a1b20c] | 176 | }
|
---|
| 177 |
|
---|
| 178 | oper_t *proto_get_oper(proto_t *proto, int method)
|
---|
| 179 | {
|
---|
[062d900] | 180 | ht_link_t *item = hash_table_find(&proto->method_oper, &method);
|
---|
[9a1b20c] | 181 | if (item == NULL) return NULL;
|
---|
| 182 |
|
---|
[062d900] | 183 | method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
|
---|
[9a1b20c] | 184 | return mo->oper;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[a000878c] | 187 | static void oper_struct_init(oper_t *oper, const char *name)
|
---|
[9a1b20c] | 188 | {
|
---|
| 189 | oper->name = name;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[a000878c] | 192 | oper_t *oper_new(const char *name, int argc, val_type_t *arg_types,
|
---|
[abf3564] | 193 | val_type_t rv_type, int respc, val_type_t *resp_types)
|
---|
[9a1b20c] | 194 | {
|
---|
| 195 | oper_t *o;
|
---|
[abf3564] | 196 | int i;
|
---|
[9a1b20c] | 197 |
|
---|
| 198 | o = malloc(sizeof(oper_t));
|
---|
| 199 | oper_struct_init(o, name);
|
---|
| 200 |
|
---|
[abf3564] | 201 | o->argc = argc;
|
---|
| 202 | for (i = 0; i < argc; i++)
|
---|
| 203 | o->arg_type[i] = arg_types[i];
|
---|
| 204 |
|
---|
| 205 | o->rv_type = rv_type;
|
---|
| 206 |
|
---|
| 207 | o->respc = respc;
|
---|
| 208 | for (i = 0; i < respc; i++)
|
---|
| 209 | o->resp_type[i] = resp_types[i];
|
---|
| 210 |
|
---|
[9a1b20c] | 211 | return o;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | /** @}
|
---|
| 215 | */
|
---|