removing client stuff again, cause it's not going well

This commit is contained in:
michael-bailey 2024-05-09 01:02:35 +01:00
parent 82f5ac30e9
commit 5eab70f5c2
23 changed files with 1 additions and 649 deletions

View File

@ -1,3 +0,0 @@
pub fn exit(s: &mut Cursive) {
s.quit();
}

View File

@ -1,56 +1,3 @@
pub mod network;
pub mod screens;
pub mod segues;
mod settings;
pub mod state;
use cursive::{event::Event, menu::Tree, views::Menubar, Cursive};
use crate::{
screens::main_screen::select_operation::methods_view,
settings::settings_panel,
state::State,
};
enum MethodSelection {
GetInfo,
Connect,
}
fn menu_bar(menu_bar: &mut Menubar) {
menu_bar.add_subtree(
"Chat Kit",
Tree::new()
.leaf("Settings", open_settings)
.delimiter()
.leaf("Quit", exit),
);
}
fn main() {
let mut scr = cursive::default();
scr.set_fps(30);
let state = State::new();
scr.set_user_data(state);
menu_bar(scr.menubar());
scr.add_global_callback(Event::Key(cursive::event::Key::Esc), |s| {
s.select_menubar()
});
scr.add_layer(methods_view("127.0.0.1:6500".into()));
scr.run()
}
fn exit(s: &mut Cursive) {
s.quit();
}
fn open_settings(s: &mut Cursive) {
let host = s.user_data::<State>().map(|s| s.get_host());
s.add_layer(settings_panel(host));
println!("Please dont use this");
}

View File

@ -1,10 +0,0 @@
use crate::network::network_connection::NetworkConnection;
pub mod network_connection;
pub mod server_reader_connection;
pub mod server_writer_connection;
pub enum NetworkState {
Disconnected,
ConnectedNetwork(NetworkConnection),
}

View File

@ -1,130 +0,0 @@
use std::{io, net::SocketAddr};
use foundation::{
networking::{read_message, write_message},
prelude::{
network_server_message,
Connect,
GetInfo,
Info,
NetworkClientMessage,
NetworkServerMessage,
Request,
},
};
use tokio::{io::split, net::TcpStream};
use uuid::Uuid;
use crate::network::{
server_reader_connection::ServerReaderConnection,
server_writer_connection::ServerWriterConnection,
};
/// # NetworkConnection
/// encapsulates the state of the network connection
/// will connect to a server and ensure it is usinghte protobuf protocol
///
/// you can then either get info or connect to the server
pub struct NetworkConnection {
pub(super) stream: TcpStream,
}
impl NetworkConnection {
pub async fn connect(address: SocketAddr) -> io::Result<Self> {
let mut stream = TcpStream::connect(address).await.unwrap();
let msg =
read_message::<NetworkServerMessage, TcpStream>(&mut stream).await?;
let NetworkServerMessage {
message: Some(network_server_message::Message::Request(Request {})),
} = msg
else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Received invalid start message from server",
));
};
Ok(Self { stream })
}
/// Will consume the connection, and fetch the servers info.
pub async fn send_get_info(mut self) -> io::Result<Info> {
_ = write_message(
&mut self.stream,
NetworkClientMessage {
message: Some(
foundation::prelude::network_client_message::Message::GetInfo(
GetInfo {},
),
),
},
)
.await;
let message =
read_message::<NetworkServerMessage, TcpStream>(&mut self.stream).await?;
let NetworkServerMessage {
message: Some(network_server_message::Message::GotInfo(msg)),
} = message
else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"sent for info got different message back",
));
};
Ok(msg)
}
/// consumes this struct and returns a tuple of the sernding and receiving ahlfs of teh connected conneciton
pub async fn send_connect(
mut self,
uuid: Uuid,
username: String,
) -> io::Result<(ServerWriterConnection, ServerReaderConnection)> {
_ = write_message(
&mut self.stream,
NetworkClientMessage {
message: Some(
foundation::prelude::network_client_message::Message::Connect(
Connect {
username,
uuid: uuid.to_string(),
},
),
),
},
)
.await;
let message =
read_message::<NetworkServerMessage, TcpStream>(&mut self.stream).await?;
let NetworkServerMessage {
message: Some(network_server_message::Message::Connected(_)),
} = message
else {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"sent connect got different message back or failed to connect",
));
};
Ok(self.into())
}
}
impl From<NetworkConnection>
for (ServerWriterConnection, ServerReaderConnection)
{
fn from(value: NetworkConnection) -> Self {
let (read_half, write_half) = split(value.stream);
(
ServerWriterConnection::new(write_half),
ServerReaderConnection::new(read_half),
)
}
}

