Last active
October 23, 2017 23:22
-
-
Save jocutajar/a85eb3cfd3aa956e95e978af596d44ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
use bytes::Bytes; | |
use tokio_service::Service; | |
use tokio_proto::streaming::{Message, Body}; | |
use futures::{future, Future, Stream}; | |
use futures::sync::oneshot; | |
use model::request::SmtpCommand; | |
use model::response::SmtpReply; | |
pub struct SmtpService; | |
impl Service for SmtpService { | |
// For non-streaming protocols, service errors are always io::Error | |
type Error = io::Error; | |
// These types must match the corresponding protocol types: | |
type Request = Message<SmtpCommand, Body<Bytes, Self::Error>>; | |
type Response = Message<SmtpReply, Body<SmtpReply, Self::Error>>; | |
// The future for computing the response; box it for simplicity. | |
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>; | |
// Produce a future for computing a response from a request. | |
fn call(&self, command: Self::Request) -> Self::Future { | |
info!("Received {:?}", command); | |
match command { | |
Message::WithBody(cmd, cmd_body) => { | |
match cmd { | |
SmtpCommand::Data => { | |
// start => SmtpReply::StartMailInputChallenge | |
// ok => SmtpReply::OkInfo | |
// err => SmtpReply::TransactionFailure | |
let (tx, rx) = oneshot::channel(); | |
let fut = cmd_body | |
.inspect(|chunk| info!("data: {:?}", chunk)) | |
.map(|_| tx.send(SmtpReply::OkInfo)) | |
.map_err(|_| tx.send(SmtpReply::TransactionFailure)) | |
.map(|_| Body::from(rx)); | |
// ??? How to wire the fut future into the response message? | |
let msg = Message::WithBody(SmtpReply::StartMailInputChallenge, fut); | |
Box::new(future::ok(msg)) as Self::Future | |
} | |
_ => Box::new(future::ok(Message::WithoutBody( | |
SmtpReply::CommandNotImplementedFailure, | |
))), | |
} | |
} | |
Message::WithoutBody(cmd) => { | |
Box::new(future::ok(Message::WithoutBody(match cmd { | |
SmtpCommand::Connect { peer_addr, .. } => SmtpReply::ServiceReadyInfo( | |
format!("Hi {:?}!", peer_addr), | |
), | |
_ => SmtpReply::CommandNotImplementedFailure, | |
}))) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment