VTK  9.1.0
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 =========================================================================*/
40 #ifndef vtkSocketCommunicator_h
41 #define vtkSocketCommunicator_h
42 
43 #include "vtkCommunicator.h"
44 #include "vtkEndian.h" // for VTK_WORDS_BIGENDIAN
45 #include "vtkParallelCoreModule.h" // For export macro
46 
47 #include "vtkByteSwap.h" // Needed for vtkSwap macros
48 
49 #ifdef VTK_WORDS_BIGENDIAN
50 #define vtkSwap4 vtkByteSwap::Swap4LE
51 #define vtkSwap4Range vtkByteSwap::Swap4LERange
52 #define vtkSwap8 vtkByteSwap::Swap8LE
53 #define vtkSwap8Range vtkByteSwap::Swap8LERange
54 #else
55 #define vtkSwap4 vtkByteSwap::Swap4BE
56 #define vtkSwap4Range vtkByteSwap::Swap4BERange
57 #define vtkSwap8 vtkByteSwap::Swap8BE
58 #define vtkSwap8Range vtkByteSwap::Swap8BERange
59 #endif
60 
61 class vtkClientSocket;
62 class vtkServerSocket;
63 
64 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
65 {
66 public:
69  void PrintSelf(ostream& os, vtkIndent indent) override;
70 
72 
76  virtual int WaitForConnection(int port);
77  virtual int WaitForConnection(vtkServerSocket* socket, unsigned long msec = 0);
79 
83  virtual void CloseConnection();
84 
88  virtual int ConnectTo(const char* hostName, int port);
89 
91 
94  vtkGetMacro(SwapBytesInReceivedData, int);
96 
101 
105  void SetNumberOfProcesses(int num) override;
106 
107  //------------------ Communication --------------------
108 
110 
115  const void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
116  int ReceiveVoidArray(void* data, vtkIdType length, int type, int remoteHandle, int tag) override;
118 
123  void Barrier() override;
124 
126 
131  int BroadcastVoidArray(void* data, vtkIdType length, int type, int srcProcessId) override;
132  int GatherVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
133  int destProcessId) override;
134  int GatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
135  vtkIdType* recvLengths, vtkIdType* offsets, int type, int destProcessId) override;
136  int ScatterVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
137  int srcProcessId) override;
138  int ScatterVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType* sendLengths,
139  vtkIdType* offsets, vtkIdType recvLength, int type, int srcProcessId) override;
141  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type) override;
142  int AllGatherVVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType sendLength,
143  vtkIdType* recvLengths, vtkIdType* offsets, int type) override;
144  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
145  int operation, int destProcessId) override;
146  int ReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
147  Operation* operation, int destProcessId) override;
149  const void* sendBuffer, void* recvBuffer, vtkIdType length, int type, int operation) override;
150  int AllReduceVoidArray(const void* sendBuffer, void* recvBuffer, vtkIdType length, int type,
151  Operation* operation) override;
153 
155 
160  vtkSetClampMacro(PerformHandshake, vtkTypeBool, 0, 1);
161  vtkBooleanMacro(PerformHandshake, vtkTypeBool);
162  vtkGetMacro(PerformHandshake, vtkTypeBool);
164 
166 
170  virtual void SetLogStream(ostream* stream);
171  virtual ostream* GetLogStream();
173 
175 
181  virtual int LogToFile(const char* name);
182  virtual int LogToFile(const char* name, int append);
184 
186 
189  vtkSetMacro(ReportErrors, int);
190  vtkGetMacro(ReportErrors, int);
192 
194 
197  vtkGetObjectMacro(Socket, vtkClientSocket);
200 
205  int Handshake();
206 
213 
220 
222 
226  vtkGetMacro(IsServer, int);
228 
233  static int GetVersion();
234 
243  void BufferCurrentMessage() { this->BufferMessage = true; }
244 
249 
250 protected:
255  int IsServer;
256 
258 
259  ostream* LogFile;
260  ostream* LogStream;
261 
264 
265  // Wrappers around send/recv calls to implement loops. Return 1 for
266  // success, and 0 for failure.
267  int SendTagged(const void* data, int wordSize, int numWords, int tag, const char* logName);
268  int ReceiveTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
269  int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, const char* logName);
270 
272  void* data, int wordSize, int numWords, int tag, const char* logName);
273 
277  void FixByteOrder(void* data, int wordSize, int numWords);
278 
279  // Internal utility methods.
280  void LogTagged(
281  const char* name, const void* data, int wordSize, int numWords, int tag, const char* logName);
284 
285 private:
287  void operator=(const vtkSocketCommunicator&) = delete;
288 
289  int SelectSocket(int socket, unsigned long msec);
290 
291  // SwapBytesInReceiveData needs an invalid / not set.
292  // This avoids checking length of endian handshake.
293  enum ErrorIds
294  {
295  SwapOff = 0,
296  SwapOn,
297  SwapNotSet
298  };
299 
300  // One may be tempted to change this to a vtkIdType, but really an int is
301  // enough since we split messages > VTK_INT_MAX.
302  int TagMessageLength;
303 
304  // Buffer to save messages received with different tag than requested.
305  class vtkMessageBuffer;
306  vtkMessageBuffer* ReceivedMessageBuffer;
307 };
308 
309 #endif
vtkSocketCommunicator::LogToFile
virtual int LogToFile(const char *name)
Log messages to the given file.
vtkSocketCommunicator::New
static vtkSocketCommunicator * New()
vtkSocketCommunicator::RemoteHas64BitIds
int RemoteHas64BitIds
Definition: vtkSocketCommunicator.h:253
vtkSocketCommunicator::PerformHandshake
vtkTypeBool PerformHandshake
Definition: vtkSocketCommunicator.h:254
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkCommunicator
Used to send/receive messages in a multiprocess environment.
Definition: vtkCommunicator.h:51
vtkIdType
int vtkIdType
Definition: vtkType.h:332
vtkSocketCommunicator
Process communication using Sockets.
Definition: vtkSocketCommunicator.h:65
vtkX3D::data
@ data
Definition: vtkX3D.h:321
vtkSocketCommunicator::LogToFile
virtual int LogToFile(const char *name, int append)
Log messages to the given file.
vtkSocketCommunicator::LogFile
ostream * LogFile
Definition: vtkSocketCommunicator.h:259
vtkCommunicator::Operation
A custom operation to use in a reduce command.
Definition: vtkCommunicator.h:104
vtkSocketCommunicator::AllReduceVoidArray
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::BufferMessage
bool BufferMessage
Definition: vtkSocketCommunicator.h:283
vtkSocketCommunicator::CheckForErrorInternal
int CheckForErrorInternal(int id)
vtkX3D::length
@ length
Definition: vtkX3D.h:399
vtkSocketCommunicator::Barrier
void Barrier() override
This class foolishly breaks the conventions of the superclass, so this overload fixes the method.
vtkSocketCommunicator::ScatterVVoidArray
int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType *sendLengths, vtkIdType *offsets, vtkIdType recvLength, int type, int srcProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::GatherVoidArray
int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::GetLogStream
virtual ostream * GetLogStream()
Get/Set the output stream to which communications should be logged.
vtkSocketCommunicator::ReceiveVoidArray
int ReceiveVoidArray(void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Performs the actual communication.
vtkSocketCommunicator::GatherVVoidArray
int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::SetSocket
void SetSocket(vtkClientSocket *)
Get/Set the actual socket used for communication.
vtkSocketCommunicator::ReceiveTagged
int ReceiveTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
vtkX3D::port
@ port
Definition: vtkX3D.h:453
vtkSocketCommunicator::vtkSocketCommunicator
vtkSocketCommunicator()
vtkSocketCommunicator::ConnectTo
virtual int ConnectTo(const char *hostName, int port)
Open a connection to host.
vtkSocketCommunicator::ReceivePartialTagged
int ReceivePartialTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
vtkSocketCommunicator::WaitForConnection
virtual int WaitForConnection(int port)
Wait for connection on a given port.
vtkSocketCommunicator::HasBufferredMessages
bool HasBufferredMessages()
Returns true if there are any messages in the receive buffer.
vtkServerSocket
Encapsulate a socket that accepts connections.
Definition: vtkServerSocket.h:30
vtkSocketCommunicator::AllGatherVoidArray
int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkByteSwap.h
vtkSocketCommunicator::GetVersion
static int GetVersion()
Uniquely identifies the version of this class.
vtkSocketCommunicator::~vtkSocketCommunicator
~vtkSocketCommunicator() override
vtkSocketCommunicator::IsServer
int IsServer
Definition: vtkSocketCommunicator.h:255
vtkSocketCommunicator::SetNumberOfProcesses
void SetNumberOfProcesses(int num) override
Set the number of processes you will be using.
vtkSocketCommunicator::ReceivedTaggedFromBuffer
int ReceivedTaggedFromBuffer(void *data, int wordSize, int numWords, int tag, const char *logName)
vtkSocketCommunicator::FixByteOrder
void FixByteOrder(void *data, int wordSize, int numWords)
Fix byte order for received data.
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkSocketCommunicator::Handshake
int Handshake()
Performs handshake.
vtkSocketCommunicator::SendTagged
int SendTagged(const void *data, int wordSize, int numWords, int tag, const char *logName)
vtkSocketCommunicator::Socket
vtkClientSocket * Socket
Definition: vtkSocketCommunicator.h:251
vtkSocketCommunicator::ScatterVoidArray
int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::SwapBytesInReceivedData
int SwapBytesInReceivedData
Definition: vtkSocketCommunicator.h:252
vtkSocketCommunicator::AllReduceVoidArray
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkSocketCommunicator::SetLogStream
virtual void SetLogStream(ostream *stream)
Get/Set the output stream to which communications should be logged.
vtkSocketCommunicator::BroadcastVoidArray
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...
vtkClientSocket
Encapsulates a client socket.
Definition: vtkClientSocket.h:28
vtkSocketCommunicator::ReduceVoidArray
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::GetIsConnected
int GetIsConnected()
Is the communicator connected?.
vtkSocketCommunicator::SendVoidArray
int SendVoidArray(const void *data, vtkIdType length, int type, int remoteHandle, int tag) override
Performs the actual communication.
vtkCommunicator.h
vtkSocketCommunicator::ClientSideHandshake
int ClientSideHandshake()
Performs ClientSide handshake.
vtkSocketCommunicator::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkSocketCommunicator::BufferCurrentMessage
void BufferCurrentMessage()
This flag is cleared before vtkCommand::WrongTagEvent is fired when ever a message with mismatched ta...
Definition: vtkSocketCommunicator.h:243
vtkSocketCommunicator::CloseConnection
virtual void CloseConnection()
Close a connection.
vtkSocketCommunicator::ReduceVoidArray
int ReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation, int destProcessId) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::ReportErrors
int ReportErrors
Definition: vtkSocketCommunicator.h:257
vtkSocketCommunicator::ServerSideHandshake
int ServerSideHandshake()
Performs ServerSide handshake.
vtkSocketCommunicator::AllGatherVVoidArray
int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type) override
This class foolishly breaks the conventions of the superclass, so the default implementations of thes...
vtkSocketCommunicator::LogTagged
void LogTagged(const char *name, const void *data, int wordSize, int numWords, int tag, const char *logName)
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkSocketCommunicator::WaitForConnection
virtual int WaitForConnection(vtkServerSocket *socket, unsigned long msec=0)
Wait for connection on a given port.
vtkSocketCommunicator::LogStream
ostream * LogStream
Definition: vtkSocketCommunicator.h:260