root/OpenSceneGraph/trunk/applications/present3D/Cluster.cpp @ 12292

Revision 12292, 12.6 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • 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__)
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)<<"recieved 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    socklen_t
341#else
342    int
343#endif
344        size = sizeof( struct sockaddr_in );
345
346    fd_set fdset;
347    FD_ZERO( &fdset );
348    FD_SET( _so, &fdset );
349
350    struct timeval tv;
351    tv.tv_sec = 0;
352    tv.tv_usec = 0;
353
354#if defined (WIN32) && !defined(__CYGWIN__)
355//    saddr.sin_port   = htons( _port );
356    recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
357//    recvfrom(sock_Receive, szMessage, 256, 0, (sockaddr*)&addr_Cli, &clilen)
358    //int err = WSAGetLastError ();
359    //int *dum = (int*) _buffer;
360
361    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
362    {
363        if( FD_ISSET( _so, &fdset ) )
364        {
365            recvfrom( _so, (char *)_buffer, _buffer_size, 0, (sockaddr*)&saddr, &size );
366        }
367    }
368#else
369    recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
370    while( select( _so+1, &fdset, 0L, 0L, &tv ) )
371    {
372        if( FD_ISSET( _so, &fdset ) )
373        {
374            recvfrom( _so, (caddr_t)_buffer, _buffer_size, 0, 0, &size );
375        }
376    }
377#endif
378}
379
380
381//////////////////////////////////////////////////////////////////////////////
382//
383//  Broadcaster
384//
385Broadcaster::Broadcaster( void )
386{
387    _port = 0;
388    _initialized = false;
389    _buffer = 0L;
390    _address = 0;
391}
392
393Broadcaster::~Broadcaster( void )
394{
395#if defined (WIN32) && !defined(__CYGWIN__)
396    closesocket( _so);
397#else
398    close( _so );
399#endif
400}
401
402bool Broadcaster::init( void )
403{
404#if defined (WIN32) && !defined(__CYGWIN__)
405    WORD version = MAKEWORD(1,1);
406    WSADATA wsaData;
407    // First, we start up Winsock
408    WSAStartup(version, &wsaData);
409#endif
410
411    if( _port == 0 )
412    {
413        fprintf( stderr, "Broadcaster::init() - port not defined\n" );
414        return false;
415    }
416
417    if( (_so = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
418    {
419        perror( "Socket" );
420        return false;
421    }
422#if defined (WIN32) && !defined(__CYGWIN__)
423    const BOOL on = TRUE;
424#else
425    int on = 1;
426#endif
427
428#if defined (WIN32) && !defined(__CYGWIN__)
429    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(int));
430#else
431    setsockopt( _so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
432#endif
433
434    saddr.sin_family = AF_INET;
435    saddr.sin_port   = htons( _port );
436    if( _address == 0 )
437    {
438#if defined (WIN32) && !defined(__CYGWIN__)
439        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, (const char *) &on, sizeof(int));
440#else
441        setsockopt( _so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
442#endif
443
444#if !defined (WIN32) || defined(__CYGWIN__)
445        struct ifreq ifr;
446#endif
447#if defined (__linux) || defined(__CYGWIN__)
448        strcpy( ifr.ifr_name, "eth0" );
449#elif defined(__sun)
450        strcpy( ifr.ifr_name, "hme0" );
451#elif !defined (WIN32)
452        strcpy( ifr.ifr_name, "ef0" );
453#endif
454#if defined (WIN32) // get the server address
455        saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
456    }
457#else
458        if( (ioctl( _so, SIOCGIFBRDADDR, &ifr)) < 0 )
459        {
460            perror( "Broadcaster::init() Cannot get Broadcast Address" );
461            return false;
462        }
463            saddr.sin_addr.s_addr = (((sockaddr_in *)&ifr.ifr_broadaddr)->sin_addr.s_addr);
464        }
465        else
466        {
467            saddr.sin_addr.s_addr = _address;
468        }
469#endif
470#define _VERBOSE 1
471#ifdef _VERBOSE
472    unsigned char *ptr = (unsigned char *)&saddr.sin_addr.s_addr;
473    printf( "Broadcast address : %u.%u.%u.%u\n", ptr[0], ptr[1], ptr[2], ptr[3] );
474#endif
475
476    _initialized = true;
477    return _initialized;
478}
479
480void Broadcaster::setHost( const char *hostname )
481{
482    struct hostent *h;
483    if( (h = gethostbyname( hostname )) == 0L )
484    {
485        fprintf( stderr, "Broadcaster::setHost() - Cannot resolv an address for \"%s\".\n", hostname );
486        _address = 0;
487    }
488    else
489        _address = *(( unsigned long  *)h->h_addr);
490}
491
492void Broadcaster::setPort( const short port )
493{
494    _port = port;
495}
496
497void Broadcaster::setBuffer( void *buffer, const unsigned int size )
498{
499    _buffer = buffer;
500    _buffer_size = size;
501}
502
503void Broadcaster::sync( void )
504{
505    if(!_initialized) init();
506
507    if( _buffer == 0L )
508    {
509        fprintf( stderr, "Broadcaster::sync() - No buffer\n" );
510        return;
511    }
512
513#if defined (WIN32) && !defined(__CYGWIN__)
514    unsigned int size = sizeof( SOCKADDR_IN );
515    sendto( _so, (const char *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
516    // int err = WSAGetLastError ();
517    // int *dum = (int*) _buffer;
518#else
519    unsigned int size = sizeof( struct sockaddr_in );
520    sendto( _so, (const void *)_buffer, _buffer_size, 0, (struct sockaddr *)&saddr, size );
521#endif
522
523}
Note: See TracBrowser for help on using the browser.