Skip to content

Commit

Permalink
Merge pull request #54 from cwahbong/plugin-refactor2
Browse files Browse the repository at this point in the history
Plugin refactor
  • Loading branch information
cwahbong committed May 8, 2016
2 parents d43544b + 13b3359 commit 4ec3dd4
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: rust

# TODO(sirver): support beta, stable
rust:
- nightly-2016-04-21
- nightly

install:
- pip install 'Sphinx' --user
Expand Down
10 changes: 5 additions & 5 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate tempdir;
extern crate test;

use swiboe::client::{RpcCaller, Client};
use swiboe::plugin_buffer;
use swiboe::plugin;
use swiboe::rpc;
use swiboe::testing::TestHarness;
use test::Bencher;
Expand All @@ -24,17 +24,17 @@ fn bench_create_and_delete_buffers(b: &mut Bencher) {
let mut active_client = Client::connect_unix(&t.socket_name).unwrap();

b.iter(|| {
let new_response: plugin_buffer::NewResponse = match active_client.call(
"buffer.new", &plugin_buffer::NewRequest {
let new_response: plugin::buffer::NewResponse = match active_client.call(
"buffer.new", &plugin::buffer::NewRequest {
content: Some("bli\nbla\nblub".into()),
}).unwrap().wait().unwrap()
{
rpc::Result::Ok(value) => serde_json::from_value(value).unwrap(),
err => panic!("{:?}", err),
};

let _: plugin_buffer::DeleteResponse = match active_client.call(
"buffer.delete", &plugin_buffer::DeleteRequest {
let _: plugin::buffer::DeleteResponse = match active_client.call(
"buffer.delete", &plugin::buffer::DeleteRequest {
buffer_index: new_response.buffer_index
}).unwrap().wait().unwrap() {
rpc::Result::Ok(value) => serde_json::from_value(value).unwrap(),
Expand Down
13 changes: 6 additions & 7 deletions gtk_gui/src/bin/gtk_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ use std::sync::mpsc;
use std::sync::{RwLock, Arc};
use std::thread;
use swiboe::client;
use swiboe::plugin_buffer;
use swiboe::plugin_list_files;
use swiboe::plugin;
use swiboe_gtk_gui::buffer_view_widget;
use swiboe_gtk_gui::buffer_views;
use swiboe_gtk_gui::command::GuiCommand;
Expand Down Expand Up @@ -110,28 +109,28 @@ impl SwiboeGtkGui {
println!("#sirver keypress: {}", time::precise_time_ns());
match &name_str as &str {
"F2" => {
let mut rpc = thin_client.call("buffer.open", &plugin_buffer::OpenRequest {
let mut rpc = thin_client.call("buffer.open", &plugin::buffer::OpenRequest {
uri: "file:///Users/sirver/Desktop/Programming/rust/Swiboe/gui/src/bin/gtk_gui.rs".into(),
});
let b: plugin_buffer::OpenResponse = rpc.wait_for().unwrap();
let b: plugin::buffer::OpenResponse = rpc.wait_for().unwrap();
println!("#sirver b: {:#?}", b);
},
"F3" => {
let mut rpc = thin_client.call("list_files", &plugin_list_files::ListFilesRequest {
let mut rpc = thin_client.call("list_files", &plugin::list_files::ListFilesRequest {
directory: "/Users/sirver/Desktop/Programming/".into(),
});
let mut num = 0;
let start = time::SteadyTime::now();
while let Some(b) = rpc.recv().unwrap() {
let b: plugin_list_files::ListFilesUpdate = serde_json::from_value(b).unwrap();
let b: plugin::list_files::ListFilesUpdate = serde_json::from_value(b).unwrap();
num += b.files.len();
println!("#sirver num: {:#?}", num);
if num > 10000 {
break;
}
}
rpc.cancel();
// let b: plugin_list_files::ListFilesResponse = rpc.wait_for().unwrap();
// let b: plugin::list_files::ListFilesResponse = rpc.wait_for().unwrap();
// println!("#sirver b: {:#?}", b);
let duration = time::SteadyTime::now() - start;
println!("#sirver duration: {:#?}", duration);
Expand Down
6 changes: 3 additions & 3 deletions gui/src/buffer_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::mpsc;
use std::sync::{RwLock, Arc, Mutex};
use swiboe::client::RpcCaller;
use swiboe::client;
use swiboe::plugin_buffer;
use swiboe::plugin;
use swiboe::rpc;
use swiboe;
use uuid::Uuid;
Expand Down Expand Up @@ -234,11 +234,11 @@ impl BufferViews {
}

pub fn new_view(&mut self, buffer_index: usize, width: usize, height: usize) -> String {
let mut rpc = self.client.call("buffer.get_content", &plugin_buffer::GetContentRequest {
let mut rpc = self.client.call("buffer.get_content", &plugin::buffer::GetContentRequest {
buffer_index: buffer_index,
}).unwrap();

let response: plugin_buffer::GetContentResponse = rpc.wait_for().unwrap();
let response: plugin::buffer::GetContentResponse = rpc.wait_for().unwrap();
let buffer_view = BufferView::new(width, height, &response.content);
let view_id = buffer_view.id().to_string();
self.buffer_views.insert(buffer_view.id().to_string(), buffer_view);
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ macro_rules! try_rpc {
mod ipc;
pub mod client;
pub mod error;
pub mod plugin_buffer;
pub mod plugin_list_files;
pub mod plugin_logger;
pub mod plugin;
pub mod rpc;
pub mod spinner;
pub mod server;
Expand Down
43 changes: 17 additions & 26 deletions src/plugin_buffer.rs → src/plugin/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use ::client::RpcCaller;
use ::client;
use ::error::{Result};
use ::error::Result;
use ::plugin;
use ::rpc;
use serde_json;
use std::collections::HashMap;
Expand Down Expand Up @@ -299,37 +300,27 @@ impl ops::Deref for BuffersManager {
}
}



pub struct BufferPlugin {
client: client::Client,
buffers: Arc<RwLock<BuffersManager>>,
_client: client::Client,
_buffers: Arc<RwLock<BuffersManager>>,
}

impl BufferPlugin {
pub fn new(socket_name: &path::Path) -> Result<Self> {
let client = try!(client::Client::connect_unix(socket_name));

let mut plugin = BufferPlugin {
buffers: Arc::new(RwLock::new(BuffersManager::new(try!(client.clone())))),
client: client,
let mut client = try!(client::Client::connect_unix(socket_name));
let buffers = Arc::new(RwLock::new(BuffersManager::new(try!(client.clone()))));
let rpc_map = rpc_map! {
"buffer.new" => New { buffers: buffers.clone() },
"buffer.delete" => Delete { buffers: buffers.clone() },
"buffer.get_content" => GetContent { buffers: buffers.clone() },
"buffer.open" => Open { buffers: buffers.clone() },
"buffer.list" => List { buffers: buffers.clone() },
};
try!(plugin::register_rpc(&mut client, rpc_map));

let new = Box::new(New { buffers: plugin.buffers.clone() });
try!(plugin.client.new_rpc("buffer.new", new));

let delete = Box::new(Delete { buffers: plugin.buffers.clone() });
try!(plugin.client.new_rpc("buffer.delete", delete));

let get_content = Box::new(GetContent { buffers: plugin.buffers.clone() });
try!(plugin.client.new_rpc("buffer.get_content", get_content));

let open = Box::new(Open { buffers: plugin.buffers.clone() });
try!(plugin.client.new_rpc("buffer.open", open));

let list = Box::new(List { buffers: plugin.buffers.clone() });
try!(plugin.client.new_rpc("buffer.list", list));

Ok(plugin)
Ok(BufferPlugin{
_client: client,
_buffers: buffers,
})
}
}
25 changes: 11 additions & 14 deletions src/plugin_list_files.rs → src/plugin/list_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use ::client;
use ::client::RpcCaller;
use ::error::{Result};
use ::error::Result;
use ::rpc;
use ::plugin_logger;
use ::plugin;
use serde_json;
use std::convert;
use std::fs::{self, DirEntry};
Expand Down Expand Up @@ -67,7 +67,7 @@ impl client::rpc::server::Rpc for ListFiles {
fn call(&self, mut context: client::rpc::server::Context, args: serde_json::Value) {
let request: ListFilesRequest = try_rpc!(context, serde_json::from_value(args));
// NOCOM handle the result
let _ = self.client.write().unwrap().call("logger", &plugin_logger::LoggerRequest {
let _ = self.client.write().unwrap().call("logger", &plugin::logger::LoggerRequest {
level: String::from("Debug"),
message: String::from("list files called"),
time: String::from("now"),
Expand Down Expand Up @@ -106,21 +106,18 @@ impl client::rpc::server::Rpc for ListFiles {
}

pub struct ListFilesPlugin {
client: client::Client,
_client: client::Client,
}

impl ListFilesPlugin {
pub fn new(socket_name: &path::Path) -> Result<Self> {
let client = try!(client::Client::connect_unix(socket_name));
let mut client = try!(client::Client::connect_unix(socket_name));
let thin_client = Arc::new(RwLock::new(try!(client.clone())));

let mut plugin = ListFilesPlugin {
client: client,
};

let list_files = Box::new(ListFiles {client: thin_client.clone()});
try!(plugin.client.new_rpc("list_files", list_files));

Ok(plugin)
try!(plugin::register_rpc(&mut client, rpc_map! {
"list_files" => ListFiles { client: thin_client.clone() },
}));
Ok(ListFilesPlugin {
_client: client,
})
}
}
17 changes: 9 additions & 8 deletions src/plugin_logger.rs → src/plugin/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use ::client;
use ::error::Result;
use ::rpc;
use ::plugin;
use serde_json;
use std::convert;
use std::path;
Expand All @@ -30,17 +31,17 @@ impl client::rpc::server::Rpc for Logger {
}

pub struct LoggerPlugin {
client: client::Client,
_client: client::Client,
}

impl LoggerPlugin {
pub fn new(socket_name: &path::Path) -> Result<Self> {
let client = try!(client::Client::connect_unix(socket_name));
let mut plugin = LoggerPlugin {
client: client,
};
let logger = Box::new(Logger);
try!(plugin.client.new_rpc("logger", logger));
Ok(plugin)
let mut client = try!(client::Client::connect_unix(socket_name));
try!(plugin::register_rpc(&mut client, rpc_map! {
"logger" => Logger,
}));
Ok(LoggerPlugin{
_client: client,
})
}
}
30 changes: 30 additions & 0 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) The Swiboe development team. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE.txt
// in the project root for license information.
use ::error::Result;
use ::client;
use std::collections::HashMap;

pub type RpcMap = HashMap<String, Box<client::rpc::server::Rpc>>;

macro_rules! rpc_map {
($($s:expr => $rpc:expr),*) => {{
let mut map = $crate::plugin::RpcMap::new();
$(map.insert(String::from($s), Box::new($rpc));)*
map
}};
($($s:expr => $rpc:expr),*,) => {
rpc_map!($($s => $rpc), *)
};
}

pub fn register_rpc(client: &mut client::Client, map: RpcMap) -> Result<()> {
for (name, rpc) in map {
try!(client.new_rpc(&name, rpc));
}
Ok(())
}

pub mod buffer;
pub mod list_files;
pub mod logger;
16 changes: 7 additions & 9 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// in the project root for license information.

use ::error::Result;
use ::plugin_buffer;
use ::plugin_list_files;
use ::plugin_logger;
use ::plugin;
use mio;
use std::fs;
use std::path::{Path, PathBuf};
Expand All @@ -23,9 +21,9 @@ pub struct Server {
ipc_bridge_commands: mio::Sender<ipc_bridge::Command>,
swiboe_thread: Option<thread::JoinHandle<()>>,
event_loop_thread: Option<thread::JoinHandle<()>>,
buffer_plugin: Option<plugin_buffer::BufferPlugin>,
list_files_plugin: Option<plugin_list_files::ListFilesPlugin>,
logger_plugin: Option<plugin_logger::LoggerPlugin>,
buffer_plugin: Option<plugin::buffer::BufferPlugin>,
list_files_plugin: Option<plugin::list_files::ListFilesPlugin>,
logger_plugin: Option<plugin::logger::LoggerPlugin>,
}

impl Server {
Expand Down Expand Up @@ -57,11 +55,11 @@ impl Server {
}));

server.buffer_plugin = Some(
try!(plugin_buffer::BufferPlugin::new(&server.unix_domain_socket_name)));
try!(plugin::buffer::BufferPlugin::new(&server.unix_domain_socket_name)));
server.list_files_plugin = Some(
try!(plugin_list_files::ListFilesPlugin::new(&server.unix_domain_socket_name)));
try!(plugin::list_files::ListFilesPlugin::new(&server.unix_domain_socket_name)));
server.logger_plugin = Some(
try!(plugin_logger::LoggerPlugin::new(&server.unix_domain_socket_name)));
try!(plugin::logger::LoggerPlugin::new(&server.unix_domain_socket_name)));
Ok(server)
}

Expand Down
1 change: 0 additions & 1 deletion src/server/swiboe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ impl spinner::Handler<Command> for Handler {
// NOCOM(#sirver): function name might not be in there.

// Special case 'core.'. We handle them immediately.
println!("{}", rpc_call.function);
if rpc_call.function.starts_with(CORE_FUNCTIONS_PREFIX) {
let result = self.plugin_core.call(client_id, &rpc_call);
try!(self.ipc_bridge_commands.send(ipc_bridge::Command::SendData(
Expand Down
8 changes: 4 additions & 4 deletions term_gui/src/bin/term_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl CompleterWidget {
// directory.
let current_dir = env::current_dir().unwrap();

let rpc = try!(client.call("list_files", &swiboe::plugin_list_files::ListFilesRequest {
let rpc = try!(client.call("list_files", &swiboe::plugin::list_files::ListFilesRequest {
directory: current_dir.to_string_lossy().into_owned(),
}));

Expand Down Expand Up @@ -105,7 +105,7 @@ impl CompleterWidget {
fn draw(&mut self, rustbox: &rustbox::RustBox) {
while let Some(b) = self.rpc.as_mut().unwrap().try_recv().unwrap() {
self.results.clear();
let b: swiboe::plugin_list_files::ListFilesUpdate = serde_json::from_value(b).unwrap();
let b: swiboe::plugin::list_files::ListFilesUpdate = serde_json::from_value(b).unwrap();
for file in &b.files {
self.candidates.insert(file);
}
Expand Down Expand Up @@ -279,10 +279,10 @@ impl TerminalGui {
CompleterState::Selected(result) => {
self.completer = None;

let mut rpc = try!(self.client.call("buffer.open", &swiboe::plugin_buffer::OpenRequest {
let mut rpc = try!(self.client.call("buffer.open", &swiboe::plugin::buffer::OpenRequest {
uri: format!("file://{}", result),
}));
let response: swiboe::plugin_buffer::OpenResponse = rpc.wait_for().unwrap();
let response: swiboe::plugin::buffer::OpenResponse = rpc.wait_for().unwrap();

let mut buffer_views = self.buffer_views.write().unwrap();
let view_id = buffer_views.new_view(response.buffer_index, self.rustbox.width(), self.rustbox.height());
Expand Down

0 comments on commit 4ec3dd4

Please sign in to comment.