VTK  9.0.1
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 =========================================================================*/
34 #ifndef vtkSocketCommunicator_h
35 #define vtkSocketCommunicator_h
36 
37 #include "vtkCommunicator.h"
38 #include "vtkParallelCoreModule.h" // For export macro
39 
40 #include "vtkByteSwap.h" // Needed for vtkSwap macros
41 
42 #ifdef VTK_WORDS_BIGENDIAN
43 #define vtkSwap4 vtkByteSwap::Swap4LE
44 #define vtkSwap4Range vtkByteSwap::Swap4LERange
45 #define vtkSwap8 vtkByteSwap::Swap8LE
46 #define vtkSwap8Range vtkByteSwap::Swap8LERange
47 #else
48 #define vtkSwap4 vtkByteSwap::Swap4BE
49 #define vtkSwap4Range vtkByteSwap::Swap4BERange
50 #define vtkSwap8 vtkByteSwap::Swap8BE
51 #define vtkSwap8Range vtkByteSwap::Swap8BERange
52 #endif
53 
54 class vtkClientSocket;
55 class vtkServerSocket;
56 
57 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
58 {
59 public:
62  void PrintSelf(ostream& os, vtkIndent indent) override;
63 
65 
69  virtual int WaitForConnection(int port);
70  virtual int WaitForConnection(vtkServerSocket* socket, unsigned long msec = 0);
72 
76  virtual void CloseConnection();
77 
81  virtual int ConnectTo(const char* hostName, int port);
82 
84 
87  vtkGetMacro(SwapBytesInReceivedData, int);
89 
94 
98  void SetNumberOfProcesses(int num) override;
99 
100  //------------------ Communication --------------------
101 
103 
108  const void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
109  int ReceiveVoidArray(void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
111 
116  void Barrier() override;
117 
119 
124  int BroadcastVoidArray(void* data, vtkIdType length, int type, int srcProcessId) override;
125  int GatherVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
126  int destProcessId) override;
127  int GatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
128  vtkIdType* recvLengths, vtkIdType* offsets, int type, int destProcessId) override;
129  int ScatterVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
130  int srcProcessId) override;
131  int ScatterVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType* sendLengths,
132  vtkIdType* offsets, vtkIdType recvLength, int type, int srcProcessId) override;
134  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type) override;
135  int AllGatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
136  vtkIdType* recvLengths, vtkIdType* offsets, int type) override;
137  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
138  int operation, int destProcessId) override;
139  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
140  Operation* operation, int destProcessId) override;
142  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type, int operation) override;
143  int AllReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
144  Operation* operation) override;
146 
148 
153  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
154  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
155  vtkGetMacro(PerformHandshake, vtkTypeBool);
157 
159 
163  virtual void SetLogStream(ostream* stream);
164  virtual ostream* GetLogStream();
166 
168 
174  virtual int LogToFile(const char* name);
175  virtual int LogToFile(const char* name, int append);
177 
179 
182  vtkSetMacro(ReportErrors, int);
183  vtkGetMacro(ReportErrors, int);
185 
187 
190  vtkGetObjectMacro(Socket, vtkClientSocket);
193 
198  int Handshake();
199 
206 
213 
215 
219  vtkGetMacro(IsServer, int);
221 
226  static int GetVersion();
227 
236  void BufferCurrentMessage() { this->BufferMessage = true; }
237 
242 
243 protected:
248  int IsServer;
249 
251 
252  ostream* LogFile;
253  ostream* LogStream;
254 
257 
258  // Wrappers around send/recv calls to implement loops. Return 1 for
259  // success, and 0 for failure.
260  int SendTagged(const void* data, int wordSize, int numWords, int tag, const char* logName);
261  int ReceiveTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
262  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
263 
265  void* data, int wordSize, int numWords, int tag, const char* logName);
266 
270  void FixByteOrder(void* data, int wordSize, int numWords);
271 
272  // Internal utility methods.
273  void LogTagged(
274  const char* name, const void* data, int wordSize, int numWords, int tag, const char* logName);
277 
278 private:
280  void operator=(const vtkSocketCommunicator&) = delete;
281 
282  int SelectSocket(int socket, unsigned long msec);
283 
284  // SwapBytesInReceiveData needs an invalid / not set.
285  // This avoids checking length of endian handshake.
286  enum ErrorIds
287  {
288  SwapOff = 0,
289  SwapOn,
290  SwapNotSet
291  };
292 
293  // One may be tempted to change this to a vtkIdType, but really an int is
294  // enough since we split messages > VTK_INT_MAX.
295  int TagMessageLength;
296 
297  // Buffer to save messages received with different tag than requested.
298  class vtkMessageBuffer;
299  vtkMessageBuffer* ReceivedMessageBuffer;
300 };
301 
302 #endif
Encapsulates a client socket.
A custom operation to use in a reduce command.
Used to send/receive messages in a multiprocess environment.
a simple class to control print indentation
Definition: vtkIndent.h:34
Encapsulate a socket that accepts connections.
Process communication using Sockets.
int ReceiveVoidArray(void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Subclasses have to supply this method to receive various arrays of data.
int ClientSideHandshake()
Performs ClientSide handshake.
int BroadcastVoidArray(void *data, vtkIdType length, int type, int srcProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
int Handshake()
Performs handshake.
int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId) override
virtual int LogToFile(const char *name, int append)
virtual int ConnectTo(const char *hostName, int port)
Open a connection to host.
int ServerSideHandshake()
Performs ServerSide handshake.
virtual ostream * GetLogStream()
void Barrier() override
This class foolishly breaks the conventions of the superclass, so this overload fixes the method.
int GetIsConnected()
Is the communicator connected?.
int SendVoidArray(const void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Performs the actual communication.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId) override
int ReceivePartialTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
virtual void CloseConnection()
Close a connection.
void FixByteOrder(void *data, int wordSize, int numWords)
Fix byte order for received data.
int SendTagged(const void *data, int wordSize, int numWords, int tag, const char *logName)
void LogTagged(const char *name, const void *data, int wordSize, int numWords, int tag, const char *logName)
int ReceivedTaggedFromBuffer(void *data, int wordSize, int numWords, int tag, const char *logName)
virtual int WaitForConnection(vtkServerSocket *socket, unsigned long msec=0)
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation, int destProcessId) override
static int GetVersion()
Uniquely identifies the version of this class.
virtual int WaitForConnection(int port)
Wait for connection on a given port.
void SetNumberOfProcesses(int num) override
Set the number of processes you will be using.
int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType *sendLengths, vtkIdType *offsets, vtkIdType recvLength, int type, int srcProcessId) override
int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId) override
int CheckForErrorInternal(int id)
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId) override
int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type) override
virtual int LogToFile(const char *name)
Log messages to the given file.
~vtkSocketCommunicator() override
bool HasBufferredMessages()
Returns true if there are any messages in the receive buffer.
int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type) override
int ReceiveTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
void SetSocket(vtkClientSocket *)
virtual void SetLogStream(ostream *stream)
Get/Set the output stream to which communications should be logged.
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation) override
static vtkSocketCommunicator * New()
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation) override
void BufferCurrentMessage()
This flag is cleared before vtkCommand::WrongTagEvent is fired when ever a message with mismatched ta...
@ length
Definition: vtkX3D.h:399
@ port
Definition: vtkX3D.h:453
@ type
Definition: vtkX3D.h:522
@ name
Definition: vtkX3D.h:225
@ data
Definition: vtkX3D.h:321
int vtkTypeBool
Definition: vtkABI.h:69
int vtkIdType
Definition: vtkType.h:338