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 "vtkParallelCoreModule.h" // For export macro
43 #include "vtkCommunicator.h"
44 
45 #include "vtkByteSwap.h" // Needed for vtkSwap macros
46 
47 #ifdef VTK_WORDS_BIGENDIAN
48 # define vtkSwap4 vtkByteSwap::Swap4LE
49 # define vtkSwap4Range vtkByteSwap::Swap4LERange
50 # define vtkSwap8 vtkByteSwap::Swap8LE
51 # define vtkSwap8Range vtkByteSwap::Swap8LERange
52 #else
53 # define vtkSwap4 vtkByteSwap::Swap4BE
54 # define vtkSwap4Range vtkByteSwap::Swap4BERange
55 # define vtkSwap8 vtkByteSwap::Swap8BE
56 # define vtkSwap8Range vtkByteSwap::Swap8BERange
57 #endif
58 
59 class vtkClientSocket;
60 class vtkServerSocket;
61 
63 {
64 public:
65  static vtkSocketCommunicator *New();
67  void PrintSelf(ostream& os, vtkIndent indent);
68 
70 
72  virtual int WaitForConnection(int port);
73  virtual int WaitForConnection(vtkServerSocket* socket,
74  unsigned long msec = 0);
76 
78  virtual void CloseConnection();
79 
81  virtual int ConnectTo(const char* hostName, int port);
82 
84 
85  vtkGetMacro(SwapBytesInReceivedData, int);
87 
89  int GetIsConnected();
90 
92  virtual void SetNumberOfProcesses(int num);
93 
94  //------------------ Communication --------------------
95 
97 
99  virtual int SendVoidArray(const void *data, vtkIdType length, int type,
100  int remoteHandle, int tag);
101  virtual int ReceiveVoidArray(void *data, vtkIdType length, int type,
102  int remoteHandle, int tag);
104 
107  virtual void Barrier();
108 
110 
113  virtual int BroadcastVoidArray(void *data, vtkIdType length, int type,
114  int srcProcessId);
115  virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer,
116  vtkIdType length, int type, int destProcessId);
117  virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer,
118  vtkIdType sendLength, vtkIdType *recvLengths,
119  vtkIdType *offsets, int type, int destProcessId);
120  virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer,
121  vtkIdType length, int type, int srcProcessId);
122  virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer,
123  vtkIdType *sendLengths, vtkIdType *offsets,
124  vtkIdType recvLength, int type,
125  int srcProcessId);
126  virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer,
127  vtkIdType length, int type);
128  virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer,
129  vtkIdType sendLength, vtkIdType *recvLengths,
130  vtkIdType *offsets, int type);
131  virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
132  vtkIdType length, int type,
133  int operation, int destProcessId);
134  virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
135  vtkIdType length, int type,
136  Operation *operation, int destProcessId);
137  virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
138  vtkIdType length, int type,
139  int operation);
140  virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
141  vtkIdType length, int type,
142  Operation *operation);
144 
146 
148  vtkSetClampMacro(PerformHandshake, int, 0, 1);
149  vtkBooleanMacro(PerformHandshake, int);
150  vtkGetMacro(PerformHandshake, int);
152 
153  //BTX
155 
157  virtual void SetLogStream(ostream* stream);
158  virtual ostream* GetLogStream();
159  //ETX
161 
163 
167  virtual int LogToFile(const char* name);
168  virtual int LogToFile(const char* name, int append);
170 
172 
173  vtkSetMacro(ReportErrors, int);
174  vtkGetMacro(ReportErrors, int);
176 
178 
179  vtkGetObjectMacro(Socket, vtkClientSocket);
180  void SetSocket(vtkClientSocket*);
182 
185  int Handshake();
186 
189  int ServerSideHandshake();
190 
193  int ClientSideHandshake();
194 
196 
198  vtkGetMacro(IsServer, int);
200 
203  static int GetVersion();
204 
206 
213  { this->BufferMessage = true; }
215 
217  bool HasBufferredMessages();
218 
219 //BTX
220 protected:
221 
226  int IsServer;
227 
229 
230  ofstream* LogFile;
231  ostream* LogStream;
232 
235 
236  // Wrappers around send/recv calls to implement loops. Return 1 for
237  // success, and 0 for failure.
238  int SendTagged(const void* data, int wordSize, int numWords, int tag,
239  const char* logName);
240  int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
241  const char* logName);
242  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
243  const char* logName);
244 
245  int ReceivedTaggedFromBuffer(
246  void* data, int wordSize, int numWords, int tag, const char* logName);
247 
249  void FixByteOrder(void* data, int wordSize, int numWords);
250 
251  // Internal utility methods.
252  void LogTagged(const char* name, const void* data, int wordSize, int numWords,
253  int tag, const char* logName);
254  int CheckForErrorInternal(int id);
256 private:
257  vtkSocketCommunicator(const vtkSocketCommunicator&); // Not implemented.
258  void operator=(const vtkSocketCommunicator&); // Not implemented.
259 
260  int SelectSocket(int socket, unsigned long msec);
261 
262  // SwapBytesInReceiveData needs an invalid / not set.
263  // This avoids checking length of endian handshake.
264  enum ErrorIds {
265  SwapOff = 0,
266  SwapOn,
267  SwapNotSet
268  };
269 
270  // One may be tempted to change this to a vtkIdType, but really an int is
271  // enough since we split messages > VTK_INT_MAX.
272  int TagMessageLength;
273 
274  // Buffer to save messages received with different tag than requested.
275  class vtkMessageBuffer;
276  vtkMessageBuffer* ReceivedMessageBuffer;
277 
278 //ETX
279 };
280 
281 #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)
#define VTKPARALLELCORE_EXPORT
Process communication using Sockets.
int vtkIdType
Definition: vtkType.h:275
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:38
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()
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.