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