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