henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ |
| 12 | #define WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | #pragma once |
| 14 | |
| 15 | #include <time.h> |
| 16 | |
| 17 | #include <queue> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | class DataSocket; |
| 22 | |
| 23 | // Represents a single peer connected to the server. |
| 24 | class ChannelMember { |
| 25 | public: |
| 26 | explicit ChannelMember(DataSocket* socket); |
| 27 | ~ChannelMember(); |
| 28 | |
| 29 | bool connected() const { return connected_; } |
| 30 | int id() const { return id_; } |
| 31 | void set_disconnected() { connected_ = false; } |
| 32 | bool is_wait_request(DataSocket* ds) const; |
| 33 | const std::string& name() const { return name_; } |
| 34 | |
| 35 | bool TimedOut(); |
| 36 | |
| 37 | std::string GetPeerIdHeader() const; |
| 38 | |
| 39 | bool NotifyOfOtherMember(const ChannelMember& other); |
| 40 | |
| 41 | // Returns a string in the form "name,id\n". |
| 42 | std::string GetEntry() const; |
| 43 | |
| 44 | void ForwardRequestToPeer(DataSocket* ds, ChannelMember* peer); |
| 45 | |
| 46 | void OnClosing(DataSocket* ds); |
| 47 | |
| 48 | void QueueResponse(const std::string& status, const std::string& content_type, |
| 49 | const std::string& extra_headers, const std::string& data); |
| 50 | |
| 51 | void SetWaitingSocket(DataSocket* ds); |
| 52 | |
| 53 | protected: |
| 54 | struct QueuedResponse { |
| 55 | std::string status, content_type, extra_headers, data; |
| 56 | }; |
| 57 | |
| 58 | DataSocket* waiting_socket_; |
| 59 | int id_; |
| 60 | bool connected_; |
| 61 | time_t timestamp_; |
| 62 | std::string name_; |
| 63 | std::queue<QueuedResponse> queue_; |
| 64 | static int s_member_id_; |
| 65 | }; |
| 66 | |
| 67 | // Manages all currently connected peers. |
| 68 | class PeerChannel { |
| 69 | public: |
| 70 | typedef std::vector<ChannelMember*> Members; |
| 71 | |
| 72 | PeerChannel() { |
| 73 | } |
| 74 | |
| 75 | ~PeerChannel() { |
| 76 | DeleteAll(); |
| 77 | } |
| 78 | |
| 79 | const Members& members() const { return members_; } |
| 80 | |
| 81 | // Returns true if the request should be treated as a new ChannelMember |
| 82 | // request. Otherwise the request is not peerconnection related. |
| 83 | static bool IsPeerConnection(const DataSocket* ds); |
| 84 | |
| 85 | // Finds a connected peer that's associated with the |ds| socket. |
| 86 | ChannelMember* Lookup(DataSocket* ds) const; |
| 87 | |
| 88 | // Checks if the request has a "peer_id" parameter and if so, looks up the |
| 89 | // peer for which the request is targeted at. |
| 90 | ChannelMember* IsTargetedRequest(const DataSocket* ds) const; |
| 91 | |
| 92 | // Adds a new ChannelMember instance to the list of connected peers and |
| 93 | // associates it with the socket. |
| 94 | bool AddMember(DataSocket* ds); |
| 95 | |
| 96 | // Closes all connections and sends a "shutting down" message to all |
| 97 | // connected peers. |
| 98 | void CloseAll(); |
| 99 | |
| 100 | // Called when a socket was determined to be closing by the peer (or if the |
| 101 | // connection went dead). |
| 102 | void OnClosing(DataSocket* ds); |
| 103 | |
| 104 | void CheckForTimeout(); |
| 105 | |
| 106 | protected: |
| 107 | void DeleteAll(); |
| 108 | void BroadcastChangedState(const ChannelMember& member, |
| 109 | Members* delivery_failures); |
| 110 | void HandleDeliveryFailures(Members* failures); |
| 111 | |
| 112 | // Builds a simple list of "name,id\n" entries for each member. |
| 113 | std::string BuildResponseForNewMember(const ChannelMember& member, |
| 114 | std::string* content_type); |
| 115 | |
| 116 | protected: |
| 117 | Members members_; |
| 118 | }; |
| 119 | |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 120 | #endif // WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ |