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:
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 
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;
114  int remoteHandle, int tag) override;
116 
121  void Barrier() override;
122 
124 
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);
207 
212  int Handshake();
213 
220 
227 
229 
233  vtkGetMacro(IsServer, int);
235 
240  static int GetVersion();
241 
251  { this->BufferMessage = true; }
252 
257 
258 protected:
259 
264  int IsServer;
265 
267 
268  ofstream* LogFile;
269  ostream* LogStream;
270 
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 
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);
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
vtkSocketCommunicator::LogToFile
virtual int LogToFile(const char *name)
Log messages to the given file.
vtkSocketCommunicator::LogFile
ofstream * LogFile
Definition: vtkSocketCommunicator.h:268
vtkSocketCommunicator::New
static vtkSocketCommunicator * New()
vtkSocketCommunicator::RemoteHas64BitIds
int RemoteHas64BitIds
Definition: vtkSocketCommunicator.h:262
vtkSocketCommunicator::PerformHandshake
vtkTypeBool PerformHandshake
Definition: vtkSocketCommunicator.h:263
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtkCommunicator
Used to send/receive messages in a multiprocess environment.
Definition: vtkCommunicator.h:54
vtkIdType
int vtkIdType
Definition: vtkType.h:347
vtkSocketCommunicator
Process communication using Sockets.
Definition: vtkSocketCommunicator.h:61
vtkX3D::data
@ data
Definition: vtkX3D.h:315
vtkSocketCommunicator::LogToFile
virtual int LogToFile(const char *name, int append)
vtkCommunicator::Operation
A custom operation to use in a reduce command.
Definition: vtkCommunicator.h:108
vtkSocketCommunicator::AllReduceVoidArray
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, Operation *operation) override
vtkSocketCommunicator::BufferMessage
bool BufferMessage
Definition: vtkSocketCommunicator.h:295
vtkSocketCommunicator::CheckForErrorInternal
int CheckForErrorInternal(int id)
vtkX3D::length
@ length
Definition: vtkX3D.h:393
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
vtkSocketCommunicator::GatherVoidArray
int GatherVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int destProcessId) override
vtkSocketCommunicator::GetLogStream
virtual ostream * GetLogStream()
vtkSocketCommunicator::ReceiveVoidArray
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.
vtkSocketCommunicator::GatherVVoidArray
int GatherVVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType sendLength, vtkIdType *recvLengths, vtkIdType *offsets, int type, int destProcessId) override
vtkSocketCommunicator::SetSocket
void SetSocket(vtkClientSocket *)
vtkSocketCommunicator::ReceiveTagged
int ReceiveTagged(void *data, int wordSize, int numWords, int tag, const char *logName)
vtkX3D::port
@ port
Definition: vtkX3D.h:447
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
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:264
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:40
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:260
vtkSocketCommunicator::ScatterVoidArray
int ScatterVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int srcProcessId) override
vtkSocketCommunicator::SwapBytesInReceivedData
int SwapBytesInReceivedData
Definition: vtkSocketCommunicator.h:261
vtkSocketCommunicator::AllReduceVoidArray
int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer, vtkIdType length, int type, int operation) override
vtkX3D::name
@ name
Definition: vtkX3D.h:219
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
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:250
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
vtkSocketCommunicator::ReportErrors
int ReportErrors
Definition: vtkSocketCommunicator.h:266
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
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)
vtkSocketCommunicator::LogStream
ostream * LogStream
Definition: vtkSocketCommunicator.h:269