root/OpenSceneGraph/trunk/applications/present3D/deprecated/Cluster.cpp @ 13720

Revision 13720, 12.7 kB (checked in by robert, 18 hours ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2 *
3 * This software is open source and may be redistributed and/or modified under 
4 * the terms of the GNU General Public License (GPL) version 2.0.
5 * The full license is in LICENSE.txt file included with this distribution,.
6 *
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * include LICENSE.txt for more details.
11*/
12
13#include "Cluster.h"
14
15#include <stdio.h>
16#include <fcntl.h>
17#include <sys/types.h>
18
19#if !defined (WIN32) || defined(__CYGWIN__)
20#include <sys/ioctl.h>
21#include <sys/uio.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <netdb.h>
25#include <arpa/inet.h>
26#include <sys/time.h>
27#include <net/if.h>
28#include <netdb.h>
29#endif
30
31#include <string.h>
32
33#if defined(__linux)
34    #include <unistd.h>
35    #include <linux/sockios.h>
36#elif defined(__FreeBSD__) || defined(__DragonFly__)
37    #include <unistd.h>
38    #include <sys/sockio.h>
39#elif defined(__sgi)
40    #include <unistd.h>
41    #include <net/soioctl.h>
42#elif defined(__CYGWIN__)
43    #include <unistd.h>
44#elif defined(__sun)
45    #include <unistd.h>
46    #include <sys/sockio.h>
47#elif defined (__APPLE__)
48    #include <unistd.h>
49    #include <sys/sockio.h>
50#elif defined (WIN32)
51    #include <winsock.h>
52    #include <stdio.h>
53#elif defined (__hpux)
54    #include <unistd.h>
55#else
56    #error Teach me how to build on this system
57#endif
58
59#include <stdio.h>
60#include <fcntl.h>
61#include <sys/types.h>
62#if defined (WIN32) && !defined(__CYGWIN__)
63#include <winsock.h>
64#else
65#include <unistd.h>
66#include <sys/uio.h>
67#include <sys/socket.h>
68#include <netinet/in.h>
69#include <netdb.h>
70#include <arpa/inet.h>
71#include <sys/time.h>
72#endif
73#include <string.h>
74
75#include <osg/io_utils>
76#include <iostream>
77
78const unsigned int CameraPacket::MAX_NUM_EVENTS = 10;
79const unsigned int CameraPacket::SWAP_BYTES_COMPARE = 0x12345678;
80
81void DataConverter::write(const osg::FrameStamp& fs)
82{
83    osg::notify(osg::NOTICE)<<"writeFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl;
84
85    writeUInt(fs.getFrameNumber());
86    writeDouble(fs.getReferenceTime());
87}
88
89void DataConverter::read(osg::FrameStamp& fs)
90{
91    fs.setFrameNumber(readUInt());
92    fs.setReferenceTime(readDouble());
93
94    osg::notify(osg::NOTICE)<<"readFramestamp = "<<fs.getFrameNumber()<<" "<<fs.getReferenceTime()<<std::endl;
95}
96
97void DataConverter::write(const osg::Matrix& matrix)
98{
99    writeDouble(matrix(0,0));
100    writeDouble(matrix(0,1));
101    writeDouble(matrix(0,2));
102    writeDouble(matrix(0,3));
103
104    writeDouble(matrix(1,0));
105    writeDouble(matrix(1,1));
106    writeDouble(matrix(1,2));
107    writeDouble(matrix(1,3));
108
109    writeDouble(matrix(2,0));
110    writeDouble(matrix(2,1));
111    writeDouble(matrix(2,2));
112    writeDouble(matrix(2,3));
113
114    writeDouble(matrix(3,0));
115    writeDouble(matrix(3,1));
116    writeDouble(matrix(3,2));
117    writeDouble(matrix(3,3));
118
119    osg::notify(osg::NOTICE)<<"writeMatrix = "<<matrix<<std::endl;
120
121}
122
123void DataConverter::read(osg::Matrix& matrix)
124{
125    matrix(0,0) = readDouble();
126    matrix(0,1) = readDouble();
127    matrix(0,2) = readDouble();
128    matrix(0,3) = readDouble();
129
130    matrix(1,0) = readDouble();
131    matrix(1,1) = readDouble();
132    matrix(1,2) = readDouble();
133    matrix(1,3) = readDouble();
134
135    matrix(2,0) = readDouble();
136    matrix(2,1) = readDouble();
137    matrix(2,2) = readDouble();
138    matrix(2,3) = readDouble();
139
140    matrix(3,0) = readDouble();
141    matrix(3,1) = readDouble();
142    matrix(3,2) = readDouble();
143    matrix(3,3) = readDouble();
144
145    osg::notify(osg::NOTICE)<<"readMatrix = "<<matrix<<std::endl;
146
147}
148
149void DataConverter::write(const osgGA::GUIEventAdapter& event)
150{
151    writeUInt(event.getEventType());
152    writeUInt(event.getKey());
153    writeUInt(event.getButton());
154    writeInt(event.getWindowX());
155    writeInt(event.getWindowY());
156    writeUInt(event.getWindowWidth());
157    writeUInt(event.getWindowHeight());
158    writeFloat(event.getXmin());
159    writeFloat(event.getYmin());
160    writeFloat(event.getXmax());
161    writeFloat(event.getYmax());
162    writeFloat(event.getX());
163    writeFloat(event.getY());
164    writeUInt(event.getButtonMask());
165    writeUInt(event.getModKeyMask());
166    writeDouble(event.getTime());
167}
168
169void DataConverter::read(osgGA::GUIEventAdapter& event)
170{
171    event.setEventType((osgGA::GUIEventAdapter::EventType)readUInt());
172    event.setKey(readUInt());
173    event.setButton(readUInt());
174    int x = readInt();
175    int y = readInt();
176    int width = readUInt();
177    int height = readUInt();
178    event.setWindowRectangle(x,y,width,height);
179    float xmin = readFloat();
180    float ymin = readFloat();
181    float xmax = readFloat();
182    float ymax = readFloat();
183    event.setInputRange(xmin,ymin,xmax,ymax);
184    event.setX(readFloat());
185    event.setY(readFloat());
186    event.setButtonMask(readUInt());
187    event.setModKeyMask(readUInt());
188    event.setTime(readDouble());
189}
190
191void DataConverter::write(CameraPacket& cameraPacket)
192{
193    writeUInt(cameraPacket._byte_order);
194
195    writeUInt(cameraPacket._masterKilled);
196
197    write(cameraPacket._matrix);
198    write(cameraPacket._frameStamp);
199
200    writeUInt(cameraPacket._events.size());
201    for(osgGA::EventQueue::Events::iterator itr = cameraPacket._events.begin();
202        itr != cameraPacket._events.end();
203        ++itr)
204    {
205        write(*(*itr));
206    }
207}
208
209void DataConverter::read(CameraPacket& cameraPacket)
210{
211    cameraPacket._byte_order = readUInt();
212    if (cameraPacket._byte_order != CameraPacket::SWAP_BYTES_COMPARE)
213    {
214        _swapBytes = !_swapBytes;
215    }
216
217    cameraPacket._masterKilled = readUInt()!=0;
218
219    read(cameraPacket._matrix);
220    read(cameraPacket._frameStamp);
221
222    cameraPacket._events.clear();
223    unsigned int numEvents = readUInt();
224    for(unsigned int i=0;i<numEvents;++i)
225    {
226        osgGA::GUIEventAdapter* event = new osgGA::GUIEventAdapter;
227        read(*(event));
228        cameraPacket._events.push_back(event);
229    }
230}
231
232void CameraPacket::readEventQueue(osgViewer::Viewer& viewer)
233{
234    _events.clear();
235
236    viewer.getEventQueue()->copyEvents(_events);
237
238    osg::notify(osg::INFO)<<"written events = "<<_events.size()<<std::endl;
239}
240
241void CameraPacket::writeEventQueue(osgViewer::Viewer& viewer)
242{
243    osg::notify(osg::INFO)<<"received events = "<<_events.size()<<std::endl;
244
245    // copy the events to osgProducer style events.
246    viewer.getEventQueue()->appendEvents(_events);
247}
248
249
250//////////////////////////////////////////////////////////////////////////////
251//
252//  Reciever
253//
254Receiver::Receiver( void )
255{
256    _port = 0;
257    _initialized = false;
258    _buffer = 0L;
259}
260
261Receiver::~Receiver( void )
262{
263#if defined (WIN32) && !defined(__CYGWIN__)
264    closesocket( _so);
265#else
266    close( _so );
267#endif
268}
269
270bool Receiver::init( void )
271{
272#if defined(WIN32) && !defined(__CYGWIN__)
273    WORD version = MAKEWORD(1,1);
274    WSADATA wsaData;
275    // First, we start up Winsock
276    WSAStartup(version, &wsaData);
277#endif
278
279    if( _port == 0 )
280    {
281    fprintf( stderr, "Receiver::init() - port not defined\n" );
282    return false;
283    }
284
285    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
286    {
287        perror( "Socket" );
288    return false;
289    }
290#if defined (WIN32) && !defined(__CYGWIN__)
291//    const BOOL on = TRUE;
292//    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, sizeof(int));
293#else
294    int on = 1;
295    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
296#endif
297
298//    struct sockaddr_in saddr;
299    saddr.sin_family = AF_INET;
300    saddr.sin_port   = htons( _port );
301#if defined (WIN32) && !defined(__CYGWIN__)
302    saddr.sin_addr.s_addr =  htonl(INADDR_ANY);
303#else
304    saddr.sin_addr.s_addr =  0;
305#endif
306
307    if( bind( _so, (struct sockaddr *)&saddr, sizeof( saddr )) < 0 )
308    {
309        perror( "bind" );
310        return false;
311    }
312
313    _initialized = true;
314    return _initialized;
315}
316
317
318void Receiver::setPort( const short port )
319{
320    _port = port;
321}
322
323void Receiver::setBuffer( void *buffer, const unsigned int size )
324{
325    _buffer = buffer;
326    _buffer_size = size;
327}
328
329void Receiver::sync( void )
330{
331    if(!_initialized) init();
332
333    if( _buffer == 0L )
334    {
335        fprintf( stderr, "Receiver::sync() - No buffer\n" );
336        return;
337    }
338
339#if defined(__linux) || defined(__FreeBSD__) || defined( __APPLE__ ) || \
340    defined(__DragonFly__)
341    socklen_t
342#else
343    int
344#endif
345        size = sizeof( struct sockaddr_in );
346
347    fd_set fdset;
348    FD_ZERO( &fdset );
349    FD_SET( _so, &fdset );
350
351    struct timeval tv;
352    tv.tv_sec = 0;
353    tv.tv_usec = 0;
354
355#if defined (WIN32) && !defined(__CYGWIN__)
356//    saddr.sin_port   = htons( _port );
357    recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
358//    recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen)
359    //int err = WSAGetLastError ();
360    //int *dum = (int*) _buffer;
361
362    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
363    {
364        if( FD_ISSET( _so, &fdset ) )
365        {
366            recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
367        }
368    }
369#else
370    recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
371    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
372    {
373        if( FD_ISSET( _so, &fdset ) )
374        {
375            recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
376        }
377    }
378#endif
379}
380
381
382//////////////////////////////////////////////////////////////////////////////
383//
384//  Broadcaster
385//
386Broadcaster::Broadcaster( void )
387{
388    _port = 0;
389    _initialized = false;
390    _buffer = 0L;
391    _address = 0;
392}
393
394Broadcaster::~Broadcaster( void )
395{
396#if defined (WIN32) && !defined(__CYGWIN__)
397    closesocket( _so);
398#else
399    close( _so );
400#endif
401}
402
403bool Broadcaster::init( void )
404{
405#if defined (WIN32) && !defined(__CYGWIN__)
406    WORD version = MAKEWORD(1,1);
407    WSADATA wsaData;
408    // First, we start up Winsock
409    WSAStartup(version, &wsaData);
410#endif
411
412    if( _port == 0 )
413    {
414        fprintf( stderr, "Broadcaster::init() - port not defined\n" );
415        return false;
416    }
417
418    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
419    {
420        perror( "Socket" );
421        return false;
422    }
423#if defined (WIN32) && !defined(__CYGWIN__)
424    const BOOL on = TRUE;
425#else
426    int on = 1;
427#endif
428
429#if defined (WIN32) && !defined(__CYGWIN__)
430    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
431#else
432    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
433#endif
434
435    saddr.sin_family = AF_INET;
436    saddr.sin_port   = htons( _port );
437    if( _address == 0 )
438    {
439#if defined (WIN32) && !defined(__CYGWIN__)
440        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
441#else
442        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
443#endif
444
445#if !defined (WIN32) || defined(__CYGWIN__)
446        struct ifreq ifr;
447#endif
448#if defined (__linux) || defined(__CYGWIN__)
449        strcpy( ifr.ifr_name, "eth0" );
450#elif defined(__sun)
451        strcpy( ifr.ifr_name, "hme0" );
452#elif !defined (WIN32)
453        strcpy( ifr.ifr_name, "ef0" );
454#endif
455#if defined (WIN32) // get the server address
456        saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
457    }
458#else
459        if( (ioctl( _so, SIOCGIFBRDADDR, &ifr)) < 0 )
460        {
461            perror( "Broadcaster::init() Cannot get Broadcast Address" );
462            return false;
463        }
464            saddr.sin_addr.s_addr = (((sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr);
465        }
466        else
467        {
468            saddr.sin_addr.s_addr = _address;
469        }
470#endif
471#define _VERBOSE 1
472#ifdef _VERBOSE
473    unsigned char *ptr = (unsigned char *)&saddr.sin_addr.s_addr;
474    printf( "Broadcast address : %u.%u.%u.%u\n", ptr[0], ptr[1], ptr[2], ptr[3] );
475#endif
476
477    _initialized = true;
478    return _initialized;
479}
480
481void Broadcaster::setHost( const char *hostname )
482{
483    struct hostent *h;
484    if( (h = gethostbyname( hostname )) == 0L )
485    {
486        fprintf( stderr, "Broadcaster::setHost() - Cannot resolv an address for \"%s\".\n", hostname );
487        _address = 0;
488    }
489    else
490        _address = *(( unsigned long  *)h->h_addr);
491}
492
493void Broadcaster::setPort( const short port )
494{
495    _port = port;
496}
497
498void Broadcaster::setBuffer( void *buffer, const unsigned int size )
499{
500    _buffer = buffer;
501    _buffer_size = size;
502}
503
504void Broadcaster::sync( void )
505{
506    if(!_initialized) init();
507
508    if( _buffer == 0L )
509    {
510        fprintf( stderr, "Broadcaster::sync() - No buffer\n" );
511        return;
512    }
513
514#if defined (WIN32) && !defined(__CYGWIN__)
515    unsigned int size = sizeof( SOCKADDR_IN );
516    sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
517    // int err = WSAGetLastError ();
518    // int *dum = (int*) _buffer;
519#else
520    unsigned int size = sizeof( struct sockaddr_in );
521    sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
522#endif
523
524}
Note: See TracBrowser for help on using the browser.