Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcnet.c
index 95c8e98695a5e6bb2a8c90e82e32c0fc8118f127..7b1551cebbd5859dba12740319c92f6e98407735 100644 (file)
@@ -1,19 +1,19 @@
 /* SILC Net API tests */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 SilcSchedule schedule;
 
 typedef struct {
   SilcFSM fsm;
-  SilcFSMSemaStruct sema;
+  SilcFSMEventStruct sema;
   SilcFSMThreadStruct thread;
-  SilcNetServer server;
+  SilcNetListener server;
   SilcStream client_stream;
-  SilcNetStatus client_status;
+  SilcResult client_status;
   SilcStream server_stream;
-  SilcNetStatus server_status;
-  bool success;
+  SilcResult server_status;
+  SilcBool success;
 } *Foo;
 
 SILC_FSM_STATE(test_st_start);
@@ -23,17 +23,17 @@ SILC_FSM_STATE(test_st_finish);
 SILC_FSM_STATE(test_st_connect);
 SILC_FSM_STATE(test_st_connected);
 
-static void test_accept_connection(SilcNetStatus status, SilcStream stream,
+static void test_accept_connection(SilcResult status, SilcStream stream,
                                   void *context)
 {
   Foo f = context;
   SILC_LOG_DEBUG(("Accepted new connection"));
   f->client_status = status;
   f->client_stream = stream;
-  SILC_FSM_SEMA_POST(&f->sema);
+  SILC_FSM_EVENT_SIGNAL(&f->sema);
 }
 
-static void test_connected(SilcNetStatus status, SilcStream stream,
+static void test_connected(SilcResult status, SilcStream stream,
                           void *context)
 {
   Foo f = context;
@@ -64,7 +64,7 @@ SILC_FSM_STATE(test_st_connected)
 
   SILC_LOG_DEBUG(("test_st_connected"));
 
-  if (f->server_status != SILC_NET_OK) {
+  if (f->server_status != SILC_OK) {
     SILC_LOG_DEBUG(("Creating connection failed"));
     return SILC_FSM_FINISH;
   }
@@ -78,11 +78,42 @@ SILC_FSM_STATE(test_st_connected)
 SILC_FSM_STATE(test_st_start)
 {
   Foo f = fsm_context;
+  int ports[3];
+  SilcUInt16 *ret_ports;
+  SilcUInt32 port_count;
 
   SILC_LOG_DEBUG(("test_st_start"));
 
+  SILC_LOG_DEBUG(("Creating network listener to ports 2000, 3000 and 4000"));
+  ports[0] = 2000;
+  ports[1] = 3000;
+  ports[2] = 4000;
+  f->server = silc_net_tcp_create_listener2(NULL, ports, 3, FALSE, TRUE, TRUE,
+                                    silc_fsm_get_schedule(fsm),
+                                    test_accept_connection, f);
+  if (!f->server) {
+    /** Creating network listener failed */
+    SILC_LOG_DEBUG(("Listener creation failed"));
+    silc_fsm_next(fsm, test_st_finish);
+    return SILC_FSM_CONTINUE;
+  }
+
+  ret_ports = silc_net_listener_get_port(f->server, &port_count);
+  if (!ret_ports) {
+    SILC_LOG_DEBUG(("Listener does not work"));
+    silc_fsm_next(fsm, test_st_finish);
+    return SILC_FSM_CONTINUE;
+  }
+  SILC_LOG_DEBUG(("Bound to port %d", ret_ports[0]));
+  SILC_LOG_DEBUG(("Bound to port %d", ret_ports[1]));
+  SILC_LOG_DEBUG(("Bound to port %d", ret_ports[2]));
+  silc_free(ret_ports);
+
+  /* Close this listener and create new one */
+  silc_net_close_listener(f->server);
+
   SILC_LOG_DEBUG(("Creating network listener"));
-  f->server = silc_net_create_server(NULL, 0, 5000, FALSE,
+  f->server = silc_net_tcp_create_listener(NULL, 0, 5000, TRUE, TRUE,
                                     silc_fsm_get_schedule(fsm),
                                     test_accept_connection, f);
   if (!f->server) {
@@ -98,7 +129,7 @@ SILC_FSM_STATE(test_st_start)
 
   /** Start waiting connection */
   SILC_LOG_DEBUG(("Start waiting for incoming connections"));
-  silc_fsm_sema_init(&f->sema, fsm, 0);
+  silc_fsm_event_init(&f->sema, fsm);
   silc_fsm_next(fsm, test_st_second);
   return SILC_FSM_CONTINUE;
 }
@@ -111,9 +142,9 @@ SILC_FSM_STATE(test_st_second)
 
   SILC_LOG_DEBUG(("test_st_second"));
 
-  SILC_FSM_SEMA_WAIT(&f->sema);
+  SILC_FSM_EVENT_WAIT(&f->sema);
 
-  if (f->client_status != SILC_NET_OK) {
+  if (f->client_status != SILC_OK) {
     /** Accepting new connection failed */
     SILC_LOG_DEBUG(("Accepting failed %d", f->client_status));
     silc_fsm_next(fsm, test_st_finish);
@@ -123,10 +154,10 @@ SILC_FSM_STATE(test_st_second)
   silc_socket_stream_get_info(f->client_stream, NULL, &host, &ip, &port);
   SILC_LOG_DEBUG(("Accepted new connection %s, %s:%d", host, ip, port));
 
-  /** Finish */
+  /** Wait thread to terminate */
   f->success = TRUE;
   silc_fsm_next(fsm, test_st_finish);
-  return SILC_FSM_CONTINUE;
+  SILC_FSM_THREAD_WAIT(&f->thread);
 }
 
 SILC_FSM_STATE(test_st_finish)
@@ -145,7 +176,7 @@ SILC_FSM_STATE(test_st_finish)
   }
 
   SILC_LOG_DEBUG(("Closing network listener"));
-  silc_net_close_server(f->server);
+  silc_net_close_listener(f->server);
 
   SILC_LOG_DEBUG(("Finish machine"));
   return SILC_FSM_FINISH;
@@ -161,18 +192,18 @@ static void destructor(SilcFSM fsm, void *fsm_context,
 
 int main(int argc, char **argv)
 {
-  bool success = FALSE;
+  SilcBool success = FALSE;
   SilcFSM fsm;
   Foo f;
 
   if (argc > 1 && !strcmp(argv[1], "-d")) {
     silc_log_debug(TRUE);
     silc_log_debug_hexdump(TRUE);
-    silc_log_set_debug_string("*net*,*stream*");
+    silc_log_set_debug_string("*net*,*stream*,*errno*");
   }
 
   SILC_LOG_DEBUG(("Allocating scheduler"));
-  schedule = silc_schedule_init(0, NULL);
+  schedule = silc_schedule_init(0, NULL, NULL);
 
   f = silc_calloc(1, sizeof(*f));
   if (!f)