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