~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/examples/peerconnection/client/peer_connection_client.h

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "talk/base/sigslot.h"
 
36
#include "talk/base/physicalsocketserver.h"
 
37
#include "talk/base/scoped_ptr.h"
 
38
 
 
39
typedef std::map<int, std::string> Peers;
 
40
 
 
41
struct PeerConnectionClientObserver {
 
42
  virtual void OnSignedIn() = 0;  // Called when we're logged on.
 
43
  virtual void OnDisconnected() = 0;
 
44
  virtual void OnPeerConnected(int id, const std::string& name) = 0;
 
45
  virtual void OnPeerDisconnected(int peer_id) = 0;
 
46
  virtual void OnMessageFromPeer(int peer_id, const std::string& message) = 0;
 
47
  virtual void OnMessageSent(int err) = 0;
 
48
 
 
49
 protected:
 
50
  virtual ~PeerConnectionClientObserver() {}
 
51
};
 
52
 
 
53
class PeerConnectionClient : public sigslot::has_slots<> {
 
54
 public:
 
55
  enum State {
 
56
    NOT_CONNECTED,
 
57
    SIGNING_IN,
 
58
    CONNECTED,
 
59
    SIGNING_OUT_WAITING,
 
60
    SIGNING_OUT,
 
61
  };
 
62
 
 
63
  PeerConnectionClient();
 
64
  ~PeerConnectionClient();
 
65
 
 
66
  int id() const;
 
67
  bool is_connected() const;
 
68
  const Peers& peers() const;
 
69
 
 
70
  void RegisterObserver(PeerConnectionClientObserver* callback);
 
71
 
 
72
  bool Connect(const std::string& server, int port,
 
73
               const std::string& client_name);
 
74
 
 
75
  bool SendToPeer(int peer_id, const std::string& message);
 
76
  bool SendHangUp(int peer_id);
 
77
  bool IsSendingMessage();
 
78
 
 
79
  bool SignOut();
 
80
 
 
81
 protected:
 
82
  void Close();
 
83
  bool ConnectControlSocket();
 
84
  void OnConnect(talk_base::AsyncSocket* socket);
 
85
  void OnHangingGetConnect(talk_base::AsyncSocket* socket);
 
86
  void OnMessageFromPeer(int peer_id, const std::string& message);
 
87
 
 
88
  // Quick and dirty support for parsing HTTP header values.
 
89
  bool GetHeaderValue(const std::string& data, size_t eoh,
 
90
                      const char* header_pattern, size_t* value);
 
91
 
 
92
  bool GetHeaderValue(const std::string& data, size_t eoh,
 
93
                      const char* header_pattern, std::string* value);
 
94
 
 
95
  // Returns true if the whole response has been read.
 
96
  bool ReadIntoBuffer(talk_base::AsyncSocket* socket, std::string* data,
 
97
                      size_t* content_length);
 
98
 
 
99
  void OnRead(talk_base::AsyncSocket* socket);
 
100
 
 
101
  void OnHangingGetRead(talk_base::AsyncSocket* socket);
 
102
 
 
103
  // Parses a single line entry in the form "<name>,<id>,<connected>"
 
104
  bool ParseEntry(const std::string& entry, std::string* name, int* id,
 
105
                  bool* connected);
 
106
 
 
107
  int GetResponseStatus(const std::string& response);
 
108
 
 
109
  bool ParseServerResponse(const std::string& response, size_t content_length,
 
110
                           size_t* peer_id, size_t* eoh);
 
111
 
 
112
  void OnClose(talk_base::AsyncSocket* socket, int err);
 
113
 
 
114
  PeerConnectionClientObserver* callback_;
 
115
  talk_base::SocketAddress server_address_;
 
116
  talk_base::scoped_ptr<talk_base::AsyncSocket> control_socket_;
 
117
  talk_base::scoped_ptr<talk_base::AsyncSocket> hanging_get_;
 
118
  std::string onconnect_data_;
 
119
  std::string control_data_;
 
120
  std::string notification_data_;
 
121
  Peers peers_;
 
122
  State state_;
 
123
  int my_id_;
 
124
};
 
125
 
 
126
#endif  // PEERCONNECTION_SAMPLES_CLIENT_PEER_CONNECTION_CLIENT_H_