Implement a Mutex
This commit is contained in:
parent
b82df13082
commit
c03be0a8c3
2 changed files with 20 additions and 6 deletions
|
@ -1,4 +1,4 @@
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use boitalettres::errors::Error as BalError;
|
use boitalettres::errors::Error as BalError;
|
||||||
use boitalettres::proto::{Request, Response};
|
use boitalettres::proto::{Request, Response};
|
||||||
|
@ -6,14 +6,16 @@ use imap_codec::types::core::AString;
|
||||||
use imap_codec::types::response::{Capability, Data};
|
use imap_codec::types::response::{Capability, Data};
|
||||||
|
|
||||||
use crate::mailstore;
|
use crate::mailstore;
|
||||||
|
use crate::service;
|
||||||
|
|
||||||
pub struct Command {
|
pub struct Command {
|
||||||
mailstore: Arc<mailstore::Mailstore>,
|
mailstore: Arc<mailstore::Mailstore>,
|
||||||
|
session: Arc<Mutex<service::Session>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
pub fn new(mailstore: Arc<mailstore::Mailstore>) -> Self {
|
pub fn new(mailstore: Arc<mailstore::Mailstore>, session: Arc<Mutex<service::Session>>) -> Self {
|
||||||
Self { mailstore }
|
Self { mailstore, session }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn capability(self) -> Result<Response, BalError> {
|
pub async fn capability(self) -> Result<Response, BalError> {
|
||||||
|
@ -36,6 +38,12 @@ impl Command {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut session = match self.session.lock() {
|
||||||
|
Err(_) => return Response::bad("[AUTHENTICATIONFAILED] Unable to acquire mutex."),
|
||||||
|
Ok(s) => s,
|
||||||
|
};
|
||||||
|
session.creds = Some(creds);
|
||||||
|
|
||||||
Response::ok("Logged in")
|
Response::ok("Logged in")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
@ -10,6 +10,7 @@ use imap_codec::types::command::CommandBody;
|
||||||
use tower::Service;
|
use tower::Service;
|
||||||
|
|
||||||
use crate::command;
|
use crate::command;
|
||||||
|
use crate::login::Credentials;
|
||||||
use crate::mailstore::Mailstore;
|
use crate::mailstore::Mailstore;
|
||||||
|
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
|
@ -36,12 +37,17 @@ impl<'a> Service<&'a AddrStream> for Instance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Session {
|
||||||
|
pub creds: Option<Credentials>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
pub mailstore: Arc<Mailstore>,
|
pub mailstore: Arc<Mailstore>,
|
||||||
|
pub session: Arc<Mutex<Session>>,
|
||||||
}
|
}
|
||||||
impl Connection {
|
impl Connection {
|
||||||
pub fn new(mailstore: Arc<Mailstore>) -> Self {
|
pub fn new(mailstore: Arc<Mailstore>) -> Self {
|
||||||
Self { mailstore }
|
Self { mailstore, session: Arc::new(Mutex::new(Session { creds: None })) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Service<Request> for Connection {
|
impl Service<Request> for Connection {
|
||||||
|
@ -55,7 +61,7 @@ impl Service<Request> for Connection {
|
||||||
|
|
||||||
fn call(&mut self, req: Request) -> Self::Future {
|
fn call(&mut self, req: Request) -> Self::Future {
|
||||||
tracing::debug!("Got request: {:#?}", req);
|
tracing::debug!("Got request: {:#?}", req);
|
||||||
let cmd = command::Command::new(self.mailstore.clone());
|
let cmd = command::Command::new(self.mailstore.clone(), self.session.clone());
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
match req.body {
|
match req.body {
|
||||||
CommandBody::Capability => cmd.capability().await,
|
CommandBody::Capability => cmd.capability().await,
|
||||||
|
|
Loading…
Reference in a new issue