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
/// Functions in this module are responsible for concealing the raw APIs
mod api;
pub(crate) mod formats;

use futures::channel::mpsc;
use futures::*;
use serde::{Deserialize, Serialize};

use crate::error;
use crate::peer::formats::EventEnum;
use formats::PeerId;
pub use formats::{
    CreatePeerQuery, CreatedResponse, PeerCallEvent, PeerCloseEvent, PeerConnectionEvent,
    PeerErrorEvent, PeerInfo, PeerOpenEvent, PeerStatusMessage,
};

/// Request to create Peer.
///
/// It's bindings for POST /peers
///
/// [API](http://35.200.46.204/#/1.peers/peer)
///
/// Notice: This api call does not guarantee that WebRTC Gateway creates a Peer Object successfully.
/// You need to wait OPEN event
/// This function returns PeerInfo just for starting receiving events
pub async fn create<'a>(
    api_key: impl Into<String>,
    domain: impl Into<String>,
    peer_id: PeerId,
    turn: bool,
) -> Result<PeerInfo, error::Error> {
    let base_url = crate::base_url();
    let result = api::create_peer(base_url, api_key, domain, peer_id, turn).await?;
    Ok(result.params)
}

/// Listen events of a Peer Object.
///
/// It's bindings for GET /peers/{peer_id}/events
///
/// [API](http://35.200.46.204/#/1.peers/peer_event)
///
/// This function need to repeat long-polling to WebRTC Gateway's peer event API.
/// When the API returns TIMEOUT events, this function ignore them and keep listening events.
/// It keep listening events till receiving CLOSE event or HTTP Error Codes.
pub async fn listen_events<'a>(
    peer_info: &PeerInfo,
    mut event_sender: mpsc::Sender<PeerEventEnum>,
) -> Result<(), error::Error> {
    let base_url = crate::base_url();
    loop {
        let result = api::event(base_url, peer_info).await?;

        match result {
            EventEnum::TIMEOUT => {}
            EventEnum::CLOSE(event) => {
                if event_sender
                    .send(PeerEventEnum::CLOSE(event))
                    .await
                    .is_err()
                {
                    return Err(error::Error::create_myerror("peer_create_and_listen_events send OPEN event, but observer doesn't receive i, but observer doesn't receive it."));
                };
                event_sender.close_channel();
                break;
            }
            EventEnum::OPEN(event) => {
                if event_sender.send(PeerEventEnum::OPEN(event)).await.is_err() {
                    return Err(error::Error::create_myerror("peer_create_and_listen_events send OPEN event, but observer doesn't receive i, but observer doesn't receive it."));
                };
            }
            EventEnum::CONNECTION(event) => {
                if event_sender
                    .send(PeerEventEnum::CONNECTION(event))
                    .await
                    .is_err()
                {
                    return Err(error::Error::create_myerror("peer_create_and_listen_events send OPEN event, but observer doesn't receive i, but observer doesn't receive it."));
                };
            }
            EventEnum::CALL(event) => {
                if event_sender.send(PeerEventEnum::CALL(event)).await.is_err() {
                    return Err(error::Error::create_myerror("peer_create_and_listen_events send OPEN event, but observer doesn't receive i, but observer doesn't receive it."));
                };
            }
            EventEnum::ERROR(event) => {
                if event_sender
                    .send(PeerEventEnum::ERROR(event))
                    .await
                    .is_err()
                {
                    return Err(error::Error::create_myerror("peer_create_and_listen_events send OPEN event, but observer doesn't receive i, but observer doesn't receive it."));
                };
            }
        }
    }
    Ok(())
}

/// Release PeerObject
///
/// It's bindings for DELETE /peers/{peer_id}
///
/// [API](http://35.200.46.204/#/1.peers/peer_destroy)
pub async fn delete(peer_info: &PeerInfo) -> Result<(), error::Error> {
    let base_url = crate::base_url();
    api::delete_peer(base_url, peer_info).await
}

/// Get status of PeerObject
///
/// It's bindings for GET /peers/{peer_id}/status
///
/// [API](http://35.200.46.204/#/1.peers/peer_status)
pub async fn status(peer_info: &PeerInfo) -> Result<formats::PeerStatusMessage, error::Error> {
    let base_url = crate::base_url();
    api::status(base_url, peer_info).await
}

/// Response from GET /peers/{peer_id}/events
///
/// [API](http://35.200.46.204/#/1.peers/peer_event)
#[derive(Serialize, Deserialize, Debug, Clone, PartialOrd, PartialEq)]
#[serde(tag = "event")]
pub enum PeerEventEnum {
    OPEN(PeerOpenEvent),
    CLOSE(PeerCloseEvent),
    CONNECTION(PeerConnectionEvent),
    CALL(PeerCallEvent),
    ERROR(PeerErrorEvent),
}