-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c71027
commit 8d5c300
Showing
10 changed files
with
237 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1 @@ | ||
use bson::Bson; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Request { | ||
Execute(String), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Response { | ||
Execute(Bson), | ||
} | ||
pub mod protocol; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use bson::Bson; | ||
use bytes::BytesMut; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::{io::{copy, AsyncReadExt, AsyncWriteExt, BufWriter}, net::TcpStream}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub enum Request { | ||
Run(String) | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub enum Response { | ||
Value(Bson) | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub enum Message { | ||
Request(Request), | ||
Response(Response) | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum CommunicationError { | ||
BsonError(bson::ser::Error), | ||
IoError(std::io::Error), | ||
GenericError(String), | ||
} | ||
|
||
impl From<std::io::Error> for CommunicationError { | ||
fn from(value: std::io::Error) -> Self { | ||
CommunicationError::IoError(value) | ||
} | ||
} | ||
|
||
impl From<bson::ser::Error> for CommunicationError { | ||
fn from(value: bson::ser::Error) -> Self { | ||
CommunicationError::BsonError(value) | ||
} | ||
} | ||
|
||
pub struct Connection { | ||
pub stream: BufWriter<TcpStream>, | ||
pub read_buffer: BytesMut, | ||
} | ||
|
||
impl Connection { | ||
pub fn new(stream: TcpStream) -> Connection { | ||
Connection { | ||
stream: BufWriter::new(stream), | ||
read_buffer: BytesMut::with_capacity(4096), | ||
} | ||
} | ||
|
||
pub async fn read(&mut self) -> Result<Option<Message>, CommunicationError> { | ||
loop { | ||
// TODO(vck): Replace .to_vec call with something cheaper | ||
if let Ok(parsed) = bson::from_slice::<Message>(&self.read_buffer.to_vec()) { | ||
return Ok(Some(parsed)); | ||
} | ||
|
||
if 0 == self.stream.read_buf(&mut self.read_buffer).await? { | ||
if self.read_buffer.is_empty() { | ||
return Ok(None); | ||
} else { | ||
return Err(CommunicationError::GenericError("Connection reset by peer".to_owned())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub async fn write(&mut self, message: Message) -> Result<(), CommunicationError> { | ||
let vec = bson::to_vec(&bson::to_bson(&message)?)?; | ||
let mut buffer = vec.as_slice(); | ||
copy(&mut buffer, &mut self.stream).await?; | ||
self.stream.flush().await?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod connection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
pub mod cli; | ||
pub mod lang; | ||
pub mod runtime; | ||
pub mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,89 @@ | ||
use lykiadb_server::cli; | ||
use std::io::Error; | ||
|
||
use liblykia::protocol::connection::{CommunicationError, Connection, Message, Request, Response}; | ||
use lykiadb_server::runtime::types::RV; | ||
use lykiadb_server::runtime::{Runtime, RuntimeMode}; | ||
use tokio::net::{TcpListener, TcpStream}; | ||
use tokio_stream::wrappers::TcpListenerStream; | ||
use tokio_stream::StreamExt as _; | ||
|
||
struct Server { | ||
listener: Option<TcpListener> | ||
} | ||
|
||
pub struct ServerSession { | ||
conn: Connection, | ||
//runtime: Runtime | ||
} | ||
|
||
impl ServerSession { | ||
pub fn new(stream: TcpStream) -> Self { | ||
ServerSession { | ||
conn: Connection::new(stream), | ||
//runtime: Runtime::new(RuntimeMode::File) | ||
} | ||
} | ||
|
||
pub async fn handle(&mut self) { | ||
if let Some(message) = self.conn.read().await.unwrap() { | ||
println!("{:?}", message); | ||
match message { | ||
Message::Request(req) => match req { | ||
Request::Run(command) => { | ||
/*let execution = self.runtime.interpret(&command); | ||
if execution.is_ok() { | ||
let bson_response = bson::to_bson(&execution.ok().or_else(|| Some(RV::Undefined))); | ||
self.conn.write(Message::Response(Response::Value(bson_response.unwrap()))).await; | ||
}*/ | ||
self.conn.write(Message::Response(Response::Value(bson::to_bson(&1515).unwrap()))).await.unwrap(); | ||
}, | ||
}, | ||
_ => panic!("Unsupported message type") | ||
} | ||
} | ||
} | ||
|
||
pub async fn send(&mut self, msg: Message) -> Result<(), CommunicationError> { | ||
self.conn.write(msg).await | ||
} | ||
} | ||
|
||
impl Server { | ||
pub fn new() -> Result<Self, Error> { | ||
Ok(Server { | ||
listener: None, | ||
}) | ||
} | ||
|
||
pub async fn listen(mut self, addr: &str) -> Result<Self, Error> { | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("Listening on {}", listener.local_addr()?); | ||
self.listener = Some(listener); | ||
Ok(self) | ||
} | ||
|
||
pub async fn serve(self) -> Result<(), Error> { | ||
if let Some(listener) = self.listener { | ||
let mut stream = TcpListenerStream::new(listener); | ||
while let Some(socket) = stream.try_next().await? { | ||
let peer = socket.peer_addr()?; | ||
tokio::spawn(async move { | ||
let mut session = ServerSession::new(socket); | ||
println!("Client {} connected", peer); | ||
session.handle().await; | ||
println!("Client {} disconnected", peer); | ||
}); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
cli::init(); | ||
} | ||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
Server::new()? | ||
.listen("0.0.0.0:19191") | ||
.await? | ||
.serve() | ||
.await | ||
} |