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 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
11 | |
12 #include "mojo/public/cpp/bindings/allocation_scope.h" | |
13 #include "mojo/public/cpp/bindings/message.h" | |
14 | |
15 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
16 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
17 #include "net/server/http_server_request_info.h" | 11 #include "net/server/http_server_request_info.h" |
18 #include "net/server/http_server_response_info.h" | 12 #include "net/server/http_server_response_info.h" |
19 #include "net/socket/tcp_listen_socket.h" | 13 #include "net/socket/tcp_listen_socket.h" |
20 | 14 |
21 namespace mojo { | 15 namespace spy { |
22 | 16 |
23 const int kNotConnected = -1; | 17 const int kNotConnected = -1; |
24 | 18 |
25 WebSocketServer::WebSocketServer(int port, | 19 WebSocketServer::WebSocketServer(int port) |
26 mojo::ScopedMessagePipeHandle server_pipe) | 20 : port_(port), connection_id_(kNotConnected) { |
27 : port_(port), | |
28 connection_id_(kNotConnected), | |
29 spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) { | |
30 spy_server_->SetClient(this); | |
31 } | 21 } |
32 | 22 |
33 WebSocketServer::~WebSocketServer() { | 23 WebSocketServer::~WebSocketServer() { |
34 } | 24 } |
35 | 25 |
36 bool WebSocketServer::Start() { | 26 bool WebSocketServer::Start() { |
37 net::TCPListenSocketFactory factory("0.0.0.0", port_); | 27 net::TCPListenSocketFactory factory("0.0.0.0", port_); |
38 web_server_ = new net::HttpServer(factory, this); | 28 server_ = new net::HttpServer(factory, this); |
39 net::IPEndPoint address; | 29 net::IPEndPoint address; |
40 int error = web_server_->GetLocalAddress(&address); | 30 int error = server_->GetLocalAddress(&address); |
41 port_ = address.port(); | 31 port_ = address.port(); |
42 return (error == net::OK); | 32 return (error == net::OK); |
43 } | 33 } |
44 | 34 |
45 void WebSocketServer::OnHttpRequest( | 35 void WebSocketServer::OnHttpRequest( |
46 int connection_id, | 36 int connection_id, |
47 const net::HttpServerRequestInfo& info) { | 37 const net::HttpServerRequestInfo& info) { |
48 web_server_->Send500(connection_id, "websockets protocol only"); | 38 server_->Send500(connection_id, "websockets protocol only"); |
49 } | 39 } |
50 | 40 |
51 void WebSocketServer::OnWebSocketRequest( | 41 void WebSocketServer::OnWebSocketRequest( |
52 int connection_id, | 42 int connection_id, |
53 const net::HttpServerRequestInfo& info) { | 43 const net::HttpServerRequestInfo& info) { |
54 if (connection_id_ != kNotConnected) { | 44 if (connection_id_ != kNotConnected) { |
55 // Reject connection since we already have our client. | 45 // Reject connection since we already have our client. |
56 base::MessageLoop::current()->PostTask( | 46 base::MessageLoop::current()->PostTask( |
57 FROM_HERE, | 47 FROM_HERE, |
58 base::Bind(&net::HttpServer::Close, web_server_, connection_id)); | 48 base::Bind(&net::HttpServer::Close, server_, connection_id)); |
59 return; | 49 return; |
60 } | 50 } |
61 // Accept the connection. | 51 // Accept the connection. |
62 web_server_->AcceptWebSocket(connection_id, info); | 52 server_->AcceptWebSocket(connection_id, info); |
63 connection_id_ = connection_id; | 53 connection_id_ = connection_id; |
64 } | 54 } |
65 | 55 |
66 void WebSocketServer::OnWebSocketMessage( | 56 void WebSocketServer::OnWebSocketMessage( |
67 int connection_id, | 57 int connection_id, |
68 const std::string& data) { | 58 const std::string& data) { |
69 AllocationScope scope; | 59 // TODO(cpu): remove this test code soon. |
70 if (data == "\"start\"") { | 60 if (data == "\"hello\"") |
71 spy_api::Version::Builder vb; | 61 server_->SendOverWebSocket(connection_id, "\"hi there!\""); |
72 vb.set_major(0); | |
73 vb.set_minor(1); | |
74 spy_server_->StartSession( | |
75 vb.Finish(), | |
76 base::Bind(&WebSocketServer::OnStartSession, base::Unretained(this))); | |
77 } else if (data == "\"stop\"") { | |
78 spy_server_->StopSession( | |
79 base::Bind(&WebSocketServer::OnSessionEnd, base::Unretained(this))); | |
80 } | |
81 } | |
82 | |
83 void WebSocketServer::OnFatalError(spy_api::Result result) { | |
84 web_server_->SendOverWebSocket(connection_id_, "\"fatal error\""); | |
85 } | 62 } |
86 | 63 |
87 void WebSocketServer::OnClose( | 64 void WebSocketServer::OnClose( |
88 int connection_id) { | 65 int connection_id) { |
89 if (connection_id != connection_id_) | 66 if (connection_id == connection_id_) |
90 return; | 67 connection_id_ = kNotConnected; |
91 connection_id_ = kNotConnected; | |
92 AllocationScope scope; | |
93 spy_server_->StopSession( | |
94 base::Bind(&WebSocketServer::OnSessionEnd, base::Unretained(this))); | |
95 } | 68 } |
96 | 69 |
97 void WebSocketServer::OnSessionEnd(spy_api::Result result) { | 70 } // namespace spy |
98 // Called when the spy session (not the websocket) ends. | |
99 } | |
100 | |
101 void WebSocketServer::OnClientConnection( | |
102 const mojo::String& name, | |
103 uint32_t id, | |
104 spy_api::ConnectionOptions options) { | |
105 std::string cc("\""); | |
106 cc += name.To<std::string>() + "\""; | |
107 web_server_->SendOverWebSocket(connection_id_, cc); | |
108 } | |
109 | |
110 void WebSocketServer::OnMessage(const spy_api::Message& message) { | |
111 } | |
112 | |
113 void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) { | |
114 web_server_->SendOverWebSocket(connection_id_, "\"ok start\""); | |
115 } | |
116 | |
117 } // namespace mojo | |
OLD | NEW |