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 =========================================================================*/
37 #ifndef vtkSocketCommunicator_h
38 #define vtkSocketCommunicator_h
39 
40 #include "vtkParallelCoreModule.h" // For export macro
41 #include "vtkCommunicator.h"
42 
43 #include "vtkByteSwap.h" // Needed for vtkSwap macros
44 
45 #ifdef VTK_WORDS_BIGENDIAN
46 # define vtkSwap4 vtkByteSwap::Swap4LE
47 # define vtkSwap4Range vtkByteSwap::Swap4LERange
48 # define vtkSwap8 vtkByteSwap::Swap8LE
49 # define vtkSwap8Range vtkByteSwap::Swap8LERange
50 #else
51 # define vtkSwap4 vtkByteSwap::Swap4BE
52 # define vtkSwap4Range vtkByteSwap::Swap4BERange
53 # define vtkSwap8 vtkByteSwap::Swap8BE
54 # define vtkSwap8Range vtkByteSwap::Swap8BERange
55 #endif
56 
57 class vtkClientSocket;
58 class vtkServerSocket;
59 
60 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
61 {
62 public:
63  static vtkSocketCommunicator *New();
65  void PrintSelf(ostream& os, vtkIndent indent) override;
66 
68 
72  virtual int WaitForConnection(int port);
73  virtual int WaitForConnection(vtkServerSocket* socket,
74  unsigned long msec = 0);
76 
80  virtual void CloseConnection();
81 
85  virtual int ConnectTo(const char* hostName, int port);
86 
88 
91  vtkGetMacro(SwapBytesInReceivedData, int);
93 
97  int GetIsConnected();
98 
102  void SetNumberOfProcesses(int num) override;
103 
104  //------------------ Communication --------------------
105 
107 
111  int SendVoidArray(const void *data, vtkIdType length, int type,
112  int remoteHandle, int tag) override;
113  int ReceiveVoidArray(void *data, vtkIdType length, int type,
114  int remoteHandle, int tag) override;
116 
121  void Barrier() override;
122 
124 
129  int BroadcastVoidArray(void *data, vtkIdType length, int type,
130  int srcProcessId) override;
131  int GatherVoidArray(const void *sendBuffer, void *recvBuffer,
132  vtkIdType length, int type, int destProcessId) override;
133  int GatherVVoidArray(const void *sendBuffer, void *recvBuffer,
134  vtkIdType sendLength, vtkIdType *recvLengths,
135  vtkIdType *offsets, int type, int destProcessId) override;
136  int ScatterVoidArray(const void *sendBuffer, void *recvBuffer,
137  vtkIdType length, int type, int srcProcessId) override;
138  int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer,
139  vtkIdType *sendLengths, vtkIdType *offsets,
140  vtkIdType recvLength, int type,
141  int srcProcessId) override;
142  int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer,
143  vtkIdType length, int type) override;
144  int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer,
145  vtkIdType sendLength, vtkIdType *recvLengths,
146  vtkIdType *offsets, int type) override;
147  int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
148  vtkIdType length, int type,
149  int operation, int destProcessId) override;
150  int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
151  vtkIdType length, int type,
152  Operation *operation, int destProcessId) override;
153  int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
154  vtkIdType length, int type,
155  int operation) override;
156  int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
157  vtkIdType length, int type,
158  Operation *operation) override;
160 
162 
167  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
168  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
169  vtkGetMacro(PerformHandshake, vtkTypeBool);
171 
173 
177  virtual void SetLogStream(ostream* stream);
178  virtual ostream* GetLogStream();
180 
182 
188  virtual int LogToFile(const char* name);
189  virtual int LogToFile(const char* name, int append);
191 
193 
196  vtkSetMacro(ReportErrors, int);
197  vtkGetMacro(ReportErrors, int);
199 
201 
204  vtkGetObjectMacro(Socket, vtkClientSocket);
205  void SetSocket(vtkClientSocket*);
207 
212  int Handshake();
213 
219  int ServerSideHandshake();
220 
226  int ClientSideHandshake();
227 
229 
233  vtkGetMacro(IsServer, int);
235 
240  static int GetVersion();
241 
251  { this->BufferMessage = true; }
252 
256  bool HasBufferredMessages();
257 
258 protected:
259 
264  int IsServer;
265 
267 
268  ofstream* LogFile;
269  ostream* LogStream;
270 
272  ~vtkSocketCommunicator() override;
273 
274  // Wrappers around send/recv calls to implement loops. Return 1 for
275  // success, and 0 for failure.
276  int SendTagged(const void* data, int wordSize, int numWords, int tag,
277  const char* logName);
278  int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
279  const char* logName);
280  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
281  const char* logName);
282 
283  int ReceivedTaggedFromBuffer(
284  void* data, int wordSize, int numWords, int tag, const char* logName);
285 
289  void FixByteOrder(void* data, int wordSize, int numWords);
290 
291  // Internal utility methods.
292  void LogTagged(const char* name, const void* data, int wordSize, int numWords,
293  int tag, const char* logName);
294  int CheckForErrorInternal(int id);
296 private:
298  void operator=(const vtkSocketCommunicator&) = delete;
299 
300  int SelectSocket(int socket, unsigned long msec);
301 
302  // SwapBytesInReceiveData needs an invalid / not set.
303  // This avoids checking length of endian handshake.
304  enum ErrorIds {
305  SwapOff = 0,
306  SwapOn,
307  SwapNotSet
308  };
309 
310  // One may be tempted to change this to a vtkIdType, but really an int is
311  // enough since we split messages > VTK_INT_MAX.
312  int TagMessageLength;
313 
314  // Buffer to save messages received with different tag than requested.
315  class vtkMessageBuffer;
316  vtkMessageBuffer* ReceivedMessageBuffer;
317 
318 };
319 
320 #endif
void BufferCurrentMessage()
This flag is cleared before vtkCommand::WrongTagEvent is fired when ever a message with mismatched ta...
A custom operation to use in a reduce command.
virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId)
Subclasses should reimplement these if they have a more efficient implementation. ...
virtual int BroadcastVoidArray(void *data, vtkIdType length, int type, int srcProcessId)
Subclasses should reimplement these if they have a more efficient implementation. ...
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
Process communication using Sockets.
int vtkIdType
Definition: vtkType.h:347
Encapsulate a socket that accepts connections.
int vtkTypeBool
Definition: vtkABI.h:69
virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type)
Subclasses should reimplement these if they have a more efficient implementation. ...
virtual int ReceiveVoidArray(void *data, vtkIdType maxlength, int type, int remoteHandle, int tag)=0
Subclasses have to supply this method to receive various arrays of data.
virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId)
Subclasses should reimplement these if they have a more efficient implementation. ...
a simple class to control print indentation
Definition: vtkIndent.h:39
virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId)
Subclasses should reimplement these if they have a more efficient implementation. ...
virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType *sendLengths, vtkIdType *offsets, vtkIdType recvLength, int type, int srcProcessId)
Subclasses should reimplement these if they have a more efficient implementation. ...
virtual int SendVoidArray(const void *data, vtkIdType length, int type, int remoteHandle, int tag)=0
Subclasses have to supply this method to send various arrays of data.
virtual void SetNumberOfProcesses(int num)
Set the number of processes you will be using.
virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type)
Subclasses should reimplement these if they have a more efficient implementation. ...
virtual void Barrier()
Will block the processes until all other processes reach the Barrier function.
virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation)
Subclasses should reimplement these if they have a more efficient implementation. ...
virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId)
Subclasses should reimplement these if they have a more efficient implementation. ...
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
Used to send/receive messages in a multiprocess environment.
Encapsulates a client socket.