VTK
dox/Parallel/Core/vtkSocketCommunicator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkSocketCommunicator.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00039 #ifndef __vtkSocketCommunicator_h
00040 #define __vtkSocketCommunicator_h
00041 
00042 #include "vtkParallelCoreModule.h" // For export macro
00043 #include "vtkCommunicator.h"
00044 
00045 #include "vtkByteSwap.h" // Needed for vtkSwap macros
00046 
00047 #ifdef VTK_WORDS_BIGENDIAN
00048 # define vtkSwap4 vtkByteSwap::Swap4LE
00049 # define vtkSwap4Range vtkByteSwap::Swap4LERange
00050 # define vtkSwap8 vtkByteSwap::Swap8LE
00051 # define vtkSwap8Range vtkByteSwap::Swap8LERange
00052 #else
00053 # define vtkSwap4 vtkByteSwap::Swap4BE
00054 # define vtkSwap4Range vtkByteSwap::Swap4BERange
00055 # define vtkSwap8 vtkByteSwap::Swap8BE
00056 # define vtkSwap8Range vtkByteSwap::Swap8BERange
00057 #endif
00058 
00059 class vtkClientSocket;
00060 class vtkServerSocket;
00061 
00062 class VTKPARALLELCORE_EXPORT vtkSocketCommunicator : public vtkCommunicator
00063 {
00064 public:
00065   static vtkSocketCommunicator *New();
00066   vtkTypeMacro(vtkSocketCommunicator,vtkCommunicator);
00067   void PrintSelf(ostream& os, vtkIndent indent);
00068 
00070 
00072   virtual int WaitForConnection(int port);
00073   virtual int WaitForConnection(vtkServerSocket* socket,
00074     unsigned  long msec = 0);
00076 
00078   virtual void CloseConnection();
00079 
00081   virtual int ConnectTo(const char* hostName, int port);
00082 
00084 
00085   vtkGetMacro(SwapBytesInReceivedData, int);
00087 
00089   int GetIsConnected();
00090 
00092   virtual void SetNumberOfProcesses(int num);
00093 
00094   //------------------ Communication --------------------
00095 
00097 
00099   virtual int SendVoidArray(const void *data, vtkIdType length, int type,
00100                             int remoteHandle, int tag);
00101   virtual int ReceiveVoidArray(void *data, vtkIdType length, int type,
00102                                int remoteHandle, int tag);
00104 
00107   virtual void Barrier();
00108 
00110 
00113   virtual int BroadcastVoidArray(void *data, vtkIdType length, int type,
00114                                  int srcProcessId);
00115   virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer,
00116                               vtkIdType length, int type, int destProcessId);
00117   virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer,
00118                                vtkIdType sendLength, vtkIdType *recvLengths,
00119                                vtkIdType *offsets, int type, int destProcessId);
00120   virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer,
00121                                vtkIdType length, int type, int srcProcessId);
00122   virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer,
00123                                 vtkIdType *sendLengths, vtkIdType *offsets,
00124                                 vtkIdType recvLength, int type,
00125                                 int srcProcessId);
00126   virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer,
00127                                  vtkIdType length, int type);
00128   virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer,
00129                                   vtkIdType sendLength, vtkIdType *recvLengths,
00130                                   vtkIdType *offsets, int type);
00131   virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00132                               vtkIdType length, int type,
00133                               int operation, int destProcessId);
00134   virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00135                               vtkIdType length, int type,
00136                               Operation *operation, int destProcessId);
00137   virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00138                                  vtkIdType length, int type,
00139                                  int operation);
00140   virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00141                                  vtkIdType length, int type,
00142                                  Operation *operation);
00144 
00146 
00148   vtkSetClampMacro(PerformHandshake, int, 0, 1);
00149   vtkBooleanMacro(PerformHandshake, int);
00150   vtkGetMacro(PerformHandshake, int);
00152 
00153   //BTX
00155 
00157   virtual void SetLogStream(ostream* stream);
00158   virtual ostream* GetLogStream();
00159   //ETX
00161 
00163 
00167   virtual int LogToFile(const char* name);
00168   virtual int LogToFile(const char* name, int append);
00170 
00172 
00173   vtkSetMacro(ReportErrors, int);
00174   vtkGetMacro(ReportErrors, int);
00176 
00178 
00179   vtkGetObjectMacro(Socket, vtkClientSocket);
00180   void SetSocket(vtkClientSocket*);
00182 
00185   int Handshake();
00186 
00189   int ServerSideHandshake();
00190 
00193   int ClientSideHandshake();
00194 
00196 
00198   vtkGetMacro(IsServer, int);
00200 
00203   static int GetVersion();
00204 
00206 
00212   void BufferCurrentMessage()
00213     { this->BufferMessage = true; }
00215 
00217   bool HasBufferredMessages();
00218 
00219 //BTX
00220 protected:
00221 
00222   vtkClientSocket* Socket;
00223   int SwapBytesInReceivedData;
00224   int RemoteHas64BitIds;
00225   int PerformHandshake;
00226   int IsServer;
00227 
00228   int ReportErrors;
00229 
00230   ofstream* LogFile;
00231   ostream* LogStream;
00232 
00233   vtkSocketCommunicator();
00234   ~vtkSocketCommunicator();
00235 
00236   // Wrappers around send/recv calls to implement loops.  Return 1 for
00237   // success, and 0 for failure.
00238   int SendTagged(const void* data, int wordSize, int numWords, int tag,
00239                  const char* logName);
00240   int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
00241                     const char* logName);
00242   int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
00243                     const char* logName);
00244 
00245   int ReceivedTaggedFromBuffer(
00246     void* data, int wordSize, int numWords, int tag, const char* logName);
00247 
00249   void FixByteOrder(void* data, int wordSize, int numWords);
00250 
00251   // Internal utility methods.
00252   void LogTagged(const char* name, const void* data, int wordSize, int numWords,
00253                  int tag, const char* logName);
00254   int CheckForErrorInternal(int id);
00255   bool BufferMessage;
00256 private:
00257   vtkSocketCommunicator(const vtkSocketCommunicator&);  // Not implemented.
00258   void operator=(const vtkSocketCommunicator&);  // Not implemented.
00259 
00260   int SelectSocket(int socket, unsigned long msec);
00261 
00262   // SwapBytesInReceiveData needs an invalid / not set.
00263   // This avoids checking length of endian handshake.
00264   enum ErrorIds {
00265     SwapOff = 0,
00266     SwapOn,
00267     SwapNotSet
00268   };
00269 
00270   // One may be tempted to change this to a vtkIdType, but really an int is
00271   // enough since we split messages > VTK_INT_MAX.
00272   int TagMessageLength;
00273 
00274   //  Buffer to save messages received with different tag than requested.
00275   class vtkMessageBuffer;
00276   vtkMessageBuffer* ReceivedMessageBuffer;
00277 
00278 //ETX
00279 };
00280 
00281 #endif