Global cosmetic change.
[silc.git] / lib / silccore / silcprotocol.c
1 /*
2
3   silcprotocol.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * Created: Tue Nov 25 19:25:33 GMT+0200 1997
22  */
23 /*
24  * $Id$
25  * $Log$
26  * Revision 1.2  2000/07/05 06:06:35  priikone
27  *      Global cosmetic change.
28  *
29  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
30  *      Imported from internal CVS/Added Log headers.
31  *
32  *
33  */
34
35 #include "silcincludes.h"
36 #include "silcprotocol.h"
37
38 /* Allocates a new protocol object. The new allocated and initialized 
39    protocol is returned to the new_protocol argument. The argument context
40    is the context to be sent as argument for the protocol. The callback
41    argument is the function to be called _after_ the protocol has finished. */
42
43 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
44                          void *context, SilcProtocolFinalCallback callback)
45 {
46   int i;
47
48   SILC_LOG_DEBUG(("Allocating new protocol type %d", type));
49
50   for (i = 0; silc_protocol_list[i].callback; i++)
51     if (silc_protocol_list[i].type == type)
52       break;
53
54   if (!silc_protocol_list[i].callback) {
55     SILC_LOG_ERROR(("Requested protocol does not exists"));
56     return;
57   }
58
59   *new_protocol = silc_calloc(1, sizeof(**new_protocol));
60   (*new_protocol)->protocol = (SilcProtocolObject *)&silc_protocol_list[i];
61   (*new_protocol)->state = SILC_PROTOCOL_STATE_UNKNOWN;
62   (*new_protocol)->context = context;
63   (*new_protocol)->execute = silc_protocol_execute;
64   (*new_protocol)->execute_final = silc_protocol_execute_final;
65   (*new_protocol)->final_callback = callback;
66 }
67
68 /* Free's a protocol object. */
69
70 void silc_protocol_free(SilcProtocol protocol)
71 {
72   if (protocol)
73     silc_free(protocol);
74 }
75
76 /* Executes next state of the protocol. The state must be set before
77    calling this function. */
78
79 void silc_protocol_execute(void *qptr, int type,
80                            void *context, int fd,
81                            long secs, long usecs)
82 {
83   SilcProtocol protocol = (SilcProtocol)context;
84
85   SILC_LOG_DEBUG(("Start"));
86
87   if (secs + usecs) 
88     silc_task_register(qptr, fd, protocol->protocol->callback, context, 
89                        secs, usecs, 
90                        SILC_TASK_TIMEOUT,
91                        SILC_TASK_PRI_NORMAL);
92   else
93     protocol->protocol->callback(qptr, 0, context, fd);
94 }
95
96 /* Executes the final callback of the protocol. */
97
98 void silc_protocol_execute_final(void *qptr, int type,
99                                  void *context, int fd)
100 {
101   SilcProtocol protocol = (SilcProtocol)context;
102  
103   SILC_LOG_DEBUG(("Start, state=%d", protocol->state));
104
105   protocol->final_callback(qptr, 0, context, fd);
106 }