root/OpenSceneGraph/trunk/src/osgPlugins/ZeroConfDevice/ReaderWriterZeroConfDevice.cpp @ 13429

Revision 13429, 5.9 kB (checked in by robert, 42 hours ago)

Release OpenSceneGraph-3.3.3

  • Property svn:executable set to *
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 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
15
16#include <osg/ValueObject>
17#include <osg/UserDataContainer>
18#include <osgDB/Registry>
19#include <osgDB/FileNameUtils>
20#include <osgDB/FileUtils>
21#include <osgGA/Device>
22#include "AutoDiscovery.h"
23
24
25class ZeroConfRegisterDevice: public osgGA::Device{
26public:
27    ZeroConfRegisterDevice()
28        : osgGA::Device()
29        , _autoDiscovery(new AutoDiscovery())
30    {
31        setCapabilities(RECEIVE_EVENTS);
32    }
33   
34    void advertise(const std::string& type, unsigned int port)
35    {
36        OSG_NOTICE << "ZeroConfDevice :: advertise: " << type << ":" << port << std::endl;
37        _autoDiscovery->registerService(type, port);
38    }
39   
40    virtual bool checkEvents()
41    {
42        _autoDiscovery->update();
43        return !(getEventQueue()->empty());
44    }
45   
46    virtual void sendEvent(const osgGA::GUIEventAdapter& event)
47    {
48        if (event.getName() == "/zeroconf/advertise")
49        {
50            std::string type;
51            unsigned int port = 0;
52            event.getUserValue("type",type);
53            event.getUserValue("port", port);
54            if (type.empty() || (port == 0))
55            {
56                OSG_WARN << "ZeroConfRegisterDevice :: could not advertise service, missing type/port " << std::endl;
57            }
58            else
59            {
60                advertise(type, port);
61            }
62        }
63    }
64       
65private:
66    osg::ref_ptr<AutoDiscovery> _autoDiscovery;
67
68};
69
70
71
72class ZeroConfDiscoverDevice : public osgGA::Device {
73public:
74    ZeroConfDiscoverDevice(const std::string& type);
75   
76    virtual bool checkEvents()
77    {
78        _autoDiscovery->update();
79        return !(getEventQueue()->empty());
80    }
81   
82private:
83    osg::ref_ptr<AutoDiscovery> _autoDiscovery;
84};
85
86
87class MyDiscoveredServicesCallback : public DiscoveredServicesCallback {
88public:
89    MyDiscoveredServicesCallback(ZeroConfDiscoverDevice* device, const std::string& type)
90    :   DiscoveredServicesCallback()
91    ,   _device(device)
92    ,   _type(type)
93    {
94    }
95   
96    virtual bool ignoreIP6Addresses() { return true; }
97    virtual void serviceAdded(const std::string& host, unsigned int port)
98    {
99        osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter();
100       
101        OSG_NOTICE << "ZeroConfDevice :: serviceAdded: " << host << ":" << port << " event " << event << std::endl;
102       
103        event->setEventType(osgGA::GUIEventAdapter::USER);
104       
105        event->setName("/zeroconf/service-added");
106        event->setUserValue("host", host);
107        event->setUserValue("port", port);
108        event->setUserValue("type", _type);
109        event->setTime(_device->getEventQueue()->getTime());
110        _device->getEventQueue()->addEvent(event);
111    }
112   
113    virtual void serviceRemoved(const std::string& host, unsigned int port)
114    {
115        osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter();
116
117        OSG_NOTICE << "ZeroConfDevice :: serviceRemoved: " << host << ":" << port << " event " << event << std::endl;
118       
119        event->setEventType(osgGA::GUIEventAdapter::USER);
120       
121        event->setName("/zeroconf/service-removed");
122        event->setUserValue("host", host);
123        event->setUserValue("port", port);
124        event->setUserValue("type", _type);
125        event->setTime(_device->getEventQueue()->getTime());
126        _device->getEventQueue()->addEvent(event);
127    }
128private:
129    osg::observer_ptr<ZeroConfDiscoverDevice> _device;
130    std::string _type;
131};
132
133
134
135ZeroConfDiscoverDevice::ZeroConfDiscoverDevice(const std::string& type)
136    : osgGA::Device()
137    , _autoDiscovery(new AutoDiscovery())
138{
139    setCapabilities(RECEIVE_EVENTS);
140    _autoDiscovery->discoverServices(type, new MyDiscoveredServicesCallback(this, type));
141}
142
143
144
145class ReaderWriterZeroConf : public osgDB::ReaderWriter
146{
147    public:
148
149        ReaderWriterZeroConf()
150        {
151            supportsExtension("zeroconf", "zeroconf plugin to advertise ip-services and discover them");
152        }
153
154        virtual const char* className() const { return "ZeroConf Virtual Device Integration plugin"; }
155
156        virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
157        {
158            if (osgDB::getFileExtension(file) == "zeroconf")
159            {
160                std::string file_name = osgDB::getNameLessExtension(file);
161               
162                if (osgDB::getFileExtension(file_name) == "discover")
163                {
164                    std::string type = osgDB::getNameLessExtension(file_name);
165                    return new ZeroConfDiscoverDevice(type);
166                }
167                else if (osgDB::getFileExtension(file_name) == "advertise")
168                {
169                    file_name = osgDB::getNameLessExtension(file_name);
170                   
171                    std::string type = file_name.substr(0,file_name.find(':'));
172                    std::string port = file_name.substr(file_name.find(':') + 1);
173                   
174                    ZeroConfRegisterDevice* device = new ZeroConfRegisterDevice();
175                    device->advertise(type, atoi(port.c_str()));
176                   
177                    return device;
178                }
179            }
180           
181            return ReadResult::FILE_NOT_HANDLED;
182        }
183private:       
184       
185};
186
187// now register with Registry to instantiate the above
188// reader/writer.
189REGISTER_OSGPLUGIN(zeroconf, ReaderWriterZeroConf)
Note: See TracBrowser for help on using the browser.