| OLD | NEW |
| (Empty) |
| 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 #include "talk/examples/peerconnection/server/peer_channel.h" | |
| 29 | |
| 30 #include <stdio.h> | |
| 31 #include <stdlib.h> | |
| 32 #include <string.h> | |
| 33 | |
| 34 #include <algorithm> | |
| 35 | |
| 36 #include "talk/examples/peerconnection/server/data_socket.h" | |
| 37 #include "talk/examples/peerconnection/server/utils.h" | |
| 38 #include "webrtc/base/stringutils.h" | |
| 39 | |
| 40 using rtc::sprintfn; | |
| 41 | |
| 42 // Set to the peer id of the originator when messages are being | |
| 43 // exchanged between peers, but set to the id of the receiving peer | |
| 44 // itself when notifications are sent from the server about the state | |
| 45 // of other peers. | |
| 46 // | |
| 47 // WORKAROUND: Since support for CORS varies greatly from one browser to the | |
| 48 // next, we don't use a custom name for our peer-id header (originally it was | |
| 49 // "X-Peer-Id: "). Instead, we use a "simple header", "Pragma" which should | |
| 50 // always be exposed to CORS requests. There is a special CORS header devoted | |
| 51 // to exposing proprietary headers (Access-Control-Expose-Headers), however | |
| 52 // at this point it is not working correctly in some popular browsers. | |
| 53 static const char kPeerIdHeader[] = "Pragma: "; | |
| 54 | |
| 55 static const char* kRequestPaths[] = { | |
| 56 "/wait", "/sign_out", "/message", | |
| 57 }; | |
| 58 | |
| 59 enum RequestPathIndex { | |
| 60 kWait, | |
| 61 kSignOut, | |
| 62 kMessage, | |
| 63 }; | |
| 64 | |
| 65 const size_t kMaxNameLength = 512; | |
| 66 | |
| 67 // | |
| 68 // ChannelMember | |
| 69 // | |
| 70 | |
| 71 int ChannelMember::s_member_id_ = 0; | |
| 72 | |
| 73 ChannelMember::ChannelMember(DataSocket* socket) | |
| 74 : waiting_socket_(NULL), id_(++s_member_id_), | |
| 75 connected_(true), timestamp_(time(NULL)) { | |
| 76 assert(socket); | |
| 77 assert(socket->method() == DataSocket::GET); | |
| 78 assert(socket->PathEquals("/sign_in")); | |
| 79 name_ = socket->request_arguments(); // TODO: urldecode | |
| 80 if (name_.empty()) | |
| 81 name_ = "peer_" + int2str(id_); | |
| 82 else if (name_.length() > kMaxNameLength) | |
| 83 name_.resize(kMaxNameLength); | |
| 84 | |
| 85 std::replace(name_.begin(), name_.end(), ',', '_'); | |
| 86 } | |
| 87 | |
| 88 ChannelMember::~ChannelMember() { | |
| 89 } | |
| 90 | |
| 91 bool ChannelMember::is_wait_request(DataSocket* ds) const { | |
| 92 return ds && ds->PathEquals(kRequestPaths[kWait]); | |
| 93 } | |
| 94 | |
| 95 bool ChannelMember::TimedOut() { | |
| 96 return waiting_socket_ == NULL && (time(NULL) - timestamp_) > 30; | |
| 97 } | |
| 98 | |
| 99 std::string ChannelMember::GetPeerIdHeader() const { | |
| 100 std::string ret(kPeerIdHeader + int2str(id_) + "\r\n"); | |
| 101 return ret; | |
| 102 } | |
| 103 | |
| 104 bool ChannelMember::NotifyOfOtherMember(const ChannelMember& other) { | |
| 105 assert(&other != this); | |
| 106 QueueResponse("200 OK", "text/plain", GetPeerIdHeader(), | |
| 107 other.GetEntry()); | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 // Returns a string in the form "name,id,connected\n". | |
| 112 std::string ChannelMember::GetEntry() const { | |
| 113 assert(name_.length() <= kMaxNameLength); | |
| 114 | |
| 115 // name, 11-digit int, 1-digit bool, newline, null | |
| 116 char entry[kMaxNameLength + 15]; | |
| 117 sprintfn(entry, sizeof(entry), "%s,%d,%d\n", | |
| 118 name_.substr(0, kMaxNameLength).c_str(), id_, connected_); | |
| 119 return entry; | |
| 120 } | |
| 121 | |
| 122 void ChannelMember::ForwardRequestToPeer(DataSocket* ds, ChannelMember* peer) { | |
| 123 assert(peer); | |
| 124 assert(ds); | |
| 125 | |
| 126 std::string extra_headers(GetPeerIdHeader()); | |
| 127 | |
| 128 if (peer == this) { | |
| 129 ds->Send("200 OK", true, ds->content_type(), extra_headers, | |
| 130 ds->data()); | |
| 131 } else { | |
| 132 printf("Client %s sending to %s\n", | |
| 133 name_.c_str(), peer->name().c_str()); | |
| 134 peer->QueueResponse("200 OK", ds->content_type(), extra_headers, | |
| 135 ds->data()); | |
| 136 ds->Send("200 OK", true, "text/plain", "", ""); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void ChannelMember::OnClosing(DataSocket* ds) { | |
| 141 if (ds == waiting_socket_) { | |
| 142 waiting_socket_ = NULL; | |
| 143 timestamp_ = time(NULL); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 void ChannelMember::QueueResponse(const std::string& status, | |
| 148 const std::string& content_type, | |
| 149 const std::string& extra_headers, | |
| 150 const std::string& data) { | |
| 151 if (waiting_socket_) { | |
| 152 assert(queue_.size() == 0); | |
| 153 assert(waiting_socket_->method() == DataSocket::GET); | |
| 154 bool ok = waiting_socket_->Send(status, true, content_type, extra_headers, | |
| 155 data); | |
| 156 if (!ok) { | |
| 157 printf("Failed to deliver data to waiting socket\n"); | |
| 158 } | |
| 159 waiting_socket_ = NULL; | |
| 160 timestamp_ = time(NULL); | |
| 161 } else { | |
| 162 QueuedResponse qr; | |
| 163 qr.status = status; | |
| 164 qr.content_type = content_type; | |
| 165 qr.extra_headers = extra_headers; | |
| 166 qr.data = data; | |
| 167 queue_.push(qr); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void ChannelMember::SetWaitingSocket(DataSocket* ds) { | |
| 172 assert(ds->method() == DataSocket::GET); | |
| 173 if (ds && !queue_.empty()) { | |
| 174 assert(waiting_socket_ == NULL); | |
| 175 const QueuedResponse& response = queue_.front(); | |
| 176 ds->Send(response.status, true, response.content_type, | |
| 177 response.extra_headers, response.data); | |
| 178 queue_.pop(); | |
| 179 } else { | |
| 180 waiting_socket_ = ds; | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 // | |
| 185 // PeerChannel | |
| 186 // | |
| 187 | |
| 188 // static | |
| 189 bool PeerChannel::IsPeerConnection(const DataSocket* ds) { | |
| 190 assert(ds); | |
| 191 return (ds->method() == DataSocket::POST && ds->content_length() > 0) || | |
| 192 (ds->method() == DataSocket::GET && ds->PathEquals("/sign_in")); | |
| 193 } | |
| 194 | |
| 195 ChannelMember* PeerChannel::Lookup(DataSocket* ds) const { | |
| 196 assert(ds); | |
| 197 | |
| 198 if (ds->method() != DataSocket::GET && ds->method() != DataSocket::POST) | |
| 199 return NULL; | |
| 200 | |
| 201 size_t i = 0; | |
| 202 for (; i < ARRAYSIZE(kRequestPaths); ++i) { | |
| 203 if (ds->PathEquals(kRequestPaths[i])) | |
| 204 break; | |
| 205 } | |
| 206 | |
| 207 if (i == ARRAYSIZE(kRequestPaths)) | |
| 208 return NULL; | |
| 209 | |
| 210 std::string args(ds->request_arguments()); | |
| 211 static const char kPeerId[] = "peer_id="; | |
| 212 size_t found = args.find(kPeerId); | |
| 213 if (found == std::string::npos) | |
| 214 return NULL; | |
| 215 | |
| 216 int id = atoi(&args[found + ARRAYSIZE(kPeerId) - 1]); | |
| 217 Members::const_iterator iter = members_.begin(); | |
| 218 for (; iter != members_.end(); ++iter) { | |
| 219 if (id == (*iter)->id()) { | |
| 220 if (i == kWait) | |
| 221 (*iter)->SetWaitingSocket(ds); | |
| 222 if (i == kSignOut) | |
| 223 (*iter)->set_disconnected(); | |
| 224 return *iter; | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 return NULL; | |
| 229 } | |
| 230 | |
| 231 ChannelMember* PeerChannel::IsTargetedRequest(const DataSocket* ds) const { | |
| 232 assert(ds); | |
| 233 // Regardless of GET or POST, we look for the peer_id parameter | |
| 234 // only in the request_path. | |
| 235 const std::string& path = ds->request_path(); | |
| 236 size_t args = path.find('?'); | |
| 237 if (args == std::string::npos) | |
| 238 return NULL; | |
| 239 size_t found; | |
| 240 const char kTargetPeerIdParam[] = "to="; | |
| 241 do { | |
| 242 found = path.find(kTargetPeerIdParam, args); | |
| 243 if (found == std::string::npos) | |
| 244 return NULL; | |
| 245 if (found == (args + 1) || path[found - 1] == '&') { | |
| 246 found += ARRAYSIZE(kTargetPeerIdParam) - 1; | |
| 247 break; | |
| 248 } | |
| 249 args = found + ARRAYSIZE(kTargetPeerIdParam) - 1; | |
| 250 } while (true); | |
| 251 int id = atoi(&path[found]); | |
| 252 Members::const_iterator i = members_.begin(); | |
| 253 for (; i != members_.end(); ++i) { | |
| 254 if ((*i)->id() == id) { | |
| 255 return *i; | |
| 256 } | |
| 257 } | |
| 258 return NULL; | |
| 259 } | |
| 260 | |
| 261 bool PeerChannel::AddMember(DataSocket* ds) { | |
| 262 assert(IsPeerConnection(ds)); | |
| 263 ChannelMember* new_guy = new ChannelMember(ds); | |
| 264 Members failures; | |
| 265 BroadcastChangedState(*new_guy, &failures); | |
| 266 HandleDeliveryFailures(&failures); | |
| 267 members_.push_back(new_guy); | |
| 268 | |
| 269 printf("New member added (total=%s): %s\n", | |
| 270 size_t2str(members_.size()).c_str(), new_guy->name().c_str()); | |
| 271 | |
| 272 // Let the newly connected peer know about other members of the channel. | |
| 273 std::string content_type; | |
| 274 std::string response = BuildResponseForNewMember(*new_guy, &content_type); | |
| 275 ds->Send("200 Added", true, content_type, new_guy->GetPeerIdHeader(), | |
| 276 response); | |
| 277 return true; | |
| 278 } | |
| 279 | |
| 280 void PeerChannel::CloseAll() { | |
| 281 Members::const_iterator i = members_.begin(); | |
| 282 for (; i != members_.end(); ++i) { | |
| 283 (*i)->QueueResponse("200 OK", "text/plain", "", "Server shutting down"); | |
| 284 } | |
| 285 DeleteAll(); | |
| 286 } | |
| 287 | |
| 288 void PeerChannel::OnClosing(DataSocket* ds) { | |
| 289 for (Members::iterator i = members_.begin(); i != members_.end(); ++i) { | |
| 290 ChannelMember* m = (*i); | |
| 291 m->OnClosing(ds); | |
| 292 if (!m->connected()) { | |
| 293 i = members_.erase(i); | |
| 294 Members failures; | |
| 295 BroadcastChangedState(*m, &failures); | |
| 296 HandleDeliveryFailures(&failures); | |
| 297 delete m; | |
| 298 if (i == members_.end()) | |
| 299 break; | |
| 300 } | |
| 301 } | |
| 302 printf("Total connected: %s\n", size_t2str(members_.size()).c_str()); | |
| 303 } | |
| 304 | |
| 305 void PeerChannel::CheckForTimeout() { | |
| 306 for (Members::iterator i = members_.begin(); i != members_.end(); ++i) { | |
| 307 ChannelMember* m = (*i); | |
| 308 if (m->TimedOut()) { | |
| 309 printf("Timeout: %s\n", m->name().c_str()); | |
| 310 m->set_disconnected(); | |
| 311 i = members_.erase(i); | |
| 312 Members failures; | |
| 313 BroadcastChangedState(*m, &failures); | |
| 314 HandleDeliveryFailures(&failures); | |
| 315 delete m; | |
| 316 if (i == members_.end()) | |
| 317 break; | |
| 318 } | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 void PeerChannel::DeleteAll() { | |
| 323 for (Members::iterator i = members_.begin(); i != members_.end(); ++i) | |
| 324 delete (*i); | |
| 325 members_.clear(); | |
| 326 } | |
| 327 | |
| 328 void PeerChannel::BroadcastChangedState(const ChannelMember& member, | |
| 329 Members* delivery_failures) { | |
| 330 // This function should be called prior to DataSocket::Close(). | |
| 331 assert(delivery_failures); | |
| 332 | |
| 333 if (!member.connected()) { | |
| 334 printf("Member disconnected: %s\n", member.name().c_str()); | |
| 335 } | |
| 336 | |
| 337 Members::iterator i = members_.begin(); | |
| 338 for (; i != members_.end(); ++i) { | |
| 339 if (&member != (*i)) { | |
| 340 if (!(*i)->NotifyOfOtherMember(member)) { | |
| 341 (*i)->set_disconnected(); | |
| 342 delivery_failures->push_back(*i); | |
| 343 i = members_.erase(i); | |
| 344 if (i == members_.end()) | |
| 345 break; | |
| 346 } | |
| 347 } | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 void PeerChannel::HandleDeliveryFailures(Members* failures) { | |
| 352 assert(failures); | |
| 353 | |
| 354 while (!failures->empty()) { | |
| 355 Members::iterator i = failures->begin(); | |
| 356 ChannelMember* member = *i; | |
| 357 assert(!member->connected()); | |
| 358 failures->erase(i); | |
| 359 BroadcastChangedState(*member, failures); | |
| 360 delete member; | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 // Builds a simple list of "name,id\n" entries for each member. | |
| 365 std::string PeerChannel::BuildResponseForNewMember(const ChannelMember& member, | |
| 366 std::string* content_type) { | |
| 367 assert(content_type); | |
| 368 | |
| 369 *content_type = "text/plain"; | |
| 370 // The peer itself will always be the first entry. | |
| 371 std::string response(member.GetEntry()); | |
| 372 for (Members::iterator i = members_.begin(); i != members_.end(); ++i) { | |
| 373 if (member.id() != (*i)->id()) { | |
| 374 assert((*i)->connected()); | |
| 375 response += (*i)->GetEntry(); | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 return response; | |
| 380 } | |
| OLD | NEW |