128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org/* 2a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis * Copyright 2011 The WebRTC Project Authors. All rights reserved. 328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * 4a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis * Use of this source code is governed by a BSD-style license 5a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis * that can be found in the LICENSE file in the root of the source 6a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis * tree. An additional intellectual property rights grant can be found 7a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis * in the file PATENTS. All contributing project authors may 8a8736448970fedd82f051c6b2cc89185b755ddf3Donald E Curtis * be found in the AUTHORS file in the root of the source tree. 928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org */ 1028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 1170625e5bf3c91c57bf704d380bcc9df86575f08ajbauch#ifndef WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ 1270625e5bf3c91c57bf704d380bcc9df86575f08ajbauch#define WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ 1328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#pragma once 1428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 1528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <time.h> 1628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 1728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <queue> 1828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <string> 1928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <vector> 2028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 2128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgclass DataSocket; 2228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 2328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org// Represents a single peer connected to the server. 2428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgclass ChannelMember { 2528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org public: 2628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org explicit ChannelMember(DataSocket* socket); 2728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org ~ChannelMember(); 2828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 2928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org bool connected() const { return connected_; } 3028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org int id() const { return id_; } 3128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void set_disconnected() { connected_ = false; } 3228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org bool is_wait_request(DataSocket* ds) const; 3328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org const std::string& name() const { return name_; } 3428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 3528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org bool TimedOut(); 3628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 3728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::string GetPeerIdHeader() const; 3828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 3928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org bool NotifyOfOtherMember(const ChannelMember& other); 4028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 4128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Returns a string in the form "name,id\n". 4228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::string GetEntry() const; 4328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 4428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void ForwardRequestToPeer(DataSocket* ds, ChannelMember* peer); 4528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 4628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void OnClosing(DataSocket* ds); 4728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 4828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void QueueResponse(const std::string& status, const std::string& content_type, 4928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org const std::string& extra_headers, const std::string& data); 5028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 5128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void SetWaitingSocket(DataSocket* ds); 5228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 5328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org protected: 5428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org struct QueuedResponse { 5528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::string status, content_type, extra_headers, data; 5628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org }; 5728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 5828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org DataSocket* waiting_socket_; 5928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org int id_; 6028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org bool connected_; 6128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org time_t timestamp_; 6228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::string name_; 6328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::queue<QueuedResponse> queue_; 6428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org static int s_member_id_; 6528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}; 6628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 6728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org// Manages all currently connected peers. 6828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgclass PeerChannel { 6928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org public: 7028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org typedef std::vector<ChannelMember*> Members; 7128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 7228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org PeerChannel() { 7328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org } 7428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 7528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org ~PeerChannel() { 7628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org DeleteAll(); 7728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org } 7828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 7928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org const Members& members() const { return members_; } 8028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 8128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Returns true if the request should be treated as a new ChannelMember 8228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // request. Otherwise the request is not peerconnection related. 8328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org static bool IsPeerConnection(const DataSocket* ds); 8428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 8528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Finds a connected peer that's associated with the |ds| socket. 8628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org ChannelMember* Lookup(DataSocket* ds) const; 8728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 8828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Checks if the request has a "peer_id" parameter and if so, looks up the 8928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // peer for which the request is targeted at. 9028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org ChannelMember* IsTargetedRequest(const DataSocket* ds) const; 9128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 9228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Adds a new ChannelMember instance to the list of connected peers and 9328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // associates it with the socket. 9428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org bool AddMember(DataSocket* ds); 9528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 9628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Closes all connections and sends a "shutting down" message to all 9728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // connected peers. 9828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void CloseAll(); 9928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 10028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Called when a socket was determined to be closing by the peer (or if the 10128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // connection went dead). 10228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void OnClosing(DataSocket* ds); 10328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 10428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void CheckForTimeout(); 10528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 10628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org protected: 10728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void DeleteAll(); 10828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void BroadcastChangedState(const ChannelMember& member, 10928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org Members* delivery_failures); 11028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org void HandleDeliveryFailures(Members* failures); 11128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 11228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org // Builds a simple list of "name,id\n" entries for each member. 11328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::string BuildResponseForNewMember(const ChannelMember& member, 11428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org std::string* content_type); 11528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 11628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org protected: 11728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org Members members_; 11828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}; 11928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org 12070625e5bf3c91c57bf704d380bcc9df86575f08ajbauch#endif // WEBRTC_EXAMPLES_PEERCONNECTION_SERVER_PEER_CHANNEL_H_ 121