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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

//! API module defines the DSF remote API
//! This allows for limited capability devices to perform network operations via
//! a full-featured device

use futures::prelude::*;

use crate::types::{Id, DataKind};


#[derive(Debug, Clone, PartialEq)]
pub struct ServiceHandle {
    pub id: Id,
}

/// A boxed future result to shorten method definitions
pub type FutureResult<I, E> = Box<dyn Future<Item=I, Error=E> + Send>;

/// Creation API used to create services
pub trait Create {
    type Options;
    type Error;

    /// Create a new service with the provided options
    fn create(options: &Self::Options) -> FutureResult<ServiceHandle, Self::Error>;
}


/// Producer API trait used to register an existing service
pub trait Register {
    type Error;

    /// Register a service in the distributed database
    fn register(&mut self, s: &mut ServiceHandle) -> FutureResult<(), Self::Error>;
}


/// Locate API trait used to find an existing service
pub trait Locate {
    type Error;

    /// Locate a DIoT service in the distributed database
    /// This returns a future that will resolve to the desired service or an error
    fn locate(&mut self, id: &Id) -> FutureResult<ServiceHandle, Self::Error>;
}


/// Publisher API trait used by publishers of service data
pub trait Publish {
    type Error;

    /// Publish service data
    fn publish(&mut self, s: &ServiceHandle, kind: Option<DataKind>, data: Option<&[u8]>) -> FutureResult<(), Self::Error>;
}

/// A boxed future stream to shorten method definitions
pub type FutureStream<I, E> = FutureResult<Box<dyn Stream<Item=I, Error=E>>, E>;

/// Subscriber API used by subscribers to service data
pub trait Subscribe {
    type Options;
    type Data;
    type Error;

    /// Locate a DIoT service in the distributed database
    /// This returns a future that will resolve to the desired service or an error
    fn subscribe(&mut self, service: &ServiceHandle, options: Self::Options) -> FutureStream<Self::Data, Self::Error>;
}