VTK
vtkSocketCommunicator.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSocketCommunicator.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
39 #ifndef __vtkSocketCommunicator_h
40 #define __vtkSocketCommunicator_h
41 
42 #include "vtkCommunicator.h"
43 
44 #include "vtkByteSwap.h" // Needed for vtkSwap macros
45 
46 #ifdef VTK_WORDS_BIGENDIAN
47 # define vtkSwap4 vtkByteSwap::Swap4LE
48 # define vtkSwap4Range vtkByteSwap::Swap4LERange
49 # define vtkSwap8 vtkByteSwap::Swap8LE
50 # define vtkSwap8Range vtkByteSwap::Swap8LERange
51 #else
52 # define vtkSwap4 vtkByteSwap::Swap4BE
53 # define vtkSwap4Range vtkByteSwap::Swap4BERange
54 # define vtkSwap8 vtkByteSwap::Swap8BE
55 # define vtkSwap8Range vtkByteSwap::Swap8BERange
56 #endif
57 
58 class vtkClientSocket;
59 class vtkServerSocket;
60 
62 {
63 public:
64  static vtkSocketCommunicator *New();
66  void PrintSelf(ostream& os, vtkIndent indent);
67 
69 
71  virtual int WaitForConnection(int port);
72  virtual int WaitForConnection(vtkServerSocket* socket,
73  unsigned long msec = 0);
75 
77  virtual void CloseConnection();
78 
80  virtual int ConnectTo(const char* hostName, int port);
81 
83 
84  vtkGetMacro(SwapBytesInReceivedData, int);
86 
88  int GetIsConnected();
89 
91  virtual void SetNumberOfProcesses(int num);
92 
93  //------------------ Communication --------------------
94 
96 
98  virtual int SendVoidArray(const void *data, vtkIdType length, int type,
99  int remoteHandle, int tag);
100  virtual int ReceiveVoidArray(void *data, vtkIdType length, int type,
101  int remoteHandle, int tag);
103 
106  virtual void Barrier();
107 
109 
112  virtual int BroadcastVoidArray(void *data, vtkIdType length, int type,
113  int srcProcessId);
114  virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer,
115  vtkIdType length, int type, int destProcessId);
116  virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer,
117  vtkIdType sendLength, vtkIdType *recvLengths,
118  vtkIdType *offsets, int type, int destProcessId);
119  virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer,
120  vtkIdType length, int type, int srcProcessId);
121  virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer,
122  vtkIdType *sendLengths, vtkIdType *offsets,
123  vtkIdType recvLength, int type,
124  int srcProcessId);
125  virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer,
126  vtkIdType length, int type);
127  virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer,
128  vtkIdType sendLength, vtkIdType *recvLengths,
129  vtkIdType *offsets, int type);
130  virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
131  vtkIdType length, int type,
132  int operation, int destProcessId);
133  virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
134  vtkIdType length, int type,
135  Operation *operation, int destProcessId);
136  virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
137  vtkIdType length, int type,
138  int operation);
139  virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
140  vtkIdType length, int type,
141  Operation *operation);
143 
145 
147  vtkSetClampMacro(PerformHandshake, int, 0, 1);
148  vtkBooleanMacro(PerformHandshake, int);
149  vtkGetMacro(PerformHandshake, int);
151 
152  //BTX
154 
156  virtual void SetLogStream(ostream* stream);
157  virtual ostream* GetLogStream();
158  //ETX
160 
162 
166  virtual int LogToFile(const char* name);
167  virtual int LogToFile(const char* name, int append);
169 
171 
172  vtkSetMacro(ReportErrors, int);
173  vtkGetMacro(ReportErrors, int);
175 
177 
178  vtkGetObjectMacro(Socket, vtkClientSocket);
179  void SetSocket(vtkClientSocket*);
181 
184  int Handshake();
185 
188  int ServerSideHandshake();
189 
192  int ClientSideHandshake();
193 
195 
197  vtkGetMacro(IsServer, int);
199 
200 //BTX
201 protected:
202 
207  int IsServer;
208 
210 
211  ofstream* LogFile;
212  ostream* LogStream;
213 
216 
217  // Wrappers around send/recv calls to implement loops. Return 1 for
218  // success, and 0 for failure.
219  int SendTagged(const void* data, int wordSize, int numWords, int tag,
220  const char* logName);
221  int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
222  const char* logName);
223  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
224  const char* logName);
225 
226  // Internal utility methods.
227  void LogTagged(const char* name, const void* data, int wordSize, int numWords,
228  int tag, const char* logName);
229  int CheckForErrorInternal(int id);
230 
231 private:
232  vtkSocketCommunicator(const vtkSocketCommunicator&); // Not implemented.
233  void operator=(const vtkSocketCommunicator&); // Not implemented.
234 
235  int SelectSocket(int socket, unsigned long msec);
236 
237  // SwapBytesInReceiveData needs an invalid / not set.
238  // This avoids checking length of endian handshake.
239  enum ErrorIds {
240  SwapOff = 0,
241  SwapOn,
242  SwapNotSet
243  };
244 
245  // One may be tempted to change this to a vtkIdType, but really an int is
246  // enough since we split messages > VTK_INT_MAX.
247  int TagMessageLength;
248 
250 
252  static int GetVersion();
253 //ETX
254 };
256 
257 #endif
virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId)
virtual int BroadcastVoidArray(void *data, vtkIdType length, int type, int srcProcessId)
Process communication using Sockets.
int vtkIdType
Definition: vtkType.h:255
Encapsulate a socket that accepts connections.
virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type)
virtual int ReceiveVoidArray(void *data, vtkIdType maxlength, int type, int remoteHandle, int tag)=0
virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId)
a simple class to control print indentation
Definition: vtkIndent.h:37
virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId)
virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType *sendLengths, vtkIdType *offsets, vtkIdType recvLength, int type, int srcProcessId)
virtual int SendVoidArray(const void *data, vtkIdType length, int type, int remoteHandle, int tag)=0
virtual void SetNumberOfProcesses(int num)
void PrintSelf(ostream &os, vtkIndent indent)
virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type)
virtual void Barrier()
#define VTK_PARALLEL_EXPORT
virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation)
virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId)
static vtkObject * New()
Used to send/receive messages in a multiprocess environment.
Encapsulates a client socket.