Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Draft: Implements the logs sender for Jason #74

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ lto = "thin"

[dependencies]
actix = "0.8"
actix-cors = "0.1"
actix-web = "1.0"
actix-web-actors = "1.0"
bb8 = "0.3"
Expand Down
3 changes: 3 additions & 0 deletions jason/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ wee_alloc = { version = "0.4", optional = true }
"console", "ConstrainDomStringParameters",
"CloseEvent",
"Event", "EventTarget",
"Headers",
"MediaDevices","MediaDeviceInfo", "MediaDeviceKind",
"MediaTrackConstraints", "MediaTrackSettings",
"MediaStream", "MediaStreamConstraints",
"MediaStreamTrack", "MediaStreamTrackState",
"MessageEvent",
"Navigator",
"Request", "RequestInit", "RequestMode", "Response",
"RtcConfiguration",
"RtcIceCandidate", "RtcIceCandidateInit",
"RtcIceConnectionState",
Expand All @@ -63,6 +65,7 @@ wee_alloc = { version = "0.4", optional = true }
"RtcSdpType",
"RtcSessionDescription", "RtcSessionDescriptionInit",
"RtcTrackEvent",
"Storage",
"WebSocket", "Window",
]

Expand Down
5 changes: 5 additions & 0 deletions jason/demo/chart/medea-demo/conf/nginx.vh.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ server {
proxy_pass http://127.0.0.1:8000/control-api/;
}

location ^~ /logs {
proxy_pass http://127.0.0.1:8080/logs;
proxy_redirect off;
}

