Skip to content

Commit

Permalink
Auto merge of #14110 - servo:ejpbruel-ws, r=metajack
Browse files Browse the repository at this point in the history
Replace rust-websocket with ws-rs in the debugger server.

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14110)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Nov 7, 2016
2 parents 3b5d1f2 + c8c5254 commit 04e2af0
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 43 deletions.
1 change: 1 addition & 0 deletions components/constellation/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ bluetooth_traits = { path = "../bluetooth_traits" }
canvas = {path = "../canvas"}
canvas_traits = {path = "../canvas_traits"}
compositing = {path = "../compositing"}
debugger = {path = "../debugger"}
devtools_traits = {path = "../devtools_traits"}
euclid = "0.10.1"
gfx = {path = "../gfx"}
Expand Down
11 changes: 11 additions & 0 deletions components/constellation/constellation.rs
Expand Up @@ -17,6 +17,7 @@ use canvas_traits::CanvasMsg;
use compositing::SendableFrameTree;
use compositing::compositor_thread::CompositorProxy;
use compositing::compositor_thread::Msg as ToCompositorMsg;
use debugger;
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg};
use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D};
Expand Down Expand Up @@ -113,6 +114,9 @@ pub struct Constellation<Message, LTF, STF> {
/// A channel through which messages can be sent to the image cache thread.
image_cache_thread: ImageCacheThread,

/// A channel through which messages can be sent to the debugger.
debugger_chan: Option<debugger::Sender>,

/// A channel through which messages can be sent to the developer tools.
devtools_chan: Option<Sender<DevtoolsControlMsg>>,

Expand Down Expand Up @@ -190,6 +194,8 @@ pub struct Constellation<Message, LTF, STF> {
pub struct InitialConstellationState {
/// A channel through which messages can be sent to the compositor.
pub compositor_proxy: Box<CompositorProxy + Send>,
/// A channel to the debugger, if applicable.
pub debugger_chan: Option<debugger::Sender>,
/// A channel to the developer tools, if applicable.
pub devtools_chan: Option<Sender<DevtoolsControlMsg>>,
/// A channel to the bluetooth thread.
Expand Down Expand Up @@ -482,6 +488,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
compositor_receiver: compositor_receiver,
layout_receiver: layout_receiver,
compositor_proxy: state.compositor_proxy,
debugger_chan: state.debugger_chan,
devtools_chan: state.devtools_chan,
bluetooth_thread: state.bluetooth_thread,
public_resource_threads: state.public_resource_threads,
Expand Down Expand Up @@ -1070,6 +1077,10 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
warn!("Exit resource thread failed ({})", e);
}

if let Some(ref chan) = self.debugger_chan {
debugger::shutdown_server(chan);
}

if let Some(ref chan) = self.devtools_chan {
debug!("Exiting devtools.");
let msg = DevtoolsControlMsg::FromChrome(ChromeToDevtoolsControlMsg::ServerExitMsg);
Expand Down
1 change: 1 addition & 0 deletions components/constellation/lib.rs
Expand Up @@ -15,6 +15,7 @@ extern crate bluetooth_traits;
extern crate canvas;
extern crate canvas_traits;
extern crate compositing;
extern crate debugger;
extern crate devtools_traits;
extern crate euclid;
#[cfg(not(target_os = "windows"))]
Expand Down
5 changes: 3 additions & 2 deletions components/debugger/Cargo.toml
Expand Up @@ -11,5 +11,6 @@ path = "lib.rs"
crate_type = ["rlib"]

[dependencies]
util = { path = "../util" }
websocket = "0.17.1"
log = "0.3.5"
util = {path = "../util"}
ws = "0.5.3"
91 changes: 55 additions & 36 deletions components/debugger/lib.rs
Expand Up @@ -2,49 +2,68 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#[macro_use]
extern crate log;
extern crate util;
extern crate websocket;
extern crate ws;

use std::sync::mpsc;
use std::sync::mpsc::channel;
use util::thread::spawn_named;
use websocket::{Message, Receiver, Sender, Server};
use websocket::message::Type;
use ws::{Builder, CloseCode, Handler, Handshake};

pub fn start_server(port: u16) {
println!("Starting debugger server.");
spawn_named("debugger-server".to_owned(), move || {
run_server(port)
});
enum Message {
ShutdownServer,
}

pub struct Sender(mpsc::Sender<Message>);

struct Connection {
sender: ws::Sender
}

impl Handler for Connection {
fn on_open(&mut self, _: Handshake) -> ws::Result<()> {
debug!("Connection opened.");
Ok(())
}

fn on_close(&mut self, _: CloseCode, _: &str) {
debug!("Connection closed.");
}

fn on_message(&mut self, message: ws::Message) -> ws::Result<()> {
self.sender.send(message)
}
}

fn run_server(port: u16) {
let server = Server::bind(("127.0.0.1", port)).unwrap();
for connection in server {
spawn_named("debugger-connection".to_owned(), move || {
let connection = connection.unwrap();
let request = connection.read_request().unwrap();
let response = request.accept();
let client = response.send().unwrap();
let (mut sender, mut receiver) = client.split();
for message in receiver.incoming_messages() {
let message: Message = message.unwrap();
match message.opcode {
Type::Close => {
let message = Message::close();
sender.send_message(&message).unwrap();
break;
}
Type::Ping => {
let message = Message::pong(message.payload);
sender.send_message(&message).unwrap();
}
Type::Text => {
sender.send_message(&message).unwrap();
}
_ => {
panic!("Unexpected message type.");
}
pub fn start_server(port: u16) -> Sender {
debug!("Starting server.");
let (sender, receiver) = channel();
spawn_named("debugger".to_owned(), move || {
let socket = Builder::new().build(|sender: ws::Sender| {
Connection { sender: sender }
}).unwrap();
let sender = socket.broadcaster();
spawn_named("debugger-websocket".to_owned(), move || {
socket.listen(("127.0.0.1", port)).unwrap();
});
while let Ok(message) = receiver.recv() {
match message {
Message::ShutdownServer => {
break;
}
}
});
}
sender.shutdown().unwrap();
});
Sender(sender)
}

pub fn shutdown_server(sender: &Sender) {
debug!("Shutting down server.");
let &Sender(ref sender) = sender;
if let Err(_) = sender.send(Message::ShutdownServer) {
warn!("Failed to shut down server.");
}
}
81 changes: 80 additions & 1 deletion components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04e2af0

Please sign in to comment.