| Index: trunk/src/mojo/spy/websocket_server.cc
|
| ===================================================================
|
| --- trunk/src/mojo/spy/websocket_server.cc (revision 272462)
|
| +++ trunk/src/mojo/spy/websocket_server.cc (working copy)
|
| @@ -4,30 +4,20 @@
|
|
|
| #include "mojo/spy/websocket_server.h"
|
|
|
| -#include <string>
|
| -
|
| #include "base/bind.h"
|
| #include "base/strings/stringprintf.h"
|
| -
|
| -#include "mojo/public/cpp/bindings/allocation_scope.h"
|
| -#include "mojo/public/cpp/bindings/message.h"
|
| -
|
| #include "net/base/ip_endpoint.h"
|
| #include "net/base/net_errors.h"
|
| #include "net/server/http_server_request_info.h"
|
| #include "net/server/http_server_response_info.h"
|
| #include "net/socket/tcp_listen_socket.h"
|
|
|
| -namespace mojo {
|
| +namespace spy {
|
|
|
| const int kNotConnected = -1;
|
|
|
| -WebSocketServer::WebSocketServer(int port,
|
| - mojo::ScopedMessagePipeHandle server_pipe)
|
| - : port_(port),
|
| - connection_id_(kNotConnected),
|
| - spy_server_(MakeProxy<spy_api::SpyServer>(server_pipe.Pass())) {
|
| - spy_server_->SetClient(this);
|
| +WebSocketServer::WebSocketServer(int port)
|
| + : port_(port), connection_id_(kNotConnected) {
|
| }
|
|
|
| WebSocketServer::~WebSocketServer() {
|
| @@ -35,9 +25,9 @@
|
|
|
| bool WebSocketServer::Start() {
|
| net::TCPListenSocketFactory factory("0.0.0.0", port_);
|
| - web_server_ = new net::HttpServer(factory, this);
|
| + server_ = new net::HttpServer(factory, this);
|
| net::IPEndPoint address;
|
| - int error = web_server_->GetLocalAddress(&address);
|
| + int error = server_->GetLocalAddress(&address);
|
| port_ = address.port();
|
| return (error == net::OK);
|
| }
|
| @@ -45,7 +35,7 @@
|
| void WebSocketServer::OnHttpRequest(
|
| int connection_id,
|
| const net::HttpServerRequestInfo& info) {
|
| - web_server_->Send500(connection_id, "websockets protocol only");
|
| + server_->Send500(connection_id, "websockets protocol only");
|
| }
|
|
|
| void WebSocketServer::OnWebSocketRequest(
|
| @@ -55,63 +45,26 @@
|
| // Reject connection since we already have our client.
|
| base::MessageLoop::current()->PostTask(
|
| FROM_HERE,
|
| - base::Bind(&net::HttpServer::Close, web_server_, connection_id));
|
| + base::Bind(&net::HttpServer::Close, server_, connection_id));
|
| return;
|
| }
|
| // Accept the connection.
|
| - web_server_->AcceptWebSocket(connection_id, info);
|
| + server_->AcceptWebSocket(connection_id, info);
|
| connection_id_ = connection_id;
|
| }
|
|
|
| void WebSocketServer::OnWebSocketMessage(
|
| int connection_id,
|
| const std::string& data) {
|
| - AllocationScope scope;
|
| - if (data == "\"start\"") {
|
| - spy_api::Version::Builder vb;
|
| - vb.set_major(0);
|
| - vb.set_minor(1);
|
| - spy_server_->StartSession(
|
| - vb.Finish(),
|
| - base::Bind(&WebSocketServer::OnStartSession, base::Unretained(this)));
|
| - } else if (data == "\"stop\"") {
|
| - spy_server_->StopSession(
|
| - base::Bind(&WebSocketServer::OnSessionEnd, base::Unretained(this)));
|
| - }
|
| + // TODO(cpu): remove this test code soon.
|
| + if (data == "\"hello\"")
|
| + server_->SendOverWebSocket(connection_id, "\"hi there!\"");
|
| }
|
|
|
| -void WebSocketServer::OnFatalError(spy_api::Result result) {
|
| - web_server_->SendOverWebSocket(connection_id_, "\"fatal error\"");
|
| -}
|
| -
|
| void WebSocketServer::OnClose(
|
| int connection_id) {
|
| - if (connection_id != connection_id_)
|
| - return;
|
| - connection_id_ = kNotConnected;
|
| - AllocationScope scope;
|
| - spy_server_->StopSession(
|
| - base::Bind(&WebSocketServer::OnSessionEnd, base::Unretained(this)));
|
| + if (connection_id == connection_id_)
|
| + connection_id_ = kNotConnected;
|
| }
|
|
|
| -void WebSocketServer::OnSessionEnd(spy_api::Result result) {
|
| - // Called when the spy session (not the websocket) ends.
|
| -}
|
| -
|
| -void WebSocketServer::OnClientConnection(
|
| - const mojo::String& name,
|
| - uint32_t id,
|
| - spy_api::ConnectionOptions options) {
|
| - std::string cc("\"");
|
| - cc += name.To<std::string>() + "\"";
|
| - web_server_->SendOverWebSocket(connection_id_, cc);
|
| -}
|
| -
|
| -void WebSocketServer::OnMessage(const spy_api::Message& message) {
|
| -}
|
| -
|
| -void WebSocketServer::OnStartSession(spy_api::Result, mojo::String) {
|
| - web_server_->SendOverWebSocket(connection_id_, "\"ok start\"");
|
| -}
|
| -
|
| -} // namespace mojo
|
| +} // namespace spy
|
|
|