View File

@ -1,24 +0,0 @@
use std::io;
use foundation::{networking::read_message, prelude::ConnectedServerMessage};
use tokio::{io::ReadHalf, net::TcpStream};
pub struct ServerReaderConnection {
reader: ReadHalf<TcpStream>,
}
impl ServerReaderConnection {
pub(crate) fn new(read_half: ReadHalf<TcpStream>) -> Self {
Self { reader: read_half }
}
// move to other one
pub async fn get_message(&mut self) -> io::Result<ConnectedServerMessage> {
let message = read_message::<ConnectedServerMessage, ReadHalf<TcpStream>>(
&mut self.reader,
)
.await
.unwrap();
Ok(message)
}
}

View File

@ -1,13 +0,0 @@
use tokio::{io::WriteHalf, net::TcpStream};
pub struct ServerWriterConnection {
writer: WriteHalf<TcpStream>,
}
impl ServerWriterConnection {
pub(crate) fn new(writer: WriteHalf<TcpStream>) -> Self {
todo!()
}
pub async fn request_clients(&mut self) {}
}

View File

@ -1,2 +0,0 @@
pub mod segues;
pub mod user_details_form;

View File

@ -1,11 +0,0 @@
use crate::{
screens::connect_setup::user_details_form::user_details_form,
segues::CursiveCB,
};
pub fn segue_to_user_details_form() -> CursiveCB {
Box::new(|s| {
s.pop_layer();
s.add_layer(user_details_form())
})
}

View File

@ -1,43 +0,0 @@
use cursive::{
view::Resizable,
views::{Dialog, EditView, LinearLayout, TextView},
Cursive,
View,
};
use crate::screens::main_screen::segues::segue_to_select_operation;
pub fn user_details_form() -> impl View {
Dialog::new()
.title("User Setup")
.content(
LinearLayout::vertical()
.child(
LinearLayout::horizontal()
.child(TextView::new("Username").min_width(9))
.child(EditView::new().full_width()),
)
.child(
LinearLayout::horizontal()
.child(TextView::new("UUID").min_width(9))
.child(EditView::new().full_width()),
),
)
.button("Cancel", on_cancel)
.button("Connect", on_connect)
.fixed_size((40, 10))
}
fn on_connect(s: &mut Cursive) {
println!("Attempting conneciton");
s.add_layer(
Dialog::new()
.title("LOL XD")
.content(TextView::new("Yeah this isnt iomplemented yet"))
.dismiss_button("Dismiss"),
)
}
fn on_cancel(s: &mut Cursive) {
_ = s.cb_sink().send(segue_to_select_operation());
}

View File

@ -1,19 +0,0 @@
use cursive::{
view::Margins,
views::{Dialog, LinearLayout, TextView},
View,
};
pub fn info_dialogue(name: String, owner: String) -> impl View {
Dialog::new()
.padding(Margins::lrtb(2, 2, 2, 2))
.content(
LinearLayout::vertical()
.child(TextView::new("Got Info:"))
.child(TextView::new(format!("name: {}", name)))
.child(TextView::new(format!("owner: {}", owner))),
)
.button("Close", |s| {
s.pop_layer();
})
}

View File

@ -1,19 +0,0 @@
use cursive::{
views::{Dialog, LinearLayout, PaddedView, TextView},
View,
};
pub fn info_error_dialogue(message: &str) -> impl View {
Dialog::new()
.title("Info Fetch Error")
.content(PaddedView::lrtb(
2,
2,
2,
2,
LinearLayout::vertical()
.child(TextView::new("Error fetching the Info from the server"))
.child(TextView::new(message)),
))
.dismiss_button("Big Oof")
}

View File

@ -1,8 +0,0 @@
use cursive::{
views::{Panel, TextView},
View,
};
pub fn info_loading_panel() -> impl View {
Panel::new(TextView::new("Loading"))
}

View File

@ -1,59 +0,0 @@
use cursive::Cursive;
use crate::{
network::network_connection::NetworkConnection,
screens::{
info_dialogue::segues::{
segue_to_info_dialgue,
segue_to_info_loading_panel,
segue_to_load_error_dialogue,
},
main_screen::segues::segue_open_invalid_address_dialogue,
},
state::StateObject,
};
pub mod info_dialogue;
pub mod info_error_dialogue;
pub mod info_loading_panel;
pub mod segues;
pub fn get_info(s: &mut Cursive) {
let sink = s.cb_sink().clone();
let state = s.state();
let address = state.get_host().parse();
let Ok(address) = address else {
_ = sink.send(segue_open_invalid_address_dialogue(state.get_host()));
return;
};
state.spawn(async move {
_ = sink.send(segue_to_info_loading_panel());
let conn = NetworkConnection::connect(address).await;
let Ok(conn) = conn else {
_ = sink.send(segue_to_load_error_dialogue(
"
failed to connect to the server
"
.into(),
));
return;
};
let res = conn.send_get_info().await;
let Ok(info) = res else {
_ = sink.send(segue_to_load_error_dialogue(
"
Failed to retrieve info
"
.into(),
));
return;
};
_ = sink.send(segue_to_info_dialgue(info.server_name, info.owner));
})
}

View File

@ -1,33 +0,0 @@
use cursive::Cursive;
use crate::{
screens::info_dialogue::{
info_dialogue::info_dialogue,
info_error_dialogue::info_error_dialogue,
info_loading_panel::info_loading_panel,
},
segues::CursiveCB,
};
pub fn segue_to_info_loading_panel() -> CursiveCB {
Box::new(|s: &mut Cursive| {
s.add_layer(info_loading_panel());
})
}
pub fn segue_to_load_error_dialogue(reason: String) -> CursiveCB {
Box::new(move |s| {
s.pop_layer();
s.add_layer(info_error_dialogue(&reason));
})
}
pub fn segue_to_info_dialgue(
name: String,
owner: String,
) -> Box<dyn FnOnce(&mut Cursive) + Send> {
Box::new(|s| {
s.pop_layer();
s.add_layer(info_dialogue(name, owner));
})
}

View File

@ -1,16 +0,0 @@
use cursive::{
views::{Dialog, TextView},
View,
};
pub fn invlaid_address_dialogue(address: String) -> impl View {
Dialog::new()
.title("error")
.content(TextView::new(format!(
"'{}' is an invalid address",
address
)))
.button("Dismiss", |s| {
s.pop_layer();
})
}

View File

@ -1,4 +0,0 @@
pub mod invalid_address_dialogue;
pub mod segues;
pub mod select_operation;
pub mod stream_failed;

View File

@ -1,18 +0,0 @@
use crate::{
screens::main_screen::{
invalid_address_dialogue::invlaid_address_dialogue,
select_operation::methods_view,
},
segues::CursiveCB,
};
pub fn segue_to_select_operation() -> CursiveCB {
Box::new(|s| {
s.pop_layer();
s.add_layer(methods_view("127.0.0.1:6500".into()))
})
}
pub fn segue_open_invalid_address_dialogue(address: String) -> CursiveCB {
Box::new(|s| s.add_layer(invlaid_address_dialogue(address)))
}

View File