# Disable unnecessary access logs.
location = /favicon.ico {
access_log off;
Expand Down
7 changes: 5 additions & 2 deletions jason/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
let inited = false;
const controlUrl = document.location.protocol + "//" +
document.location.host + "/control-api/";
const logsUrl = document.location.protocol + "//" +
document.location.host + "/logs";

async function createRoom(roomId) {
try {
Expand Down Expand Up @@ -86,6 +88,7 @@
await init();
inited = true;
const jason = new Jason();
jason.init_log_sender(logsUrl, 5000);

async function getDevices(audio_select, video_select, current_stream) {
const current_audio = current_stream.getAudioTracks().pop().label
Expand Down Expand Up @@ -168,8 +171,8 @@
}
});

room.on_failed_local_stream(function (error) {
console.error(error);
room.on_failed_local_stream((e) => {
logError("Get local stream failed", e);
});

room.on_new_connection((connection) => {
Expand Down
2 changes: 2 additions & 0 deletions jason/e2e-demo/js/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const controlUrl = "http://127.0.0.1:8000/control-api/";
const logslUrl = "http://127.0.0.1:8080/logs";
const baseUrl = 'ws://127.0.0.1:8080/ws/';

let roomId = window.location.hash.replace("#", "");
Expand Down Expand Up @@ -207,6 +208,7 @@ const controlDebugWindows = {
window.onload = async function() {
let rust = await import("../../pkg");
let jason = new rust.Jason();
jason.init_log_sender(logslUrl, 3000);
console.log(baseUrl);

Object.values(controlDebugWindows).forEach(s => s());
Expand Down
18 changes: 18 additions & 0 deletions jason/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
peer,
rpc::{ClientDisconnect, RpcClient as _, WebSocketRpcClient},
set_panic_hook,
utils::{FetchHTTPClient, JasonError, LogSender},
};

#[doc(inline)]
Expand All @@ -31,6 +32,7 @@ pub struct Jason(Rc<RefCell<Inner>>);
struct Inner {
media_manager: Rc<MediaManager>,
rooms: Vec<Room>,
logs_sender: Option<LogSender>,
}

#[wasm_bindgen]
Expand All @@ -42,6 +44,20 @@ impl Jason {
Self::default()
}

/// Instantiates new [`LogSender`] to send saved logs to the specified URL
/// at the specified interval.
pub fn init_log_sender(
&self,
url: &str,
interval: i32,
) -> Result<(), JsValue> {
let client = Rc::new(FetchHTTPClient::new(url));
let sender =
LogSender::new(client, interval).map_err(JasonError::from)?;
self.0.borrow_mut().logs_sender = Some(sender);
Ok(())
}

/// Returns [`RoomHandle`] for [`Room`].
pub fn init_room(&self) -> RoomHandle {
let rpc = Rc::new(WebSocketRpcClient::new(3000));
Expand All @@ -62,6 +78,7 @@ impl Jason {
.drain(..)
.for_each(|room| room.close(reason.clone()));
inner.borrow_mut().media_manager = Rc::default();
inner.borrow_mut().logs_sender.take();
}));

let room = Room::new(rpc, peer_repository);
Expand All @@ -82,5 +99,6 @@ impl Jason {
self.0.borrow_mut().rooms.drain(..).for_each(|room| {
room.close(ClientDisconnect::RoomClosed.into());
});
self.0.borrow_mut().logs_sender.take();
}
}
45 changes: 28 additions & 17 deletions jason/src/api/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,12 @@ impl RoomHandle {
///
/// Effectively returns `Result<(), JasonError>`.
pub fn join(&self, token: String) -> Promise {
future_to_promise(
self.inner_join(token).map(|result| {
result.map(|_| JsValue::null()).map_err(Into::into)
}),
)
future_to_promise(self.inner_join(token).map(|result| {
result
.map(|_| JsValue::null())
.map_err(store_jason_error!(None))
.map_err(Into::into)
}))
}

/// Injects local media stream for all created and new [`PeerConnection`]s
Expand Down Expand Up @@ -518,8 +519,10 @@ impl InnerRoom {
.inject_local_stream(&injected_stream)
.await
.map_err(tracerr::map_from_and_wrap!(=> RoomError))
.map_err(JasonError::from)
{
error_callback.call(JasonError::from(err));
store_jason_error!(&err, Some(peer.id()));
error_callback.call(err);
}
}
});
Expand Down Expand Up @@ -553,10 +556,12 @@ impl EventHandler for InnerRoom {
self.enabled_video,
)
.map_err(tracerr::map_from_and_wrap!(=> RoomError))
.map_err(JasonError::from)
{
Ok(peer) => peer,
Err(err) => {
JasonError::from(err).print();
store_jason_error!(&err, None);
err.print();
return;
}
};
Expand Down Expand Up @@ -596,14 +601,15 @@ impl EventHandler for InnerRoom {
};
Result::<_, Traced<RoomError>>::Ok(())
}
.then(|result| {
.then(move |result| {
async move {
if let Err(err) = result {
let (err, trace) = err.into_parts();
match err {
RoomError::InvalidLocalStream(_)
| RoomError::CouldNotGetLocalMedia(_) => {
let e = JasonError::from((err, trace));
store_jason_error!(&e, Some(peer_id));
e.print();
error_callback.call(e);
}
Expand All @@ -623,8 +629,10 @@ impl EventHandler for InnerRoom {
.set_remote_answer(sdp_answer)
.await
.map_err(tracerr::map_from_and_wrap!(=> RoomError))
.map_err(JasonError::from)
{
JasonError::from(err).print()
store_jason_error!(&err, Some(peer_id));
err.print();
}
});
} else {
Expand All @@ -649,9 +657,11 @@ impl EventHandler for InnerRoom {
candidate.sdp_mid,
)
.await
.map_err(tracerr::map_from_and_wrap!(=> RoomError));
.map_err(tracerr::map_from_and_wrap!(=> RoomError))
.map_err(JasonError::from);
if let Err(err) = add {
JasonError::from(err).print();
store_jason_error!(&err, Some(peer_id));
err.print();
}
});
} else {
Expand Down Expand Up @@ -701,12 +711,13 @@ impl PeerEventHandler for InnerRoom {
sender_id: PeerId,
remote_stream: MediaStream,
) {
match self.connections.get(&sender_id) {
Some(conn) => conn.on_remote_stream(remote_stream.new_handle()),
None => {
JasonError::from(tracerr::new!(RoomError::UnknownRemotePeer))
.print()
}
if let Some(conn) = self.connections.get(&sender_id) {
conn.on_remote_stream(remote_stream.new_handle());
} else {
let err =
JasonError::from(tracerr::new!(RoomError::UnknownRemotePeer));
store_jason_error!(&err, Some(sender_id));
err.print();
}
}

Expand Down
14 changes: 11 additions & 3 deletions jason/src/media/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use web_sys::{

use crate::{
media::MediaStreamConstraints,
utils::{window, JasonError, JsCaused, JsError},
utils::{store_error, window, JasonError, JsCaused, JsError},
};

use super::InputDeviceInfo;
Expand Down Expand Up @@ -263,7 +263,11 @@ impl MediaManagerHandle {
.into()
})
.map_err(tracerr::wrap!(=> MediaManagerError))
.map_err(|e| JasonError::from(e).into())
.map_err(JasonError::from)
.map_err(|err| {
store_error(&err, None);
err.into()
})
}),
Err(e) => future_to_promise(future::err(e)),
}
Expand All @@ -279,7 +283,11 @@ impl MediaManagerHandle {
.await
.map(|(stream, _)| stream.into())
.map_err(tracerr::wrap!(=> MediaManagerError))
.map_err(|e| JasonError::from(e).into())
.map_err(JasonError::from)
.map_err(|err| {
store_error(&err, None);
err.into()
})
}),
Err(err) => future_to_promise(future::err(err)),
}
Expand Down
8 changes: 7 additions & 1 deletion jason/src/peer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use std::{cell::RefCell, collections::HashMap, convert::TryFrom, rc::Rc};
use derive_more::{Display, From};
use futures::{channel::mpsc, future};
use medea_client_api_proto::{
Direction, IceConnectionState, IceServer, PeerId as Id, Track, TrackId,
Direction, IceConnectionState, IceServer, PeerId as Id, PeerId, Track,
TrackId,
};
use medea_macro::dispatchable;
use tracerr::Traced;
Expand Down Expand Up @@ -239,6 +240,11 @@ impl PeerConnection {
Ok(peer)
}

/// Returns ID of [`PeerConnection`].
pub fn id(&self) -> PeerId {
self.id
}

/// Returns inner [`IceCandidate`]'s buffer len. Used in tests.
pub fn candidates_buffer_len(&self) -> usize {
self.ice_candidates_buffer.borrow().len()
Expand Down
Loading