mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-21 23:04:49 +01:00
chore: removed rest of the project
This commit is contained in:
parent
45a5667e5a
commit
cca36db3ed
15 changed files with 0 additions and 1351 deletions
|
@ -1,16 +0,0 @@
|
|||
[package]
|
||||
name = "codemp-nvim"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
codemp = { path = "../.." }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
uuid = { version = "1.3.1", features = ["v4"] }
|
||||
serde = "1"
|
||||
serde_json = "1"
|
||||
rmpv = "1"
|
||||
clap = { version = "4.2.1", features = ["derive"] }
|
||||
nvim-rs = { version = "0.5", features = ["use_tokio"] }
|
||||
async-trait = "0.1.68"
|
|
@ -1,190 +0,0 @@
|
|||
local BINARY = vim.g.codemp_binary or "./codemp-client-nvim"
|
||||
|
||||
local M = {}
|
||||
|
||||
M.jobid = nil
|
||||
M.create = function(path, content) return vim.rpcrequest(M.jobid, "create", path, content) end
|
||||
M.insert = function(path, txt, pos) return vim.rpcrequest(M.jobid, "insert", path, txt, pos) end
|
||||
M.delete = function(path, pos, count) return vim.rpcrequest(M.jobid, "delete", path, pos, count) end
|
||||
M.replace = function(path, txt) return vim.rpcrequest(M.jobid, "replace", path, txt) end
|
||||
M.cursor = function(path, cur) return vim.rpcrequest(M.jobid, "cursor", path, cur[1][1], cur[1][2], cur[2][1], cur[2][2]) end
|
||||
M.attach = function(path) return vim.rpcrequest(M.jobid, "attach", path) end
|
||||
M.listen = function(path) return vim.rpcrequest(M.jobid, "listen", path) end
|
||||
M.detach = function(path) return vim.rpcrequest(M.jobid, "detach", path) end
|
||||
|
||||
local function cursor_offset()
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
return vim.fn.line2byte(cursor[1]) + cursor[2] - 1
|
||||
end
|
||||
|
||||
local codemp_autocmds = vim.api.nvim_create_augroup("CodempAuGroup", { clear = true })
|
||||
|
||||
local function get_cursor_range()
|
||||
local mode = vim.fn.mode()
|
||||
if mode == "" or mode == "s" or mode == "Vs" or mode == "V" or mode == "vs" or mode == "v" then
|
||||
local start = vim.fn.getpos("'<")
|
||||
local finish = vim.fn.getpos("'>")
|
||||
return {
|
||||
{ start[2], start[3] },
|
||||
{ finish[2], finish[3] }
|
||||
}
|
||||
else
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
return {
|
||||
{ cursor[1], cursor[2] },
|
||||
{ cursor[1], cursor[2] + 1 },
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
local function hook_callbacks(path, buffer)
|
||||
vim.api.nvim_create_autocmd(
|
||||
{ "InsertCharPre" },
|
||||
{
|
||||
callback = function(_)
|
||||
pcall(M.insert, path, vim.v.char, cursor_offset()) -- TODO log errors
|
||||
end,
|
||||
buffer = buffer,
|
||||
group = codemp_autocmds,
|
||||
}
|
||||
)
|
||||
vim.api.nvim_create_autocmd(
|
||||
{ "CursorMoved", "CompleteDone", "InsertEnter", "InsertLeave" },
|
||||
{
|
||||
callback = function(args)
|
||||
local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false)
|
||||
pcall(M.replace, path, vim.fn.join(lines, "\n")) -- TODO log errors
|
||||
pcall(M.cursor, path, get_cursor_range()) -- TODO log errors
|
||||
end,
|
||||
buffer = buffer,
|
||||
group = codemp_autocmds,
|
||||
}
|
||||
)
|
||||
local last_line = 0
|
||||
vim.api.nvim_create_autocmd(
|
||||
{ "CursorMovedI" },
|
||||
{
|
||||
callback = function(args)
|
||||
local cursor = get_cursor_range()
|
||||
pcall(M.cursor, path, cursor) -- TODO log errors
|
||||
if cursor[1][1] == last_line then
|
||||
return
|
||||
end
|
||||
last_line = cursor[1][1]
|
||||
local lines = vim.api.nvim_buf_get_lines(args.buf, 0, -1, false)
|
||||
pcall(M.replace, path, vim.fn.join(lines, "\n")) -- TODO log errors
|
||||
end,
|
||||
buffer = buffer,
|
||||
group = codemp_autocmds,
|
||||
}
|
||||
)
|
||||
vim.keymap.set('i', '<BS>', function() pcall(M.delete, path, cursor_offset(), 1) return '<BS>' end, {expr = true, buffer = buffer}) -- TODO log errors
|
||||
vim.keymap.set('i', '<Del>', function() pcall(M.delete, path, cursor_offset() + 1, 1) return '<Del>' end, {expr = true, buffer = buffer}) -- TODO log errors
|
||||
end
|
||||
|
||||
local function unhook_callbacks(buffer)
|
||||
vim.api.nvim_clear_autocmds({ group = codemp_autocmds, buffer = buffer })
|
||||
vim.keymap.del('i', '<BS>', { buffer = buffer })
|
||||
vim.keymap.del('i', '<Del>', { buffer = buffer })
|
||||
end
|
||||
|
||||
local function auto_address(addr)
|
||||
if not string.find(addr, "://") then
|
||||
addr = string.format("http://%s", addr)
|
||||
end
|
||||
if not string.find(addr, ":", 7) then -- skip first 7 chars because 'https://'
|
||||
addr = string.format("%s:50051", addr)
|
||||
end
|
||||
return addr
|
||||
end
|
||||
|
||||
vim.api.nvim_create_user_command('Connect',
|
||||
function(args)
|
||||
if M.jobid ~= nil and M.jobid > 0 then
|
||||
print("already connected, disconnect first")
|
||||
return
|
||||
end
|
||||
local bin_args = { BINARY }
|
||||
if #args.fargs > 0 then
|
||||
table.insert(bin_args, "--host")
|
||||
table.insert(bin_args, auto_address(args.fargs[1]))
|
||||
end
|
||||
if vim.g.codemp_remote_debug then
|
||||
table.insert(bin_args, "--remote-debug")
|
||||
table.insert(bin_args, vim.g.codemp_remote_debug)
|
||||
end
|
||||
if args.bang then
|
||||
table.insert(bin_args, "--debug")
|
||||
end
|
||||
M.jobid = vim.fn.jobstart(
|
||||
bin_args,
|
||||
{
|
||||
rpc = true,
|
||||
on_stderr = function(_, data, _)
|
||||
for _, line in pairs(data) do
|
||||
print(line)
|
||||
end
|
||||
-- print(vim.fn.join(data, "\n"))
|
||||
end,
|
||||
stderr_buffered = false,
|
||||
env = { RUST_BACKTRACE = 1 }
|
||||
}
|
||||
)
|
||||
if M.jobid <= 0 then
|
||||
print("[!] could not start codemp client")
|
||||
end
|
||||
end,
|
||||
{ nargs='?', bang=true })
|
||||
|
||||
vim.api.nvim_create_user_command('Stop',
|
||||
function(_)
|
||||
vim.fn.jobstop(M.jobid)
|
||||
M.jobid = nil
|
||||
end,
|
||||
{ bang=true })
|
||||
|
||||
vim.api.nvim_create_user_command('Share',
|
||||
function(args)
|
||||
if M.jobid == nil or M.jobid <= 0 then
|
||||
print("[!] connect to codemp server first")
|
||||
return
|
||||
end
|
||||
local path = args.fargs[1]
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
vim.opt.fileformat = "unix"
|
||||
M.create(path, vim.fn.join(lines, "\n"))
|
||||
hook_callbacks(path, bufnr)
|
||||
M.attach(path)
|
||||
M.listen(path)
|
||||
end,
|
||||
{ nargs=1 })
|
||||
|
||||
vim.api.nvim_create_user_command('Join',
|
||||
function(args)
|
||||
if M.jobid == nil or M.jobid <= 0 then
|
||||
print("[!] connect to codemp server first")
|
||||
return
|
||||
end
|
||||
local path = args.fargs[1]
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
vim.opt.fileformat = "unix"
|
||||
hook_callbacks(path, bufnr)
|
||||
M.attach(path)
|
||||
M.listen(path)
|
||||
end,
|
||||
{ nargs=1 })
|
||||
|
||||
vim.api.nvim_create_user_command('Detach',
|
||||
function(args)
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
if M.detach(args.fargs[1]) then
|
||||
unhook_callbacks(bufnr)
|
||||
print("[/] detached from buffer")
|
||||
else
|
||||
print("[!] error detaching from buffer")
|
||||
end
|
||||
end,
|
||||
{ nargs=1 })
|
||||
|
||||
return M
|
|
@ -1,318 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
use std::{net::TcpStream, sync::Mutex, collections::BTreeMap};
|
||||
|
||||
use codemp::client::CodempClient;
|
||||
use codemp::controller::buffer::{OperationControllerHandle, OperationControllerSubscriber};
|
||||
use codemp::controller::cursor::{CursorControllerHandle, CursorSubscriber};
|
||||
use codemp::factory::OperationFactory;
|
||||
use codemp::proto::buffer_client::BufferClient;
|
||||
use codemp::tokio;
|
||||
|
||||
use rmpv::Value;
|
||||
use clap::Parser;
|
||||
|
||||
use nvim_rs::{compat::tokio::Compat, create::tokio as create, Handler, Neovim};
|
||||
use tracing::{error, warn, debug, info};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct NeovimHandler {
|
||||
client: CodempClient,
|
||||
factories: Arc<Mutex<BTreeMap<String, OperationControllerHandle>>>,
|
||||
cursors: Arc<Mutex<BTreeMap<String, CursorControllerHandle>>>,
|
||||
}
|
||||
|
||||
fn nullable_optional_str(args: &[Value], index: usize) -> Option<String> {
|
||||
Some(args.get(index)?.as_str()?.to_string())
|
||||
}
|
||||
|
||||
fn default_empty_str(args: &[Value], index: usize) -> String {
|
||||
nullable_optional_str(args, index).unwrap_or("".into())
|
||||
}
|
||||
|
||||
fn nullable_optional_number(args: &[Value], index: usize) -> Option<i64> {
|
||||
args.get(index)?.as_i64()
|
||||
}
|
||||
|
||||
fn default_zero_number(args: &[Value], index: usize) -> i64 {
|
||||
nullable_optional_number(args, index).unwrap_or(0)
|
||||
}
|
||||
|
||||
impl NeovimHandler {
|
||||
fn buffer_controller(&self, path: &String) -> Option<OperationControllerHandle> {
|
||||
Some(self.factories.lock().unwrap().get(path)?.clone())
|
||||
}
|
||||
|
||||
fn cursor_controller(&self, path: &String) -> Option<CursorControllerHandle> {
|
||||
Some(self.cursors.lock().unwrap().get(path)?.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Handler for NeovimHandler {
|
||||
type Writer = Compat<tokio::io::Stdout>;
|
||||
|
||||
async fn handle_request(
|
||||
&self,
|
||||
name: String,
|
||||
args: Vec<Value>,
|
||||
nvim: Neovim<Compat<tokio::io::Stdout>>,
|
||||
) -> Result<Value, Value> {
|
||||
debug!("processing '{}' - {:?}", name, args);
|
||||
match name.as_ref() {
|
||||
"ping" => Ok(Value::from("pong")),
|
||||
|
||||
"create" => {
|
||||
if args.is_empty() {
|
||||
return Err(Value::from("no path given"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
let content = nullable_optional_str(&args, 1);
|
||||
let mut c = self.client.clone();
|
||||
match c.create(path, content).await {
|
||||
Ok(r) => match r {
|
||||
true => Ok(Value::Nil),
|
||||
false => Err(Value::from("rejected")),
|
||||
},
|
||||
Err(e) => Err(Value::from(format!("could not create buffer: {}", e))),
|
||||
}
|
||||
},
|
||||
|
||||
"insert" => {
|
||||
if args.len() < 3 {
|
||||
return Err(Value::from("not enough arguments"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
let txt = default_empty_str(&args, 1);
|
||||
let mut pos = default_zero_number(&args, 2);
|
||||
|
||||
if pos <= 0 { pos = 0 } // TODO wtf vim??
|
||||
|
||||
match self.buffer_controller(&path) {
|
||||
None => Err(Value::from("no controller for given path")),
|
||||
Some(controller) => {
|
||||
controller.apply(controller.insert(&txt, pos as u64)).await;
|
||||
Ok(Value::Nil)
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
"delete" => {
|
||||
if args.len() < 3 {
|
||||
return Err(Value::from("not enough arguments"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
let pos = default_zero_number(&args, 1) as u64;
|
||||
let count = default_zero_number(&args, 2) as u64;
|
||||
|
||||
match self.buffer_controller(&path) {
|
||||
None => Err(Value::from("no controller for given path")),
|
||||
Some(controller) => {
|
||||
controller.apply(controller.delete(pos, count)).await;
|
||||
Ok(Value::Nil)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"replace" => {
|
||||
if args.len() < 2 {
|
||||
return Err(Value::from("not enough arguments"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
let txt = default_empty_str(&args, 1);
|
||||
|
||||
match self.buffer_controller(&path) {
|
||||
None => Err(Value::from("no controller for given path")),
|
||||
Some(controller) => {
|
||||
if let Some(op) = controller.replace(&txt) {
|
||||
controller.apply(op).await;
|
||||
}
|
||||
Ok(Value::Nil)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"attach" => {
|
||||
if args.is_empty() {
|
||||
return Err(Value::from("no path given"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
let buffer = match nvim.get_current_buf().await {
|
||||
Ok(b) => b,
|
||||
Err(e) => return Err(Value::from(format!("could not get current buffer: {}", e))),
|
||||
};
|
||||
|
||||
let mut c = self.client.clone();
|
||||
|
||||
match c.attach(path.clone()).await {
|
||||
Err(e) => Err(Value::from(format!("could not attach to stream: {}", e))),
|
||||
Ok(controller) => {
|
||||
let mut _controller = controller.clone();
|
||||
let lines : Vec<String> = _controller.content().split('\n').map(|x| x.to_string()).collect();
|
||||
match buffer.set_lines(0, -1, false, lines).await {
|
||||
Err(e) => Err(Value::from(format!("could not sync buffer: {}", e))),
|
||||
Ok(()) => {
|
||||
tokio::spawn(async move {
|
||||
while let Some(_change) = _controller.poll().await {
|
||||
let lines : Vec<String> = _controller.content().split('\n').map(|x| x.to_string()).collect();
|
||||
// TODO only change lines affected!
|
||||
if let Err(e) = buffer.set_lines(0, -1, false, lines).await {
|
||||
error!("could not update buffer: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
self.factories.lock().unwrap().insert(path, controller);
|
||||
Ok(Value::Nil)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
"detach" => {
|
||||
Err(Value::String("not implemented".into()))
|
||||
// if args.is_empty() {
|
||||
// return Err(Value::from("no path given"));
|
||||
// }
|
||||
// let path = default_empty_str(&args, 0);
|
||||
// match self.buffer_controller(&path) {
|
||||
// None => Err(Value::from("no controller for given path")),
|
||||
// Some(controller) => Ok(Value::from(controller.stop())),
|
||||
// }
|
||||
},
|
||||
|
||||
"listen" => {
|
||||
if args.is_empty() {
|
||||
return Err(Value::from("no path given"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
|
||||
let ns = nvim.create_namespace("Cursor").await
|
||||
.map_err(|e| Value::from(format!("could not create namespace: {}", e)))?;
|
||||
|
||||
let buf = nvim.get_current_buf().await
|
||||
.map_err(|e| Value::from(format!("could not get current buf: {}", e)))?;
|
||||
|
||||
let mut c = self.client.clone();
|
||||
match c.listen().await {
|
||||
Err(e) => Err(Value::from(format!("could not listen cursors: {}", e))),
|
||||
Ok(mut cursor) => {
|
||||
self.cursors.lock().unwrap().insert(path, cursor.clone());
|
||||
debug!("spawning cursor processing worker");
|
||||
tokio::spawn(async move {
|
||||
while let Some(cur) = cursor.poll().await {
|
||||
if let Err(e) = buf.clear_namespace(ns, 0, -1).await {
|
||||
error!("could not clear previous cursor highlight: {}", e);
|
||||
}
|
||||
let start = cur.start();
|
||||
let end = cur.end();
|
||||
let end_col = if start.row == end.row {
|
||||
end.col
|
||||
} else {
|
||||
0 // TODO what the fuck
|
||||
};
|
||||
if let Err(e) = buf.add_highlight(
|
||||
ns, "ErrorMsg",
|
||||
start.row as i64 - 1,
|
||||
start.col as i64,
|
||||
end_col as i64
|
||||
).await {
|
||||
error!("could not create highlight for cursor: {}", e);
|
||||
}
|
||||
}
|
||||
if let Err(e) = buf.clear_namespace(ns, 0, -1).await {
|
||||
error!("could not clear previous cursor highlight: {}", e);
|
||||
}
|
||||
});
|
||||
Ok(Value::Nil)
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
"cursor" => {
|
||||
if args.len() < 3 {
|
||||
return Err(Value::from("not enough args"));
|
||||
}
|
||||
let path = default_empty_str(&args, 0);
|
||||
let row = default_zero_number(&args, 1) as i32;
|
||||
let col = default_zero_number(&args, 2) as i32;
|
||||
let row_end = default_zero_number(&args, 3) as i32;
|
||||
let col_end = default_zero_number(&args, 4) as i32;
|
||||
|
||||
match self.cursor_controller(&path) {
|
||||
None => Err(Value::from("no path given")),
|
||||
Some(cur) => {
|
||||
cur.send(&path, (row, col).into(), (row_end, col_end).into()).await;
|
||||
Ok(Value::Nil)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_ => Err(Value::from("unimplemented")),
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_notify(
|
||||
&self,
|
||||
_name: String,
|
||||
_args: Vec<Value>,
|
||||
_nvim: Neovim<Compat<tokio::io::Stdout>>,
|
||||
) {
|
||||
warn!("notify not handled");
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct CliArgs {
|
||||
/// server host to connect to
|
||||
#[arg(long, default_value = "http://[::1]:50051")]
|
||||
host: String,
|
||||
|
||||
/// show debug level logs
|
||||
#[arg(long, default_value_t = false)]
|
||||
debug: bool,
|
||||
|
||||
/// dump raw tracing logs into this TCP host
|
||||
#[arg(long)]
|
||||
remote_debug: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = CliArgs::parse();
|
||||
|
||||
match args.remote_debug {
|
||||
Some(host) =>
|
||||
tracing_subscriber::fmt()
|
||||
.with_writer(Mutex::new(TcpStream::connect(host)?))
|
||||
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
||||
.init(),
|
||||
|
||||
None =>
|
||||
tracing_subscriber::fmt()
|
||||
.compact()
|
||||
.without_time()
|
||||
.with_ansi(false)
|
||||
.with_writer(std::io::stderr)
|
||||
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
||||
.init(),
|
||||
}
|
||||
|
||||
let client = BufferClient::connect(args.host.clone()).await?;
|
||||
|
||||
let handler: NeovimHandler = NeovimHandler {
|
||||
client: client.into(),
|
||||
factories: Arc::new(Mutex::new(BTreeMap::new())),
|
||||
cursors: Arc::new(Mutex::new(BTreeMap::new())),
|
||||
};
|
||||
|
||||
let (_nvim, io_handler) = create::new_parent(handler).await;
|
||||
|
||||
info!("++ codemp connected: {}", args.host);
|
||||
|
||||
if let Err(e) = io_handler.await? {
|
||||
error!("worker stopped with error: {}", e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
rm codemp.vsix
|
||||
mkdir -p .vsix/extension
|
||||
cp package.json .vsix/extension/package.json
|
||||
cp README.md .vsix/extension/README.md
|
||||
mkdir .vsix/extension/out
|
||||
cp -R src/*.js .vsix/extension/out
|
||||
cp -R codemp.node .vsix/extension/out/codemp.node
|
||||
cd .vsix/
|
||||
zip ../codemp.vsix -r *
|
||||
cd ..
|
||||
rm -rf .vsix/
|
|
@ -1,22 +0,0 @@
|
|||
[package]
|
||||
name = "codemp-vscode"
|
||||
version = "0.0.1"
|
||||
description = "VSCode extension for CodeMP"
|
||||
edition = "2021"
|
||||
exclude = ["index.node"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
codemp = { path = "../.." }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
uuid = { version = "1.3.1", features = ["v4"] }
|
||||
once_cell = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
rmpv = "1"
|
||||
clap = { version = "4.2.1", features = ["derive"] }
|
||||
async-trait = "0.1.68"
|
||||
neon = { version = "0.10.1", default-features = false, features = ["channel-api", "napi-6", "promise-api"] }
|
|
@ -1,38 +0,0 @@
|
|||
{
|
||||
"name": "codemp-vscode",
|
||||
"version": "0.0.1",
|
||||
"description": "VSCode extension for CodeMP",
|
||||
"main": "./out/extension.js",
|
||||
"engines": {
|
||||
"vscode": "^1.32.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "cargo-cp-artifact --artifact cdylib codemp-vscode codemp.node -- cargo build --release --message-format=json-render-diagnostics",
|
||||
"install": "npm run build",
|
||||
"test": "cargo test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cargo-cp-artifact": "^0.1"
|
||||
},
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "codemp.connect",
|
||||
"title": "Connect to CodeMP"
|
||||
},
|
||||
{
|
||||
"command": "codemp.join",
|
||||
"title": "Join remote session"
|
||||
},
|
||||
{
|
||||
"command": "codemp.share",
|
||||
"title": "Share local session"
|
||||
}
|
||||
]
|
||||
},
|
||||
"activationEvents": [
|
||||
"onCommand:codemp.connect",
|
||||
"onCommand:codemp.join",
|
||||
"onCommand:codemp.share"
|
||||
]
|
||||
}
|
|
@ -1,146 +0,0 @@
|
|||
const vscode = require("vscode");
|
||||
const codemp = require("./codemp.node");
|
||||
|
||||
var CLIENT = null
|
||||
var CONTROLLER
|
||||
var CURSOR
|
||||
var DECORATION = null
|
||||
var OP_CACHE = new Set()
|
||||
|
||||
async function activate(context) {
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand("codemp.connect", connect),
|
||||
vscode.commands.registerCommand("codemp.share", share),
|
||||
vscode.commands.registerCommand("codemp.join", join),
|
||||
)
|
||||
}
|
||||
|
||||
async function connect() {
|
||||
let host = await vscode.window.showInputBox({prompt: "server host (default to http://fantabos.co:50051)"})
|
||||
if (host === undefined) return // user cancelled with ESC
|
||||
if (host.length == 0) host = "http://fantabos.co:50051"
|
||||
CLIENT = await codemp.connect(host)
|
||||
vscode.window.showInformationMessage(`Connected to codemp @[${host}]`);
|
||||
}
|
||||
|
||||
async function share() {
|
||||
if (CLIENT === null) {
|
||||
vscode.window.showErrorMessage("No connected client");
|
||||
}
|
||||
|
||||
let path = await vscode.window.showInputBox({prompt: "buffer uri (default to file path)"})
|
||||
if (path === undefined) return // user cancelled with ESC
|
||||
if (path.length == 0) path = doc.uri.toString()
|
||||
|
||||
let doc = vscode.window.activeTextEditor.document;
|
||||
|
||||
try {
|
||||
if (!await CLIENT.create(path, doc.getText())) {
|
||||
vscode.window.showErrorMessage("Could not share buffer");
|
||||
}
|
||||
|
||||
await _attach(path)
|
||||
|
||||
vscode.window.showInformationMessage(`Shared document on buffer "${path}"`);
|
||||
} catch (err) {
|
||||
vscode.window.showErrorMessage("Error sharing: " + err)
|
||||
}
|
||||
}
|
||||
|
||||
async function join() {
|
||||
if (CLIENT === null) {
|
||||
vscode.window.showErrorMessage("No connected client");
|
||||
}
|
||||
|
||||
let path = await vscode.window.showInputBox({prompt: "buffer uri"})
|
||||
|
||||
try {
|
||||
let controller = await _attach(path)
|
||||
|
||||
vscode.window.showInformationMessage(`Joined buffer "${path}"`);
|
||||
|
||||
let editor = vscode.window.activeTextEditor
|
||||
|
||||
let range = new vscode.Range(
|
||||
editor.document.positionAt(0),
|
||||
editor.document.positionAt(editor.document.getText().length)
|
||||
)
|
||||
let content = controller.content()
|
||||
OP_CACHE.add((range, content))
|
||||
editor.edit(editBuilder => editBuilder.replace(range, content))
|
||||
} catch (err) {
|
||||
vscode.window.showErrorMessage("error joining " + err)
|
||||
}
|
||||
}
|
||||
|
||||
function _order_tuples(a, b) {
|
||||
if (a[0] < b[0]) return (a, b)
|
||||
if (a[0] > b[0]) return (b, a)
|
||||
if (a[1] < b[1]) return (a, b)
|
||||
return (b, a)
|
||||
}
|
||||
|
||||
async function _attach(path) {
|
||||
let editor = vscode.window.activeTextEditor
|
||||
let doc = editor.document;
|
||||
|
||||
CURSOR = await CLIENT.listen()
|
||||
CURSOR.callback((usr, path, start, end) => {
|
||||
try {
|
||||
if (DECORATION != null) {
|
||||
DECORATION.dispose()
|
||||
DECORATION = null
|
||||
}
|
||||
const range_start = new vscode.Position(start[0] - 1, start[1]);
|
||||
const range_end = new vscode.Position(end[0] - 1, end[1]);
|
||||
const decorationRange = new vscode.Range(range_start, range_end);
|
||||
DECORATION = vscode.window.createTextEditorDecorationType(
|
||||
{backgroundColor: 'red', color: 'white'}
|
||||
)
|
||||
editor.setDecorations(DECORATION, [decorationRange])
|
||||
} catch (err) {
|
||||
vscode.window.showErrorMessage("error setting cursor decoration: " + err)
|
||||
}
|
||||
})
|
||||
vscode.window.onDidChangeTextEditorSelection(async (e) => {
|
||||
let buf = e.textEditor.document.uri.toString()
|
||||
let selection = e.selections[0] // TODO there may be more than one cursor!!
|
||||
let anchor = [selection.anchor.line+1, selection.anchor.character]
|
||||
let position = [selection.active.line+1, selection.active.character+1]
|
||||
// (anchor, position) = _order_tuples(anchor, position)
|
||||
await CURSOR.send(buf, anchor, position)
|
||||
})
|
||||
|
||||
CONTROLLER = await CLIENT.attach(path)
|
||||
CONTROLLER.callback((start, end, text) => {
|
||||
try {
|
||||
let range = new vscode.Range(
|
||||
editor.document.positionAt(start),
|
||||
editor.document.positionAt(end)
|
||||
)
|
||||
OP_CACHE.add((range, text))
|
||||
editor.edit(editBuilder => editBuilder.replace(range, text))
|
||||
} catch (err) {
|
||||
vscode.window.showErrorMessage("could not set buffer: " + err)
|
||||
}
|
||||
})
|
||||
vscode.workspace.onDidChangeTextDocument(async (e) => {
|
||||
if (e.document != doc) return
|
||||
for (let change of e.contentChanges) {
|
||||
if (OP_CACHE.has((change.range, change.text))) {
|
||||
OP_CACHE.delete((change.range, change.text))
|
||||
continue
|
||||
}
|
||||
try {
|
||||
await CONTROLLER.apply(change.rangeOffset, change.text, change.rangeOffset + change.rangeLength)
|
||||
} catch (err) {
|
||||
vscode.window.showErrorMessage("failed sending change: " + err)
|
||||
}
|
||||
}
|
||||
})
|
||||
return CONTROLLER
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
activate,
|
||||
}
|
|
@ -1,270 +0,0 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use neon::prelude::*;
|
||||
use once_cell::sync::OnceCell;
|
||||
use codemp::{
|
||||
cursor::controller::{CursorSubscriber, CursorControllerHandle},
|
||||
buffer::{
|
||||
controller::{OperationControllerHandle, OperationControllerSubscriber},
|
||||
client::CodempClient,
|
||||
factory::OperationFactory,
|
||||
},
|
||||
proto::buffer_client::BufferClient,
|
||||
};
|
||||
use codemp::tokio::{runtime::Runtime, sync::Mutex};
|
||||
|
||||
fn runtime<'a, C: Context<'a>>(cx: &mut C) -> NeonResult<&'static Runtime> {
|
||||
static RUNTIME: OnceCell<Runtime> = OnceCell::new();
|
||||
|
||||
RUNTIME.get_or_try_init(|| {
|
||||
Runtime::new()
|
||||
.or_else(|err| cx.throw_error(err.to_string()))
|
||||
})
|
||||
}
|
||||
|
||||
fn tuple<'a, C: Context<'a>>(cx: &mut C, a: i32, b: i32) -> NeonResult<Handle<'a, JsArray>> {
|
||||
let obj = cx.empty_array();
|
||||
let a_val = cx.number(a);
|
||||
obj.set(cx, 0, a_val)?;
|
||||
let b_val = cx.number(b);
|
||||
obj.set(cx, 1, b_val)?;
|
||||
Ok(obj)
|
||||
}
|
||||
|
||||
fn unpack_tuple<'a, C: Context<'a>>(cx: &mut C, arr: Handle<'a, JsArray>) -> NeonResult<(i32, i32)> {
|
||||
Ok((
|
||||
arr.get::<JsNumber, _, u32>(cx, 0)?.value(cx) as i32,
|
||||
arr.get::<JsNumber, _, u32>(cx, 1)?.value(cx) as i32,
|
||||
))
|
||||
}
|
||||
|
||||
struct ClientHandle(Arc<Mutex<CodempClient>>);
|
||||
impl Finalize for ClientHandle {}
|
||||
|
||||
fn connect(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let host = cx.argument::<JsString>(0).ok().map(|x| x.value(&mut cx));
|
||||
|
||||
let (deferred, promise) = cx.promise();
|
||||
let channel = cx.channel();
|
||||
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
match BufferClient::connect(host.unwrap_or("".into())).await {
|
||||
Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::<String, neon::handle::Handle<JsString>>(format!("{}", e))),
|
||||
Ok(c) => deferred.settle_with(&channel, |mut cx| {
|
||||
let obj = cx.empty_object();
|
||||
let boxed_value = cx.boxed(ClientHandle(Arc::new(Mutex::new(c.into()))));
|
||||
obj.set(&mut cx, "boxed", boxed_value)?;
|
||||
let method_create = JsFunction::new(&mut cx, create_client)?;
|
||||
obj.set(&mut cx, "create", method_create)?;
|
||||
let method_listen = JsFunction::new(&mut cx, listen_client)?;
|
||||
obj.set(&mut cx, "listen", method_listen)?;
|
||||
let method_attach = JsFunction::new(&mut cx, attach_client)?;
|
||||
obj.set(&mut cx, "attach", method_attach)?;
|
||||
Ok(obj)
|
||||
}),
|
||||
}
|
||||
});
|
||||
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
fn create_client(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let path = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let content = cx.argument::<JsString>(1).ok().map(|x| x.value(&mut cx));
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<ClientHandle>> = this.get(&mut cx, "boxed")?;
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let (deferred, promise) = cx.promise();
|
||||
let channel = cx.channel();
|
||||
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
match rc.lock().await.create(&path, content.as_deref()).await {
|
||||
Ok(accepted) => deferred.settle_with(&channel, move |mut cx| Ok(cx.boolean(accepted))),
|
||||
Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::<String, neon::handle::Handle<JsString>>(e.to_string())),
|
||||
}
|
||||
});
|
||||
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
fn listen_client(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<ClientHandle>> = this.get(&mut cx, "boxed")?;
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let (deferred, promise) = cx.promise();
|
||||
let channel = cx.channel();
|
||||
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
match rc.lock().await.listen().await {
|
||||
Ok(controller) => {
|
||||
deferred.settle_with(&channel, move |mut cx| {
|
||||
let obj = cx.empty_object();
|
||||
let boxed_value = cx.boxed(CursorEventsHandle(controller));
|
||||
obj.set(&mut cx, "boxed", boxed_value)?;
|
||||
let callback_method = JsFunction::new(&mut cx, callback_cursor)?;
|
||||
obj.set(&mut cx, "callback", callback_method)?;
|
||||
let send_method = JsFunction::new(&mut cx, send_cursor)?;
|
||||
obj.set(&mut cx, "send", send_method)?;
|
||||
Ok(obj)
|
||||
})
|
||||
},
|
||||
Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::<String, neon::handle::Handle<JsString>>(e.to_string())),
|
||||
}
|
||||
});
|
||||
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
fn attach_client(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<ClientHandle>> = this.get(&mut cx, "boxed")?;
|
||||
let path = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let (deferred, promise) = cx.promise();
|
||||
let channel = cx.channel();
|
||||
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
match rc.lock().await.attach(&path).await {
|
||||
Ok(controller) => {
|
||||
deferred.settle_with(&channel, move |mut cx| {
|
||||
let obj = cx.empty_object();
|
||||
let boxed_value = cx.boxed(OperationControllerJs(controller));
|
||||
obj.set(&mut cx, "boxed", boxed_value)?;
|
||||
let apply_method = JsFunction::new(&mut cx, apply_operation)?;
|
||||
obj.set(&mut cx, "apply", apply_method)?;
|
||||
let content_method = JsFunction::new(&mut cx, content_operation)?;
|
||||
obj.set(&mut cx, "content", content_method)?;
|
||||
let callback_method = JsFunction::new(&mut cx, callback_operation)?;
|
||||
obj.set(&mut cx, "callback", callback_method)?;
|
||||
Ok(obj)
|
||||
})
|
||||
},
|
||||
Err(e) => deferred.settle_with(&channel, move |mut cx| cx.throw_error::<String, neon::handle::Handle<JsString>>(e.to_string())),
|
||||
}
|
||||
});
|
||||
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
|
||||
struct OperationControllerJs(OperationControllerHandle);
|
||||
impl Finalize for OperationControllerJs {}
|
||||
|
||||
fn apply_operation(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<OperationControllerJs>> = this.get(&mut cx, "boxed")?;
|
||||
let skip = cx.argument::<JsNumber>(0)?.value(&mut cx).round() as usize;
|
||||
let text = cx.argument::<JsString>(1)?.value(&mut cx);
|
||||
let tail = cx.argument::<JsNumber>(2)?.value(&mut cx).round() as usize;
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let (deferred, promise) = cx.promise();
|
||||
let channel = cx.channel();
|
||||
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
if let Some(op) = rc.delta(skip, text.as_str(), tail) {
|
||||
rc.apply(op).await;
|
||||
deferred.settle_with(&channel, move |mut cx| Ok(cx.boolean(true)));
|
||||
} else {
|
||||
deferred.settle_with(&channel, move |mut cx| Ok(cx.undefined()));
|
||||
}
|
||||
});
|
||||
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
fn content_operation(mut cx: FunctionContext) -> JsResult<JsString> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<OperationControllerJs>> = this.get(&mut cx, "boxed")?;
|
||||
Ok(cx.string(boxed.0.content()))
|
||||
}
|
||||
|
||||
fn callback_operation(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<OperationControllerJs>> = this.get(&mut cx, "boxed")?;
|
||||
let callback = Arc::new(cx.argument::<JsFunction>(0)?.root(&mut cx));
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let channel = cx.channel();
|
||||
|
||||
// TODO when garbage collecting OperationController stop this worker
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
while let Some(edit) = rc.poll().await {
|
||||
let cb = callback.clone();
|
||||
channel.send(move |mut cx| {
|
||||
cb.to_inner(&mut cx)
|
||||
.call_with(&cx)
|
||||
.arg(cx.number(edit.span.start as i32))
|
||||
.arg(cx.number(edit.span.end as i32))
|
||||
.arg(cx.string(edit.content))
|
||||
.apply::<JsUndefined, _>(&mut cx)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Ok(cx.undefined())
|
||||
}
|
||||
|
||||
struct CursorEventsHandle(CursorControllerHandle);
|
||||
impl Finalize for CursorEventsHandle {}
|
||||
|
||||
fn callback_cursor(mut cx: FunctionContext) -> JsResult<JsUndefined> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<CursorEventsHandle>> = this.get(&mut cx, "boxed")?;
|
||||
let callback = Arc::new(cx.argument::<JsFunction>(0)?.root(&mut cx));
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let channel = cx.channel();
|
||||
|
||||
// TODO when garbage collecting OperationController stop this worker
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
while let Some(op) = rc.poll().await {
|
||||
let cb = callback.clone();
|
||||
channel.send(move |mut cx| {
|
||||
cb.to_inner(&mut cx)
|
||||
.call_with(&cx)
|
||||
.arg(cx.string(&op.user))
|
||||
.arg(cx.string(&op.buffer))
|
||||
.arg(tuple(&mut cx, op.start().row, op.start().col)?)
|
||||
.arg(tuple(&mut cx, op.end().row, op.end().col)?)
|
||||
.apply::<JsUndefined, _>(&mut cx)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Ok(cx.undefined())
|
||||
}
|
||||
|
||||
fn send_cursor(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
||||
let this = cx.this();
|
||||
let boxed : Handle<JsBox<CursorEventsHandle>> = this.get(&mut cx, "boxed")?;
|
||||
let path = cx.argument::<JsString>(0)?.value(&mut cx);
|
||||
let start_obj = cx.argument::<JsArray>(1)?;
|
||||
let start = unpack_tuple(&mut cx, start_obj)?;
|
||||
let end_obj = cx.argument::<JsArray>(2)?;
|
||||
let end = unpack_tuple(&mut cx, end_obj)?;
|
||||
|
||||
let rc = boxed.0.clone();
|
||||
let (deferred, promise) = cx.promise();
|
||||
let channel = cx.channel();
|
||||
|
||||
runtime(&mut cx)?.spawn(async move {
|
||||
rc.send(&path, start.into(), end.into()).await;
|
||||
deferred.settle_with(&channel, |mut cx| Ok(cx.undefined()))
|
||||
});
|
||||
|
||||
Ok(promise)
|
||||
}
|
||||
|
||||
#[neon::main]
|
||||
fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
||||
cx.export_function("connect", connect)?;
|
||||
|
||||
Ok(())
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
[package]
|
||||
name = "codemp-server"
|
||||
version = "0.2.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
codemp = { path = ".." }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
tonic = { version = "0.9", features = ["tls", "tls-roots"] }
|
||||
prost = "0.11.8"
|
||||
md5 = "0.7.0"
|
||||
uuid = { version = "1.3.1", features = ["v4"] }
|
||||
operational-transform = { version = "0.6", features = ["serde"] }
|
||||
tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "sync", "full"] }
|
||||
tokio-stream = "0.1"
|
||||
serde = "1"
|
||||
serde_json = "1"
|
||||
clap = { version = "4.2.1", features = ["derive"] }
|
|
@ -1,101 +0,0 @@
|
|||
use codemp::{proto::{RawOp, OperationRequest}, errors::IgnorableError};
|
||||
use tokio::sync::{mpsc, broadcast, watch, oneshot};
|
||||
use tracing::{error, warn};
|
||||
// use md5::Digest;
|
||||
|
||||
use operational_transform::OperationSeq;
|
||||
|
||||
pub trait BufferStore<T> {
|
||||
fn get(&self, key: &T) -> Option<&BufferHandle>;
|
||||
fn put(&mut self, key: T, handle: BufferHandle) -> Option<BufferHandle>;
|
||||
|
||||
fn handle(&mut self, key: T, content: Option<String>) {
|
||||
let handle = BufferHandle::new(content);
|
||||
self.put(key, handle);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BufferHandle {
|
||||
pub edit: mpsc::Sender<(oneshot::Sender<bool>, OperationRequest)>,
|
||||
events: broadcast::Sender<RawOp>,
|
||||
// pub digest: watch::Receiver<Digest>,
|
||||
pub content: watch::Receiver<String>,
|
||||
}
|
||||
|
||||
impl BufferHandle {
|
||||
fn new(init: Option<String>) -> Self {
|
||||
let init_val = init.unwrap_or("".into());
|
||||
let (edits_tx, edits_rx) = mpsc::channel(64); // TODO hardcoded size
|
||||
let (events_tx, _events_rx) = broadcast::channel(64); // TODO hardcoded size
|
||||
// let (digest_tx, digest_rx) = watch::channel(md5::compute(&init_val));
|
||||
let (content_tx, content_rx) = watch::channel(init_val.clone());
|
||||
|
||||
let events_tx_clone = events_tx.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let worker = BufferWorker {
|
||||
store: init_val,
|
||||
edits: edits_rx,
|
||||
events: events_tx_clone,
|
||||
// digest: digest_tx,
|
||||
content: content_tx,
|
||||
};
|
||||
worker.work().await
|
||||
});
|
||||
|
||||
BufferHandle {
|
||||
edit: edits_tx,
|
||||
events: events_tx,
|
||||
// digest: digest_rx,
|
||||
content: content_rx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> broadcast::Receiver<RawOp> {
|
||||
self.events.subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
struct BufferWorker {
|
||||
store: String,
|
||||
edits: mpsc::Receiver<(oneshot::Sender<bool>, OperationRequest)>,
|
||||
events: broadcast::Sender<RawOp>,
|
||||
// digest: watch::Sender<Digest>,
|
||||
content: watch::Sender<String>,
|
||||
}
|
||||
|
||||
impl BufferWorker {
|
||||
async fn work(mut self) {
|
||||
loop {
|
||||
match self.edits.recv().await {
|
||||
None => break warn!("channel closed"),
|
||||
Some((ack, v)) => match serde_json::from_str::<OperationSeq>(&v.opseq) {
|
||||
Err(e) => {
|
||||
ack.send(false).unwrap_or_warn("could not reject undeserializable opseq");
|
||||
error!("could not deserialize opseq: {}", e);
|
||||
},
|
||||
Ok(op) => match op.apply(&self.store) {
|
||||
Err(e) => {
|
||||
ack.send(false).unwrap_or_warn("could not reject unappliable opseq");
|
||||
error!("coult not apply OpSeq '{:?}' on '{}' : {}", v, self.store, e); // TODO
|
||||
}
|
||||
Ok(res) => {
|
||||
self.store = res;
|
||||
let msg = RawOp {
|
||||
opseq: v.opseq,
|
||||
user: v.user
|
||||
};
|
||||
// if let Err(e) = self.digest.send(md5::compute(&self.store)) {
|
||||
// error!("could not update digest: {}", e);
|
||||
// }
|
||||
ack.send(true).unwrap_or_warn("could not accept opseq");
|
||||
self.content.send(self.store.clone()).unwrap_or_warn("could not update content");
|
||||
self.events.send(msg).unwrap_or_warn("could not broadcast OpSeq");
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
pub mod actor;
|
||||
pub mod service;
|
|
@ -1,113 +0,0 @@
|
|||
use std::{pin::Pin, sync::{Arc, RwLock}, collections::HashMap};
|
||||
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this?
|
||||
|
||||
use codemp::proto::{buffer_server::Buffer, RawOp, BufferPayload, BufferResponse, OperationRequest, BufferEditResponse, BufferCreateResponse};
|
||||
use tracing::info;
|
||||
|
||||
use super::actor::{BufferHandle, BufferStore};
|
||||
|
||||
type OperationStream = Pin<Box<dyn Stream<Item = Result<RawOp, Status>> + Send>>;
|
||||
|
||||
struct BufferMap {
|
||||
store: HashMap<String, BufferHandle>,
|
||||
}
|
||||
|
||||
impl From::<HashMap<String, BufferHandle>> for BufferMap {
|
||||
fn from(value: HashMap<String, BufferHandle>) -> Self {
|
||||
BufferMap { store: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl BufferStore<String> for BufferMap {
|
||||
fn get(&self, key: &String) -> Option<&BufferHandle> {
|
||||
self.store.get(key)
|
||||
}
|
||||
fn put(&mut self, key: String, handle: BufferHandle) -> Option<BufferHandle> {
|
||||
self.store.insert(key, handle)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BufferService {
|
||||
map: Arc<RwLock<BufferMap>>,
|
||||
}
|
||||
|
||||
impl Default for BufferService {
|
||||
fn default() -> BufferService {
|
||||
BufferService {
|
||||
map: Arc::new(RwLock::new(HashMap::new().into())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Buffer for BufferService {
|
||||
type AttachStream = OperationStream;
|
||||
|
||||
async fn attach(&self, req: Request<BufferPayload>) -> Result<Response<OperationStream>, Status> {
|
||||
let request = req.into_inner();
|
||||
let myself = request.user;
|
||||
match self.map.read().unwrap().get(&request.path) {
|
||||
None => Err(Status::not_found("path not found")),
|
||||
Some(handle) => {
|
||||
let (tx, rx) = mpsc::channel(128);
|
||||
let mut sub = handle.subscribe();
|
||||
tokio::spawn(async move {
|
||||
while let Ok(v) = sub.recv().await {
|
||||
if v.user == myself { continue }
|
||||
tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel?
|
||||
}
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
info!("registered new subscriber on buffer");
|
||||
Ok(Response::new(Box::pin(output_stream)))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn edit(&self, req:Request<OperationRequest>) -> Result<Response<BufferEditResponse>, Status> {
|
||||
let request = req.into_inner();
|
||||
let tx = match self.map.read().unwrap().get(&request.path) {
|
||||
Some(handle) => {
|
||||
// if format!("{:x}", *handle.digest.borrow()) != request.hash {
|
||||
// return Ok(Response::new(BufferResponse { accepted : false } ));
|
||||
// }
|
||||
handle.edit.clone()
|
||||
},
|
||||
None => return Err(Status::not_found("path not found")),
|
||||
};
|
||||
info!("sending edit to buffer: {}", request.opseq);
|
||||
let (ack, status) = oneshot::channel();
|
||||
match tx.send((ack, request)).await {
|
||||
Err(e) => Err(Status::internal(format!("error sending edit to buffer actor: {}", e))),
|
||||
Ok(()) => {
|
||||
match status.await {
|
||||
Ok(_accepted) => Ok(Response::new(BufferEditResponse { })),
|
||||
Err(e) => Err(Status::internal(format!("error receiving edit result: {}", e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(&self, req:Request<BufferPayload>) -> Result<Response<BufferCreateResponse>, Status> {
|
||||
let request = req.into_inner();
|
||||
let _handle = self.map.write().unwrap().handle(request.path, request.content);
|
||||
info!("created new buffer");
|
||||
Ok(Response::new(BufferCreateResponse { }))
|
||||
}
|
||||
|
||||
async fn sync(&self, req: Request<BufferPayload>) -> Result<Response<BufferResponse>, Status> {
|
||||
let request = req.into_inner();
|
||||
match self.map.read().unwrap().get(&request.path) {
|
||||
None => Err(Status::not_found("requested buffer does not exist")),
|
||||
Some(buf) => {
|
||||
info!("synching buffer");
|
||||
let answ = BufferResponse { content: buf.content.borrow().clone() };
|
||||
Ok(Response::new(answ))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
pub mod service;
|
|
@ -1,52 +0,0 @@
|
|||
use std::pin::Pin;
|
||||
|
||||
use tokio::sync::{mpsc, broadcast};
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
use tokio_stream::{Stream, wrappers::ReceiverStream}; // TODO example used this?
|
||||
|
||||
use codemp::proto::{cursor_server::Cursor, UserIdentity, CursorPosition, MovedResponse};
|
||||
use tracing::info;
|
||||
|
||||
type CursorStream = Pin<Box<dyn Stream<Item = Result<CursorPosition, Status>> + Send>>;
|
||||
|
||||
pub struct CursorService {
|
||||
cursor: broadcast::Sender<CursorPosition>,
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Cursor for CursorService {
|
||||
type ListenStream = CursorStream;
|
||||
|
||||
async fn listen(&self, req: Request<UserIdentity>) -> Result<Response<CursorStream>, Status> {
|
||||
let mut sub = self.cursor.subscribe();
|
||||
let myself = req.into_inner().id;
|
||||
let (tx, rx) = mpsc::channel(128);
|
||||
tokio::spawn(async move {
|
||||
while let Ok(v) = sub.recv().await {
|
||||
if v.user == myself { continue }
|
||||
tx.send(Ok(v)).await.unwrap(); // TODO unnecessary channel?
|
||||
}
|
||||
});
|
||||
let output_stream = ReceiverStream::new(rx);
|
||||
info!("registered new subscriber to cursor updates");
|
||||
Ok(Response::new(Box::pin(output_stream)))
|
||||
}
|
||||
|
||||
async fn moved(&self, req:Request<CursorPosition>) -> Result<Response<MovedResponse>, Status> {
|
||||
match self.cursor.send(req.into_inner()) {
|
||||
Ok(_) => Ok(Response::new(MovedResponse { })),
|
||||
Err(e) => Err(Status::internal(format!("could not broadcast cursor update: {}", e))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CursorService {
|
||||
fn default() -> Self {
|
||||
let (cur_tx, _cur_rx) = broadcast::channel(64); // TODO hardcoded capacity
|
||||
// TODO don't drop receiver because sending event when there are no receivers throws an error
|
||||
CursorService {
|
||||
cursor: cur_tx,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
//! # codemp Server
|
||||
//!
|
||||
//! The codemp server itself, in charge of handling the global state, merging operations from
|
||||
//! all clients and synching everyone's cursor.
|
||||
//!
|
||||
|
||||
use clap::Parser;
|
||||
use codemp::proto::buffer_server::BufferServer;
|
||||
use codemp::proto::cursor_server::CursorServer;
|
||||
use tracing::info;
|
||||
use tonic::transport::Server;
|
||||
|
||||
mod buffer;
|
||||
mod cursor;
|
||||
|
||||
use crate::buffer::service::BufferService;
|
||||
use crate::cursor::service::CursorService;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct CliArgs {
|
||||
|
||||
/// address to listen on
|
||||
#[arg(long, default_value = "[::1]:50051")]
|
||||
host: String,
|
||||
|
||||
/// enable debug log level
|
||||
#[arg(long, default_value_t = false)]
|
||||
debug: bool,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = CliArgs::parse();
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_writer(std::io::stdout)
|
||||
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
||||
.init();
|
||||
|
||||
info!(">> codemp server");
|
||||
info!("binding on {}", args.host);
|
||||
|
||||
Server::builder()
|
||||
.add_service(BufferServer::new(BufferService::default()))
|
||||
.add_service(CursorServer::new(CursorService::default()))
|
||||
.serve(args.host.parse()?)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue