Skip to content

Commit

Permalink
fix: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Feb 23, 2024
1 parent 3f7124b commit b780a5e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
55 changes: 4 additions & 51 deletions server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use lykiadb_server::net::{CommunicationError, Connection, Message, Request, Response};
use lykiadb_server::runtime::types::RV;
use lykiadb_server::runtime::{Runtime, RuntimeMode};

use lykiadb_server::runtime::ServerSession;
use std::io::Error;
use tokio::net::{TcpListener, TcpStream};
use tokio::net::TcpListener;
use tokio_stream::wrappers::TcpListenerStream;
use tokio_stream::StreamExt as _;
use tracing::{error, info};
use tracing::info;

const ASCII_ART: &str = r"
$$\ $$\ $$\ $$$$$$$\ $$$$$$$\
Expand All @@ -25,52 +24,6 @@ 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) {
while let Some(message) = self.conn.read().await.unwrap() {
info!("{:?}", message);
match message {
Message::Request(req) => match req {
Request::Ast(source) => {
let ast = self.runtime.ast(&source);
self.conn
.write(Message::Response(Response::Program(ast.unwrap())))
.await
.unwrap();
}
Request::Run(command) => {
let execution = self.runtime.interpret(&command);
let response = if execution.is_ok() {
Response::Value(execution.ok().or_else(|| Some(RV::Undefined)).unwrap())
} else {
Response::Error(execution.err().unwrap())
};

self.conn.write(Message::Response(response)).await.unwrap();
}
},
_ => error!("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 })
Expand Down
52 changes: 50 additions & 2 deletions server/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ::std::sync::Arc;

use serde_json::Value;
use tracing::info;
use tokio::net::TcpStream;
use tracing::{error, info};

use self::environment::Environment;
use self::error::ExecutionError;
Expand All @@ -10,9 +11,10 @@ use self::resolver::Resolver;
use self::std::stdlib;
use crate::lang::ast::visitor::VisitorMut;

use crate::lang::ast::parser::{ParseError, Parser};
use crate::lang::ast::parser::Parser;
use crate::lang::ast::program::AstArena;
use crate::lang::tokens::scanner::Scanner;
use crate::net::{CommunicationError, Connection, Message, Request, Response};
use crate::runtime::interpreter::Interpreter;
use crate::runtime::types::RV;
use crate::util::Shared;
Expand All @@ -24,6 +26,52 @@ mod resolver;
mod std;
pub mod types;

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) {
while let Some(message) = self.conn.read().await.unwrap() {
info!("{:?}", message);
match message {
Message::Request(req) => match req {
Request::Ast(source) => {
let ast = self.runtime.ast(&source);
self.conn
.write(Message::Response(Response::Program(ast.unwrap())))
.await
.unwrap();
}
Request::Run(command) => {
let execution = self.runtime.interpret(&command);
let response = if execution.is_ok() {
Response::Value(execution.ok().or_else(|| Some(RV::Undefined)).unwrap())
} else {
Response::Error(execution.err().unwrap())
};

self.conn.write(Message::Response(response)).await.unwrap();
}
},
_ => error!("Unsupported message type"),
}
}
}

pub async fn send(&mut self, msg: Message) -> Result<(), CommunicationError> {
self.conn.write(msg).await
}
}

pub struct Runtime {
mode: RuntimeMode,
out: Option<Shared<Output>>,
Expand Down

0 comments on commit b780a5e

Please sign in to comment.