| [3542] | 1 | #include <stdio.h>
 | 
|---|
 | 2 | #include <stdlib.h>
 | 
|---|
 | 3 | #include <math.h>
 | 
|---|
 | 4 | #include <iostream>
 | 
|---|
 | 5 | 
 | 
|---|
 | 6 | #include "sopnamsp.h"
 | 
|---|
 | 7 | #include "array.h"
 | 
|---|
 | 8 | #include "tarrinit.h"
 | 
|---|
 | 9 | #include "timing.h"
 | 
|---|
 | 10 | #include "ctimer.h"
 | 
|---|
 | 11 | #include "swrapsock.h"
 | 
|---|
 | 12 | #include "sockrawstream.h"
 | 
|---|
 | 13 | #include "burawstream.h"
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // Numero de port pour la connection IP
 | 
|---|
 | 16 | #define PORTID 1962
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | void burawtst(); 
 | 
|---|
| [3544] | 19 | int serverside(); 
 | 
|---|
 | 20 | int clientside(string& servname, string& msg, string& args);
 | 
|---|
| [3542] | 21 | 
 | 
|---|
| [3544] | 22 | static size_t RIOS_NPAQ = 16;
 | 
|---|
 | 23 |  
 | 
|---|
 | 24 | //---------------------------------------------------------------------------
 | 
|---|
 | 25 | //  main program de test des classes socket  - R. Ansari 2005-2008
 | 
|---|
 | 26 | //---------------------------------------------------------------------------
 | 
|---|
| [3542] | 27 | int main(int narg, char *arg[])
 | 
|---|
 | 28 | {
 | 
|---|
 | 29 |   cout << " ---- Programme tsok.cc - Test classes Socket --- " << endl;
 | 
|---|
 | 30 |   if (narg < 2) {
 | 
|---|
 | 31 |     cout << " Erreur Argument / tsok.cc : Socket class tests \n" 
 | 
|---|
| [3544] | 32 |          << "    Usage : tsok S/C [servername=localhost] [OPEMsg=Hello] [NLoop,SizeX,SizeY] [RIOS_NPaq=16]\n" 
 | 
|---|
| [3542] | 33 |          << "    Ex: tsok S   or  tsok C serverName or \n" 
 | 
|---|
| [3544] | 34 |          << "        tsok C serverName PPFSOCKET  5,1000,800 \n" 
 | 
|---|
| [3542] | 35 |          << "   S : run tsok as server side \n" 
 | 
|---|
 | 36 |          << "   C : run tsok as client side \n"
 | 
|---|
 | 37 |          << "   OPEMsg : Operation select on client side (=XXX , PPFSOCKET , STOP)   \n"
 | 
|---|
 | 38 |          << "   serverName : Name or IP adress of the machine where tsok S is running \n" 
 | 
|---|
 | 39 |          << "   NLoop,SizeX,SizeY: Number of transfers, 2D array size for OPEMsg=PPFSOCKET \n" << endl;
 | 
|---|
 | 40 |     return 1;
 | 
|---|
 | 41 |   }  
 | 
|---|
 | 42 |   bool fgserv = false;
 | 
|---|
 | 43 |   if (*arg[1] == 'S')  fgserv = true;
 | 
|---|
 | 44 |   string servname = "localhost";
 | 
|---|
 | 45 |   if (narg > 2)  servname = arg[2];
 | 
|---|
 | 46 |   string opemsg = "Hello";
 | 
|---|
 | 47 |   if (narg > 3) opemsg = arg[3];
 | 
|---|
 | 48 |   string oargs = "1,100,50";
 | 
|---|
 | 49 |   if (narg > 4) oargs = arg[4];
 | 
|---|
| [3544] | 50 |   if (narg > 5) RIOS_NPAQ = atol(arg[5]);
 | 
|---|
| [3542] | 51 | 
 | 
|---|
| [3544] | 52 |   int rc = 0;
 | 
|---|
| [3542] | 53 |   try {
 | 
|---|
 | 54 |     SophyaInit();  
 | 
|---|
 | 55 |     InitTim(); 
 | 
|---|
| [3544] | 56 |     if (fgserv) rc=serverside();
 | 
|---|
 | 57 |     else if (*arg[1] == 'C') rc=clientside(servname, opemsg, oargs);
 | 
|---|
| [3542] | 58 |     else burawtst();
 | 
|---|
 | 59 |   }
 | 
|---|
 | 60 |   catch(PThrowable exc ) {
 | 
|---|
 | 61 |     cerr << "tsok-main() , Catched exception: \n" << exc.Msg() << endl;
 | 
|---|
| [3544] | 62 |     rc=97;
 | 
|---|
| [3542] | 63 |   }
 | 
|---|
 | 64 |   catch(std::exception ex) {
 | 
|---|
 | 65 |     cerr << "tsok-main() , Catched std::exception " << (string)(ex.what()) << endl;
 | 
|---|
| [3544] | 66 |     rc=98;
 | 
|---|
| [3542] | 67 |   }
 | 
|---|
 | 68 |   catch(...) {
 | 
|---|
 | 69 |     cerr << "tsok-main() , Catched ... ! " << endl;
 | 
|---|
| [3544] | 70 |     rc=99;
 | 
|---|
| [3542] | 71 |   }
 | 
|---|
| [3544] | 72 |   cout << " --------- End of tsok.cc Rc=" << rc << " ---------------- " << endl;
 | 
|---|
| [3542] | 73 | }
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | /* --Fonction-- */
 | 
