Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / examples / peerconnection / client / peer_connection_client.h
1 /*
2  * libjingle
3  * Copyright 2011, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_
29 #define PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_
30 #pragma once
31
32 #include <map>
33 #include <string>
34
35 #include "webrtc/base/nethelpers.h"
36 #include "webrtc/base/physicalsocketserver.h"
37 #include "webrtc/base/scoped_ptr.h"
38 #include "webrtc/base/signalthread.h"
39 #include "webrtc/base/sigslot.h"
40
41 typedef std::map<int, std::string> Peers;
42
43 struct PeerConnectionClientObserver {
44   virtual void OnSignedIn() = 0;  // Called when we're logged on.
45   virtual void OnDisconnected() = 0;
46   virtual void OnPeerConnected(int id, const std::string& name) = 0;
47   virtual void OnPeerDisconnected(int peer_id) = 0;
48   virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
49   virtual void OnMessageSent(int err) = 0;
50   virtual void OnServerConnectionFailure() = 0;
51
52  protected:
53   virtual ~PeerConnectionClientObserver() {}
54 };
55
56 class PeerConnectionClient : public sigslot::has_slots<>,
57                              public rtc::MessageHandler {
58  public:
59   enum State {
60     NOT_CONNECTED,
61     RESOLVING,
62     SIGNING_IN,
63     CONNECTED,
64     SIGNING_OUT_WAITING,
65     SIGNING_OUT,
66   };
67
68   PeerConnectionClient();
69   ~PeerConnectionClient();
70
71   int id() const;
72   bool is_connected() const;
73   const Peers& peers() const;
74
75   void RegisterObserver(PeerConnectionClientObserver* callback);
76
77   void Connect(const std::string& server, int port,
78                const std::string& client_name);
79
80   bool SendToPeer(int peer_id, const std::string& message);
81   bool SendHangUp(int peer_id);
82   bool IsSendingMessage();
83
84   bool SignOut();
85
86   // implements the MessageHandler interface
87   void OnMessage(rtc::Message* msg);
88
89  protected:
90   void DoConnect();
91   void Close();
92   void InitSocketSignals();
93   bool ConnectControlSocket();
94   void OnConnect(rtc::AsyncSocket* socket);
95   void OnHangingGetConnect(rtc::AsyncSocket* socket);
96   void OnMessageFromPeer(int peer_id, const std::string& message);
97
98   // Quick and dirty support for parsing HTTP header values.
99   bool GetHeaderValue(const std::string& data, size_t eoh,
100                       const char* header_pattern, size_t* value);
101
102   bool GetHeaderValue(const std::string& data, size_t eoh,
103                       const char* header_pattern, std::string* value);
104
105   // Returns true if the whole response has been read.
106   bool ReadIntoBuffer(rtc::AsyncSocket* socket, std::string* data,
107                       size_t* content_length);
108
109   void OnRead(rtc::AsyncSocket* socket);
110
111   void OnHangingGetRead(rtc::AsyncSocket* socket);
112
113   // Parses a single line entry in the form "<name>,<id>,<connected>"
114   bool ParseEntry(const std::string& entry, std::string* name, int* id,
115                   bool* connected);
116
117   int GetResponseStatus(const std::string& response);
118
119   bool ParseServerResponse(const std::string& response, size_t content_length,
120                            size_t* peer_id, size_t* eoh);
121
122   void OnClose(rtc::AsyncSocket* socket, int err);
123
124   void OnResolveResult(rtc::AsyncResolverInterface* resolver);
125
126   PeerConnectionClientObserver* callback_;
127   rtc::SocketAddress server_address_;
128   rtc::AsyncResolver* resolver_;
129   rtc::scoped_ptr<rtc::AsyncSocket> control_socket_;
130   rtc::scoped_ptr<rtc::AsyncSocket> hanging_get_;
131   std::string onconnect_data_;
132   std::string control_data_;
133   std::string notification_data_;
134   std::string client_name_;
135   Peers peers_;
136   State state_;
137   int my_id_;
138 };
139
140 #endif  // PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_