SocketClient.cxx 8.14 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    SocketClient.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
9
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
13
14
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
Berk Geveci's avatar
Berk Geveci committed
15
#include "vtkActor.h"
16
17
18
#include "vtkContourFilter.h"
#include "vtkDataSetMapper.h"
#include "vtkDebugLeaks.h"
Berk Geveci's avatar
Berk Geveci committed
19
#include "vtkDoubleArray.h"
20
#include "vtkPolyData.h"
Berk Geveci's avatar
Berk Geveci committed
21
#include "vtkPolyDataMapper.h"
22
23
24
25
#include "vtkRectilinearGrid.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
26
#include "vtkSocketCommunicator.h"
27
28
#include "vtkSocketController.h"
#include "vtkStructuredGrid.h"
29
#include "vtkImageData.h"
30
#include "vtkUnstructuredGrid.h"
31
32
33
34
#include "vtkCamera.h"
#include "vtkImageActor.h"

#include "vtkRenderWindowInteractor.h"
Berk Geveci's avatar
Berk Geveci committed
35
36
37
38
39

static const int scMsgLength = 10;

static void CleanUp(vtkSocketCommunicator* comm, vtkSocketController* contr)
{
Berk Geveci's avatar
Berk Geveci committed
40
41
  // This will close the connection as well as delete 
  // the communicator
Berk Geveci's avatar
Berk Geveci committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  comm->Delete();
  contr->Delete();
}

int main(int argc, char** argv)
{
  vtkSocketController* contr = vtkSocketController::New();
  contr->Initialize();

  vtkSocketCommunicator* comm = vtkSocketCommunicator::New();

  int i;

  // Get the host name from the command line arguments
  char* hostname = new char[30];
  strcpy(hostname, "localhost");
  int dataIndex=-1;
  for (i=0; i<argc; i++)
    {
    if ( strcmp("-H", argv[i]) == 0 )
      {
      if ( i < argc-1 )
Brad King's avatar
Brad King committed
64
65
66
        {
        dataIndex = i+1;
        }
Berk Geveci's avatar
Berk Geveci committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
      }
    }
  if (dataIndex != -1)
    {
    delete[] hostname;
    hostname = new char[strlen(argv[dataIndex])+1];
    strcpy(hostname, argv[dataIndex]);
    }

  // Get the port from the command line arguments
  int port=11111;

  dataIndex=-1;
  for (i=0; i<argc; i++)
    {
    if ( strcmp("-P", argv[i]) == 0 )
      {
      if ( i < argc-1 )
Brad King's avatar
Brad King committed
85
86
87
        {
        dataIndex = i+1;
        }
Berk Geveci's avatar
Berk Geveci committed
88
89
90
91
92
93
94
95
96
97
98
      }
    }
  if (dataIndex != -1)
    {
    port = atoi(argv[dataIndex]);
    }

  // Establish connection
  if (!comm->ConnectTo(hostname, port))
    {
    cerr << "Client error: Could not connect to the server."
Brad King's avatar
Brad King committed
99
         << endl;
Berk Geveci's avatar
Berk Geveci committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    comm->Delete();
    contr->Delete();
    delete[] hostname;
    return 1;
    }
  delete[] hostname;

  // Test sending all supported types of arrays
  int datai[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    datai[i] = i;
    }
  if (!comm->Send(datai, scMsgLength, 1, 11))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }

  unsigned long dataul[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    dataul[i] = static_cast<unsigned long>(i);
    }
  if (!comm->Send(dataul, scMsgLength, 1, 22))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }

  char datac[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    datac[i] = static_cast<char>(i);
    }
  if (!comm->Send(datac, scMsgLength, 1, 33))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }

  unsigned char datauc[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    datauc[i] = static_cast<unsigned char>(i);
    }
  if (!comm->Send(datauc, scMsgLength, 1, 44))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }

  float dataf[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    dataf[i] = static_cast<float>(i);
    }
  if (!comm->Send(dataf, scMsgLength, 1, 7))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }


  double datad[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    datad[i] = static_cast<double>(i);
    }
  if (!comm->Send(datad, scMsgLength, 1, 7))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }

  vtkIdType datait[scMsgLength];
  for (i=0; i<scMsgLength; i++)
    {
    datait[i] = static_cast<vtkIdType>(i);
    }
  if (!comm->Send(datait, scMsgLength, 1, 7))
    {
    cerr << "Client error: Error sending data." << endl;
    CleanUp(comm, contr);
    return 1;
    }

  // Test receiving vtkDataObject
  vtkUnstructuredGrid* ugrid = vtkUnstructuredGrid::New();

  if (!comm->Receive(ugrid, 1, 9))
    {
    cerr << "Client error: Error receiving data." << endl;
    CleanUp(comm, contr);
    ugrid->Delete();
    return 1;
    }

  vtkDataSetMapper* umapper = vtkDataSetMapper::New();
  umapper->SetInput(ugrid);

  vtkActor* uactor = vtkActor::New();
  uactor->SetMapper(umapper);
  uactor->SetPosition(5, 0, 0);
  uactor->SetScale(0.2, 0.2, 0.2);
  umapper->UnRegister(0);

  // Test receiving vtkDataArray
  vtkDoubleArray* da = vtkDoubleArray::New();
  if (!comm->Receive(da, 1, 9))
    {
    cerr << "Client error: Error receiving data." << endl;
    CleanUp(comm, contr);
    ugrid->Delete();
    uactor->Delete();
    da->Delete();
    return 1;
    }
  for (i=0; i<40; i++)
    {
    if (da->GetValue(i) != static_cast<double>(i))
      {
      cerr << "Server error: Corrupt vtkDoubleArray." << endl;
      CleanUp(comm, contr);
      ugrid->Delete();
      uactor->Delete();
      da->Delete();
      return 1;
      }
    }

  da->Delete();

