Play 'round
include/Network.h
00001 #ifndef __NETWORK_H__
00002 #define __NETWORK_H__
00003 
00004 #include <iostream>
00005 #include <map>
00006 #include <string>
00007 #include <vector>
00008 #include <math.h>
00009 #include <arpa/inet.h>
00010 
00011 #include "stk/Thread.h"
00012 #include "osc/OscPacketListener.h"
00013 #include "osc/OscOutboundPacketStream.h"
00014 #include "ip/UdpSocket.h"
00015 
00016 #include "Engine.h"
00017 #include "Widget.h"
00018 
00022 class Peer
00023 {
00024   public:
00025     Peer(const IpEndpointName&);
00026     Peer(const char*, const int);
00027     ~Peer();
00028 
00029     const IpEndpointName& getLocation();
00030 
00031     void setMousePosition(float, float);
00032     const Point2D& getMousePosition();
00033     void setMouseDown(bool);
00034     void setDisplayName(std::string displayName);
00035     const bool getMouseDown();
00036     void draw();
00037 
00038     void sendMessage(const osc::OutboundPacketStream&);
00039 
00040   private:
00041     IpEndpointName m_location;
00042     UdpTransmitSocket* m_socket;
00043 
00044     Point2D m_mousePosition;
00045     Text *m_mousePositionText;
00046     bool m_mouseDown;
00047     Spiral *m_circle;
00048 };
00049 
00053 class Network : public osc::OscPacketListener
00054 {
00055   public:
00056     typedef std::map<IpEndpointName, Peer*> PeerMap;
00057     typedef std::pair<IpEndpointName, Peer*> PeerData;
00058 
00059     typedef void(Network::*HandlerFunction)(const osc::ReceivedMessage&, const IpEndpointName&);
00060     typedef std::map<std::string, HandlerFunction> HandlerMap;
00061     typedef std::pair<std::string, HandlerFunction> HandlerData;
00062 
00063     Network();
00064     ~Network();
00065     void listen(int);
00066 
00067     void addPeer(Peer&);
00068     PeerMap* getPeers();
00069 
00070     int getPort();
00071 
00072     void setEngine(Engine*);
00073 
00074     void sendPeerUpMessage();
00075     void sendPeerDownMessage();
00076     void sendPeerMessage(const std::string);
00077     void sendPeerTextMessage(const std::string);
00078     void sendMousePosition(const float x, const float y, const bool down);
00079     void sendPlucker(const Track*);
00080 
00081     void sendObjectMessage(const Widget*, bool = false);
00082 
00083     void sendRoundPadTextMessage(const RoundPad*);
00084 
00085   protected:
00086     static void *listen(void*);
00087 
00088     virtual void ProcessMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00089 
00090     void broadcast(const osc::OutboundPacketStream&);
00091 
00092     void handlePeerUpMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00093     void handlePeerDownMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00094     void handlePeerTextMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00095     void handleObjectPadMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00096     void handleObjectPadTextMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00097     void handlePluckerMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00098     void handleObjectSpiralMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00099     void handleObjectLineMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00100     void handleObjectStringMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00101     void handleObjectDeleteMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00102     void handleObjectQueryMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00103     void handleMousePositionMessage(const osc::ReceivedMessage&, const IpEndpointName&);
00104 
00105     void rescueOrphans();
00106 
00107   private:
00108     PeerMap m_peers;
00109     HandlerMap m_handlers;
00110 
00111     stk::Thread m_thread;
00112     int m_port;
00113 
00114     WidgetMap m_orphans;
00115 
00116     Engine* m_engine;
00117 };
00118 
00119 #endif
 All Classes Functions Enumerations