1 /*
2  * This file is part of Wireless Display Software for Linux OS
3  *
4  * Copyright (C) 2014 Intel Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  */
21 
22 
23 #ifndef MIRAC_BROKER_HPP
24 #define MIRAC_BROKER_HPP
25 
26 #include <glib.h>
27 #include <memory>
28 #include <map>
29 #include <vector>
30 
31 #include "libwds/public/peer.h"
32 #include "mirac-network.hpp"
33 
34 class MiracBroker : public wds::Peer::Delegate
35 {
36     public:
37         MiracBroker (const std::string& listen_port);
38         MiracBroker(const std::string& peer_address, const std::string& peer_port, uint timeout = 3000);
39         virtual ~MiracBroker ();
40         unsigned short get_host_port() const;
41         std::string get_peer_address() const;
42         virtual wds::Peer* Peer() const = 0;
43         void OnTimeout(uint timer_id);
44 
45     protected:
46         enum ConnectionFailure {
47             CONNECTION_TIMEOUT,
48             CONNECTION_LOST,
49         };
50 
51         // wds::Peer::Delegate
52         void SendRTSPData(const std::string& data) override;
53         std::string GetLocalIPAddress() const override;
54         uint CreateTimer(int seconds) override;
55         void ReleaseTimer(uint timer_id) override;
56         int GetNextCSeq(int* initial_peer_cseq = nullptr) const override;
57 
got_message(const std::string & data)58         virtual void got_message(const std::string& data) {}
on_connected()59         virtual void on_connected() {};
on_connection_failure(ConnectionFailure failure)60         virtual void on_connection_failure(ConnectionFailure failure) {};
61 
62     private:
63         static gboolean send_cb (gint fd, GIOCondition condition, gpointer data_ptr);
64         static gboolean receive_cb (gint fd, GIOCondition condition, gpointer data_ptr);
65         static gboolean listen_cb (gint fd, GIOCondition condition, gpointer data_ptr);
66         static gboolean connect_cb (gint fd, GIOCondition condition, gpointer data_ptr);
67         static gboolean try_connect(gpointer data_ptr);
68         static void on_timeout_remove(gpointer user_data);
69 
70         gboolean send_cb (gint fd, GIOCondition condition);
71         gboolean receive_cb (gint fd, GIOCondition condition);
72         gboolean listen_cb (gint fd, GIOCondition condition);
73         gboolean connect_cb (gint fd, GIOCondition condition);
74         void try_connect();
75 
76         void handle_body(const std::string msg);
77         void handle_header(const std::string msg);
78 
79         void network(MiracNetwork* connection);
80         std::unique_ptr<MiracNetwork> network_;
81         MiracBroker *network_source_ptr_;
82 
83         void connection(MiracNetwork* connection);
84         std::unique_ptr<MiracNetwork> connection_;
85         MiracBroker *connection_source_ptr_;
86 
87         std::vector<uint> timers_;
88 
89         std::string peer_address_;
90         std::string peer_port_;
91 
92         GTimer *connect_timer_;
93         uint connect_wait_id_;
94         uint connect_timeout_;
95         static const uint connect_wait_ = 200;
96 };
97 
98 
99 #endif  /* MIRAC_BROKER_HPP */
100 
101