1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use super::packet::*;
use super::IldcpAccount;
use futures::future::ok;
use interledger_packet::*;
use interledger_service::*;
use std::{marker::PhantomData, str};

/// A simple service that intercepts incoming ILDCP requests
/// and responds using the information in the Account struct.
#[derive(Clone)]
pub struct IldcpService<S, A> {
    next: S,
    account_type: PhantomData<A>,
}

impl<S, A> IldcpService<S, A>
where
    S: IncomingService<A>,
    A: IldcpAccount,
{
    pub fn new(next: S) -> Self {
        IldcpService {
            next,
            account_type: PhantomData,
        }
    }
}

impl<S, A> IncomingService<A> for IldcpService<S, A>
where
    S: IncomingService<A>,
    A: IldcpAccount,
{
    type Future = BoxedIlpFuture;

    fn handle_request(&mut self, request: IncomingRequest<A>) -> Self::Future {
        if is_ildcp_request(&request.prepare) {
            let builder = IldcpResponseBuilder {
                client_address: &request.from.client_address(),
                asset_code: request.from.asset_code(),
                asset_scale: request.from.asset_scale(),
            };
            debug!(
                "Responding to query for ILDCP info by account: {:?}",
                str::from_utf8(&request.from.client_address()[..]).unwrap_or("<not utf8>")
            );
            let response = builder.build();
            let fulfill = Fulfill::from(response);
            Box::new(ok(fulfill))
        } else {
            Box::new(self.next.handle_request(request))
        }
    }
}