239
240
241
242
243
244
245
246
247
248
249
  // Test receiving null vtkDataArray
  vtkDoubleArray *da2 = vtkDoubleArray::New();
  if (!comm->Receive(da2, 1, 9))
    {
    cerr << "Client error: Error receiving data." << endl;
    CleanUp(comm, contr);
    ugrid->Delete();
    uactor->Delete();
    da2->Delete();
    return 1;
    }
Mathieu Malaterre's avatar
Mathieu Malaterre committed
250
251
252
253
254
255
256
257
  if (da2->GetNumberOfTuples() == 0) 
    {
    cout << "receive null data array successful" << endl;
    } 
  else 
    {
    cout << "receive null data array failed" << endl;
    }
258
259
  da2->Delete();

Berk Geveci's avatar
Berk Geveci committed
260
261
  contr->SetCommunicator(comm);

Berk Geveci's avatar
Berk Geveci committed
262
263
264
265
266
267
268
269
  // The following lines were added for coverage
  // These methods have empty implementations
  contr->SingleMethodExecute();
  contr->MultipleMethodExecute();
  contr->CreateOutputWindow();
  contr->Barrier();
  contr->Finalize();

Berk Geveci's avatar
Berk Geveci committed
270
  vtkPolyDataMapper* pmapper = vtkPolyDataMapper::New();
271
  vtkPolyData* pd = vtkPolyData::New();
Berk Geveci's avatar
Berk Geveci committed
272
  comm->Receive(pd, 1, 11);
273
  pmapper->SetInput(pd);
274
  pd->Delete();
Berk Geveci's avatar
Berk Geveci committed
275
276
277
278
279
280

  vtkActor* pactor = vtkActor::New();
  pactor->SetMapper(pmapper);
  pmapper->UnRegister(0);

  vtkDataSetMapper* rgmapper = vtkDataSetMapper::New();
281
  vtkRectilinearGrid* rg = vtkRectilinearGrid::New();
Berk Geveci's avatar
Berk Geveci committed
282
  comm->Receive(rg, 1, 11);
283
  rgmapper->SetInput(rg);
284
  rg->Delete();
Berk Geveci's avatar
Berk Geveci committed
285
286
287
288
289
290
291
292

  vtkActor* rgactor = vtkActor::New();
  rgactor->SetMapper(rgmapper);
  rgactor->SetPosition(0, -5, 0);
  rgactor->SetScale(2, 2, 2);
  rgmapper->UnRegister(0);

  vtkContourFilter* iso2 = vtkContourFilter::New();
293
  vtkStructuredGrid* sg = vtkStructuredGrid::New();
Berk Geveci's avatar
Berk Geveci committed
294
  comm->Receive(sg, 1, 11);
295
  iso2->SetInput(sg);
296
  sg->Delete();
Berk Geveci's avatar
Berk Geveci committed
297
298
299
  iso2->SetValue(0, .205);

  vtkPolyDataMapper* sgmapper = vtkPolyDataMapper::New();
Berk Geveci's avatar
Berk Geveci committed
300
  sgmapper->SetInputConnection(0, iso2->GetOutputPort());
Berk Geveci's avatar
Berk Geveci committed
301
302
303
304
  iso2->UnRegister(0);

  vtkActor* sgactor = vtkActor::New();
  sgactor->SetMapper(sgmapper);
305
  sgactor->SetPosition(10, -5, -40);
Berk Geveci's avatar
Berk Geveci committed
306
307
308
  sgmapper->UnRegister(0);


309
  vtkImageData* id = vtkImageData::New();
Berk Geveci's avatar
Berk Geveci committed
310
  comm->Receive(id, 1, 11);
Berk Geveci's avatar
Berk Geveci committed
311

312
313
314
315
  vtkImageActor* imactor = vtkImageActor::New();
  imactor->SetInput(id);
  id->Delete();
  imactor->SetPosition(10, 0, 10);
Berk Geveci's avatar
Berk Geveci committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  imactor->SetScale(0.02, 0.02, 0.02);

  vtkRenderer* ren = vtkRenderer::New();
  ren->AddActor(uactor);
  ren->AddActor(pactor);
  ren->AddActor(rgactor);
  ren->AddActor(sgactor);
  ren->AddActor(imactor);
  uactor->UnRegister(0);
  pactor->UnRegister(0);
  rgactor->UnRegister(0);
  sgactor->UnRegister(0);
  imactor->UnRegister(0);

  vtkRenderWindow* renWin = vtkRenderWindow::New();
331
  renWin->SetSize(500,400);
Berk Geveci's avatar
Berk Geveci committed
332
  renWin->AddRenderer(ren);
Sebastien Barre's avatar
Sebastien Barre committed
333
  ren->ResetCamera();
334
  ren->GetActiveCamera()->Zoom(2.2);
Berk Geveci's avatar
Berk Geveci committed
335
336
337
338
339
340
341
342
343
  ren->UnRegister(0);

  renWin->Render();

  int retVal = vtkRegressionTestImage( renWin );

  renWin->Delete();
  ugrid->Delete();
  CleanUp(comm, contr);
344

345
  return !retVal;
Berk Geveci's avatar
Berk Geveci committed
346
}