root/OpenSceneGraph/trunk/src/osgPlugins/osc/OscReceivingDevice.hpp @ 13429

Revision 13429, 4.5 kB (checked in by robert, 31 hours ago)

Added shaders to support experimental shader based displacement mapping technique osgTerrain::ShaderTerrain?.

  • Property svn:executable set to *
Line 
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
15#pragma once
16
17#include <osg/Referenced>
18#include <OpenThreads/Thread>
19#include <osgGA/Device>
20#include <osc/OscPacketListener.h>
21#include <ip/UdpSocket.h>
22#include "OscSendingDevice.hpp"
23
24
25
26class OscReceivingDevice : public osgGA::Device, OpenThreads::Thread, osc::OscPacketListener {
27
28public:
29    typedef OscSendingDevice::MsgIdType MsgIdType;
30   
31    class RequestHandler : public osg::Referenced {
32    public:
33        RequestHandler(const std::string& request_path)
34            : osg::Referenced()
35            , _requestPath(request_path)
36            , _device(NULL)
37        {
38        }
39       
40        virtual bool operator()(const std::string& request_path, const std::string& full_request_path, const osc::ReceivedMessage& m) = 0;
41       
42        const std::string& getRequestPath() const { return _requestPath; }
43       
44        virtual void describeTo(std::ostream& out) const
45        {
46            out << getRequestPath() << ": no description available";
47        }
48       
49    protected:
50        void setDevice(OscReceivingDevice* device) { _device = device; }
51        OscReceivingDevice* getDevice() const { return _device; }
52           
53        /// set the request-path, works only from the constructor
54        void setRequestPath(const std::string& request_path) { _requestPath = request_path; }
55   
56        void handleException(const osc::Exception& e)
57        {
58            OSG_WARN << "OscDevice :: error while handling " << getRequestPath() << ": " << e.what() << std::endl;
59        }
60       
61        double getLocalTime() const { return getDevice()->getEventQueue()->getTime(); }
62    private:
63        std::string _requestPath;
64        OscReceivingDevice* _device;
65    friend class OscReceivingDevice;
66    };
67   
68    typedef std::multimap<std::string, osg::ref_ptr<RequestHandler> > RequestHandlerMap;
69   
70    OscReceivingDevice(const std::string& server_address, int listening_port);
71    ~OscReceivingDevice();
72   
73       
74    virtual void run();
75   
76   
77    virtual void ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint );
78    virtual void ProcessPacket( const char *data, int size, const IpEndpointName& remoteEndpoint );
79    virtual void ProcessBundle( const osc::ReceivedBundle& b, const IpEndpointName& remoteEndpoint );
80    void addRequestHandler(RequestHandler* handler);
81   
82    void describeTo(std::ostream& out) const;
83   
84    friend std::ostream& operator<<(std::ostream& out, const OscReceivingDevice& device)
85    {
86        device.describeTo(out);
87        return out;
88    }
89   
90    osgGA::GUIEventAdapter* getOrCreateUserDataEvent()
91    {
92        if (!_userDataEvent.valid())
93        {
94            _userDataEvent = new osgGA::GUIEventAdapter();
95            _userDataEvent->setEventType(osgGA::GUIEventAdapter::USER);
96        }
97        return _userDataEvent.get();
98    }
99   
100    virtual const char* className() const { return "OSC receiving device"; }
101   
102private:
103    std::string _listeningAddress;
104    unsigned int _listeningPort;
105    UdpListeningReceiveSocket* _socket;
106    RequestHandlerMap _map;
107    osg::ref_ptr<osgGA::GUIEventAdapter> _userDataEvent;
108    MsgIdType _lastMsgId;
109    osg::Timer_t _lastMsgTimeStamp;
110
111};
112
113
114class SendKeystrokeRequestHandler : public OscReceivingDevice::RequestHandler {
115public:
116    SendKeystrokeRequestHandler(const std::string& request_path, int key) : OscReceivingDevice::RequestHandler(request_path), _key(key) {}
117    virtual bool operator()(const std::string& request_path, const std::string& full_request_path, const osc::ReceivedMessage& arguments)
118    {
119        getDevice()->getEventQueue()->keyPress(_key);
120        getDevice()->getEventQueue()->keyRelease(_key);
121       
122        return true;
123    }
124   
125    virtual void describeTo(std::ostream& out) const
126    {
127        out << getRequestPath() << ": send KEY_DOWN + KEY_UP, code: 0x" << std::hex << _key << std::dec;
128    }
129private:
130    int _key;
131};
Note: See TracBrowser for help on using the browser.