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

Revision 13319, 4.5 kB (checked in by robert, 4 hours ago)

Added simple test script for osgUI's TabWidget?

  • 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 checkEvents() {}
75    virtual void run();
76   
77   
78    virtual void ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& remoteEndpoint );
79    virtual void ProcessPacket( const char *data, int size, const IpEndpointName& remoteEndpoint );
80    virtual void ProcessBundle( const osc::ReceivedBundle& b, const IpEndpointName& remoteEndpoint );
81    void addRequestHandler(RequestHandler* handler);
82   
83    void describeTo(std::ostream& out) const;
84   
85    friend std::ostream& operator<<(std::ostream& out, const OscReceivingDevice& device)
86    {
87        device.describeTo(out);
88        return out;
89    }
90   
91    osgGA::GUIEventAdapter* getOrCreateUserDataEvent()
92    {
93        if (!_userDataEvent.valid())
94        {
95            _userDataEvent = new osgGA::GUIEventAdapter();
96            _userDataEvent->setEventType(osgGA::GUIEventAdapter::USER);
97        }
98        return _userDataEvent.get();
99    }
100   
101    virtual const char* className() const { return "OSC receiving device"; }
102   
103private:
104    std::string _listeningAddress;
105    unsigned int _listeningPort;
106    UdpListeningReceiveSocket* _socket;
107    RequestHandlerMap _map;
108    osg::ref_ptr<osgGA::GUIEventAdapter> _userDataEvent;
109    MsgIdType _lastMsgId;
110    osg::Timer_t _lastMsgTimeStamp;
111
112};
113
114
115class SendKeystrokeRequestHandler : public OscReceivingDevice::RequestHandler {
116public:
117    SendKeystrokeRequestHandler(const std::string& request_path, int key) : OscReceivingDevice::RequestHandler(request_path), _key(key) {}
118    virtual bool operator()(const std::string& request_path, const std::string& full_request_path, const osc::ReceivedMessage& arguments)
119    {
120        getDevice()->getEventQueue()->keyPress(_key);
121        getDevice()->getEventQueue()->keyRelease(_key);
122       
123        return true;
124    }
125   
126    virtual void describeTo(std::ostream& out) const
127    {
128        out << getRequestPath() << ": send KEY_DOWN + KEY_UP, code: 0x" << std::hex << _key << std::dec;
129    }
130private:
131    int _key;
132};
Note: See TracBrowser for help on using the browser.