|---|
| [3544] | 77 | int serverside()
 | 
|---|
| [3542] | 78 | {
 | 
|---|
| [3544] | 79 |   int rc = 66;
 | 
|---|
| [3542] | 80 |   cout << " ======== tsok.cc / ServerSide ============ " << endl;
 | 
|---|
 | 81 |   cout << " Creating server socket ... , PortId= " << PORTID << endl;
 | 
|---|
 | 82 |   ServerSocket srv(PORTID, 1);
 | 
|---|
 | 83 |   bool fgstop = false;
 | 
|---|
 | 84 |   int nc = 0;
 | 
|---|
 | 85 |   while (!fgstop) {
 | 
|---|
 | 86 |     cout << " Waiting for client connection ... nc=" << nc << endl;
 | 
|---|
 | 87 |     Socket skt = srv.WaitClientConnection();
 | 
|---|
 | 88 |     nc ++;
 | 
|---|
 | 89 |     cout << " Connection established ... nc = " << nc << endl;
 | 
|---|
 | 90 |     bool fgbye = false;
 | 
|---|
 | 91 |     while (! fgbye) {
 | 
|---|
 | 92 |       char buff[256];
 | 
|---|
 | 93 |       for(int i=0; i<255; i++) buff[i] = '\0';
 | 
|---|
 | 94 |       size_t l = skt.Receive(buff, 256);
 | 
|---|
 | 95 |       buff[255] = '\0';
 | 
|---|
 | 96 |       cout << "Rcv/len=" << l << "Msg=" << buff << endl;
 | 
|---|
 | 97 |       if (l < 1) fgbye = true;
 | 
|---|
 | 98 |       else if (strncmp(buff,"STOP",4) == 0) fgstop = true;
 | 
|---|
 | 99 |       else if (strncmp(buff,"BYE",3) == 0) fgbye = true;
 | 
|---|
 | 100 |       else if (strncmp(buff,"PPFSOCKET", 9) == 0) {
 | 
|---|
 | 101 |         int nloop = 1;
 | 
|---|
 | 102 |         int sx = 100;
 | 
|---|
 | 103 |         int sy = 100;
 | 
|---|
 | 104 |         sscanf(buff+10,"%d,%d,%d",&nloop, &sx, &sy);
 | 
|---|
 | 105 |         cout << " ServerSide: Test SocketStream NLoop= " << nloop 
 | 
|---|
 | 106 |              << " Sx= " << sx << " Sy=" << sy << endl;
 | 
|---|
 | 107 |         Timer tm("tsok/Server");
 | 
|---|
 | 108 |         size_t totnkb = 0;
 | 
|---|
 | 109 |         for(int kl=0; kl<nloop; kl++) {
 | 
|---|
 | 110 |           ostringstream oss; 
 | 
|---|
 | 111 |           oss << " ServerSide/PPFSocket[" << kl <<"]"; 
 | 
|---|
 | 112 |           tm.Split(oss.str().c_str());
 | 
|---|
 | 113 | //          tm.Split("Server/StartRead");
 | 
|---|
 | 114 |           TMatrix<int_4> mx;
 | 
|---|
 | 115 |         //      cout << " waiting ... sleep(1) " << endl;
 | 
|---|
 | 116 |         //      sleep(1);
 | 
|---|
 | 117 |           {
 | 
|---|
| [3544] | 118 |           RawInOutSocketStream sstr(skt, RIOS_NPAQ);
 | 
|---|
| [3542] | 119 |           PInPersist pis(&sstr, false, false);
 | 
|---|
 | 120 |           pis.GetObject(mx);
 | 
|---|
 | 121 |           }
 | 
|---|
 | 122 |           if (kl%5 == 0)  mx.Show() ;
 | 
|---|
 | 123 |           for(sa_size_t ir=0; ir<mx.NRows(); ir++)
 | 
|---|
 | 124 |             mx.Row(ir) += (int_4)(ir+3);
 | 
|---|
 | 125 |           {
 | 
|---|
 | 126 |           // cout << " --- writing back modified mx to socket stream ... " << endl;
 | 
|---|
| [3544] | 127 |           RawInOutSocketStream sstr(skt, RIOS_NPAQ);
 | 
|---|
| [3542] | 128 |           POutPersist pos(&sstr, false);
 | 
|---|
 | 129 |           pos.PutObject(mx);
 | 
|---|
 | 130 |           }
 | 
|---|
 | 131 |           tm.Split("WriteBackDone");
 | 
|---|
 | 132 |           float tmillis = tm.PartialElapsedTime()*1000.;
 | 
|---|
 | 133 |           if (tmillis < 1.e-5) tmillis = 1e-5;
 | 
|---|
 | 134 |           cout << "ServerSide-Rate=" << 2.*(float)(mx.Size()*4)/tmillis << " kbytes/sec" << endl;
 | 
|---|
 | 135 |           totnkb += 2*mx.Size()*4/1000;
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 |         }  // fin boucle kl->nloop
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |         float ttms  = tm.TotalElapsedTime();
 | 
|---|
 | 140 |         if (ttms < 1.e-6) ttms = 1e-6;
 | 
|---|
 | 141 |         cout << "  ===> Server/PPFSocket-MeanRate= " << (float)totnkb/ttms << " kbytes/sec" << endl;
 | 
|---|
 | 142 |       }  // fin if(PPFSOCKET)
 | 
|---|
 | 143 |     }  // fin while(! fgbye) 
 | 
|---|
 | 144 |     cout << "ServerSide: Closing current socket ..." << endl;
 | 
|---|
 | 145 |     skt.Close();
 | 
|---|
| [3544] | 146 |     rc = 0;
 | 
|---|
| [3542] | 147 |   }  // fin while(!fgstop)
 | 
|---|
 | 148 |   cout << " ======= End of tsok.cc / ServerSide ======== " << endl;
 | 
|---|
| [3544] | 149 |   return rc;
 | 
|---|
| [3542] | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 | /* --Fonction-- */
 | 
|---|
| [3544] | 153 | int clientside(string& servname, string& msg, string& args)
 | 
|---|
| [3542] | 154 | {
 | 
|---|
| [3544] | 155 |   int rc = 77;
 | 
|---|
| [3542] | 156 |   cout << " ======== tsok.cc / ClientSide ============ " << endl;
 | 
|---|
 | 157 |   cout << "  ServerName= " << servname << " OpeMsg= " << msg << endl;
 | 
|---|
 | 158 |   cout << "  Creating client socket  ServerName= " << servname << endl;
 | 
|---|
 | 159 |   ClientSocket cli(servname, PORTID);
 | 
|---|
 | 160 |   char buff[256];
 | 
|---|
 | 161 |   string maa = msg;
 | 
|---|
 | 162 |   if (msg == "PPFSOCKET")  { maa += ' '; maa += args; } 
 | 
|---|
 | 163 |   strncpy(buff, maa.c_str(), 255);
 | 
|---|
 | 164 |   buff[255] = '\0';
 | 
|---|
 | 165 |   cout << "  Sending Message : " << buff << endl;
 | 
|---|
 | 166 |   cli.Send(buff, 256);
 | 
|---|
| [3544] | 167 |   rc = 0;
 | 
|---|
| [3542] | 168 |   if (msg == "PPFSOCKET") {
 | 
|---|
| [3544] | 169 |     rc = 7;
 | 
|---|
| [3542] | 170 |     int nloop = 1;
 | 
|---|
 | 171 |     int sx = 100;
 | 
|---|
 | 172 |     int sy = 100;
 | 
|---|
 | 173 |     sscanf(args.c_str(),"%d,%d,%d",&nloop, &sx, &sy);
 | 
|---|
 | 174 |     cout << " ClientSide: Test SocketStream NLoop= " << nloop 
 | 
|---|
 | 175 |          << " Sx= " << sx << " Sy=" << sy << endl;
 | 
|---|
 | 176 |     Timer tm("tsok/Client");
 | 
|---|
 | 177 |     size_t totnkb = 0;
 | 
|---|
 | 178 |     int npb = 0;
 | 
|---|
 | 179 |     for(int kl=0; kl<nloop; kl++) {
 | 
|---|
 | 180 |       ostringstream oss; 
 | 
|---|
 | 181 |       oss << "ClientSide/PPFSocket[" << kl <<"]";
 | 
|---|
 | 182 |       tm.Split(oss.str().c_str());
 | 
|---|
 | 183 | //      tm.Split("Client/StartSend");
 | 
|---|
 | 184 |       TMatrix<int_4> mx(sy, sx);
 | 
|---|
 | 185 |       //        cout << " waiting ... sleep(1) " << endl;
 | 
|---|
 | 186 |       mx = RandomSequence(RandomSequence::Flat, 50, 10);
 | 
|---|
 | 187 |       {
 | 
|---|
 | 188 |       // cout << " --- writing mx to socket stream ... " << endl;
 | 
|---|
| [3544] | 189 |       RawInOutSocketStream sstr(cli, RIOS_NPAQ);
 | 
|---|
| [3542] | 190 |       POutPersist pos(&sstr, false);
 | 
|---|
 | 191 |       pos.PutObject(mx);
 | 
|---|
 | 192 |       }
 | 
|---|
 | 193 |       TMatrix<int_4> mxr;
 | 
|---|
 | 194 |       {
 | 
|---|
| [3544] | 195 |       RawInOutSocketStream sstr(cli, RIOS_NPAQ);
 | 
|---|
| [3542] | 196 |       PInPersist pis(&sstr, false, false);
 | 
|---|
 | 197 |       pis.GetObject(mxr);
 | 
|---|
 | 198 |       }
 | 
|---|
 | 199 |       if (kl%5==0) mxr.Show();
 | 
|---|
 | 200 |       for(sa_size_t ir=0; ir<mx.NRows(); ir++)  {
 | 
|---|
 | 201 |         mxr.Row(ir) -= (int_4)(ir+3);
 | 
|---|
 | 202 |         mxr.Row(ir) -= mx.Row(ir);
 | 
|---|
 | 203 |       }
 | 
|---|
 | 204 |      // Check for correct return matrix
 | 
|---|
 | 205 |       int_4 rmin, rmax;
 | 
|---|
 | 206 |       mxr.MinMax(rmin, rmax);
 | 
|---|
 | 207 |       if ((rmin!=0) || (rmax!=0))  npb++;
 | 
|---|
 | 208 |       cout << " ClientSide/CheckReturnMinMax=" << rmin << "," << rmax << endl;
 | 
|---|
 | 209 |       tm.Split("ReadCheckDone");
 | 
|---|
 | 210 |       float tmillis = tm.PartialElapsedTime()*1000.;
 | 
|---|
 | 211 |       if (tmillis < 1.e-5) tmillis = 1e-5;
 | 
|---|
 | 212 |       cout << "ClientSide-Rate=" << 2.*(float)(mx.Size()*4)/tmillis << " kbytes/sec" << endl;
 | 
|---|
 | 213 |       totnkb += 2*mx.Size()*4/1000;
 | 
|---|
 | 214 |     }
 | 
|---|
 | 215 |     cout << "ClientSide/End of PPFSocket  NPb-Check-returnMatrix=" << npb << " /NLoop=" << nloop << endl;  
 | 
|---|
| [3544] | 216 |     if (npb==0) rc=0;
 | 
|---|
 | 217 |     else rc=70;
 | 
|---|
| [3542] | 218 |     float ttms  = tm.TotalElapsedTime();
 | 
|---|
 | 219 |     if (ttms < 1.e-6) ttms = 1e-6;
 | 
|---|
 | 220 |     cout << "  ===> MeanRate= " << (float)totnkb/ttms << " kbytes/sec" << endl;
 | 
|---|
 | 221 |   }
 | 
|---|
 | 222 |   strncpy(buff, "BYE", 255);
 | 
|---|
 | 223 |   cout << "  Sending Message : " << buff << endl;
 | 
|---|
 | 224 |   cli.Send(buff, 256);
 | 
|---|
 | 225 |   cout << " ======= End of tsok.cc / ClientSide ======== " << endl;
 | 
|---|
| [3544] | 226 |   return rc;
 | 
|---|
| [3542] | 227 | }
 | 
|---|
 | 228 | 
 | 
|---|
 | 229 | 
 | 
|---|
 | 230 | /* --Fonction-- */
 | 
|---|
 | 231 | void burawtst()
 | 
|---|
 | 232 | {
 | 
|---|
 | 233 |   cout << " --------- burawtst() ------------ " << endl;
 | 
|---|
 | 234 |   {
 | 
|---|
 | 235 |     char buff[10000];
 | 
|---|
 | 236 |     for(int k=0; k<10000; k++) buff[k] = k%256;
 | 
|---|
 | 237 |     RawInOutBuffStream sstr("xxx.str");
 | 
|---|
 | 238 |     for(int i=1; i<10; i++) {
 | 
|---|
 | 239 |       cout << "Appel RawInOutBuffStream.write(s, " << i*650 << endl;
 | 
|---|
 | 240 |       sstr.write(buff, i*650);
 | 
|---|
 | 241 |     }
 | 
|---|
 | 242 |   }
 | 
|---|
 | 243 |   Matrix mx(8,6);
 | 
|---|
 | 244 |   mx = RegularSequence();
 | 
|---|
 | 245 |   cout << mx;
 | 
|---|
 | 246 |   {
 | 
|---|
 | 247 |     cout << " --- writing mx to buff stream ... " << endl;
 | 
|---|
 | 248 |     RawInOutBuffStream sstr("ftsok.str");
 | 
|---|
 | 249 |     POutPersist pos(&sstr, false);
 | 
|---|
 | 250 |     pos.PutObject(mx);
 | 
|---|
 | 251 |   }
 | 
|---|
 | 252 |   {
 | 
|---|
 | 253 |     cout << " --- writing mx to file ... " << endl;
 | 
|---|
 | 254 |     POutPersist pos("ftsok.ppf");
 | 
|---|
 | 255 |     pos.PutObject(mx);
 | 
|---|
 | 256 |   }
 | 
|---|
 | 257 | 
 | 
|---|
 | 258 |   cout << " --------- FIN-burawtst() ------------ " << endl;
 | 
|---|
 | 259 |   
 | 
|---|
 | 260 | }
 | 
|---|