aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/src/logic/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'filamento/src/logic/mod.rs')
-rw-r--r--filamento/src/logic/mod.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/filamento/src/logic/mod.rs b/filamento/src/logic/mod.rs
new file mode 100644
index 0000000..638f682
--- /dev/null
+++ b/filamento/src/logic/mod.rs
@@ -0,0 +1,90 @@
+use std::{collections::HashMap, sync::Arc};
+
+use lampada::{Logic, error::ReadError};
+use stanza::client::Stanza;
+use tokio::sync::{Mutex, mpsc, oneshot};
+
+use crate::{Command, UpdateMessage, db::Db};
+
+mod abort;
+mod connect;
+mod connection_error;
+mod disconnect;
+mod offline;
+mod online;
+mod process_stanza;
+
+#[derive(Clone)]
+pub struct ClientLogic {
+ db: Db,
+ pending: Arc<Mutex<HashMap<String, oneshot::Sender<Result<Stanza, ReadError>>>>>,
+ update_sender: mpsc::Sender<UpdateMessage>,
+}
+
+impl ClientLogic {
+ pub fn new(
+ db: Db,
+ pending: Arc<Mutex<HashMap<String, oneshot::Sender<Result<Stanza, ReadError>>>>>,
+ update_sender: mpsc::Sender<UpdateMessage>,
+ ) -> Self {
+ Self {
+ db,
+ pending,
+ update_sender,
+ }
+ }
+
+ pub fn db(&self) -> &Db {
+ &self.db
+ }
+
+ pub fn pending(&self) -> &Mutex<HashMap<String, oneshot::Sender<Result<Stanza, ReadError>>>> {
+ &self.pending.as_ref()
+ }
+
+ pub fn update_sender(&self) -> &mpsc::Sender<UpdateMessage> {
+ &self.update_sender
+ }
+}
+
+impl Logic for ClientLogic {
+ type Cmd = Command;
+
+ // pub async fn handle_stream_error(self, error) {}
+ // stanza errors (recoverable)
+ // pub async fn handle_error(self, error: Error) {}
+ // when it aborts, must clear iq map no matter what
+
+ async fn handle_connect(self, connection: lampada::Connected) {
+ connect::handle_connect(self, connection).await;
+ }
+
+ async fn handle_disconnect(self, connection: lampada::Connected) {
+ disconnect::handle_disconnect(self, connection).await;
+ }
+
+ async fn handle_stanza(
+ self,
+ stanza: ::stanza::client::Stanza,
+ connection: lampada::Connected,
+ supervisor: lampada::SupervisorSender,
+ ) {
+ process_stanza::handle_stanza(self, stanza, connection, supervisor).await;
+ }
+
+ async fn handle_online(self, command: Self::Cmd, connection: lampada::Connected) {
+ online::handle_online(self, command, connection).await;
+ }
+
+ async fn handle_offline(self, command: Self::Cmd) {
+ offline::handle_offline(self, command).await;
+ }
+
+ async fn on_abort(self) {
+ abort::on_abort(self).await;
+ }
+
+ async fn handle_connection_error(self, error: lampada::error::ConnectionError) {
+ connection_error::handle_connection_error(self, error).await;
+ }
+}