VTK
dox/Parallel/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 "vtkCommunicator.h"
00043 
00044 #include "vtkByteSwap.h" // Needed for vtkSwap macros
00045 
00046 #ifdef VTK_WORDS_BIGENDIAN
00047 # define vtkSwap4 vtkByteSwap::Swap4LE
00048 # define vtkSwap4Range vtkByteSwap::Swap4LERange
00049 # define vtkSwap8 vtkByteSwap::Swap8LE
00050 # define vtkSwap8Range vtkByteSwap::Swap8LERange
00051 #else
00052 # define vtkSwap4 vtkByteSwap::Swap4BE
00053 # define vtkSwap4Range vtkByteSwap::Swap4BERange
00054 # define vtkSwap8 vtkByteSwap::Swap8BE
00055 # define vtkSwap8Range vtkByteSwap::Swap8BERange
00056 #endif
00057 
00058 class vtkClientSocket;
00059 class vtkServerSocket;
00060 
00061 class VTK_PARALLEL_EXPORT vtkSocketCommunicator : public vtkCommunicator
00062 {
00063 public:
00064   static vtkSocketCommunicator *New();
00065   vtkTypeMacro(vtkSocketCommunicator,vtkCommunicator);
00066   void PrintSelf(ostream& os, vtkIndent indent);
00067 
00069 
00071   virtual int WaitForConnection(int port);
00072   virtual int WaitForConnection(vtkServerSocket* socket,
00073     unsigned  long msec = 0);
00075 
00077   virtual void CloseConnection();
00078 
00080   virtual int ConnectTo(const char* hostName, int port);
00081 
00083 
00084   vtkGetMacro(SwapBytesInReceivedData, int);
00086 
00088   int GetIsConnected();
00089 
00091   virtual void SetNumberOfProcesses(int num);
00092 
00093   //------------------ Communication --------------------
00094 
00096 
00098   virtual int SendVoidArray(const void *data, vtkIdType length, int type,
00099                             int remoteHandle, int tag);
00100   virtual int ReceiveVoidArray(void *data, vtkIdType length, int type,
00101                                int remoteHandle, int tag);
00103 
00106   virtual void Barrier();
00107 
00109 
00112   virtual int BroadcastVoidArray(void *data, vtkIdType length, int type,
00113                                  int srcProcessId);
00114   virtual int GatherVoidArray(const void *sendBuffer, void *recvBuffer,
00115                               vtkIdType length, int type, int destProcessId);
00116   virtual int GatherVVoidArray(const void *sendBuffer, void *recvBuffer,
00117                                vtkIdType sendLength, vtkIdType *recvLengths,
00118                                vtkIdType *offsets, int type, int destProcessId);
00119   virtual int ScatterVoidArray(const void *sendBuffer, void *recvBuffer,
00120                                vtkIdType length, int type, int srcProcessId);
00121   virtual int ScatterVVoidArray(const void *sendBuffer, void *recvBuffer,
00122                                 vtkIdType *sendLengths, vtkIdType *offsets,
00123                                 vtkIdType recvLength, int type,
00124                                 int srcProcessId);
00125   virtual int AllGatherVoidArray(const void *sendBuffer, void *recvBuffer,
00126                                  vtkIdType length, int type);
00127   virtual int AllGatherVVoidArray(const void *sendBuffer, void *recvBuffer,
00128                                   vtkIdType sendLength, vtkIdType *recvLengths,
00129                                   vtkIdType *offsets, int type);
00130   virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00131                               vtkIdType length, int type,
00132                               int operation, int destProcessId);
00133   virtual int ReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00134                               vtkIdType length, int type,
00135                               Operation *operation, int destProcessId);
00136   virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00137                                  vtkIdType length, int type,
00138                                  int operation);
00139   virtual int AllReduceVoidArray(const void *sendBuffer, void *recvBuffer,
00140                                  vtkIdType length, int type,
00141                                  Operation *operation);
00143 
00145 
00147   vtkSetClampMacro(PerformHandshake, int, 0, 1);
00148   vtkBooleanMacro(PerformHandshake, int);
00149   vtkGetMacro(PerformHandshake, int);
00151 
00152   //BTX
00154 
00156   virtual void SetLogStream(ostream* stream);
00157   virtual ostream* GetLogStream();
00158   //ETX
00160   
00162 
00166   virtual int LogToFile(const char* name);
00167   virtual int LogToFile(const char* name, int append);
00169 
00171 
00172   vtkSetMacro(ReportErrors, int);
00173   vtkGetMacro(ReportErrors, int);
00175 
00177 
00178   vtkGetObjectMacro(Socket, vtkClientSocket);
00179   void SetSocket(vtkClientSocket*);
00181 
00184   int Handshake();
00185 
00188   int ServerSideHandshake();
00189 
00192   int ClientSideHandshake();
00193 
00195 
00197   vtkGetMacro(IsServer, int);
00199 
00202   static int GetVersion();
00203 
00205 
00211   void BufferCurrentMessage()
00212     { this->BufferMessage = true; }
00214 
00216   bool HasBufferredMessages();
00217 
00218 //BTX
00219 protected:
00220 
00221   vtkClientSocket* Socket;
00222   int SwapBytesInReceivedData;
00223   int RemoteHas64BitIds;
00224   int PerformHandshake;
00225   int IsServer;
00226   
00227   int ReportErrors;
00228 
00229   ofstream* LogFile;
00230   ostream* LogStream;
00231   
00232   vtkSocketCommunicator();
00233   ~vtkSocketCommunicator();
00234   
00235   // Wrappers around send/recv calls to implement loops.  Return 1 for
00236   // success, and 0 for failure.
00237   int SendTagged(const void* data, int wordSize, int numWords, int tag,
00238                  const char* logName);
00239   int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
00240                     const char* logName);
00241   int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
00242                     const char* logName);
00243 
00244   int ReceivedTaggedFromBuffer(
00245     void* data, int wordSize, int numWords, int tag, const char* logName);
00246   
00248   void FixByteOrder(void* data, int wordSize, int numWords);
00249 
00250   // Internal utility methods.
00251   void LogTagged(const char* name, const void* data, int wordSize, int numWords,
00252                  int tag, const char* logName);
00253   int CheckForErrorInternal(int id);
00254   bool BufferMessage;
00255 private:
00256   vtkSocketCommunicator(const vtkSocketCommunicator&);  // Not implemented.
00257   void operator=(const vtkSocketCommunicator&);  // Not implemented.
00258 
00259   int SelectSocket(int socket, unsigned long msec);
00260 
00261   // SwapBytesInReceiveData needs an invalid / not set.
00262   // This avoids checking length of endian handshake.
00263   enum ErrorIds {
00264     SwapOff = 0,
00265     SwapOn,
00266     SwapNotSet
00267   };
00268 
00269   // One may be tempted to change this to a vtkIdType, but really an int is
00270   // enough since we split messages > VTK_INT_MAX.
00271   int TagMessageLength;
00272 
00273   //  Buffer to save messages received with different tag than requested.
00274   class vtkMessageBuffer;
00275   vtkMessageBuffer* ReceivedMessageBuffer;
00276   
00277 //ETX
00278 };
00279 
00280 #endif