@ -1,69 +0,0 @@
use cursive::{
view::{Nameable, Resizable},
views::{
Button,
EditView,
LinearLayout,
PaddedView,
Panel,
SelectView,
TextView,
},
Cursive,
View,
};
use crate::{
exit,
screens::{
connect_setup::segues::segue_to_user_details_form,
info_dialogue::get_info,
},
state::StateObject,
MethodSelection,
};
pub fn methods_view(host: String) -> impl View {
let horizontal = LinearLayout::horizontal();
Panel::new(PaddedView::lrtb(
2,
2,
2,
2,
LinearLayout::vertical()
.child(
EditView::new()
.content(host)
.on_edit(Cursive::set_host)
.with_name("host_input"),
)
.child(TextView::new("Select option"))
.child(
SelectView::new()
.item("Get Info", MethodSelection::GetInfo)
.item("Connect...", MethodSelection::Connect)
.on_submit(execute)
.with_name("method_selector"),
)
.child(horizontal.child(Button::new("Cancel", exit))),
))
.fixed_size((40, 10))
}
fn execute(s: &mut Cursive, item: &MethodSelection) {
println!("executing");
match item {
MethodSelection::GetInfo => run_get_info(s),
MethodSelection::Connect => {
_ = s.cb_sink().send(segue_to_user_details_form());
}
}
}
// mark: - this should be removed
fn run_get_info(s: &mut Cursive) {
let sink = s.cb_sink().clone();
_ = sink.send(Box::new(get_info));
}

View File

@ -1,8 +0,0 @@
use cursive::{
views::{Dialog, TextView},
View,
};
pub fn stream_failed_dialogue() -> impl View {
Dialog::new().content(TextView::new("stream failed to open?"))
}

View File

@ -1,3 +0,0 @@
pub mod connect_setup;
pub mod info_dialogue;
pub mod main_screen;

View File

@ -1,9 +0,0 @@
use cursive::Cursive;
pub type CursiveCB = Box<dyn FnOnce(&mut Cursive) + Send>;
pub fn segue_pop_layer() -> CursiveCB {
Box::new(|s| {
s.pop_layer();
})
}

View File

@ -1,31 +0,0 @@
use cursive::{
view::{Margins, Nameable, Resizable},
views::{Button, EditView, LinearLayout, PaddedView, Panel},
Cursive,
View,
XY,
};
use crate::state::State;
pub fn settings_panel(host: Option<String>) -> impl View {
Panel::new(PaddedView::new(
Margins::lrtb(2, 2, 2, 2),
LinearLayout::vertical()
.child(
EditView::new()
.content(host.unwrap_or("localhost:6500".into()))
.on_edit(set_host)
.with_name("host_input"),
)
.child(Button::new("Close", |s| {
s.pop_layer();
}))
.min_size(XY { x: 30, y: 8 }),
))
.title("Settings")
}
fn set_host(s: &mut Cursive, host: &str, _: usize) {
s.user_data::<State>().unwrap().set_host(host);
}

View File

@ -1,63 +0,0 @@
use std::future::Future;
use cursive::Cursive;
use tokio::runtime::Runtime;
use crate::network::NetworkState;
pub struct State {
runtime: Runtime,
connection_state: NetworkState,
host: String,
}
impl State {
pub fn new() -> Self {
Self {
runtime: Runtime::new().unwrap(),
connection_state: NetworkState::Disconnected,
host: "127.0.0.1:6500".into(),
}
}
pub fn get_host(&self) -> String {
self.host.clone()
}
pub fn set_host<T: Into<String>>(&mut self, value: T) {
self.host = value.into()
}
pub fn get_rt(&mut self) -> &mut Runtime {
&mut self.runtime
}
pub fn spawn<F>(&mut self, future: F)
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.runtime.spawn(future);
}
}
impl Default for State {
fn default() -> Self {
Self::new()
}
}
pub trait StateObject {
fn state(&mut self) -> &mut State;
fn set_host(&mut self, host: &str, _: usize);
}
impl StateObject for Cursive {
fn set_host(&mut self, host: &str, _: usize) {
self.user_data::<State>().unwrap().set_host(host);
}
fn state(&mut self) -> &mut State {
self.user_data::<State>().unwrap()
}
}