updates.
[silc.git] / lib / silccore / silcprotocol.c
index dfdf0321af317e18190fc7c3ce263f1520ada964..3703c30e0073233bc0953a0136067cc46c23db95 100644 (file)
 /*
  * Created: Tue Nov 25 19:25:33 GMT+0200 1997
  */
-/*
- * $Id$
- * $Log$
- * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
- *     Importet from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 #include "silcprotocol.h"
 
+/* Dynamically registered protocols */
+SilcProtocolObject *silc_protocol_list = NULL;
+
+/* Dynamically registers new protocol. The protocol is added into protocol
+   list and can be unregistered with silc_protocol_unregister. */
+
+void silc_protocol_register(SilcProtocolType type,
+                           SilcProtocolCallback callback)
+{
+  SilcProtocolObject *new;
+
+  new = silc_calloc(1, sizeof(*new));
+  new->type = type;
+  new->callback = callback;
+
+  if (!silc_protocol_list)
+    silc_protocol_list = new;
+  else {
+    new->next = silc_protocol_list;
+    silc_protocol_list = new;
+  }
+}
+
+/* Unregisters protocol. The unregistering is done by both protocol type
+   and the protocol callback. */
+
+void silc_protocol_unregister(SilcProtocolType type,
+                              SilcProtocolCallback callback)
+{
+  SilcProtocolObject *protocol, *prev;
+
+  protocol = silc_protocol_list;
+  prev = NULL;
+  while (protocol && (protocol->type != type && 
+                      protocol->callback != callback)) {
+    prev = protocol;
+    protocol = protocol->next;
+  }
+
+  if (protocol) {
+    if (prev)
+      prev->next = protocol->next;
+    else
+      silc_protocol_list = protocol->next;
+
+    silc_free(protocol);
+  }
+}
+
 /* Allocates a new protocol object. The new allocated and initialized 
    protocol is returned to the new_protocol argument. The argument context
    is the context to be sent as argument for the protocol. The callback
 void silc_protocol_alloc(SilcProtocolType type, SilcProtocol *new_protocol,
                         void *context, SilcProtocolFinalCallback callback)
 {
-  int i;
+  SilcProtocolObject *protocol;
 
   SILC_LOG_DEBUG(("Allocating new protocol type %d", type));
 
-  for (i = 0; silc_protocol_list[i].callback; i++)
-    if (silc_protocol_list[i].type == type)
-      break;
+  protocol = silc_protocol_list;
+  while (protocol && protocol->type != type)
+    protocol = protocol->next;
 
-  if (!silc_protocol_list[i].callback) {
+  if (!protocol) {
     SILC_LOG_ERROR(("Requested protocol does not exists"));
+    *new_protocol = NULL;
     return;
   }
 
   *new_protocol = silc_calloc(1, sizeof(**new_protocol));
-  if (*new_protocol == NULL) {
-    SILC_LOG_ERROR(("Cannot allocate new protocol object"));
-    return;
-  }
-
-  (*new_protocol)->protocol = (SilcProtocolObject *)&silc_protocol_list[i];
+  (*new_protocol)->protocol = protocol;
   (*new_protocol)->state = SILC_PROTOCOL_STATE_UNKNOWN;
   (*new_protocol)->context = context;
-  (*new_protocol)->execute = silc_protocol_execute;
-  (*new_protocol)->execute_final = silc_protocol_execute_final;
   (*new_protocol)->final_callback = callback;
 }
 
-/* Free's a protocol object. */
+/* Frees a protocol object. */
 
 void silc_protocol_free(SilcProtocol protocol)
 {
@@ -78,31 +114,29 @@ void silc_protocol_free(SilcProtocol protocol)
 /* Executes next state of the protocol. The state must be set before
    calling this function. */
 
-void silc_protocol_execute(void *qptr, int type,
-                          void *context, int fd,
+void silc_protocol_execute(SilcProtocol protocol, SilcSchedule schedule,
                           long secs, long usecs)
 {
-  SilcProtocol protocol = (SilcProtocol)context;
-
-  SILC_LOG_DEBUG(("Start"));
-
   if (secs + usecs) 
-    silc_task_register(qptr, fd, protocol->protocol->callback, context, 
-                      secs, usecs, 
-                      SILC_TASK_TIMEOUT,
-                      SILC_TASK_PRI_NORMAL);
+    silc_schedule_task_add(schedule, 0, 
+                          protocol->protocol->callback, (void *)protocol, 
+                          secs, usecs, 
+                          SILC_TASK_TIMEOUT,
+                          SILC_TASK_PRI_NORMAL);
   else
-    protocol->protocol->callback(qptr, 0, context, fd);
+    protocol->protocol->callback(schedule, 0, 0, (void *)protocol);
 }
 
 /* Executes the final callback of the protocol. */
 
-void silc_protocol_execute_final(void *qptr, int type,
-                                void *context, int fd)
+void silc_protocol_execute_final(SilcProtocol protocol, SilcSchedule schedule)
 {
-  SilcProtocol protocol = (SilcProtocol)context;
-  SILC_LOG_DEBUG(("Start, state=%d", protocol->state));
+  protocol->final_callback(schedule, 0, 0, (void *)protocol);
+}
+
+/* Cancels the execution of the next state of the protocol. */
 
-  protocol->final_callback(qptr, 0, context, fd);
+void silc_protocol_cancel(SilcProtocol protocol, SilcSchedule schedule)
+{
+  silc_schedule_task_del_by_context(schedule, protocol);
 }