VTKFIG  0.20.20181114
Easy VTK based in situ visualization
int main ( int  argc,
const char *  argv[] 
)

Start client

Definition at line 16 of file example-comm-client-server-vtk.cxx.

17 {
18  int rc;
19  const int port=35000;
20 
21  char xname[10];
22  bool server=true;
23 
24  strncpy(xname,"server",10);
25 
26  // if called with -c run as client.
27  if (argc>1 && argv[1][0]=='-'&& argv[1][1]=='c')
28  {
29  strcpy(xname,"client");
30  server=false;
31  }
32 
33  printf("%s start\n",xname);
34 
35 
36  // Use server socket when establishing connection
37  // this allows for a timeout in wait. Otherwis
38  // wait would be blocking.
39  bool use_ssocket=true;
40  vtkSmartPointer<vtkServerSocket> ssocket;
41  if (server)
42  {
43 
45  char command[256];
46  snprintf(command,256,"%s -c &",argv[0]);
47  std::system(command);
48  }
49 
50 
51  vtkSmartPointer<vtkSocketController> controller =vtkSmartPointer<vtkSocketController>::New();
52  vtkSmartPointer<vtkSocketCommunicator> comm =vtkSmartPointer<vtkSocketCommunicator>::New();
53 
54  controller->SetCommunicator(comm);
55  controller->Initialize();
56 // comm->SetReportErrors(0);
57 
58 
59  int nwait=10;
60  int iwait=0;
61  if (server)
62  {
63 
64 // comm->LogToFile("server.log");
65  if (use_ssocket)
66  {
67  ssocket=vtkSmartPointer<vtkServerSocket>::New();
68  rc=ssocket->CreateServer(port);
69  printf("%s vtkServerSocket::CreateServer: rc=%d\n",xname, rc);
70  assert(rc==0);
71  }
72 
73  rc=0;
74  while (iwait<nwait && rc==0)
75  {
76  printf("%s wait...\n",xname);
77  if (use_ssocket)
78  rc=comm->WaitForConnection(ssocket,100);
79  else
80  rc=comm->WaitForConnection(port);
81 
82  printf("%s wait: rc=%d\n",xname, rc);
83  std::this_thread::sleep_for (std::chrono::milliseconds(100));
84  iwait++;
85  }
86  }
87  else
88  {
89 // comm->LogToFile("client.log");
90  rc=0;
91  while (iwait<nwait && rc==0)
92  {
93  printf("%s connect to ...\n",xname);
94  rc=comm->ConnectTo("localhost",port);
95  printf("%s connect: rc=%d\n",xname, rc);
96  iwait++;
97  }
98  }
99 
100  if (rc!=0)
101  {
102  printf("%s connected \n",xname);
103  }
104  else
105  {
106  printf("%s give up waiting\n",xname);
107  return 0;
108  }
109 
110  rc=comm->Handshake();
111  printf("%s handshake %d\n",xname,rc);
112 
113  if (server)
114  assert(comm->GetIsServer());
115  else
116  assert(!comm->GetIsServer());
117 
118 
119  int iproc=comm->GetLocalProcessId();
120  int nproc=comm->GetNumberOfProcesses();
121  printf("%s: nproc=%d, iproc=%d\n",xname, nproc,iproc);
122 
123  // There seems no way to set the correct Ids
124  // see http://www.vtk.org/doc/nightly/html/classvtkSocketController.html
125  // search for "FOOLISH MORTALS!"
126 
127  // Consequently, if I am 0 "the other" always seems to be 1
128  // (this was guessed...)
129  int remoteHandle=1;
130 
131 
132  comm->Barrier();
133  printf("%s Barrier\n",xname);
134 
135 
136  int zahl=0;
137  // Send data server->client
138  int tag=123;
139  if (server)
140  {
141  zahl=42;
142  rc=comm->Send(&zahl,1,remoteHandle,tag);
143  printf("%s send %d: %d\n",xname,zahl, rc);
144  std::this_thread::sleep_for (std::chrono::milliseconds(100));
145  }
146  else
147  {
148  zahl=0;
149  rc=comm->Receive(&zahl,1,remoteHandle,tag);
150  printf("%s receive %d: %d\n",xname,zahl, rc);
151  }
152 
153  // Send data client->server
154  tag=124;
155  if (!server)
156  {
157  zahl=42;
158  rc=comm->Send(&zahl,1,remoteHandle,tag);
159  printf("%s send %d: %d\n",xname,zahl, rc);
160  }
161  else
162  {
163  zahl=0;
164  rc=comm->Receive(&zahl,1,remoteHandle,tag);
165  printf("%s receive %d: %d\n",xname,zahl, rc);
166  }
167 
168 
169  std::this_thread::sleep_for(std::chrono::milliseconds(20));
170  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
171 
172  tag=123;
173  if (server)
174  {
175  int Nx=10;
176  int Ny=10;
177 
178  for (int j = 0; j < Ny; j++)
179  for (int i = 0; i < Nx; i++)
180  {
181  points->InsertNextPoint(i, j, 100*i+j);
182  }
183  rc=comm->Send(points->GetData(),remoteHandle,tag);
184  printf("%s send %d points: %d\n",xname,static_cast<int>(points->GetNumberOfPoints()), rc);
185  }
186  else
187  {
188  rc=comm->Receive(points->GetData(),remoteHandle,tag);
189  printf("%s received %d points: %d\n",xname,static_cast<int>(points->GetNumberOfPoints()), rc);
190  std::this_thread::sleep_for(std::chrono::milliseconds(100));
191  }
192 
193  for (int i=0;i<points->GetNumberOfPoints();i++)
194  {
195  double x[3];
196  points->GetPoint(i,x);
197  printf("%s: point %03.1f %03.1f %03.1f\n",xname,x[0],x[1],x[2]);
198  }
199  printf("\n");
200 
201  comm->Barrier();
202 
203 
204  rc=comm->GetIsConnected();
205  printf("%s connected %d\n",xname,rc);
206  rc=comm->Handshake();
207  printf("%s handshake %d\n",xname,rc);
208 
209 
210  comm->Barrier();
211  printf("%s Barrier\n",xname);
212 
213 
214 
215  // What does the server feel if the client exits ?
216  if (server)
217  {
218  std::this_thread::sleep_for(std::chrono::milliseconds(500));
219  rc=comm->GetIsConnected();
220  printf("%s connected %d\n",xname,rc);
221  rc=comm->Handshake();
222  printf("%s handshake %d\n",xname,rc);
223  if (!rc)
224  printf("%s lost connection\n",xname);
225 
226  }
227  comm->CloseConnection();
228  printf("%s stop\n",xname);
229 }