Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: mojo/spy/websocket_server.cc

Issue 284743002: Adding a mojo interface to the mojo spy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: clang 2 Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/spy/websocket_server.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/allocation_scope.h"
13 #include "mojo/public/cpp/bindings/message.h"
14
9 #include "net/base/ip_endpoint.h" 15 #include "net/base/ip_endpoint.h"
10 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
11 #include "net/server/http_server_request_info.h" 17 #include "net/server/http_server_request_info.h"
12 #include "net/server/http_server_response_info.h" 18 #include "net/server/http_server_response_info.h"
13 #include "net/socket/tcp_listen_socket.h" 19 #include "net/socket/tcp_listen_socket.h"
14 20
15 namespace spy { 21 namespace mojo {
16 22
17 const int kNotConnected = -1; 23 const int kNotConnected = -1;
18 24
19 WebSocketServer::WebSocketServer(int port) 25 WebSocketServer::WebSocketServer(int port,
20 : port_(port), connection_id_(kNotConnected) { 26 mojo::ScopedMessagePipeHandle server_pipe)
27 : port_(port),
28 connection_id_(kNotConnected),
29 spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) {
30 spy_server_->SetClient(this);
21 } 31 }
22 32
23 WebSocketServer::~WebSocketServer() { 33 WebSocketServer::~WebSocketServer() {
24 } 34 }
25 35
26 bool WebSocketServer::Start() { 36 bool WebSocketServer::Start() {
27 net::TCPListenSocketFactory factory("0.0.0.0", port_); 37 net::TCPListenSocketFactory factory("0.0.0.0", port_);
28 server_ = new net::HttpServer(factory, this); 38 web_server_ = new net::HttpServer(factory, this);
29 net::IPEndPoint address; 39 net::IPEndPoint address;
30 int error = server_->GetLocalAddress(&address); 40 int error = web_server_->GetLocalAddress(&address);
31 port_ = address.port(); 41 port_ = address.port();
32 return (error == net::OK); 42 return (error == net::OK);
33 } 43 }
34 44
35 void WebSocketServer::OnHttpRequest( 45 void WebSocketServer::OnHttpRequest(
36 int connection_id, 46 int connection_id,
37 const net::HttpServerRequestInfo& info) { 47 const net::HttpServerRequestInfo& info) {
38 server_->Send500(connection_id, "websockets protocol only"); 48 web_server_->Send500(connection_id, "websockets protocol only");
39 } 49 }
40 50
41 void WebSocketServer::OnWebSocketRequest( 51 void WebSocketServer::OnWebSocketRequest(
42 int connection_id, 52 int connection_id,
43 const net::HttpServerRequestInfo& info) { 53 const net::HttpServerRequestInfo& info) {
44 if (connection_id_ != kNotConnected) { 54 if (connection_id_ != kNotConnected) {
45 // Reject connection since we already have our client. 55 // Reject connection since we already have our client.
46 base::MessageLoop::current()->PostTask( 56 base::MessageLoop::current()->PostTask(
47 FROM_HERE, 57 FROM_HERE,
48 base::Bind(&net::HttpServer::Close, server_, connection_id)); 58 base::Bind(&net::HttpServer::Close, web_server_, connection_id));
49 return; 59 return;
50 } 60 }
51 // Accept the connection. 61 // Accept the connection.
52 server_->AcceptWebSocket(connection_id, info); 62 web_server_->AcceptWebSocket(connection_id, info);
53 connection_id_ = connection_id; 63 connection_id_ = connection_id;
54 } 64 }
55 65
56 void WebSocketServer::OnWebSocketMessage( 66 void WebSocketServer::OnWebSocketMessage(
57 int connection_id, 67 int connection_id,
58 const std::string& data) { 68 const std::string& data) {
59 // TODO(cpu): remove this test code soon. 69 AllocationScope scope;
60 if (data == "\"hello\"") 70 if (data == "\"start\"") {
61 server_->SendOverWebSocket(connection_id, "\"hi there!\""); 71 spy_api::Version::Builder vb;
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\"");
62 } 85 }
63 86
64 void WebSocketServer::OnClose( 87 void WebSocketServer::OnClose(
65 int connection_id) { 88 int connection_id) {
66 if (connection_id == connection_id_) 89 if (connection_id != connection_id_)
67 connection_id_ = kNotConnected; 90 return;
91 connection_id_ = kNotConnected;
92 AllocationScope scope;
93 spy_server_->StopSession(
94 base::Bind(&WebSocketServer::OnSessionEnd, base::Unretained(this)));
68 } 95 }
69 96
70 } // namespace spy 97 void WebSocketServer::OnSessionEnd(spy_api::Result result) {
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
OLDNEW
« no previous file with comments | « mojo/spy/websocket_server.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698