source: Sophya/trunk/AddOn/TAcq/tsok.cc@ 3542

Last change on this file since 3542 was 3542, checked in by ansari, 17 years ago

Ajouts fichiers du repertoire Remote (Classes Socket, ServerSocker RawInOutSocketStream ...) et fichier README , Reza 23/10/2008

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