Struct tdn::prelude::HandleResult

source ·
pub struct HandleResult {
    pub owns: Vec<SendType>,
    pub rpcs: Vec<Value>,
    pub groups: Vec<SendType>,
    pub layers: Vec<(u64, SendType)>,
    pub networks: Vec<NetworkType>,
}
Expand description

Helper: this is the group/layer/rpc handle result in the network.

Fields§

§owns: Vec<SendType>

P2P network with same PeerId.

§rpcs: Vec<Value>

rpc tasks: [(method, params)].

§groups: Vec<SendType>

group tasks: [GroupSendMessage]

§layers: Vec<(u64, SendType)>

layer tasks: [LayerSendMessage]

§networks: Vec<NetworkType>

network tasks: NetworkType

Implementations§

source§

impl<'a> HandleResult

source

pub fn new() -> HandleResult

source

pub fn own(m: SendType) -> HandleResult

source

pub fn rpc(p: Value) -> HandleResult

Examples found in repository?
examples/simple.rs (line 18)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
async fn main() {
    let (peer_addr, send, mut out_recv) = start().await.unwrap();
    println!("Example: peer id: {:?}", peer_addr);

    let mut rpc_handler = RpcHandler::new(State(1));
    rpc_handler.add_method(
        "echo",
        |params: Vec<RpcParam>, state: Arc<State>| async move {
            let _value = params[0].as_str().ok_or(RpcError::ParseError)?;
            assert_eq!(1, state.0);
            Ok(HandleResult::rpc(json!(params)))
        },
    );

    rpc_handler.add_method("say_hello", |_params, state: Arc<State>| async move {
        assert_eq!(1, state.0);
        Ok(HandleResult::rpc(json!("hello")))
    });

    while let Some(message) = out_recv.recv().await {
        match message {
            ReceiveMessage::Own(msg) => match msg {
                RecvType::Connect(peer, _data) => {
                    println!("receive own peer {} join", peer.id.short_show());
                }
                RecvType::Leave(peer) => {
                    println!("receive own peer {} leave", peer.id.short_show());
                }
                RecvType::Event(peer_id, _data) => {
                    println!("receive own event from {}", peer_id.short_show());
                }
                _ => {
                    println!("nerver here!")
                }
            },
            ReceiveMessage::Group(msg) => match msg {
                RecvType::Connect(peer, _data) => {
                    println!("receive group peer {} join", peer.id.short_show());
                }
                RecvType::Result(..) => {
                    //
                }
                RecvType::Leave(peer) => {
                    println!("receive group peer {} leave", peer.id.short_show());
                }
                RecvType::Event(peer_id, _data) => {
                    println!("receive group event from {}", peer_id.short_show());
                }
                _ => {}
            },
            ReceiveMessage::Layer(fgid, _tgid, msg) => match msg {
                RecvType::Connect(peer, _data) => {
                    println!("Layer Join: {}, Addr: {}.", fgid, peer.id.short_show());
                }
                RecvType::Result(..) => {
                    //
                }
                _ => {}
            },
            ReceiveMessage::Rpc(uid, params, is_ws) => {
                if let Ok(HandleResult {
                    mut rpcs,
                    owns: _,
                    groups: _,
                    layers: _,
                    networks: _,
                }) = rpc_handler.handle(params).await
                {
                    loop {
                        if rpcs.len() != 0 {
                            let msg = rpcs.remove(0);
                            send.send(SendMessage::Rpc(uid, msg, is_ws))
                                .await
                                .expect("TDN channel closed");
                        } else {
                            break;
                        }
                    }
                }
            }
            ReceiveMessage::NetworkLost => {
                println!("No network connections");
            }
        }
    }
}
source

pub fn group(m: SendType) -> HandleResult

source

pub fn layer(gid: u64, m: SendType) -> HandleResult

source

pub fn network(m: NetworkType) -> HandleResult

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more