2018-08-12 14:08:14 -05:00
|
|
|
mod handlers;
|
2018-08-30 08:27:09 -05:00
|
|
|
mod subscriptions;
|
2018-08-12 14:08:14 -05:00
|
|
|
|
2018-08-13 05:46:05 -05:00
|
|
|
use std::{
|
2018-09-01 10:03:57 -05:00
|
|
|
path::PathBuf,
|
2018-08-31 04:04:33 -05:00
|
|
|
collections::{HashMap},
|
2018-08-13 05:46:05 -05:00
|
|
|
};
|
2018-08-12 16:09:30 -05:00
|
|
|
|
2018-09-01 09:40:45 -05:00
|
|
|
use serde::{Serialize, de::DeserializeOwned};
|
2018-09-03 20:13:22 -05:00
|
|
|
use crossbeam_channel::{unbounded, Sender, Receiver};
|
2018-09-04 12:43:37 -05:00
|
|
|
use rayon::{self, ThreadPool};
|
2018-08-31 04:04:33 -05:00
|
|
|
use languageserver_types::{NumberOrString};
|
2018-09-16 04:54:24 -05:00
|
|
|
use ra_analysis::{FileId, JobHandle, JobToken, LibraryData};
|
2018-09-01 12:21:11 -05:00
|
|
|
use gen_lsp_server::{
|
|
|
|
RawRequest, RawNotification, RawMessage, RawResponse, ErrorCode,
|
|
|
|
handle_shutdown,
|
|
|
|
};
|
2018-08-12 16:09:30 -05:00
|
|
|
|
2018-08-12 14:08:14 -05:00
|
|
|
use {
|
2018-09-01 09:40:45 -05:00
|
|
|
req,
|
|
|
|
Result,
|
|
|
|
vfs::{self, FileEvent},
|
2018-08-17 11:54:08 -05:00
|
|
|
server_world::{ServerWorldState, ServerWorld},
|
2018-08-30 08:27:09 -05:00
|
|
|
main_loop::subscriptions::{Subscriptions},
|
2018-09-02 06:46:15 -05:00
|
|
|
project_model::{CargoWorkspace, workspace_loader},
|
2018-09-08 05:15:01 -05:00
|
|
|
thread_watcher::Worker,
|
2018-08-12 14:08:14 -05:00
|
|
|
};
|
|
|
|
|
2018-09-01 12:21:11 -05:00
|
|
|
#[derive(Debug)]
|
2018-09-01 09:40:45 -05:00
|
|
|
enum Task {
|
|
|
|
Respond(RawResponse),
|
|
|
|
Notify(RawNotification),
|
|
|
|
}
|
|
|
|
|
2018-09-01 10:16:08 -05:00
|
|
|
pub fn main_loop(
|
2018-09-03 15:32:42 -05:00
|
|
|
internal_mode: bool,
|
2018-09-01 10:03:57 -05:00
|
|
|
root: PathBuf,
|
|
|
|
msg_receriver: &mut Receiver<RawMessage>,
|
|
|
|
msg_sender: &mut Sender<RawMessage>,
|
2018-08-12 14:08:14 -05:00
|
|
|
) -> Result<()> {
|
2018-09-04 12:43:37 -05:00
|
|
|
let pool = rayon::ThreadPoolBuilder::new()
|
2018-09-08 01:18:42 -05:00
|
|
|
.num_threads(4)
|
|
|
|
.panic_handler(|_| error!("thread panicked :("))
|
|
|
|
.build()
|
2018-09-04 12:43:37 -05:00
|
|
|
.unwrap();
|
2018-09-03 20:13:22 -05:00
|
|
|
let (task_sender, task_receiver) = unbounded::<Task>();
|
2018-09-08 05:15:01 -05:00
|
|
|
let (fs_worker, fs_watcher) = vfs::roots_loader();
|
|
|
|
let (ws_worker, ws_watcher) = workspace_loader();
|
2018-09-01 09:40:45 -05:00
|
|
|
|
2018-08-12 14:08:14 -05:00
|
|
|
info!("server initialized, serving requests");
|
2018-08-17 11:54:08 -05:00
|
|
|
let mut state = ServerWorldState::new();
|
|
|
|
|
2018-09-01 09:40:45 -05:00
|
|
|
let mut pending_requests = HashMap::new();
|
2018-08-30 08:27:09 -05:00
|
|
|
let mut subs = Subscriptions::new();
|
2018-09-02 06:46:15 -05:00
|
|
|
let main_res = main_loop_inner(
|
2018-09-03 15:32:42 -05:00
|
|
|
internal_mode,
|
2018-09-03 13:03:37 -05:00
|
|
|
root,
|
2018-09-01 09:40:45 -05:00
|
|
|
&pool,
|
2018-09-01 10:03:57 -05:00
|
|
|
msg_sender,
|
2018-09-03 13:03:37 -05:00
|
|
|
msg_receriver,
|
2018-09-01 09:40:45 -05:00
|
|
|
task_sender,
|
2018-09-03 13:03:37 -05:00
|
|
|
task_receiver.clone(),
|
2018-09-08 05:15:01 -05:00
|
|
|
fs_worker,
|
|
|
|
ws_worker,
|
2018-09-01 09:40:45 -05:00
|
|
|
&mut state,
|
|
|
|
&mut pending_requests,
|
|
|
|
&mut subs,
|
2018-09-01 12:21:11 -05:00
|
|
|
);
|
2018-09-01 09:40:45 -05:00
|
|
|
|
2018-09-01 12:21:11 -05:00
|
|
|
info!("waiting for tasks to finish...");
|
2018-09-01 10:03:57 -05:00
|
|
|
task_receiver.for_each(|task| on_task(task, msg_sender, &mut pending_requests));
|
2018-09-01 12:21:11 -05:00
|
|
|
info!("...tasks have finished");
|
|
|
|
info!("joining threadpool...");
|
2018-09-04 12:43:37 -05:00
|
|
|
drop(pool);
|
2018-09-01 12:21:11 -05:00
|
|
|
info!("...threadpool has finished");
|
2018-09-01 09:40:45 -05:00
|
|
|
|
2018-09-03 13:03:37 -05:00
|
|
|
let fs_res = fs_watcher.stop();
|
2018-09-02 06:46:15 -05:00
|
|
|
let ws_res = ws_watcher.stop();
|
|
|
|
|
|
|
|
main_res?;
|
2018-09-03 13:03:37 -05:00
|
|
|
fs_res?;
|
2018-09-02 06:46:15 -05:00
|
|
|
ws_res?;
|
|
|
|
|
|
|
|
Ok(())
|
2018-09-01 09:40:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main_loop_inner(
|
2018-09-03 15:32:42 -05:00
|
|
|
internal_mode: bool,
|
2018-09-03 13:03:37 -05:00
|
|
|
ws_root: PathBuf,
|
2018-09-01 09:40:45 -05:00
|
|
|
pool: &ThreadPool,
|
|
|
|
msg_sender: &mut Sender<RawMessage>,
|
2018-09-03 13:03:37 -05:00
|
|
|
msg_receiver: &mut Receiver<RawMessage>,
|
2018-09-01 09:40:45 -05:00
|
|
|
task_sender: Sender<Task>,
|
2018-09-03 13:03:37 -05:00
|
|
|
task_receiver: Receiver<Task>,
|
2018-09-08 05:15:01 -05:00
|
|
|
fs_worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
|
|
|
|
ws_worker: Worker<PathBuf, Result<CargoWorkspace>>,
|
2018-09-01 09:40:45 -05:00
|
|
|
state: &mut ServerWorldState,
|
|
|
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
|
|
|
subs: &mut Subscriptions,
|
2018-09-01 12:21:11 -05:00
|
|
|
) -> Result<()> {
|
2018-09-03 20:13:22 -05:00
|
|
|
let (libdata_sender, libdata_receiver) = unbounded();
|
2018-09-08 05:15:01 -05:00
|
|
|
ws_worker.send(ws_root.clone());
|
|
|
|
fs_worker.send(ws_root.clone());
|
2018-08-12 14:08:14 -05:00
|
|
|
loop {
|
2018-09-01 12:21:11 -05:00
|
|
|
#[derive(Debug)]
|
2018-08-12 14:08:14 -05:00
|
|
|
enum Event {
|
2018-09-01 09:40:45 -05:00
|
|
|
Msg(RawMessage),
|
2018-08-12 14:08:14 -05:00
|
|
|
Task(Task),
|
2018-09-03 13:03:37 -05:00
|
|
|
Fs(PathBuf, Vec<FileEvent>),
|
2018-09-02 06:46:15 -05:00
|
|
|
Ws(Result<CargoWorkspace>),
|
2018-09-03 13:26:59 -05:00
|
|
|
Lib(LibraryData),
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
2018-09-01 12:21:11 -05:00
|
|
|
trace!("selecting");
|
2018-08-12 14:08:14 -05:00
|
|
|
let event = select! {
|
2018-09-01 09:40:45 -05:00
|
|
|
recv(msg_receiver, msg) => match msg {
|
2018-08-12 14:08:14 -05:00
|
|
|
Some(msg) => Event::Msg(msg),
|
2018-09-01 09:40:45 -05:00
|
|
|
None => bail!("client exited without shutdown"),
|
2018-08-12 14:08:14 -05:00
|
|
|
},
|
2018-08-13 05:46:05 -05:00
|
|
|
recv(task_receiver, task) => Event::Task(task.unwrap()),
|
2018-09-08 05:15:01 -05:00
|
|
|
recv(fs_worker.out, events) => match events {
|
2018-09-03 13:03:37 -05:00
|
|
|
None => bail!("roots watcher died"),
|
|
|
|
Some((pb, events)) => Event::Fs(pb, events),
|
2018-08-13 05:46:05 -05:00
|
|
|
}
|
2018-09-08 05:15:01 -05:00
|
|
|
recv(ws_worker.out, ws) => match ws {
|
2018-09-02 06:46:15 -05:00
|
|
|
None => bail!("workspace watcher died"),
|
|
|
|
Some(ws) => Event::Ws(ws),
|
|
|
|
}
|
2018-09-03 13:26:59 -05:00
|
|
|
recv(libdata_receiver, data) => Event::Lib(data.unwrap())
|
2018-08-12 14:08:14 -05:00
|
|
|
};
|
2018-08-30 08:27:09 -05:00
|
|
|
let mut state_changed = false;
|
2018-08-12 14:08:14 -05:00
|
|
|
match event {
|
2018-09-01 10:03:57 -05:00
|
|
|
Event::Task(task) => on_task(task, msg_sender, pending_requests),
|
2018-09-03 13:03:37 -05:00
|
|
|
Event::Fs(root, events) => {
|
|
|
|
info!("fs change, {}, {} events", root.display(), events.len());
|
|
|
|
if root == ws_root {
|
|
|
|
state.apply_fs_changes(events);
|
|
|
|
} else {
|
2018-09-10 04:57:40 -05:00
|
|
|
let (files, resolver) = state.events_to_files(events);
|
2018-09-03 13:26:59 -05:00
|
|
|
let sender = libdata_sender.clone();
|
2018-09-04 12:43:37 -05:00
|
|
|
pool.spawn(move || {
|
2018-09-03 20:13:22 -05:00
|
|
|
let start = ::std::time::Instant::now();
|
|
|
|
info!("indexing {} ... ", root.display());
|
2018-09-10 04:57:40 -05:00
|
|
|
let data = LibraryData::prepare(files, resolver);
|
2018-09-03 20:13:22 -05:00
|
|
|
info!("indexed {:?} {}", start.elapsed(), root.display());
|
2018-09-03 13:26:59 -05:00
|
|
|
sender.send(data);
|
|
|
|
});
|
2018-09-03 13:03:37 -05:00
|
|
|
}
|
2018-08-30 08:27:09 -05:00
|
|
|
state_changed = true;
|
2018-08-13 05:46:05 -05:00
|
|
|
}
|
2018-09-02 06:46:15 -05:00
|
|
|
Event::Ws(ws) => {
|
|
|
|
match ws {
|
|
|
|
Ok(ws) => {
|
2018-09-03 13:03:37 -05:00
|
|
|
let workspaces = vec![ws];
|
2018-09-03 15:32:42 -05:00
|
|
|
feedback(internal_mode, "workspace loaded", msg_sender);
|
2018-09-03 13:03:37 -05:00
|
|
|
for ws in workspaces.iter() {
|
|
|
|
for pkg in ws.packages().filter(|pkg| !pkg.is_member(ws)) {
|
|
|
|
debug!("sending root, {}", pkg.root(ws).to_path_buf().display());
|
2018-09-08 05:15:01 -05:00
|
|
|
fs_worker.send(pkg.root(ws).to_path_buf());
|
2018-09-03 13:03:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state.set_workspaces(workspaces);
|
2018-09-02 06:46:15 -05:00
|
|
|
state_changed = true;
|
|
|
|
}
|
|
|
|
Err(e) => warn!("loading workspace failed: {}", e),
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 13:26:59 -05:00
|
|
|
Event::Lib(lib) => {
|
2018-09-03 16:49:21 -05:00
|
|
|
feedback(internal_mode, "library loaded", msg_sender);
|
2018-09-03 13:26:59 -05:00
|
|
|
state.add_lib(lib);
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
Event::Msg(msg) => {
|
2018-08-12 16:09:30 -05:00
|
|
|
match msg {
|
2018-09-01 09:40:45 -05:00
|
|
|
RawMessage::Request(req) => {
|
2018-09-01 12:21:11 -05:00
|
|
|
let req = match handle_shutdown(req, msg_sender) {
|
|
|
|
Some(req) => req,
|
|
|
|
None => return Ok(()),
|
2018-09-01 09:40:45 -05:00
|
|
|
};
|
|
|
|
match on_request(state, pending_requests, pool, &task_sender, req)? {
|
|
|
|
None => (),
|
|
|
|
Some(req) => {
|
|
|
|
error!("unknown request: {:?}", req);
|
|
|
|
let resp = RawResponse::err(
|
|
|
|
req.id,
|
|
|
|
ErrorCode::MethodNotFound as i32,
|
|
|
|
"unknown request".to_string(),
|
|
|
|
);
|
|
|
|
msg_sender.send(RawMessage::Response(resp))
|
|
|
|
}
|
2018-08-12 16:09:30 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-01 09:40:45 -05:00
|
|
|
RawMessage::Notification(not) => {
|
|
|
|
on_notification(msg_sender, state, pending_requests, subs, not)?;
|
2018-08-30 08:27:09 -05:00
|
|
|
state_changed = true;
|
2018-08-12 16:09:30 -05:00
|
|
|
}
|
2018-09-01 09:40:45 -05:00
|
|
|
RawMessage::Response(resp) => {
|
2018-08-31 04:04:33 -05:00
|
|
|
error!("unexpected response: {:?}", resp)
|
2018-08-12 16:09:30 -05:00
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-08-30 08:27:09 -05:00
|
|
|
|
|
|
|
if state_changed {
|
|
|
|
update_file_notifications_on_threadpool(
|
|
|
|
pool,
|
|
|
|
state.snapshot(),
|
|
|
|
task_sender.clone(),
|
|
|
|
subs.subscriptions(),
|
|
|
|
)
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 10:03:57 -05:00
|
|
|
fn on_task(
|
|
|
|
task: Task,
|
|
|
|
msg_sender: &mut Sender<RawMessage>,
|
|
|
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
|
|
|
) {
|
|
|
|
match task {
|
|
|
|
Task::Respond(response) => {
|
|
|
|
if let Some(handle) = pending_requests.remove(&response.id) {
|
|
|
|
assert!(handle.has_completed());
|
|
|
|
}
|
|
|
|
msg_sender.send(RawMessage::Response(response))
|
|
|
|
}
|
|
|
|
Task::Notify(n) =>
|
|
|
|
msg_sender.send(RawMessage::Notification(n)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 16:09:30 -05:00
|
|
|
fn on_request(
|
2018-08-21 10:30:10 -05:00
|
|
|
world: &mut ServerWorldState,
|
2018-08-31 04:04:33 -05:00
|
|
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
2018-08-12 16:09:30 -05:00
|
|
|
pool: &ThreadPool,
|
2018-08-12 14:08:14 -05:00
|
|
|
sender: &Sender<Task>,
|
2018-08-12 16:09:30 -05:00
|
|
|
req: RawRequest,
|
2018-09-01 09:40:45 -05:00
|
|
|
) -> Result<Option<RawRequest>> {
|
2018-08-29 10:03:14 -05:00
|
|
|
let mut pool_dispatcher = PoolDispatcher {
|
|
|
|
req: Some(req),
|
2018-08-31 04:04:33 -05:00
|
|
|
res: None,
|
2018-08-29 10:03:14 -05:00
|
|
|
pool, world, sender
|
|
|
|
};
|
2018-08-31 04:04:33 -05:00
|
|
|
let req = pool_dispatcher
|
2018-08-29 10:03:14 -05:00
|
|
|
.on::<req::SyntaxTree>(handlers::handle_syntax_tree)?
|
|
|
|
.on::<req::ExtendSelection>(handlers::handle_extend_selection)?
|
|
|
|
.on::<req::FindMatchingBrace>(handlers::handle_find_matching_brace)?
|
|
|
|
.on::<req::JoinLines>(handlers::handle_join_lines)?
|
|
|
|
.on::<req::OnTypeFormatting>(handlers::handle_on_type_formatting)?
|
|
|
|
.on::<req::DocumentSymbolRequest>(handlers::handle_document_symbol)?
|
|
|
|
.on::<req::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
|
|
|
|
.on::<req::GotoDefinition>(handlers::handle_goto_definition)?
|
|
|
|
.on::<req::ParentModule>(handlers::handle_parent_module)?
|
|
|
|
.on::<req::Runnables>(handlers::handle_runnables)?
|
|
|
|
.on::<req::DecorationsRequest>(handlers::handle_decorations)?
|
|
|
|
.on::<req::Completion>(handlers::handle_completion)?
|
2018-08-31 04:04:33 -05:00
|
|
|
.on::<req::CodeActionRequest>(handlers::handle_code_action)?
|
2018-09-23 10:13:27 -05:00
|
|
|
.on::<req::FoldingRangeRequest>(handlers::handle_folding_range)?
|
2018-08-31 04:04:33 -05:00
|
|
|
.finish();
|
|
|
|
match req {
|
|
|
|
Ok((id, handle)) => {
|
|
|
|
let inserted = pending_requests.insert(id, handle).is_none();
|
|
|
|
assert!(inserted, "duplicate request: {}", id);
|
2018-09-01 09:40:45 -05:00
|
|
|
Ok(None)
|
2018-08-31 04:04:33 -05:00
|
|
|
},
|
2018-09-01 09:40:45 -05:00
|
|
|
Err(req) => Ok(Some(req)),
|
2018-08-12 16:09:30 -05:00
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
|
2018-08-12 16:09:30 -05:00
|
|
|
fn on_notification(
|
2018-09-01 09:40:45 -05:00
|
|
|
msg_sender: &mut Sender<RawMessage>,
|
2018-08-17 11:54:08 -05:00
|
|
|
state: &mut ServerWorldState,
|
2018-08-31 04:04:33 -05:00
|
|
|
pending_requests: &mut HashMap<u64, JobHandle>,
|
2018-08-30 08:27:09 -05:00
|
|
|
subs: &mut Subscriptions,
|
2018-08-12 16:09:30 -05:00
|
|
|
not: RawNotification,
|
|
|
|
) -> Result<()> {
|
2018-09-01 09:40:45 -05:00
|
|
|
let not = match not.cast::<req::Cancel>() {
|
|
|
|
Ok(params) => {
|
|
|
|
let id = match params.id {
|
|
|
|
NumberOrString::Number(id) => id,
|
|
|
|
NumberOrString::String(id) => {
|
|
|
|
panic!("string id's not supported: {:?}", id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(handle) = pending_requests.remove(&id) {
|
|
|
|
handle.cancel();
|
2018-08-31 04:04:33 -05:00
|
|
|
}
|
2018-09-01 09:40:45 -05:00
|
|
|
return Ok(())
|
2018-08-31 04:04:33 -05:00
|
|
|
}
|
2018-09-01 09:40:45 -05:00
|
|
|
Err(not) => not,
|
|
|
|
};
|
|
|
|
let not = match not.cast::<req::DidOpenTextDocument>() {
|
|
|
|
Ok(params) => {
|
|
|
|
let uri = params.text_document.uri;
|
|
|
|
let path = uri.to_file_path()
|
|
|
|
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
|
|
|
let file_id = state.add_mem_file(path, params.text_document.text);
|
|
|
|
subs.add_sub(file_id);
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
Err(not) => not,
|
|
|
|
};
|
|
|
|
let not = match not.cast::<req::DidChangeTextDocument>() {
|
|
|
|
Ok(mut params) => {
|
|
|
|
let uri = params.text_document.uri;
|
|
|
|
let path = uri.to_file_path()
|
|
|
|
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
|
|
|
let text = params.content_changes.pop()
|
|
|
|
.ok_or_else(|| format_err!("empty changes"))?
|
|
|
|
.text;
|
|
|
|
state.change_mem_file(path.as_path(), text)?;
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
Err(not) => not,
|
|
|
|
};
|
|
|
|
let not = match not.cast::<req::DidCloseTextDocument>() {
|
|
|
|
Ok(params) => {
|
|
|
|
let uri = params.text_document.uri;
|
|
|
|
let path = uri.to_file_path()
|
|
|
|
.map_err(|()| format_err!("invalid uri: {}", uri))?;
|
|
|
|
let file_id = state.remove_mem_file(path.as_path())?;
|
|
|
|
subs.remove_sub(file_id);
|
|
|
|
let params = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() };
|
2018-09-02 07:18:43 -05:00
|
|
|
let not = RawNotification::new::<req::PublishDiagnostics>(¶ms);
|
2018-09-01 09:40:45 -05:00
|
|
|
msg_sender.send(RawMessage::Notification(not));
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
Err(not) => not,
|
|
|
|
};
|
|
|
|
error!("unhandled notification: {:?}", not);
|
2018-08-12 16:09:30 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-29 10:03:14 -05:00
|
|
|
struct PoolDispatcher<'a> {
|
|
|
|
req: Option<RawRequest>,
|
2018-08-31 04:04:33 -05:00
|
|
|
res: Option<(u64, JobHandle)>,
|
2018-08-29 10:03:14 -05:00
|
|
|
pool: &'a ThreadPool,
|
|
|
|
world: &'a ServerWorldState,
|
|
|
|
sender: &'a Sender<Task>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PoolDispatcher<'a> {
|
2018-09-01 09:40:45 -05:00
|
|
|
fn on<'b, R>(
|
2018-08-31 04:04:33 -05:00
|
|
|
&'b mut self,
|
|
|
|
f: fn(ServerWorld, R::Params, JobToken) -> Result<R::Result>
|
2018-09-01 09:40:45 -05:00
|
|
|
) -> Result<&'b mut Self>
|
|
|
|
where R: req::Request,
|
|
|
|
R::Params: DeserializeOwned + Send + 'static,
|
|
|
|
R::Result: Serialize + 'static,
|
|
|
|
{
|
2018-08-31 04:04:33 -05:00
|
|
|
let req = match self.req.take() {
|
|
|
|
None => return Ok(self),
|
|
|
|
Some(req) => req,
|
|
|
|
};
|
2018-09-01 09:40:45 -05:00
|
|
|
match req.cast::<R>() {
|
|
|
|
Ok((id, params)) => {
|
|
|
|
let (handle, token) = JobHandle::new();
|
2018-09-01 10:03:57 -05:00
|
|
|
let world = self.world.snapshot();
|
|
|
|
let sender = self.sender.clone();
|
2018-09-04 12:43:37 -05:00
|
|
|
self.pool.spawn(move || {
|
2018-09-01 09:40:45 -05:00
|
|
|
let resp = match f(world, params, token) {
|
2018-09-02 07:18:43 -05:00
|
|
|
Ok(resp) => RawResponse::ok::<R>(id, &resp),
|
2018-09-01 09:40:45 -05:00
|
|
|
Err(e) => RawResponse::err(id, ErrorCode::InternalError as i32, e.to_string()),
|
|
|
|
};
|
|
|
|
let task = Task::Respond(resp);
|
|
|
|
sender.send(task);
|
|
|
|
});
|
|
|
|
self.res = Some((id, handle));
|
|
|
|
}
|
|
|
|
Err(req) => {
|
|
|
|
self.req = Some(req)
|
|
|
|
}
|
2018-08-31 04:04:33 -05:00
|
|
|
}
|
2018-08-29 10:03:14 -05:00
|
|
|
Ok(self)
|
|
|
|
}
|
2018-08-31 04:04:33 -05:00
|
|
|
|
|
|
|
fn finish(&mut self) -> ::std::result::Result<(u64, JobHandle), RawRequest> {
|
|
|
|
match (self.res.take(), self.req.take()) {
|
|
|
|
(Some(res), None) => Ok(res),
|
|
|
|
(None, Some(req)) => Err(req),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_file_notifications_on_threadpool(
|
|
|
|
pool: &ThreadPool,
|
2018-08-17 11:54:08 -05:00
|
|
|
world: ServerWorld,
|
2018-08-12 14:08:14 -05:00
|
|
|
sender: Sender<Task>,
|
2018-08-30 08:27:09 -05:00
|
|
|
subscriptions: Vec<FileId>,
|
2018-08-12 14:08:14 -05:00
|
|
|
) {
|
2018-09-04 12:43:37 -05:00
|
|
|
pool.spawn(move || {
|
2018-08-30 08:27:09 -05:00
|
|
|
for file_id in subscriptions {
|
|
|
|
match handlers::publish_diagnostics(world.clone(), file_id) {
|
|
|
|
Err(e) => {
|
|
|
|
error!("failed to compute diagnostics: {:?}", e)
|
|
|
|
}
|
|
|
|
Ok(params) => {
|
2018-09-02 07:18:43 -05:00
|
|
|
let not = RawNotification::new::<req::PublishDiagnostics>(¶ms);
|
2018-08-30 08:27:09 -05:00
|
|
|
sender.send(Task::Notify(not));
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
2018-08-30 08:27:09 -05:00
|
|
|
match handlers::publish_decorations(world.clone(), file_id) {
|
|
|
|
Err(e) => {
|
|
|
|
error!("failed to compute decorations: {:?}", e)
|
|
|
|
}
|
|
|
|
Ok(params) => {
|
2018-09-02 07:18:43 -05:00
|
|
|
let not = RawNotification::new::<req::PublishDecorations>(¶ms);
|
2018-08-30 08:27:09 -05:00
|
|
|
sender.send(Task::Notify(not))
|
|
|
|
}
|
2018-08-12 14:08:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-03 15:32:42 -05:00
|
|
|
|
|
|
|
fn feedback(intrnal_mode: bool, msg: &str, sender: &Sender<RawMessage>) {
|
|
|
|
if !intrnal_mode {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let not = RawNotification::new::<req::InternalFeedback>(&msg.to_string());
|
|
|
|
sender.send(RawMessage::Notification(not));
|
|
|
|
}
|