eg_net.ex.c

00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /** @file
00021  * @ingroup EGnet */
00022 /** @addtogroup EGnet */
00023 /** @{ */
00024 /* ========================================================================= */
00025 #include <stdio.h>
00026 #include <time.h>
00027 #include <string.h>
00028 #include <strings.h>
00029 #include <stdlib.h>
00030 #include "eg_net.h"
00031 #ifdef LINUX
00032 #include <getopt.h>
00033 #endif
00034 
00035 static void net_usage (char *program)
00036 {
00037   fprintf (stdout, "Usage: %s [options]\n", program);
00038   fprintf (stdout, "Options:\n");
00039   fprintf (stdout, "    -b     boss mode\n");
00040   fprintf (stdout, "    -c     client mode\n");
00041   fprintf (stdout, "    -h c   host name of boss\n");
00042   fprintf (stdout, "    -k     send kill signal to boss\n");
00043   fprintf (stdout, "    -m n   send 'n' chars to boss and exit\n");
00044   fprintf (stdout, "    -p n   port number\n");
00045 }
00046 
00047 #define BOSS 1
00048 #define CLIENT 2
00049 #define KILL 3
00050 static int nchars = 0;
00051 static unsigned short port = 0;
00052 static char *hname = 0;
00053 static int mode = 0;
00054 static int parseargs (int argc,
00055                       char **argv)
00056 {
00057   int c;
00058   while ((c = getopt (argc, argv, "bch:km:p:")) != EOF)
00059   {
00060     switch (c)
00061     {
00062     case 'b':
00063       if (mode)
00064         goto FAIL;
00065       mode = BOSS;
00066       break;
00067     case 'c':
00068       if (mode)
00069         goto FAIL;
00070       mode = CLIENT;
00071       break;
00072     case 'k':
00073       if (mode)
00074         goto FAIL;
00075       mode = KILL;
00076       break;
00077     case 'h':
00078       hname = strdup (optarg);
00079       break;
00080     case 'm':
00081       nchars = atoi (optarg);
00082       break;
00083     case 'p':
00084       port = atoi (optarg);
00085       break;
00086     default:
00087       goto FAIL;
00088     }                           /* end switch */
00089   }                             /* end while */
00090 
00091   /* check that the input is correct */
00092   if (!mode)
00093   {
00094     MESSAGE (0, "you must only one of the c b k mode");
00095     goto FAIL;
00096   }
00097   if ((mode == CLIENT) && (!nchars))
00098   {
00099     MESSAGE (0,
00100              "you must choose how many bytes to send/recieve in client mode");
00101     goto FAIL;
00102   }
00103   if (!port)
00104   {
00105     MESSAGE (0, "you must choose a connection port");
00106     goto FAIL;
00107   }
00108 
00109   /* ending correctly */
00110   return 0;
00111 
00112   /* failure status */
00113 FAIL:
00114   net_usage (argv[0]);
00115   exit (1);
00116 }
00117 
00118 int main (int argc,
00119           char **argv)
00120 {
00121   /* local variables */
00122   EGmemPool_t *mem = EGnewMemPool (100, EGmemPoolNewSize, EGmemPoolNewSize (1));
00123   EGsocket_t *skt = EGnewSocket (mem);
00124   char *str = 0,
00125     type,
00126     ctmp,
00127     ctmp2;
00128   int rval,
00129     itmp,
00130     itmp2;
00131   unsigned int utmp,
00132     utmp2;
00133   short stmp,
00134     stmp2;
00135   unsigned short ustmp,
00136     ustmp2;
00137   double dtmp,
00138     dtmp2;
00139   register int i;
00140   parseargs (argc, argv);
00141   srand ((unsigned int) (time (0)));
00142   switch (mode)
00143   {
00144   case BOSS:
00145     rval = EGnetListen (skt, port);
00146     CHECKRVAL (rval);
00147     while (1)
00148     {
00149       rval = EGnetStartRead (skt);
00150       CHECKRVAL (rval);
00151       rval = EGnetRecvChar (skt, &type);
00152       CHECKRVAL (rval);
00153       if (type == KILL)
00154       {
00155         MESSAGE (0, "the boss is being killed");
00156         rval = EGnetStopRead (skt);
00157         CHECKRVAL (rval);
00158         break;
00159       }
00160       rval = EGnetRecvUint (skt, &nchars);
00161       CHECKRVAL (rval);
00162       MESSAGE (0, "we are going to recieve/send %d chars", nchars);
00163       for (i = nchars; i--;)
00164       {
00165         rval = EGnetRecvChar (skt, &ctmp);
00166         CHECKRVAL (rval);
00167         rval = EGnetSendChar (skt, ctmp);
00168         CHECKRVAL (rval);
00169       }
00170       MESSAGE (0, "we are going to recieve/send %d uint", nchars);
00171       for (i = nchars; i--;)
00172       {
00173         rval = EGnetRecvUint (skt, &utmp);
00174         CHECKRVAL (rval);
00175         rval = EGnetSendUint (skt, utmp);
00176         CHECKRVAL (rval);
00177       }
00178       MESSAGE (0, "we are going to recieve/send %d int", nchars);
00179       for (i = nchars; i--;)
00180       {
00181         rval = EGnetRecvInt (skt, &itmp);
00182         CHECKRVAL (rval);
00183         rval = EGnetSendInt (skt, itmp);
00184         CHECKRVAL (rval);
00185       }
00186       MESSAGE (0, "we are going to recieve/send %d ushort", nchars);
00187       for (i = nchars; i--;)
00188       {
00189         rval = EGnetRecvUshort (skt, &ustmp);
00190         CHECKRVAL (rval);
00191         rval = EGnetSendUshort (skt, ustmp);
00192         CHECKRVAL (rval);
00193       }
00194       MESSAGE (0, "we are going to recieve/send %d short", nchars);
00195       for (i = nchars; i--;)
00196       {
00197         rval = EGnetRecvShort (skt, &stmp);
00198         CHECKRVAL (rval);
00199         rval = EGnetSendShort (skt, stmp);
00200         CHECKRVAL (rval);
00201       }
00202       MESSAGE (0, "we are going to recieve/send %d double", nchars);
00203       for (i = nchars; i--;)
00204       {
00205         rval = EGnetRecvDouble (skt, &dtmp);
00206         CHECKRVAL (rval);
00207       }
00208       for (i = nchars; i--;)
00209       {
00210         rval = EGnetSendDouble (skt, dtmp);
00211         CHECKRVAL (rval);
00212       }
00213 
00214       rval = EGnetStopRead (skt);
00215       CHECKRVAL (rval);
00216     }
00217     break;
00218   case CLIENT:
00219     rval = EGnetConnect (skt, hname, port);
00220     CHECKRVAL (rval);
00221     rval = EGnetSendChar (skt, mode);
00222     CHECKRVAL (rval);
00223     rval = EGnetSendUint (skt, nchars);
00224     CHECKRVAL (rval);
00225     MESSAGE (0, "we are going to send/recieve %d chars", nchars);
00226     for (i = nchars; i--;)
00227     {
00228       ctmp = random ();
00229       rval = EGnetSendChar (skt, ctmp);
00230       CHECKRVAL (rval);
00231       rval = EGnetRecvChar (skt, &ctmp2);
00232       CHECKRVAL (rval);
00233       TEST (ctmp != ctmp2, "wrong communication");
00234     }
00235     MESSAGE (0, "we are going to send/recieve %d uint", nchars);
00236     for (i = nchars; i--;)
00237     {
00238       utmp = random ();
00239       rval = EGnetSendUint (skt, utmp);
00240       CHECKRVAL (rval);
00241       rval = EGnetRecvUint (skt, &utmp2);
00242       CHECKRVAL (rval);
00243       TEST (utmp != utmp2, "wrong communication %u != %u", utmp, utmp2);
00244     }
00245     MESSAGE (0, "we are going to send/recieve %d int", nchars);
00246     for (i = nchars; i--;)
00247     {
00248       itmp = random ();
00249       if (random () | 1)
00250         itmp = -itmp;
00251       rval = EGnetSendInt (skt, itmp);
00252       CHECKRVAL (rval);
00253       rval = EGnetRecvInt (skt, &itmp2);
00254       CHECKRVAL (rval);
00255       TEST (itmp != itmp2, "wrong communication %d != %d", itmp, itmp2);
00256     }
00257     MESSAGE (0, "we are going to send/recieve %d ushort", nchars);
00258     for (i = nchars; i--;)
00259     {
00260       ustmp = random ();
00261       rval = EGnetSendUshort (skt, ustmp);
00262       CHECKRVAL (rval);
00263       rval = EGnetRecvUshort (skt, &ustmp2);
00264       CHECKRVAL (rval);
00265       TEST (ustmp != ustmp2, "wrong communication %u != %u", ustmp, ustmp2);
00266     }
00267     MESSAGE (0, "we are going to send/recieve %d short", nchars);
00268     for (i = nchars; i--;)
00269     {
00270       stmp = random ();
00271       if (random () | 1)
00272         stmp = -stmp;
00273       rval = EGnetSendShort (skt, stmp);
00274       CHECKRVAL (rval);
00275       rval = EGnetRecvShort (skt, &stmp2);
00276       CHECKRVAL (rval);
00277       TEST (stmp != stmp2, "wrong communication %d != %d", stmp, stmp2);
00278     }
00279     MESSAGE (0, "we are going to send/recieve %d double", nchars);
00280     for (i = nchars; i--;)
00281     {
00282       dtmp = random ();
00283       dtmp /= random () + 1;
00284       if (random () | 1)
00285         dtmp = -dtmp;
00286       rval = EGnetSendDouble (skt, dtmp);
00287       CHECKRVAL (rval);
00288     }
00289     for (i = nchars; i--;)
00290     {
00291       rval = EGnetRecvDouble (skt, &dtmp2);
00292       CHECKRVAL (rval);
00293     }
00294 
00295     rval = EGnetDisconnect (skt);
00296     CHECKRVAL (rval);
00297     break;
00298   case KILL:
00299     rval = EGnetConnect (skt, hname, port);
00300     CHECKRVAL (rval);
00301     MESSAGE (0, "Killing boss");
00302     rval = EGnetSendChar (skt, mode);
00303     CHECKRVAL (rval);
00304     rval = EGnetDisconnect (skt);
00305     CHECKRVAL (rval);
00306     break;
00307   }
00308 
00309   /* ending */
00310   EGfreeSocket (skt, mem);
00311   EGfreeMemPool (mem);
00312   free (str);
00313   return 0;
00314 }
00315 
00316 /* ========================================================================= */
00317 /** @} */

Generated on Mon Jan 30 08:48:52 2006 for EGlib by  doxygen 1.4.5