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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Copyright (c) 2017, 2020 ADLINK Technology Inc.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ADLINK zenoh team, <zenoh@adlink-labs.tech>
//
pub mod authenticator;
pub mod defaults;
mod initial;
mod manager;
mod primitives;
mod transport;

use crate::core::{PeerId, WhatAmI, ZInt};
use crate::link::Link;
use crate::proto::{smsg, ZenohMessage};
use async_std::sync::{Arc, Weak};
use async_trait::async_trait;
pub use manager::*;
pub use primitives::*;
use std::fmt;
use transport::*;
use zenoh_util::core::{ZError, ZErrorKind, ZResult};

/*********************************************************/
/* Session Callback to be implemented by the Upper Layer */
/*********************************************************/
#[async_trait]
pub trait SessionEventHandler {
    async fn handle_message(&self, msg: ZenohMessage) -> ZResult<()>;
    async fn new_link(&self, link: Link);
    async fn del_link(&self, link: Link);
    async fn closing(&self);
    async fn closed(&self);
}

#[async_trait]
pub trait SessionHandler {
    async fn new_session(
        &self,
        session: Session,
    ) -> ZResult<Arc<dyn SessionEventHandler + Send + Sync>>;
}

// Define an empty SessionCallback for the listener session
#[derive(Default)]
pub struct DummySessionEventHandler;

impl DummySessionEventHandler {
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl SessionEventHandler for DummySessionEventHandler {
    async fn handle_message(&self, _message: ZenohMessage) -> ZResult<()> {
        Ok(())
    }

    async fn new_link(&self, _link: Link) {}

    async fn del_link(&self, _link: Link) {}

    async fn closing(&self) {}

    async fn closed(&self) {}
}

/*************************************/
/*              SESSION              */
/*************************************/
const STR_ERR: &str = "Session closed";

/// [`Session`] is the session handler returned when opening a new session
#[derive(Clone)]
pub struct Session(Weak<SessionTransport>);

impl Session {
    fn new(inner: Weak<SessionTransport>) -> Self {
        Self(inner)
    }

    /*************************************/
    /*         SESSION ACCESSORS         */
    /*************************************/
    #[inline]
    pub(super) fn get_transport(&self) -> ZResult<Arc<SessionTransport>> {
        let transport = zweak!(self.0, STR_ERR);
        Ok(transport)
    }

    /*************************************/
    /*          PUBLIC ACCESSORS         */
    /*************************************/
    #[inline]
    pub fn get_pid(&self) -> ZResult<PeerId> {
        let transport = zweak!(self.0, STR_ERR);
        Ok(transport.get_pid())
    }

    #[inline]
    pub fn get_whatami(&self) -> ZResult<WhatAmI> {
        let transport = zweak!(self.0, STR_ERR);
        Ok(transport.get_whatami())
    }

    #[inline]
    pub fn get_sn_resolution(&self) -> ZResult<ZInt> {
        let transport = zweak!(self.0, STR_ERR);
        Ok(transport.get_sn_resolution())
    }

    #[inline]
    pub async fn close(&self) -> ZResult<()> {
        log::trace!("{:?}. Close", self);
        let transport = zweak!(self.0, STR_ERR);
        transport.close(smsg::close_reason::GENERIC).await
    }

    #[inline]
    pub async fn close_link(&self, link: &Link) -> ZResult<()> {
        let transport = zweak!(self.0, STR_ERR);
        transport
            .close_link(link, smsg::close_reason::GENERIC)
            .await?;
        Ok(())
    }

    #[inline]
    pub async fn get_links(&self) -> ZResult<Vec<Link>> {
        log::trace!("{:?}. Get links", self);
        let transport = zweak!(self.0, STR_ERR);
        Ok(transport.get_links().await)
    }

    #[inline]
    pub async fn schedule(&self, message: ZenohMessage) -> ZResult<()> {
        log::trace!("{:?}. Schedule: {:?}", self, message);
        let transport = zweak!(self.0, STR_ERR);
        transport.schedule(message).await;
        Ok(())
    }
}

#[async_trait]
impl SessionEventHandler for Session {
    #[inline]
    async fn handle_message(&self, message: ZenohMessage) -> ZResult<()> {
        self.schedule(message).await
    }

    async fn new_link(&self, _link: Link) {}
    async fn del_link(&self, _link: Link) {}
    async fn closing(&self) {}
    async fn closed(&self) {}
}

impl Eq for Session {}

impl PartialEq for Session {
    fn eq(&self, other: &Self) -> bool {
        Weak::ptr_eq(&self.0, &other.0)
    }
}

impl fmt::Debug for Session {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(transport) = self.0.upgrade() {
            f.debug_struct("Session")
                .field("peer", &transport.get_pid())
                .field("sn_resolution", &transport.get_sn_resolution())
                .finish()
        } else {
            write!(f, "{}", STR_ERR)
        }
    }
}