5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 * Created: Tue Nov 25 19:25:33 GMT+0200 1997
24 #include "silcincludes.h"
26 /* Dynamically registered protocols */
27 SilcProtocolObject *silc_protocol_list = NULL;
29 /* Dynamically registers new protocol. The protocol is added into protocol
30 list and can be unregistered with silc_protocol_unregister. */
32 void silc_protocol_register(SilcProtocolType type,
33 SilcProtocolCallback callback)
35 SilcProtocolObject *proto_new;
37 proto_new = silc_calloc(1, sizeof(*proto_new));
38 proto_new->type = type;
39 proto_new->callback = callback;
41 if (!silc_protocol_list)
42 silc_protocol_list = proto_new;
44 proto_new->next = silc_protocol_list;
45 silc_protocol_list = proto_new;
49 /* Unregisters protocol. The unregistering is done by both protocol type
50 and the protocol callback. */
52 void silc_protocol_unregister(SilcProtocolType type,
53 SilcProtocolCallback callback)
55 SilcProtocolObject *protocol, *prev;
57 protocol = silc_protocol_list;
59 while (protocol && (protocol->type != type &&
60 protocol->callback != callback)) {
62 protocol = protocol->next;
67 prev->next = protocol->next;
69 silc_protocol_list = protocol->next;
75 /* Allocates a new protocol object. The new allocated and initialized
76 protocol is returned to the new_protocol argument. The argument context
77 is the context to be sent as argument for the protocol. The callback
78 argument is the function to be called _after_ the protocol has finished. */
80 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
81 void *context, SilcProtocolFinalCallback callback)
83 SilcProtocolObject *protocol;
85 SILC_LOG_DEBUG(("Allocating new protocol type %d", type));
87 protocol = silc_protocol_list;
88 while (protocol && protocol->type != type)
89 protocol = protocol->next;
92 SILC_LOG_ERROR(("Requested protocol does not exists"));
97 *new_protocol = silc_calloc(1, sizeof(**new_protocol));
98 (*new_protocol)->protocol = protocol;
99 (*new_protocol)->state = SILC_PROTOCOL_STATE_UNKNOWN;
100 (*new_protocol)->context = context;
101 (*new_protocol)->final_callback = callback;
104 /* Frees a protocol object. */
106 void silc_protocol_free(SilcProtocol protocol)
112 /* Executes next state of the protocol. The state must be set before
113 calling this function. */
115 void silc_protocol_execute(SilcProtocol protocol, SilcSchedule schedule,
116 long secs, long usecs)
119 silc_schedule_task_add(schedule, 0,
120 protocol->protocol->callback, (void *)protocol,
123 SILC_TASK_PRI_NORMAL);
125 protocol->protocol->callback(schedule, silc_schedule_get_context(schedule),
126 0, 0, (void *)protocol);
129 /* Executes the final callback of the protocol. */
131 void silc_protocol_execute_final(SilcProtocol protocol, SilcSchedule schedule)
133 protocol->final_callback(schedule, silc_schedule_get_context(schedule),
134 0, 0, (void *)protocol);
137 /* Cancels the execution of the next state of the protocol. */
139 void silc_protocol_cancel(SilcProtocol protocol, SilcSchedule schedule)
141 silc_schedule_task_del_by_context(schedule, protocol);