tests/sk/test_sk.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following functions.
  1. t_func_atexit
  2. t_func_sighup
  3. t_func_sigint
  4. TH_run
  5. CO_get_socket_logging
  6. CO_get_socket_logfile
  7. main

   1 /***************************************
   2   $Revision: 1.1.1.1 $
   3 
   4   Example code: Unit test driver for socket.c
   5 
   6   ******************/ /******************
   7   Modification History:
   8         ottrey (21/04/1999) Created.
   9   ******************/ /******************
  10   Copyright (c) 1999                              RIPE NCC
  11  
  12   All Rights Reserved
  13   
  14   Permission to use, copy, modify, and distribute this software and its
  15   documentation for any purpose and without fee is hereby granted,
  16   provided that the above copyright notice appear in all copies and that
  17   both that copyright notice and this permission notice appear in
  18   supporting documentation, and that the name of the author not be
  19   used in advertising or publicity pertaining to distribution of the
  20   software without specific, written prior permission.
  21   
  22   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  23   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  24   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  25   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  26   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  27   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  28   ***************************************/
  29 #include "../test_.c"     /* test driver template */
  30 
  31 #include <sys/socket.h>
  32 #include <netinet/in.h>
  33 
  34 #include <signal.h>
  35 
  36 #define TEST1 "1 General test"
  37 
  38 #define SYSTEM_CALL ". input/test_sk.in1 &"
  39 
  40 #define RENDEZVOUS_PORT "3333"
  41 
  42 
  43 void t_func_atexit(void) {
     /* [<][>][^][v][top][bottom][index][help] */
  44   printf("t_SK: t_func_atexit() called\n");
  45 }
  46 
  47 void t_func_sighup(int n) {
     /* [<][>][^][v][top][bottom][index][help] */
  48   printf("t_SK: t_func_sighup(%d) called\n", n);
  49 }
  50 
  51 void t_func_sigint(int n) {
     /* [<][>][^][v][top][bottom][index][help] */
  52   printf("t_SK: t_func_sigint(%d) called\n", n);
  53 }
  54 
  55 
  56 TH_run(int sock) {
     /* [<][>][^][v][top][bottom][index][help] */
  57   char input[STR_M];
  58   int gets_value;
  59 
  60   /* Map all of the signals and exit routine */
  61   atexit(t_func_atexit);
  62   /* signal.h has a full list of signal names */
  63   signal(SIGHUP, t_func_sighup);
  64   signal(SIGINT, t_func_sigint);
  65 
  66   SK_puts(sock, "\nStart new thread here\n");
  67   do {
  68     gets_value = SK_gets(sock, input, STR_M);
  69     if(gets_value == -1) {
  70       printf("Client closed unexpectedly\n");
  71       return;
  72     }
  73     else if(gets_value == -2) {
  74       printf("Client hit control-c\n");
  75     }
  76 
  77     SK_puts(sock, "You typed: [");
  78     SK_puts(sock, input);
  79     SK_puts(sock, "]\n");
  80     printf(" The client typed: [%s]\n", input);
  81   } while (strcmp(input, "quit") != 0);
  82 
  83 } /* TH_run() */
  84 
  85 int CO_get_socket_logging() {
     /* [<][>][^][v][top][bottom][index][help] */
  86   return 1;
  87 }
  88 
  89 char *CO_get_socket_logfile() {
     /* [<][>][^][v][top][bottom][index][help] */
  90   return "stdout";
  91 }
  92 
  93 int main(int argc, char** argv) {
     /* [<][>][^][v][top][bottom][index][help] */
  94   char *str;
  95   int i;
  96 
  97   int sock = -1;
  98   int port = -1;
  99   int connected_sock = -1;
 100 
 101 
 102   /* Get options */
 103   get_options(argc, argv);
 104 
 105   Infile_name = "";
 106 
 107   /* TEST1 */
 108   if(Test[1] == 1) {
 109     print_title(TEST1);
 110 
 111     /* Create a socket on the RENDEZVOUS_PORT. */
 112     port = SK_atoport(RENDEZVOUS_PORT, "tcp");
 113     if(port == -1) {
 114       printf("Failed to create a socket on the rendezvous_port %s\n", RENDEZVOUS_PORT);
 115       exit(-1);
 116     }
 117 
 118     printf("Waiting for clients to connect to the rendezvous_port %s\n", RENDEZVOUS_PORT);
 119 
 120     system(SYSTEM_CALL);
 121 
 122     sock = SK_getsock(SOCK_STREAM, port, INADDR_ANY);
 123 
 124     for (i=0; i < 2; i++) {
 125       /* 7. Wait for a new client. */
 126       connected_sock = SK_accept_connection(sock);
 127 
 128       /* 8. Start a new thread. */
 129       TH_run(connected_sock);
 130       close(connected_sock);
 131     }
 132 
 133     printf("Not Connected\n");
 134 
 135     /* 9. Close the socket. */
 136     close(sock);
 137   } /* TEST1 */
 138 
 139   return(0);
 140 
 141 } /* main() */

/* [<][>][^][v][top][bottom][index][help] */