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 %ls\n" \ | |
viettrungluu
2014/06/30 19:16:54
Do you really need this macro? It's only used in o
ananta
2014/07/03 00:04:40
Leaving the macro as is as it makes it easier to a
ananta
2014/07/03 00:04:40
Done.
| |
25 "Message Time Hours : %d\n"\ | |
viettrungluu
2014/06/30 19:16:55
No space before ':', please.
Also, probably you c
ananta
2014/07/03 00:04:40
Done.
| |
26 "Message Time Mins : %d\n"\ | |
27 "Message Time Secs : %d\n"\ | |
28 "Message Bytes : %d\n"\ | |
29 "Message Fields : %d\n"\ | |
30 "Message name : %d\n"\ | |
viettrungluu
2014/06/30 19:16:55
Nit: name -> Name (capitalized; be consistent -- y
ananta
2014/07/03 00:04:40
Done.
| |
31 "Message Request ID : %d\n"\ | |
viettrungluu
2014/06/30 19:16:55
%llu (it's 64-bit)
ananta
2014/07/03 00:04:40
Done.
| |
32 "Message Type : %s\n"\ | |
33 "End of message\n\"" | |
34 | |
19 WebSocketServer::WebSocketServer(int port) | 35 WebSocketServer::WebSocketServer(int port) |
20 : port_(port), connection_id_(kNotConnected) { | 36 : port_(port), connection_id_(kNotConnected) { |
21 } | 37 } |
22 | 38 |
23 WebSocketServer::~WebSocketServer() { | 39 WebSocketServer::~WebSocketServer() { |
24 } | 40 } |
25 | 41 |
26 bool WebSocketServer::Start() { | 42 bool WebSocketServer::Start() { |
27 net::TCPListenSocketFactory factory("0.0.0.0", port_); | 43 net::TCPListenSocketFactory factory("0.0.0.0", port_); |
28 server_ = new net::HttpServer(factory, this); | 44 server_ = new net::HttpServer(factory, this); |
29 net::IPEndPoint address; | 45 net::IPEndPoint address; |
30 int error = server_->GetLocalAddress(&address); | 46 int error = server_->GetLocalAddress(&address); |
31 port_ = address.port(); | 47 port_ = address.port(); |
32 return (error == net::OK); | 48 return (error == net::OK); |
33 } | 49 } |
34 | 50 |
51 void WebSocketServer::LogMessageInfo( | |
52 const mojo::MojoMessageHeader& message_header, | |
53 const GURL& url, | |
54 const base::Time& message_time) { | |
55 base::Time::Exploded exploded; | |
56 message_time.LocalExplode(&exploded); | |
57 | |
58 if (Connected()) { | |
59 std::string output_message = base::StringPrintf( | |
60 MOJO_DEBUGGER_MESSAGE_FORMAT, | |
61 url.spec(), | |
viettrungluu
2014/06/30 19:16:55
url.spec().c_str()?
ananta
2014/07/03 00:04:40
Done.
| |
62 exploded.hour, | |
63 exploded.second, | |
64 message_header.num_bytes, | |
viettrungluu
2014/06/30 19:16:55
Strictly speaking, for the uint32_t fields, you sh
ananta
2014/07/03 00:04:40
Done.
| |
65 message_header.num_fields, | |
66 message_header.name, | |
67 message_header.num_fields == 3 ? message_header.request_id : -1, | |
viettrungluu
2014/06/30 19:16:54
Hmmm. You should probably use "%llu" and static_ca
ananta
2014/07/03 00:04:40
Done. If the field is not present we print beef.
ananta
2014/07/09 02:33:56
If the field is not present we print 0 followed by
| |
68 message_header.flags & mojo::kMessageExpectsResponse ? | |
69 "Expects response" : "Response message"); | |
70 server_->SendOverWebSocket(connection_id_, output_message.c_str()); | |
71 } else { | |
72 DLOG(INFO) << "\n\nStart of message for url :" << url.spec(); | |
viettrungluu
2014/06/30 19:16:55
Maybe you should just generate the same string (as
ananta
2014/07/03 00:04:40
Done.
| |
73 DLOG(INFO) << "Message Time Hours :" << exploded.hour; | |
74 DLOG(INFO) << "Message Time Mins :" << exploded.minute; | |
75 DLOG(INFO) << "Message Time Secs :" << exploded.second; | |
76 DLOG(INFO) << "Message Bytes : " << message_header.num_bytes; | |
77 DLOG(INFO) << "Message Fields : " << message_header.num_fields; | |
78 DLOG(INFO) << "Message name : " << message_header.name; | |
79 if (message_header.num_fields == 3) { | |
80 DLOG(INFO) << "Message has request id"; | |
81 DLOG(INFO) << "Message Request ID : " | |
82 << message_header.request_id; | |
83 } | |
84 if (message_header.flags & mojo::kMessageExpectsResponse) { | |
85 DLOG(INFO) << "Message expects response"; | |
86 } else if (message_header.flags & mojo::kMessageIsResponse) { | |
87 DLOG(INFO) << "Message is response"; | |
88 } | |
89 DLOG(INFO) << "End of message : "; | |
90 } | |
91 } | |
92 | |
35 void WebSocketServer::OnHttpRequest( | 93 void WebSocketServer::OnHttpRequest( |
36 int connection_id, | 94 int connection_id, |
37 const net::HttpServerRequestInfo& info) { | 95 const net::HttpServerRequestInfo& info) { |
38 server_->Send500(connection_id, "websockets protocol only"); | 96 server_->Send500(connection_id, "websockets protocol only"); |
39 } | 97 } |
40 | 98 |
41 void WebSocketServer::OnWebSocketRequest( | 99 void WebSocketServer::OnWebSocketRequest( |
42 int connection_id, | 100 int connection_id, |
43 const net::HttpServerRequestInfo& info) { | 101 const net::HttpServerRequestInfo& info) { |
44 if (connection_id_ != kNotConnected) { | 102 if (connection_id_ != kNotConnected) { |
(...skipping 15 matching lines...) Expand all Loading... | |
60 if (data == "\"hello\"") | 118 if (data == "\"hello\"") |
61 server_->SendOverWebSocket(connection_id, "\"hi there!\""); | 119 server_->SendOverWebSocket(connection_id, "\"hi there!\""); |
62 } | 120 } |
63 | 121 |
64 void WebSocketServer::OnClose( | 122 void WebSocketServer::OnClose( |
65 int connection_id) { | 123 int connection_id) { |
66 if (connection_id == connection_id_) | 124 if (connection_id == connection_id_) |
67 connection_id_ = kNotConnected; | 125 connection_id_ = kNotConnected; |
68 } | 126 } |
69 | 127 |
128 bool WebSocketServer::Connected() const { | |
129 return connection_id_ != kNotConnected; | |
130 } | |
131 | |
70 } // namespace spy | 132 } // namespace spy |
OLD | NEW |