Remove CLI interface
This commit is contained in:
parent
cabb4a9fa6
commit
90f4905de1
|
@ -13,17 +13,6 @@ dependencies = [
|
|||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
|
@ -48,45 +37,6 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "3.1.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2dbdf4bdacb33466e854ce889eee8dfd5729abf7ccd7664d0a2d60cd384440b"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"bitflags",
|
||||
"clap_derive",
|
||||
"clap_lex",
|
||||
"indexmap",
|
||||
"lazy_static",
|
||||
"strsim",
|
||||
"termcolor",
|
||||
"textwrap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "3.1.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25320346e922cffe59c0bbc5410c8d8784509efb321488971081313cb1e1a33c"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
|
||||
dependencies = [
|
||||
"os_str_bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.4"
|
||||
|
@ -263,43 +213,12 @@ dependencies = [
|
|||
"wasi 0.10.2+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f647032dfaa1f8b6dc29bd3edb7bbef4861b8b8007ebb118d6db284fd59f6ee"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.2"
|
||||
|
@ -448,12 +367,6 @@ version = "1.12.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225"
|
||||
|
||||
[[package]]
|
||||
name = "os_str_bytes"
|
||||
version = "6.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "029d8d0b2f198229de29dca79676f2738ff952edf3fde542eb8bf94d8c21b435"
|
||||
|
||||
[[package]]
|
||||
name = "owning_ref"
|
||||
version = "0.4.1"
|
||||
|
@ -502,30 +415,6 @@ dependencies = [
|
|||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.39"
|
||||
|
@ -654,12 +543,6 @@ version = "1.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.95"
|
||||
|
@ -671,21 +554,6 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.31"
|
||||
|
@ -740,7 +608,6 @@ dependencies = [
|
|||
name = "udp-mud"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"crossbeam-channel",
|
||||
"cursive",
|
||||
"num_enum",
|
||||
|
@ -802,15 +669,6 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
|
|
|
@ -10,7 +10,6 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "3", features = ["derive"] }
|
||||
crossbeam-channel = "0.5"
|
||||
cursive = { version = "0.18", default-features = false, features = ["crossterm-backend"] }
|
||||
num_enum = "0.5"
|
||||
|
|
272
src/main.rs
272
src/main.rs
|
@ -1,260 +1,22 @@
|
|||
use clap::Parser;
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use cursive::Cursive;
|
||||
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
||||
use protocol::*;
|
||||
use protocol_derive::{Decode, Encode};
|
||||
use std::collections::HashMap;
|
||||
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
|
||||
|
||||
mod pronouns;
|
||||
mod protocol;
|
||||
mod state;
|
||||
mod tui;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(
|
||||
author = "Marceline Cramer",
|
||||
about = "Experimental distributed UDP chat."
|
||||
)]
|
||||
pub struct Args {
|
||||
/// Name you appear as to other peers.
|
||||
#[clap(short, long)]
|
||||
pub username: String,
|
||||
|
||||
/// Address to bind to.
|
||||
#[clap(short, long)]
|
||||
pub bind_addr: SocketAddr,
|
||||
|
||||
/// Other address to initiate connection with.
|
||||
#[clap(short, long)]
|
||||
pub connect: Option<SocketAddr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, IntoPrimitive, TryFromPrimitive)]
|
||||
#[repr(u16)]
|
||||
pub enum PacketKind {
|
||||
Ping,
|
||||
Pong,
|
||||
RequestUserInfo,
|
||||
RequestRoomInfo,
|
||||
RequestRoomList,
|
||||
UserInfo,
|
||||
RoomInfo,
|
||||
RoomList,
|
||||
Message,
|
||||
}
|
||||
|
||||
#[derive(Debug, Decode, Encode)]
|
||||
pub struct UserInfo {
|
||||
pub id: String,
|
||||
pub username: String,
|
||||
pub about: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Decode, Encode)]
|
||||
pub struct RoomInfo {
|
||||
pub id: String,
|
||||
pub title: String,
|
||||
pub short_about: String,
|
||||
pub long_about: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Decode, Encode)]
|
||||
pub struct RoomList {
|
||||
pub room_ids: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Decode, Encode)]
|
||||
pub struct Message {
|
||||
pub sender: String,
|
||||
pub contents: String,
|
||||
}
|
||||
|
||||
pub struct Room {
|
||||
pub info: RoomInfo,
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
args: Args,
|
||||
socket: UdpSocket,
|
||||
cursive: Cursive,
|
||||
owned_rooms: HashMap<String, Room>,
|
||||
remote_rooms: HashMap<String, Room>,
|
||||
message_sender: Sender<String>,
|
||||
message_receiver: Receiver<String>,
|
||||
// TODO connection management
|
||||
other: Option<SocketAddr>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(args: Args) -> Self {
|
||||
let socket = UdpSocket::bind(args.bind_addr).unwrap();
|
||||
socket.set_nonblocking(true).unwrap();
|
||||
|
||||
let (message_sender, message_receiver) = crossbeam_channel::unbounded();
|
||||
|
||||
let cursive = tui::make_cursive(message_sender.to_owned());
|
||||
|
||||
let mut app = Self {
|
||||
args,
|
||||
socket,
|
||||
cursive,
|
||||
owned_rooms: Default::default(),
|
||||
remote_rooms: Default::default(),
|
||||
message_sender,
|
||||
message_receiver,
|
||||
other: None,
|
||||
};
|
||||
app.startup();
|
||||
app
|
||||
}
|
||||
|
||||
pub fn startup(&mut self) {
|
||||
if let Some(connect) = self.args.connect.as_ref() {
|
||||
self.send_empty_packet(connect, PacketKind::Ping).unwrap();
|
||||
}
|
||||
|
||||
let room = Room {
|
||||
info: RoomInfo {
|
||||
id: format!("{}_owned_room", self.args.username),
|
||||
title: format!("{}'s Bombass Owned Room", self.args.username),
|
||||
short_about: "An automatically-created room for testing.".into(),
|
||||
long_about: "".into(),
|
||||
},
|
||||
};
|
||||
|
||||
self.owned_rooms.insert(room.info.id.clone(), room);
|
||||
}
|
||||
|
||||
pub fn run(mut self) {
|
||||
let mut siv = tui::make_cursive(self.message_sender.to_owned());
|
||||
let siv_backend = cursive::backends::try_default().unwrap();
|
||||
let mut siv_runner = siv.runner(siv_backend);
|
||||
siv_runner.refresh();
|
||||
|
||||
while siv_runner.is_running() {
|
||||
siv_runner.step();
|
||||
|
||||
let mut buf = [0u8; 65507];
|
||||
// TODO error handling of non-non-blocking errors
|
||||
if let Ok((len, from)) = self.socket.recv_from(&mut buf) {
|
||||
let mut buf = buf.as_slice();
|
||||
|
||||
let kind = Var::<u16>::decode(&mut buf).unwrap().0;
|
||||
let kind: PacketKind = match kind.try_into() {
|
||||
Ok(kind) => kind,
|
||||
Err(int) => {
|
||||
eprintln!("unrecognized packet kind {}", int);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(message) = self.on_packet(from, kind, buf) {
|
||||
tui::add_message(&mut siv_runner, &message);
|
||||
siv_runner.refresh(); // TODO better refresh management
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(other) = self.other.as_ref() {
|
||||
while let Ok(message) = self.message_receiver.try_recv() {
|
||||
eprintln!("sending message: {}", message);
|
||||
self.send_packet(other, PacketKind::Message, |writer| {
|
||||
let message = Message {
|
||||
sender: self.args.username.clone(),
|
||||
contents: message,
|
||||
};
|
||||
message.encode(writer)
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_packet(&mut self, from: SocketAddr, kind: PacketKind, mut reader: &[u8]) -> Option<Message> {
|
||||
println!("handling {:?}", kind);
|
||||
|
||||
// TODO proper connection management
|
||||
self.other = Some(from);
|
||||
|
||||
match kind {
|
||||
PacketKind::Ping => self.send_empty_packet(from, PacketKind::Pong).unwrap(),
|
||||
PacketKind::Pong => self
|
||||
.send_empty_packet(from, PacketKind::RequestRoomList)
|
||||
.unwrap(),
|
||||
PacketKind::RequestRoomList => self
|
||||
.send_packet(from, PacketKind::RoomList, |writer| {
|
||||
let room_list = self.build_room_list();
|
||||
room_list.encode(writer)
|
||||
})
|
||||
.unwrap(),
|
||||
PacketKind::RoomList => {
|
||||
let room_list: RoomList = Decode::decode(&mut reader).unwrap();
|
||||
for room_id in room_list.room_ids.iter() {
|
||||
self.send_packet(from, PacketKind::RequestRoomInfo, |writer| {
|
||||
room_id.encode(writer)
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
PacketKind::RequestRoomInfo => {
|
||||
let room_id = String::decode(&mut reader).unwrap();
|
||||
if let Some(room) = self.owned_rooms.get(&room_id) {
|
||||
self.send_packet(from, PacketKind::RoomInfo, |writer| {
|
||||
room.info.encode(writer)
|
||||
})
|
||||
.unwrap();
|
||||
} else {
|
||||
eprintln!("Unrecognized room info request for {}", room_id);
|
||||
}
|
||||
}
|
||||
PacketKind::RoomInfo => {
|
||||
let info = RoomInfo::decode(&mut reader).unwrap();
|
||||
eprintln!("Received room info: {:#?}", info);
|
||||
self.remote_rooms.insert(info.id.clone(), Room { info });
|
||||
}
|
||||
PacketKind::Message => {
|
||||
let message = Message::decode(&mut reader).unwrap();
|
||||
return Some(message);
|
||||
}
|
||||
kind => eprintln!("unimplemented packet handler for {:?}", kind),
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn build_room_list(&self) -> RoomList {
|
||||
let room_ids: Vec<_> = self
|
||||
.owned_rooms
|
||||
.iter()
|
||||
.map(|(id, _room)| id.to_owned())
|
||||
.collect();
|
||||
RoomList { room_ids }
|
||||
}
|
||||
|
||||
pub fn send_packet(
|
||||
&self,
|
||||
addr: impl ToSocketAddrs,
|
||||
kind: PacketKind,
|
||||
encode: impl FnOnce(&mut Vec<u8>) -> std::io::Result<()>,
|
||||
) -> std::io::Result<()> {
|
||||
let mut buf = Vec::new();
|
||||
Var(kind as u16).encode(&mut buf)?;
|
||||
encode(&mut buf)?;
|
||||
self.socket.send_to(&buf, addr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn send_empty_packet(
|
||||
&self,
|
||||
addr: impl ToSocketAddrs,
|
||||
kind: PacketKind,
|
||||
) -> std::io::Result<()> {
|
||||
self.send_packet(addr, kind, |_| Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
let app = App::new(args);
|
||||
app.run();
|
||||
let mut siv = tui::make_cursive();
|
||||
let siv_backend = cursive::backends::try_default().unwrap();
|
||||
let mut siv_runner = siv.runner(siv_backend);
|
||||
siv_runner.refresh();
|
||||
|
||||
while siv_runner.is_running() {
|
||||
siv_runner.step();
|
||||
|
||||
let err = siv_runner.with_user_data(|state: &mut state::State| state.poll().err());
|
||||
|
||||
if let Some(Some(err)) = err {
|
||||
let dialog = cursive::views::Dialog::info(format!("{:?}", err));
|
||||
siv_runner.add_layer(dialog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,10 @@ pub fn make_presets() -> Vec<Pronouns> {
|
|||
|
||||
#[derive(Clone, Debug, Decode, Encode)]
|
||||
pub struct Pronouns {
|
||||
/// Disables capitalization of [PronounTable] entries.
|
||||
pub case_sensitive: bool,
|
||||
|
||||
/// Enables grammatical plurality.
|
||||
pub plural: bool,
|
||||
|
||||
/// Ex. he, she, they, fae.
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
use num_enum::{IntoPrimitive, TryFromPrimitive};
|
||||
use protocol_derive::{Encode, Decode};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
|
||||
#[repr(u16)]
|
||||
pub enum PacketKind {
|
||||
HandshakeRequest,
|
||||
HandshakeAccept,
|
||||
HandshakeFinal,
|
||||
Ping,
|
||||
Pong,
|
||||
Message,
|
||||
}
|
||||
|
||||
#[derive(Debug, Decode, Encode)]
|
||||
pub struct Message {
|
||||
pub sender: String,
|
||||
pub contents: String,
|
||||
}
|
|
@ -0,0 +1,210 @@
|
|||
use crate::pronouns::Pronouns;
|
||||
use crate::protocol::*;
|
||||
use protocol::*;
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult};
|
||||
use std::net::{Ipv4Addr, SocketAddr, UdpSocket};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Identity {
|
||||
pub username: String,
|
||||
pub about: String,
|
||||
pub pronouns: Option<Pronouns>,
|
||||
}
|
||||
|
||||
pub struct LoginInfo {
|
||||
pub identity: Identity,
|
||||
pub listen_port: u16,
|
||||
}
|
||||
|
||||
pub struct LocalSocket {
|
||||
pub listen_socket: UdpSocket,
|
||||
}
|
||||
|
||||
impl LocalSocket {
|
||||
pub fn new(port: u16) -> Result<Self, IoError> {
|
||||
let ip = Ipv4Addr::UNSPECIFIED.into();
|
||||
let addr = SocketAddr::new(ip, port);
|
||||
let listen_socket = UdpSocket::bind(addr)?;
|
||||
listen_socket.set_nonblocking(true)?;
|
||||
Ok(Self { listen_socket })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub local_socket: Arc<LocalSocket>,
|
||||
pub connections: Vec<Connection>,
|
||||
pub identity: Identity,
|
||||
pub next_uid: usize,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn new(info: LoginInfo) -> Result<Self, IoError> {
|
||||
let LoginInfo {
|
||||
identity,
|
||||
listen_port,
|
||||
} = info;
|
||||
|
||||
let local_socket = Arc::new(LocalSocket::new(listen_port)?);
|
||||
let connections = Vec::new();
|
||||
let identity = identity;
|
||||
let next_uid = 0;
|
||||
|
||||
Ok(Self {
|
||||
local_socket,
|
||||
connections,
|
||||
identity,
|
||||
next_uid,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn poll(&mut self) -> Result<(), IoError> {
|
||||
let mut buf = [0u8; 65507];
|
||||
|
||||
loop {
|
||||
match self.local_socket.listen_socket.recv_from(&mut buf) {
|
||||
Ok((len, from)) => {
|
||||
let packet = &buf[0..len];
|
||||
self.on_packet(from, packet)?;
|
||||
}
|
||||
Err(e) if e.kind() == IoErrorKind::WouldBlock => break,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_packet_kind(reader: &mut impl std::io::Read) -> IoResult<PacketKind> {
|
||||
let kind = Var::<u16>::decode(reader)?.0;
|
||||
kind.try_into()
|
||||
.map_err(|_| IoErrorKind::InvalidInput.into())
|
||||
}
|
||||
|
||||
pub fn on_packet(&mut self, from: SocketAddr, mut packet: &[u8]) -> IoResult<()> {
|
||||
let kind = Self::get_packet_kind(&mut packet)?;
|
||||
let connection = self.connections.iter_mut().find(|c| c.other_addr == from);
|
||||
|
||||
if let Some(connection) = connection {
|
||||
connection.on_packet(kind, &mut packet)?;
|
||||
} else if kind == PacketKind::HandshakeRequest {
|
||||
let sock = self.local_socket.to_owned();
|
||||
let uid = self.next_uid;
|
||||
let connection = Connection::new_requested(sock, uid, from);
|
||||
self.connections.push(connection);
|
||||
self.next_uid += 1;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn connect(&mut self, to: SocketAddr) -> IoResult<()> {
|
||||
let sock = self.local_socket.to_owned();
|
||||
let uid = self.next_uid;
|
||||
let connection = Connection::connect(sock, uid, to)?;
|
||||
self.connections.push(connection);
|
||||
self.next_uid += 1;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Connection {
|
||||
/// A handle to the local socket this connection is using.
|
||||
pub local_socket: Arc<LocalSocket>,
|
||||
|
||||
/// Local unique identifier for this connection.
|
||||
pub uid: usize,
|
||||
|
||||
/// The remote address of the other end of this connection.
|
||||
pub other_addr: SocketAddr,
|
||||
|
||||
/// The current state.
|
||||
pub state: ConnectionState,
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub fn connect(
|
||||
local_socket: Arc<LocalSocket>,
|
||||
uid: usize,
|
||||
other_addr: SocketAddr,
|
||||
) -> IoResult<Self> {
|
||||
let connection = Self {
|
||||
local_socket,
|
||||
uid,
|
||||
other_addr,
|
||||
state: ConnectionState::Connecting,
|
||||
};
|
||||
|
||||
connection.send_empty_packet(PacketKind::HandshakeRequest)?;
|
||||
Ok(connection)
|
||||
}
|
||||
|
||||
pub fn new_requested(
|
||||
local_socket: Arc<LocalSocket>,
|
||||
uid: usize,
|
||||
other_addr: SocketAddr,
|
||||
) -> Self {
|
||||
Self {
|
||||
local_socket,
|
||||
uid,
|
||||
other_addr,
|
||||
state: ConnectionState::Pending,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_packet(&mut self, kind: PacketKind, mut packet: &[u8]) -> Result<(), IoError> {
|
||||
match self.state {
|
||||
ConnectionState::Connecting => match kind {
|
||||
PacketKind::HandshakeAccept => {
|
||||
self.state = ConnectionState::Connected;
|
||||
self.send_empty_packet(PacketKind::HandshakeFinal)?;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
ConnectionState::Pending => {}
|
||||
ConnectionState::Accepted => match kind {
|
||||
PacketKind::HandshakeFinal => {
|
||||
self.state = ConnectionState::Connected;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
ConnectionState::Connected => match kind {
|
||||
PacketKind::Ping => self.send_empty_packet(PacketKind::Pong)?,
|
||||
PacketKind::Pong => {}
|
||||
PacketKind::Message => {
|
||||
let message = Message::decode(&mut packet)?;
|
||||
eprintln!("message: {:?}", message);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn send_packet(
|
||||
&self,
|
||||
kind: PacketKind,
|
||||
encode: impl FnOnce(&mut Vec<u8>) -> std::io::Result<()>,
|
||||
) -> IoResult<()> {
|
||||
let mut buf = Vec::new();
|
||||
Var(kind as u16).encode(&mut buf)?;
|
||||
encode(&mut buf)?;
|
||||
self.local_socket
|
||||
.listen_socket
|
||||
.send_to(&buf, self.other_addr)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn send_empty_packet(&self, kind: PacketKind) -> IoResult<()> {
|
||||
self.send_packet(kind, |_| Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum ConnectionState {
|
||||
Connecting,
|
||||
Pending,
|
||||
Accepted,
|
||||
Connected,
|
||||
}
|
168
src/tui.rs
168
src/tui.rs
|
@ -1,4 +1,6 @@
|
|||
use crate::pronouns::Pronouns;
|
||||
use crate::protocol::Message;
|
||||
use crate::state::State;
|
||||
use crossbeam_channel::Sender;
|
||||
use cursive::align::*;
|
||||
use cursive::event::{Event, Key};
|
||||
|
@ -8,9 +10,17 @@ use cursive::view::*;
|
|||
use cursive::views::*;
|
||||
use cursive::Cursive;
|
||||
|
||||
pub fn make_cursive(message_sender: Sender<String>) -> Cursive {
|
||||
#[derive(Clone, Default)]
|
||||
pub struct LoginForm {
|
||||
pub username: Option<String>,
|
||||
pub about: Option<String>,
|
||||
pub port: Option<String>,
|
||||
pub pronouns: Option<Pronouns>,
|
||||
}
|
||||
|
||||
pub fn make_cursive() -> Cursive {
|
||||
let mut cursive = Cursive::new();
|
||||
cursive.set_user_data(message_sender);
|
||||
cursive.set_user_data(LoginForm::default());
|
||||
|
||||
cursive.update_theme(|theme| {
|
||||
theme.shadow = false;
|
||||
|
@ -32,7 +42,7 @@ pub fn make_cursive(message_sender: Sender<String>) -> Cursive {
|
|||
cursive
|
||||
}
|
||||
|
||||
pub fn add_message(siv: &mut Cursive, message: &crate::Message) {
|
||||
pub fn add_message(siv: &mut Cursive, message: &Message) {
|
||||
siv.call_on_name("messages_list", |messages: &mut LinearLayout| {
|
||||
let text = format!("{:<16}{}", message.sender, message.contents);
|
||||
let mut text = TextView::new(text);
|
||||
|
@ -52,31 +62,104 @@ pub fn add_message(siv: &mut Cursive, message: &crate::Message) {
|
|||
|
||||
pub fn show_welcome_mat(siv: &mut Cursive) {
|
||||
let logo = TextView::new(include_str!("logo.txt")).center();
|
||||
let labels = TextView::new("Identity:");
|
||||
let values = Button::new_raw("<none>", |siv| edit_identity(siv));
|
||||
let labels = make_vertical_labels(&["Identity:", "Listen port:"]);
|
||||
|
||||
let identity = Button::new_raw("<none>", |siv| edit_identity(siv)).with_name("identity_button");
|
||||
let port = EditView::new().with_name("port_edit");
|
||||
|
||||
let values = LinearLayout::vertical().child(identity).child(port);
|
||||
|
||||
let config = LinearLayout::horizontal().child(labels).child(values);
|
||||
let layout = LinearLayout::vertical().child(logo).child(config);
|
||||
let dialog = Dialog::around(layout)
|
||||
.title("Welcome User!")
|
||||
.button("Link start!", |siv| show_main(siv))
|
||||
.button("Link start!", |siv| {
|
||||
let mut form = siv.with_user_data(|f: &mut LoginForm| f.clone()).unwrap();
|
||||
|
||||
let port = get_edit_contents(siv, "port_edit");
|
||||
form.port = Some(port.clone());
|
||||
|
||||
let username = match form.username.as_ref() {
|
||||
Some(val) => val.clone(),
|
||||
None => {
|
||||
let dialog = Dialog::info("Please select an identity!");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
};
|
||||
|
||||
let about = match form.about.as_ref() {
|
||||
Some(val) => val.clone(),
|
||||
None => {
|
||||
let dialog = Dialog::info("Please select an identity!");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
};
|
||||
|
||||
let pronouns = form.pronouns.clone();
|
||||
|
||||
let listen_port: u16 = match port.parse() {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
let dialog = Dialog::info("Please select a valid port number!");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
};
|
||||
|
||||
let info = crate::state::LoginInfo {
|
||||
identity: crate::state::Identity {
|
||||
username,
|
||||
about,
|
||||
pronouns,
|
||||
},
|
||||
listen_port,
|
||||
};
|
||||
|
||||
let state = match crate::state::State::new(info) {
|
||||
Ok(val) => val,
|
||||
Err(err) => {
|
||||
let message = format!("{:?}", err);
|
||||
let dialog = Dialog::info(message).title("Login Error");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
};
|
||||
|
||||
siv.set_user_data(state);
|
||||
siv.pop_layer();
|
||||
show_main(siv);
|
||||
})
|
||||
.button("Quit", Cursive::quit);
|
||||
siv.add_layer(dialog);
|
||||
}
|
||||
|
||||
pub fn edit_identity(siv: &mut Cursive) {
|
||||
let labels = make_vertical_labels(&["Name:", "About:", "Pronouns:"]).fixed_width(10);
|
||||
let labels = make_vertical_labels(&["Username:", "About:", "Pronouns:"]).fixed_width(10);
|
||||
|
||||
let values = LinearLayout::vertical()
|
||||
.child(EditView::new().with_name("name_edit"))
|
||||
.child(EditView::new().with_name("username_edit"))
|
||||
.child(EditView::new().with_name("about_edit"))
|
||||
.child(TextView::new("<none>").with_name("pronouns_text"))
|
||||
.child(Button::new("none", |siv| select_pronouns(siv)).with_name("pronouns_button"))
|
||||
.fixed_width(45);
|
||||
|
||||
let columns = LinearLayout::horizontal().child(labels).child(values);
|
||||
let dialog = Dialog::around(columns)
|
||||
.title("Edit Identity")
|
||||
.button("Select Pronouns...", |siv| select_pronouns(siv))
|
||||
.dismiss_button("Ok");
|
||||
let dialog =
|
||||
Dialog::around(columns)
|
||||
.title("Edit Identity")
|
||||
.button("Ok", |siv: &mut Cursive| {
|
||||
let username = get_edit_contents(siv, "username_edit");
|
||||
let about = get_edit_contents(siv, "about_edit");
|
||||
|
||||
if username.chars().count() < 1 {
|
||||
let dialog = Dialog::info("Usernames must be at least one character!");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
|
||||
siv.with_user_data(|form: &mut LoginForm| {
|
||||
form.username = Some(username);
|
||||
form.about = Some(about);
|
||||
});
|
||||
|
||||
siv.pop_layer();
|
||||
});
|
||||
|
||||
siv.add_layer(dialog);
|
||||
}
|
||||
|
@ -94,10 +177,14 @@ fn update_pronouns_edit(siv: &mut Cursive, pronouns: &Pronouns) {
|
|||
view.set_content(pronouns.make_example_usage());
|
||||
});
|
||||
|
||||
siv.call_on_name("pronouns_text", |view: &mut TextView| {
|
||||
view.set_content(pronouns.format_full());
|
||||
siv.call_on_name("pronouns_button", |view: &mut Button| {
|
||||
view.set_label(pronouns.format_full());
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
siv.with_user_data(|form: &mut LoginForm| {
|
||||
form.pronouns = Some(pronouns.clone());
|
||||
});
|
||||
}
|
||||
|
||||
pub fn select_pronouns(siv: &mut Cursive) {
|
||||
|
@ -127,10 +214,15 @@ pub fn select_pronouns(siv: &mut Cursive) {
|
|||
})
|
||||
.dismiss_button("Ok")
|
||||
.button("None", |siv| {
|
||||
siv.call_on_name("pronouns_text", |view: &mut TextView| {
|
||||
view.set_content("<none>");
|
||||
siv.call_on_name("pronouns_button", |view: &mut Button| {
|
||||
view.set_label("none");
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
siv.with_user_data(|form: &mut LoginForm| {
|
||||
form.pronouns = None;
|
||||
});
|
||||
|
||||
siv.pop_layer();
|
||||
});
|
||||
siv.add_layer(dialog);
|
||||
|
@ -244,7 +336,7 @@ pub fn show_main(siv: &mut Cursive) {
|
|||
});
|
||||
add_message(
|
||||
siv,
|
||||
&crate::Message {
|
||||
&Message {
|
||||
sender: "me".to_string(),
|
||||
contents: text.to_owned(),
|
||||
},
|
||||
|
@ -287,12 +379,12 @@ pub fn show_main(siv: &mut Cursive) {
|
|||
.title_position(HAlign::Left)
|
||||
.with_name("room_select");
|
||||
|
||||
let mut connections = SelectView::new();
|
||||
connections.add_item("Connection 1", "connection_1");
|
||||
let connections = LinearLayout::vertical().with_name("connections_list");
|
||||
|
||||
let connections = Dialog::around(connections)
|
||||
.button("Add...", |siv| show_create_connection(siv))
|
||||
.title("Connections")
|
||||
.title_position(HAlign::Left)
|
||||
.with_name("connection_s");
|
||||
.title_position(HAlign::Left);
|
||||
|
||||
let sidebar = LinearLayout::vertical()
|
||||
.child(rooms)
|
||||
|
@ -304,6 +396,38 @@ pub fn show_main(siv: &mut Cursive) {
|
|||
siv.add_fullscreen_layer(layout);
|
||||
}
|
||||
|
||||
fn show_create_connection(siv: &mut Cursive) {
|
||||
let labels = make_vertical_labels(&["IP:Port:"]);
|
||||
let values = EditView::new().with_name("address_edit").min_width(20);
|
||||
let layout = LinearLayout::horizontal().child(labels).child(values);
|
||||
let dialog = Dialog::around(layout)
|
||||
.title("Create Connection")
|
||||
.button("Ok", |siv| {
|
||||
let address = get_edit_contents(siv, "address_edit");
|
||||
|
||||
let address = match address.parse() {
|
||||
Ok(address) => address,
|
||||
Err(err) => {
|
||||
let message = format!("{:?}", err);
|
||||
let dialog = Dialog::info(message).title("Create Connection Error");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
};
|
||||
|
||||
let err = siv.with_user_data(|state: &mut State| state.connect(address).err());
|
||||
|
||||
if let Some(Some(err)) = err {
|
||||
let message = format!("{:?}", err);
|
||||
let dialog = Dialog::info(message).title("Connection Error");
|
||||
return siv.add_layer(dialog);
|
||||
}
|
||||
|
||||
siv.pop_layer();
|
||||
})
|
||||
.dismiss_button("Cancel");
|
||||
siv.add_layer(dialog);
|
||||
}
|
||||
|
||||
fn get_edit_contents(siv: &mut Cursive, name: &str) -> String {
|
||||
siv.call_on_name(name, |view: &mut EditView| view.get_content())
|
||||
.unwrap()
|
||||
|
|
Loading…
Reference in New Issue