VTK
|
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 00200 //BTX 00201 protected: 00202 00203 vtkClientSocket* Socket; 00204 int SwapBytesInReceivedData; 00205 int RemoteHas64BitIds; 00206 int PerformHandshake; 00207 int IsServer; 00208 00209 int ReportErrors; 00210 00211 ofstream* LogFile; 00212 ostream* LogStream; 00213 00214 vtkSocketCommunicator(); 00215 ~vtkSocketCommunicator(); 00216 00217 // Wrappers around send/recv calls to implement loops. Return 1 for 00218 // success, and 0 for failure. 00219 int SendTagged(const void* data, int wordSize, int numWords, int tag, 00220 const char* logName); 00221 int ReceiveTagged(void* data, int wordSize, int numWords, int tag, 00222 const char* logName); 00223 int ReceivePartialTagged(void* data, int wordSize, int numWords, int tag, 00224 const char* logName); 00225 00226 // Internal utility methods. 00227 void LogTagged(const char* name, const void* data, int wordSize, int numWords, 00228 int tag, const char* logName); 00229 int CheckForErrorInternal(int id); 00230 00231 private: 00232 vtkSocketCommunicator(const vtkSocketCommunicator&); // Not implemented. 00233 void operator=(const vtkSocketCommunicator&); // Not implemented. 00234 00235 int SelectSocket(int socket, unsigned long msec); 00236 00237 // SwapBytesInReceiveData needs an invalid / not set. 00238 // This avoids checking length of endian handshake. 00239 enum ErrorIds { 00240 SwapOff = 0, 00241 SwapOn, 00242 SwapNotSet 00243 }; 00244 00245 // One may be tempted to change this to a vtkIdType, but really an int is 00246 // enough since we split messages > VTK_INT_MAX. 00247 int TagMessageLength; 00248 00250 00252 static int GetVersion(); 00253 //ETX 00254 }; 00256 00257 #endif