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

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