Main Page | Directories | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

vtkSocketCommunicator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkSocketCommunicator.h,v $
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 =========================================================================*/
00053 #ifndef __vtkSocketCommunicator_h
00054 #define __vtkSocketCommunicator_h
00055 
00056 #include "vtkCommunicator.h"
00057 
00058 #include "vtkByteSwap.h" // Needed for vtkSwap macros
00059 
00060 #ifdef VTK_WORDS_BIGENDIAN
00061 # define vtkSwap4 vtkByteSwap::Swap4LE
00062 # define vtkSwap4Range vtkByteSwap::Swap4LERange
00063 # define vtkSwap8 vtkByteSwap::Swap8LE
00064 # define vtkSwap8Range vtkByteSwap::Swap8LERange
00065 #else
00066 # define vtkSwap4 vtkByteSwap::Swap4BE
00067 # define vtkSwap4Range vtkByteSwap::Swap4BERange
00068 # define vtkSwap8 vtkByteSwap::Swap8BE
00069 # define vtkSwap8Range vtkByteSwap::Swap8BERange
00070 #endif
00071 
00072 class VTK_PARALLEL_EXPORT vtkSocketCommunicator : public vtkCommunicator
00073 {
00074 public:
00075   static vtkSocketCommunicator *New();
00076   vtkTypeRevisionMacro(vtkSocketCommunicator,vtkCommunicator);
00077   void PrintSelf(ostream& os, vtkIndent indent);
00078 
00082   virtual int OpenSocket(int port, const char* network = 0);
00083   
00085   virtual int GetPort(int sock);
00086   
00089   virtual int WaitForConnectionOnSocket(int socket);
00090   
00092   virtual int WaitForConnection(int port);
00093 
00095   virtual void CloseConnection();
00096 
00098   virtual int ConnectTo( char* hostName, int port);
00099 
00101 
00102   vtkGetMacro(SwapBytesInReceivedData, int);
00104 
00106 
00107   vtkGetMacro(IsConnected, int);
00109 
00110   //------------------ Communication --------------------
00111   
00113 
00115   int Send(int *data, int length, int remoteProcessId, int tag);
00116   int Send(unsigned long *data, int length, int remoteProcessId, int tag);
00117   int Send(char *data, int length, int remoteProcessId, int tag);
00118   int Send(unsigned char *data, int length, int remoteProcessId, int tag);
00119   int Send(float *data, int length, int remoteProcessId, int tag);
00120   int Send(double *data, int length, int remoteProcessId, int tag);
00121 #ifdef VTK_USE_64BIT_IDS
00122   int Send(vtkIdType *data, int length, int remoteProcessId, int tag);
00124 #endif
00125   int Send(vtkDataObject *data, int remoteId, int tag)
00126     {return this->vtkCommunicator::Send(data,remoteId,tag);}
00127   int Send(vtkDataArray *data, int remoteId, int tag)
00128     {return this->vtkCommunicator::Send(data,remoteId,tag);}
00129 
00131 
00134   int Receive(int *data, int length, int remoteProcessId, int tag);
00135   int Receive(unsigned long *data, int length, int remoteProcessId, int tag);
00136   int Receive(char *data, int length, int remoteProcessId, int tag);
00137   int Receive(unsigned char *data, int length, int remoteProcessId, int tag);
00138   int Receive(float *data, int length, int remoteProcessId, int tag);
00139   int Receive(double *data, int length, int remoteProcessId, int tag);
00140 #ifdef VTK_USE_64BIT_IDS
00141   int Receive(vtkIdType *data, int length, int remoteProcessId, int tag);
00143 #endif
00144   int Receive(vtkDataObject *data, int remoteId, int tag)
00145     {return this->vtkCommunicator::Receive(data, remoteId, tag);}
00146   int Receive(vtkDataArray *data, int remoteId, int tag)
00147     {return this->vtkCommunicator::Receive(data, remoteId, tag);}
00148 
00150 
00152   vtkSetClampMacro(PerformHandshake, int, 0, 1);
00153   vtkBooleanMacro(PerformHandshake, int);
00154   vtkGetMacro(PerformHandshake, int);
00156 
00157   //BTX
00159 
00161   virtual void SetLogStream(ostream* stream);
00162   virtual ostream* GetLogStream();
00163   //ETX
00165   
00167 
00171   virtual int LogToFile(const char* name);
00172   virtual int LogToFile(const char* name, int append);
00174   
00175 protected:
00176 
00177   int Socket;
00178   int IsConnected;
00179   int NumberOfProcesses;
00180   int SwapBytesInReceivedData;
00181   int PerformHandshake;
00182   
00183   ofstream* LogFile;
00184   ostream* LogStream;
00185   
00186   vtkSocketCommunicator();
00187   ~vtkSocketCommunicator();
00188   
00189   // Wrappers around send/recv calls to implement loops.  Return 1 for
00190   // success, and 0 for failure.
00191   int SendInternal(int socket, void* data, int length);
00192   int ReceiveInternal(int socket, void* data, int length);
00193   int SendTagged(void* data, int wordSize, int numWords, int tag,
00194                  const char* logName);
00195   int ReceiveTagged(void* data, int wordSize, int numWords, int tag,
00196                     const char* logName);
00197   int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag,
00198                     const char* logName);
00199   
00200   // Internal utility methods.
00201   void LogTagged(const char* name, void* data, int wordSize, int numWords,
00202                  int tag, const char* logName);
00203   int CheckForErrorInternal(int id);
00204 private:
00205   vtkSocketCommunicator(const vtkSocketCommunicator&);  // Not implemented.
00206   void operator=(const vtkSocketCommunicator&);  // Not implemented.
00207 
00208 //BTX
00209   // SwapBytesInReceiveData needs an invalid / not set.
00210   // This avoids checking length of endian handshake.
00211   enum ErrorIds {
00212     SwapOff = 0,
00213     SwapOn,
00214     SwapNotSet
00215   };
00216 //ETX
00217 };
00218 
00219 #endif