| 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>
|
|---|
| 37 | #include <adt/hash_table.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "trace.h"
|
|---|
| 40 | #include "proto.h"
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | /* Maps service number to protocol */
|
|---|
| 44 | static hash_table_t srv_proto;
|
|---|
| 45 |
|
|---|
| 46 | typedef struct {
|
|---|
| 47 | int srv;
|
|---|
| 48 | proto_t *proto;
|
|---|
| 49 | ht_link_t link;
|
|---|
| 50 | } srv_proto_t;
|
|---|
| 51 |
|
|---|
| 52 | typedef struct {
|
|---|
| 53 | int method;
|
|---|
| 54 | oper_t *oper;
|
|---|
| 55 | ht_link_t link;
|
|---|
| 56 | } method_oper_t;
|
|---|
| 57 |
|
|---|
| 58 | /* Hash table operations. */
|
|---|
| 59 |
|
|---|
| 60 | static size_t srv_proto_key_hash(void *key)
|
|---|
| 61 | {
|
|---|
| 62 | return *(int *)key;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | static size_t srv_proto_hash(const ht_link_t *item)
|
|---|
| 66 | {
|
|---|
| 67 | srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
|
|---|
| 68 | return sp->srv;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static bool srv_proto_key_equal(void *key, const ht_link_t *item)
|
|---|
| 72 | {
|
|---|
| 73 | srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
|
|---|
| 74 | return sp->srv == *(int *)key;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 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,
|
|---|
| 81 | .equal = NULL,
|
|---|
| 82 | .remove_callback = NULL
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | static size_t method_oper_key_hash(void *key)
|
|---|
| 87 | {
|
|---|
| 88 | return *(int *)key;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | static size_t method_oper_hash(const ht_link_t *item)
|
|---|
| 92 | {
|
|---|
| 93 | method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
|
|---|
| 94 | return mo->method;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static bool method_oper_key_equal(void *key, const ht_link_t *item)
|
|---|
| 98 | {
|
|---|
| 99 | method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
|
|---|
| 100 | return mo->method == *(int *)key;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 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,
|
|---|
| 107 | .equal = NULL,
|
|---|
| 108 | .remove_callback = NULL
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | void proto_init(void)
|
|---|
| 113 | {
|
|---|
| 114 | /* todo: check return value. */
|
|---|
| 115 | bool ok = hash_table_create(&srv_proto, 0, 0, &srv_proto_ops);
|
|---|
| 116 | assert(ok);
|
|---|
| 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 |
|
|---|
| 132 | hash_table_insert(&srv_proto, &sp->link);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | proto_t *proto_get_by_srv(int srv)
|
|---|
| 136 | {
|
|---|
| 137 | ht_link_t *item = hash_table_find(&srv_proto, &srv);
|
|---|
| 138 | if (item == NULL) return NULL;
|
|---|
| 139 |
|
|---|
| 140 | srv_proto_t *sp = hash_table_get_inst(item, srv_proto_t, link);
|
|---|
| 141 | return sp->proto;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static void proto_struct_init(proto_t *proto, const char *name)
|
|---|
| 145 | {
|
|---|
| 146 | proto->name = name;
|
|---|
| 147 | /* todo: check return value. */
|
|---|
| 148 | bool ok = hash_table_create(&proto->method_oper, 0, 0, &method_oper_ops);
|
|---|
| 149 | assert(ok);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | proto_t *proto_new(const char *name)
|
|---|
| 153 | {
|
|---|
| 154 | proto_t *p;
|
|---|
| 155 |
|
|---|
| 156 | p = malloc(sizeof(proto_t));
|
|---|
| 157 | proto_struct_init(p, name);
|
|---|
| 158 |
|
|---|
| 159 | return p;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | void proto_delete(proto_t *proto)
|
|---|
| 163 | {
|
|---|
| 164 | free(proto);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 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 |
|
|---|
| 175 | hash_table_insert(&proto->method_oper, &mo->link);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | oper_t *proto_get_oper(proto_t *proto, int method)
|
|---|
| 179 | {
|
|---|
| 180 | ht_link_t *item = hash_table_find(&proto->method_oper, &method);
|
|---|
| 181 | if (item == NULL) return NULL;
|
|---|
| 182 |
|
|---|
| 183 | method_oper_t *mo = hash_table_get_inst(item, method_oper_t, link);
|
|---|
| 184 | return mo->oper;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | static void oper_struct_init(oper_t *oper, const char *name)
|
|---|
| 188 | {
|
|---|
| 189 | oper->name = name;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | oper_t *oper_new(const char *name, int argc, val_type_t *arg_types,
|
|---|
| 193 | val_type_t rv_type, int respc, val_type_t *resp_types)
|
|---|
| 194 | {
|
|---|
| 195 | oper_t *o;
|
|---|
| 196 | int i;
|
|---|
| 197 |
|
|---|
| 198 | o = malloc(sizeof(oper_t));
|
|---|
| 199 | oper_struct_init(o, name);
|
|---|
| 200 |
|
|---|
| 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 |
|
|---|
| 211 | return o;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /** @}
|
|---|
| 215 | */
|
|---|