skyway_webrtc_gateway_api/data/
mod.rs

1mod api;
2pub(crate) mod formats;
3
4use futures::channel::mpsc;
5use futures::*;
6use serde::{Deserialize, Serialize};
7
8use crate::common::formats::{SerializableId, SocketInfo};
9use crate::error;
10
11pub use formats::{
12    ConnectQuery, ConnectQueryOption, DataConnectionId, DataConnectionIdWrapper,
13    DataConnectionStatus, DataId, DataIdWrapper, DcInit, RedirectDataParams, RedirectDataResponse,
14};
15
16#[derive(Serialize, Deserialize, Debug, Clone, PartialOrd, PartialEq)]
17/// Shows DataConnection events.
18///
19/// It's response from GET /data/connections/{data_connection_id}/events
20///
21/// [API](http://35.200.46.204/#/2.data/data_connection_events)
22#[serde(tag = "event")]
23pub enum DataConnectionEventEnum {
24    OPEN(DataConnectionIdWrapper),
25    CLOSE(DataConnectionIdWrapper),
26    ERROR((DataConnectionId, String)),
27    TIMEOUT,
28}
29
30/// This function let a WebRTC Gateway open a socket to receive media which will be redirected to neighbour peer.
31///
32/// # Examples
33/// ```
34/// use skyway_webrtc_gateway_api::data::open_data_socket;
35///
36/// async fn example() {
37///     let result = open_data_socket().await;
38/// }
39/// ```
40pub async fn open_data_socket() -> Result<SocketInfo<DataId>, error::Error> {
41    let base_url = super::base_url();
42    api::create_data(base_url).await
43}
44
45/// This function let a WebRTC Gateway close a socket to receive media which will be redirected to neighbour peer.
46///
47/// # Examples
48/// ```
49/// use skyway_webrtc_gateway_api::data::close_data_socket;
50/// use skyway_webrtc_gateway_api::prelude::{DataId, SerializableId};
51///
52/// async fn example() {
53///     let data_id = DataId::try_create("da-50a32bab-b3d9-4913-8e20-f79c90a6a211").unwrap();
54///     let result = close_data_socket(&data_id).await;
55/// }
56/// ```
57pub async fn close_data_socket(data_id: &DataId) -> Result<(), error::Error> {
58    let base_url = super::base_url();
59    api::delete_data(base_url, data_id.as_str()).await
60}
61
62/// This function let a WebRTC Gateway establish a DataChannel to neighbour
63///
64/// # Examples
65/// ```
66/// use skyway_webrtc_gateway_api::data::ConnectQuery;
67/// use skyway_webrtc_gateway_api::prelude::{PeerId, Token};
68///
69/// let query = ConnectQuery {
70///     peer_id: PeerId::new("peer_id"),
71///     token: Token::try_create("pt-9749250e-d157-4f80-9ee2-359ce8524308").unwrap(),
72///     options: None,
73///     target_id: PeerId::new("target_id"),
74///     params: None,
75///     redirect_params: None,
76/// };
77/// ```
78pub async fn connect(query: ConnectQuery) -> Result<DataConnectionId, error::Error> {
79    let base_url = super::base_url();
80    let result = api::create_data_connection(base_url, &query).await?;
81    Ok(result.params.data_connection_id)
82}
83
84/// This function let a WebRTC Gateway close a DataChannel
85///
86/// # Examples
87/// ```
88/// use skyway_webrtc_gateway_api::data::disconnect;
89/// use skyway_webrtc_gateway_api::prelude::DataConnectionId;
90///
91/// async fn example() {
92///     let data_connection_id = DataConnectionId::try_create("dc-4995f372-fb6a-4196-b30a-ce11e5c7f56c").unwrap();
93///     let result = disconnect(&data_connection_id).await;
94/// }
95/// ```
96pub async fn disconnect(data_connection_id: &DataConnectionId) -> Result<(), error::Error> {
97    let base_url = super::base_url();
98    api::delete_data_connection(base_url, data_connection_id.as_str()).await
99}
100
101/// DataConnection is automatically established when neighbour connect to this side.
102/// In that case, the connection doesn't have source and destination port information.
103/// This function set the information.
104///
105/// # Example
106/// ```
107/// use skyway_webrtc_gateway_api::prelude::{DataId, DataConnectionId, PhantomId, SocketInfo, SerializableSocket, SerializableId};
108/// use skyway_webrtc_gateway_api::data::{DataIdWrapper, RedirectDataParams, redirect};
109///
110/// async fn example() {
111///     let data_connection_id = DataConnectionId::try_create("dc-4995f372-fb6a-4196-b30a-ce11e5c7f56c").unwrap();
112///     let feed_params = Some(DataIdWrapper {
113///         data_id: DataId::try_create("da-50a32bab-b3d9-4913-8e20-f79c90a6a211").unwrap()
114///     });
115///     let redirect_params = SocketInfo::<PhantomId>::try_create(None, "127.0.0.1", 8000).unwrap();
116///     let redirect_params = RedirectDataParams {
117///         feed_params: feed_params,
118///         redirect_params: Some(redirect_params)
119///     };
120///     let result = redirect(&data_connection_id, &redirect_params).await;
121/// }
122/// ```
123pub async fn redirect(
124    data_connection_id: &DataConnectionId,
125    redirect_data_params: &RedirectDataParams,
126) -> Result<RedirectDataResponse, error::Error> {
127    let base_url = super::base_url();
128    api::redirect_data_connection(base_url, data_connection_id.as_str(), redirect_data_params).await
129}
130
131/// This function to get status of DataChannel
132///
133/// # Example
134/// ```
135/// use skyway_webrtc_gateway_api::prelude::DataConnectionId;
136/// use skyway_webrtc_gateway_api::data::status;
137///
138/// async fn example() {
139///     let data_connection_id = DataConnectionId::try_create("dc-4995f372-fb6a-4196-b30a-ce11e5c7f56c").unwrap();
140///     let result = status(&data_connection_id).await;
141/// }
142/// ```
143pub async fn status(
144    data_connection_id: &DataConnectionId,
145) -> Result<DataConnectionStatus, error::Error> {
146    let base_url = super::base_url();
147    api::status(base_url, data_connection_id.as_str()).await
148}
149
150/// This function get a single event from a WebRTC Gateway.
151///
152/// # Example
153/// ```
154/// use futures::future::{self, *};
155/// use futures::stream::*;
156/// use futures::*;
157///
158/// use skyway_webrtc_gateway_api::data::{DataConnectionEventEnum, listen_events, event};
159/// use skyway_webrtc_gateway_api::prelude::DataConnectionId;
160///
161/// async fn example() {
162///     let data_connection_id = DataConnectionId::try_create("dc-4995f372-fb6a-4196-b30a-ce11e5c7f56c").unwrap();
163///     let event_result = event(&data_connection_id).await;
164/// }
165/// ```
166pub async fn event<'a>(
167    data_connection_id: &DataConnectionId,
168) -> Result<DataConnectionEventEnum, error::Error> {
169    let base_url = super::base_url();
170    let event = api::event(base_url, data_connection_id.as_str()).await?;
171    let event = match event {
172        formats::EventEnum::OPEN => DataConnectionEventEnum::OPEN(DataConnectionIdWrapper {
173            data_connection_id: data_connection_id.clone(),
174        }),
175        formats::EventEnum::CLOSE => DataConnectionEventEnum::CLOSE(DataConnectionIdWrapper {
176            data_connection_id: data_connection_id.clone(),
177        }),
178        formats::EventEnum::ERROR {
179            error_message: message,
180        } => DataConnectionEventEnum::ERROR((data_connection_id.clone(), message)),
181        formats::EventEnum::TIMEOUT => DataConnectionEventEnum::TIMEOUT,
182    };
183    Ok(event)
184}
185
186/// This function keep listening events from a WebRTC Gateway.
187/// It keep accessing event API endpoint until receiving a CLOSE event or HTTP Error Code.
188///
189/// # Example
190/// ```
191/// use futures::channel::mpsc;
192/// use futures::future::{self, *};
193/// use futures::stream::*;
194/// use futures::*;
195///
196/// use skyway_webrtc_gateway_api::data::{DataConnectionEventEnum, listen_events};
197/// use skyway_webrtc_gateway_api::prelude::DataConnectionId;
198///
199/// async fn example() {
200///     let data_connection_id = DataConnectionId::try_create("dc-4995f372-fb6a-4196-b30a-ce11e5c7f56c").unwrap();
201///     let (dc_event_notifier, dc_event_observer) = mpsc::channel::<DataConnectionEventEnum>(0);
202///     let dc_event_observer = dc_event_observer.for_each(|event| async move {
203///     // Do something
204///     });
205///     let events_fut = listen_events(data_connection_id, dc_event_notifier);
206///     let _ = join!(dc_event_observer, events_fut);
207/// }
208/// ```
209pub async fn listen_events<'a>(
210    data_connection_id: DataConnectionId,
211    mut event_notifier: mpsc::Sender<DataConnectionEventEnum>,
212) -> Result<(), error::Error> {
213    let base_url = super::base_url();
214
215    loop {
216        let result = api::event(base_url, data_connection_id.as_str()).await?;
217        match result {
218            formats::EventEnum::OPEN => {
219                if event_notifier
220                    .send(DataConnectionEventEnum::OPEN(DataConnectionIdWrapper {
221                        data_connection_id: data_connection_id.clone(),
222                    }))
223                    .await
224                    .is_err()
225                {
226                    return Err(error::Error::create_local_error("fail to notify an event"));
227                };
228            }
229            formats::EventEnum::CLOSE => {
230                if event_notifier
231                    .send(DataConnectionEventEnum::CLOSE(DataConnectionIdWrapper {
232                        data_connection_id: data_connection_id.clone(),
233                    }))
234                    .await
235                    .is_err()
236                {
237                    return Err(error::Error::create_local_error("fail to notify an event"));
238                };
239                break;
240            }
241            formats::EventEnum::ERROR {
242                error_message: message,
243            } => {
244                if event_notifier
245                    .send(DataConnectionEventEnum::ERROR((
246                        data_connection_id.clone(),
247                        message,
248                    )))
249                    .await
250                    .is_err()
251                {
252                    return Err(error::Error::create_local_error("fail to notify an event"));
253                };
254            }
255            formats::EventEnum::TIMEOUT => {}
256        }
257    }
258
259    Ok(())
260}