1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 * (at your option) any later version. The full license is in LICENSE file 6 * included with this distribution, and on the openscenegraph.org website. 7 * 8 * This library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * OpenSceneGraph Public License for more details. 12 */ 13 14 #ifndef CLUSTER_H 15 #define CLUSTER_H 16 17 #include <osg/Matrix> 18 #include <osg/FrameStamp> 19 20 #include <osgViewer/Viewer> 21 22 #if !defined(WIN32) || defined(__CYGWIN__) 23 #include <netinet/in.h> 24 #else 25 #include "winsock.h" 26 #endif 27 28 //////////////////////////////////////////////////////////// 29 // Receiver.h 30 // 31 // Class definition for the recipient of a broadcasted message 32 // 33 class Receiver 34 { 35 public : 36 37 Receiver(); 38 ~Receiver(); 39 40 // setBuffer defines the buffer into which the broadcasted 41 // message will be received. 42 void setBuffer( void *buffer, const unsigned int size ); 43 44 // Define what port to listen and bind to 45 void setPort( const short port ); 46 47 // Sync does a blocking wait to receive next message 48 void sync( void ); 49 50 private : 51 bool init( void ); 52 53 private : 54 #if defined (WIN32) && !defined(__CYGWIN__) 55 SOCKET _so; 56 SOCKADDR_IN saddr; 57 #else 58 int _so; 59 struct sockaddr_in saddr; 60 #endif 61 bool _initialized; 62 short _port; 63 void *_buffer; 64 unsigned int _buffer_size; 65 }; 66 67 //////////////////////////////////////////////////////////// 68 // Broadcaster.h 69 // 70 // Class definition for broadcasting a buffer to a LAN 71 // 72 class Broadcaster 73 { 74 public : 75 76 Broadcaster( void ); 77 ~Broadcaster( void ); 78 79 // Set the broadcast port 80 void setPort( const short port ); 81 82 // Set the buffer to be broadcast 83 void setBuffer( void *buffer, const unsigned int buffer_size ); 84 85 // Set a recipient host. If this is used, the Broadcaster 86 // no longer broadcasts, but rather directs UDP packets at 87 // host. 88 void setHost( const char *hostname ); 89 90 // Sync broadcasts the buffer 91 void sync( void ); 92 93 private : 94 bool init( void ); 95 96 private : 97 #if defined(WIN32) && !defined(__CYGWIN__) 98 SOCKET _so; 99 #else 100 int _so; 101 #endif 102 bool _initialized; 103 short _port; 104 void *_buffer; 105 unsigned int _buffer_size; 106 #if defined(WIN32) && !defined(__CYGWIN__) 107 SOCKADDR_IN saddr; 108 #else 109 struct sockaddr_in saddr; 110 #endif 111 unsigned long _address; 112 }; 113 114 class CameraPacket { 115 public: 116 117 static const unsigned int MAX_NUM_EVENTS; 118 static const unsigned int SWAP_BYTES_COMPARE; 119 CameraPacket()120 CameraPacket():_masterKilled(false) 121 { 122 _byte_order = SWAP_BYTES_COMPARE; 123 } 124 setPacket(const osg::Matrix & matrix,const osg::FrameStamp * frameStamp)125 void setPacket(const osg::Matrix& matrix,const osg::FrameStamp* frameStamp) 126 { 127 _matrix = matrix; 128 if (frameStamp) 129 { 130 _frameStamp = *frameStamp; 131 } 132 } 133 134 void getModelView(osg::Matrix& matrix,float angle_offset=0.0f) 135 { 136 137 matrix = _matrix * osg::Matrix::rotate(osg::DegreesToRadians(angle_offset),0.0f,1.0f,0.0f); 138 } 139 140 void readEventQueue(osgViewer::Viewer& viewer); 141 142 void writeEventQueue(osgViewer::Viewer& viewer); 143 setMasterKilled(const bool flag)144 void setMasterKilled(const bool flag) { _masterKilled = flag; } getMasterKilled()145 bool getMasterKilled() const { return _masterKilled; } 146 147 unsigned int _byte_order; 148 bool _masterKilled; 149 osg::Matrix _matrix; 150 151 // note don't use a ref_ptr as used elsewhere for FrameStamp 152 // since we don't want to copy the pointer - but the memory. 153 // FrameStamp doesn't have a private destructor to allow 154 // us to do this, even though its a reference counted object. 155 osg::FrameStamp _frameStamp; 156 157 osgGA::EventQueue::Events _events; 158 159 }; 160 161 class DataConverter 162 { 163 public: 164 DataConverter(unsigned int numBytes)165 DataConverter(unsigned int numBytes): 166 _startPtr(0), 167 _endPtr(0), 168 _swapBytes(false), 169 _currentPtr(0) 170 { 171 _currentPtr = _startPtr = new char[numBytes]; 172 _endPtr = _startPtr+numBytes; 173 _numBytes = numBytes; 174 } 175 ~DataConverter()176 ~DataConverter() 177 { 178 delete [] _startPtr; 179 } 180 reset()181 void reset() 182 { 183 _currentPtr = _startPtr; 184 } 185 write1(char * ptr)186 inline void write1(char* ptr) 187 { 188 if (_currentPtr+1>=_endPtr) return; 189 190 *(_currentPtr++) = *(ptr); 191 } 192 read1(char * ptr)193 inline void read1(char* ptr) 194 { 195 if (_currentPtr+1>=_endPtr) return; 196 197 *(ptr) = *(_currentPtr++); 198 } 199 write2(char * ptr)200 inline void write2(char* ptr) 201 { 202 if (_currentPtr+2>=_endPtr) return; 203 204 *(_currentPtr++) = *(ptr++); 205 *(_currentPtr++) = *(ptr); 206 } 207 read2(char * ptr)208 inline void read2(char* ptr) 209 { 210 if (_currentPtr+2>=_endPtr) return; 211 212 if (_swapBytes) 213 { 214 *(ptr+1) = *(_currentPtr++); 215 *(ptr) = *(_currentPtr++); 216 } 217 else 218 { 219 *(ptr++) = *(_currentPtr++); 220 *(ptr) = *(_currentPtr++); 221 } 222 } 223 write4(char * ptr)224 inline void write4(char* ptr) 225 { 226 if (_currentPtr+4>=_endPtr) return; 227 228 *(_currentPtr++) = *(ptr++); 229 *(_currentPtr++) = *(ptr++); 230 *(_currentPtr++) = *(ptr++); 231 *(_currentPtr++) = *(ptr); 232 } 233 read4(char * ptr)234 inline void read4(char* ptr) 235 { 236 if (_currentPtr+4>=_endPtr) return; 237 238 if (_swapBytes) 239 { 240 *(ptr+3) = *(_currentPtr++); 241 *(ptr+2) = *(_currentPtr++); 242 *(ptr+1) = *(_currentPtr++); 243 *(ptr) = *(_currentPtr++); 244 } 245 else 246 { 247 *(ptr++) = *(_currentPtr++); 248 *(ptr++) = *(_currentPtr++); 249 *(ptr++) = *(_currentPtr++); 250 *(ptr) = *(_currentPtr++); 251 } 252 } 253 write8(char * ptr)254 inline void write8(char* ptr) 255 { 256 if (_currentPtr+8>=_endPtr) return; 257 258 *(_currentPtr++) = *(ptr++); 259 *(_currentPtr++) = *(ptr++); 260 *(_currentPtr++) = *(ptr++); 261 *(_currentPtr++) = *(ptr++); 262 263 *(_currentPtr++) = *(ptr++); 264 *(_currentPtr++) = *(ptr++); 265 *(_currentPtr++) = *(ptr++); 266 *(_currentPtr++) = *(ptr); 267 } 268 read8(char * ptr)269 inline void read8(char* ptr) 270 { 271 char* endPtr = _currentPtr+8; 272 if (endPtr>=_endPtr) return; 273 274 if (_swapBytes) 275 { 276 *(ptr+7) = *(_currentPtr++); 277 *(ptr+6) = *(_currentPtr++); 278 *(ptr+5) = *(_currentPtr++); 279 *(ptr+4) = *(_currentPtr++); 280 281 *(ptr+3) = *(_currentPtr++); 282 *(ptr+2) = *(_currentPtr++); 283 *(ptr+1) = *(_currentPtr++); 284 *(ptr) = *(_currentPtr++); 285 } 286 else 287 { 288 *(ptr++) = *(_currentPtr++); 289 *(ptr++) = *(_currentPtr++); 290 *(ptr++) = *(_currentPtr++); 291 *(ptr++) = *(_currentPtr++); 292 293 *(ptr++) = *(_currentPtr++); 294 *(ptr++) = *(_currentPtr++); 295 *(ptr++) = *(_currentPtr++); 296 *(ptr) = *(_currentPtr++); 297 } 298 } 299 writeChar(char c)300 inline void writeChar(char c) { write1(&c); } writeUChar(unsigned char c)301 inline void writeUChar(unsigned char c) { write1((char*)&c); } writeShort(short c)302 inline void writeShort(short c) { write2((char*)&c); } writeUShort(unsigned short c)303 inline void writeUShort(unsigned short c) { write2((char*)&c); } writeInt(int c)304 inline void writeInt(int c) { write4((char*)&c); } writeUInt(unsigned int c)305 inline void writeUInt(unsigned int c) { write4((char*)&c); } writeFloat(float c)306 inline void writeFloat(float c) { write4((char*)&c); } writeDouble(double c)307 inline void writeDouble(double c) { write8((char*)&c); } 308 readChar()309 inline char readChar() { char c=0; read1(&c); return c; } readUChar()310 inline unsigned char readUChar() { unsigned char c=0; read1((char*)&c); return c; } readShort()311 inline short readShort() { short c=0; read2((char*)&c); return c; } readUShort()312 inline unsigned short readUShort() { unsigned short c=0; read2((char*)&c); return c; } readInt()313 inline int readInt() { int c=0; read4((char*)&c); return c; } readUInt()314 inline unsigned int readUInt() { unsigned int c=0; read4((char*)&c); return c; } readFloat()315 inline float readFloat() { float c=0.0f; read4((char*)&c); return c; } readDouble()316 inline double readDouble() { double c=0.0; read8((char*)&c); return c; } 317 318 void write(const osg::FrameStamp& fs); 319 void read(osg::FrameStamp& fs); 320 321 void write(const osg::Matrix& matrix); 322 void read(osg::Matrix& matrix); 323 324 void write(const osgGA::GUIEventAdapter& event); 325 void read(osgGA::GUIEventAdapter& event); 326 327 void write(CameraPacket& cameraPacket); 328 void read(CameraPacket& cameraPacket); 329 startPtr()330 char* startPtr() { return _startPtr; } numBytes()331 unsigned int numBytes() { return _numBytes; } 332 333 protected: 334 335 char* _startPtr; 336 char* _endPtr; 337 unsigned int _numBytes; 338 bool _swapBytes; 339 340 char* _currentPtr; 341 }; 342 343 344 345 #endif 346