Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/spy/websocket_server.h" | 5 #include "mojo/spy/websocket_server.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | |
| 8 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/time/time.h" | |
| 9 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
| 10 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 11 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
| 12 #include "net/server/http_server_response_info.h" | 16 #include "net/server/http_server_response_info.h" |
| 13 #include "net/socket/tcp_listen_socket.h" | 17 #include "net/socket/tcp_listen_socket.h" |
| 18 #include "url/gurl.h" | |
| 14 | 19 |
| 15 namespace spy { | 20 namespace spy { |
| 16 | 21 |
| 17 const int kNotConnected = -1; | 22 const int kNotConnected = -1; |
| 18 | 23 |
| 24 #define MOJO_DEBUGGER_MESSAGE_FORMAT "\"Start of message for url %s\n" \ | |
| 25 "Message Time: %02d:%02d:%02d\n"\ | |
| 26 "Message Bytes: %u\n"\ | |
| 27 "Message Fields: %u\n"\ | |
| 28 "Message Name: %u\n"\ | |
| 29 "Message Request ID: %llu\n"\ | |
| 30 "Message Type: %s\n"\ | |
|
cpu_(ooo_6.6-7.5)
2014/07/09 18:18:45
for this to come out properly formatted you need
n
ananta
2014/07/09 21:24:55
Leaving \n as is. Fixed overall formatting.
| |
| 31 "End of message\n\"" | |
| 32 | |
| 19 WebSocketServer::WebSocketServer(int port) | 33 WebSocketServer::WebSocketServer(int port) |
| 20 : port_(port), connection_id_(kNotConnected) { | 34 : port_(port), connection_id_(kNotConnected) { |
| 21 } | 35 } |
| 22 | 36 |
| 23 WebSocketServer::~WebSocketServer() { | 37 WebSocketServer::~WebSocketServer() { |
| 24 } | 38 } |
| 25 | 39 |
| 26 bool WebSocketServer::Start() { | 40 bool WebSocketServer::Start() { |
| 27 net::TCPListenSocketFactory factory("0.0.0.0", port_); | 41 net::TCPListenSocketFactory factory("0.0.0.0", port_); |
| 28 server_ = new net::HttpServer(factory, this); | 42 server_ = new net::HttpServer(factory, this); |
| 29 net::IPEndPoint address; | 43 net::IPEndPoint address; |
| 30 int error = server_->GetLocalAddress(&address); | 44 int error = server_->GetLocalAddress(&address); |
| 31 port_ = address.port(); | 45 port_ = address.port(); |
| 32 return (error == net::OK); | 46 return (error == net::OK); |
| 33 } | 47 } |
| 34 | 48 |
| 49 void WebSocketServer::LogMessageInfo( | |
| 50 const mojo::MojoRequestHeader& message_header, | |
| 51 const GURL& url, | |
| 52 const base::Time& message_time) { | |
| 53 base::Time::Exploded exploded; | |
| 54 message_time.LocalExplode(&exploded); | |
| 55 | |
| 56 std::string output_message = base::StringPrintf( | |
| 57 MOJO_DEBUGGER_MESSAGE_FORMAT, | |
| 58 url.spec().c_str(), | |
| 59 exploded.hour, | |
| 60 exploded.minute, | |
| 61 exploded.second, | |
| 62 static_cast<unsigned>(message_header.num_bytes), | |
| 63 static_cast<unsigned>(message_header.num_fields), | |
| 64 static_cast<unsigned>(message_header.name), | |
| 65 static_cast<unsigned long long>( | |
| 66 message_header.num_fields == 3 ? message_header.request_id | |
| 67 : 0), | |
| 68 message_header.flags != 0 ? | |
| 69 (message_header.flags & mojo::kMessageExpectsResponse ? | |
| 70 "Expects response" : "Response message") | |
| 71 : "Not a request or response message"); | |
| 72 if (Connected()) { | |
| 73 server_->SendOverWebSocket(connection_id_, output_message.c_str()); | |
| 74 } else { | |
| 75 DLOG(INFO) << output_message; | |
| 76 } | |
| 77 } | |
| 78 | |
| 35 void WebSocketServer::OnHttpRequest( | 79 void WebSocketServer::OnHttpRequest( |
| 36 int connection_id, | 80 int connection_id, |
| 37 const net::HttpServerRequestInfo& info) { | 81 const net::HttpServerRequestInfo& info) { |
| 38 server_->Send500(connection_id, "websockets protocol only"); | 82 server_->Send500(connection_id, "websockets protocol only"); |
| 39 } | 83 } |
| 40 | 84 |
| 41 void WebSocketServer::OnWebSocketRequest( | 85 void WebSocketServer::OnWebSocketRequest( |
| 42 int connection_id, | 86 int connection_id, |
| 43 const net::HttpServerRequestInfo& info) { | 87 const net::HttpServerRequestInfo& info) { |
| 44 if (connection_id_ != kNotConnected) { | 88 if (connection_id_ != kNotConnected) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 60 if (data == "\"hello\"") | 104 if (data == "\"hello\"") |
| 61 server_->SendOverWebSocket(connection_id, "\"hi there!\""); | 105 server_->SendOverWebSocket(connection_id, "\"hi there!\""); |
| 62 } | 106 } |
| 63 | 107 |
| 64 void WebSocketServer::OnClose( | 108 void WebSocketServer::OnClose( |
| 65 int connection_id) { | 109 int connection_id) { |
| 66 if (connection_id == connection_id_) | 110 if (connection_id == connection_id_) |
| 67 connection_id_ = kNotConnected; | 111 connection_id_ = kNotConnected; |
| 68 } | 112 } |
| 69 | 113 |
| 114 bool WebSocketServer::Connected() const { | |
| 115 return connection_id_ != kNotConnected; | |
| 116 } | |
| 117 | |
| 70 } // namespace spy | 118 } // namespace spy |
| OLD | NEW |