From d4ff876b34628a54bb61c8f6adf838a17c9673df Mon Sep 17 00:00:00 2001 From: Christian Hotz-Behofsits Date: Sun, 29 Dec 2013 10:21:24 +0100 Subject: [PATCH] refactored. connect returns feature and tests added --- lib/Messenger/src/peers/jswebrtcpeer.dart | 16 ++++++++++------ lib/Messenger/src/peers/peer.dart | 6 +++++- tests/jsrtcpeer.dart | 17 +++++++++-------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lib/Messenger/src/peers/jswebrtcpeer.dart b/lib/Messenger/src/peers/jswebrtcpeer.dart index 90d518f..bdfc722 100644 --- a/lib/Messenger/src/peers/jswebrtcpeer.dart +++ b/lib/Messenger/src/peers/jswebrtcpeer.dart @@ -44,7 +44,7 @@ class JsWebRtcPeer extends Peer{ /** * connect to WebrtcPeer */ - connect(JsWebRtcPeer o){ + Future connect(JsWebRtcPeer o){ /// add ice candidates rtcPeerConnection.onicecandidate = (event) { @@ -71,7 +71,7 @@ class JsWebRtcPeer extends Peer{ } }; - /// add Datachannel + /// create datachannel try { dc = rtcPeerConnection.createDataChannel("sendDataChannel", js.map(dataChannelOptions)); @@ -94,13 +94,17 @@ class JsWebRtcPeer extends Peer{ //test datachannel //dataChannel.send("test"); + connection_completer.complete("wuhuu"); }); - }, (e)=>print(e), {}); - - + }, (e){ + connection_completer.completeError(e, e.stackTrace); + }, {}); + } catch (e) { - log.warning("could not create DataChannel: " + e.toString()); + connection_completer.completeError(e, e.stackTrace); } + + return connection_completer.future; } diff --git a/lib/Messenger/src/peers/peer.dart b/lib/Messenger/src/peers/peer.dart index a1551bd..0c0702f 100644 --- a/lib/Messenger/src/peers/peer.dart +++ b/lib/Messenger/src/peers/peer.dart @@ -29,6 +29,10 @@ abstract class Peer{ ///todo: generalize and add to connections to support multiple states StreamController readyStateEvent; + ///completer for connection + ///TODO: use another generic type + var connection_completer = new Completer(); + /** * constuctor */ @@ -69,7 +73,7 @@ abstract class Peer{ * * @param Peer other */ - connect(Peer other); + Future connect(Peer other); /** * send Message to other peer diff --git a/tests/jsrtcpeer.dart b/tests/jsrtcpeer.dart index 06a2100..06cd904 100644 --- a/tests/jsrtcpeer.dart +++ b/tests/jsrtcpeer.dart @@ -35,26 +35,27 @@ void main() { }); /** - * Test Connection + * Test connection */ test('Webrtc connect',(){ JsWebRtcPeer alice = new JsWebRtcPeer("alice"); JsWebRtcPeer bob = new JsWebRtcPeer("bob"); - alice.connect(bob); + expect(alice.connect(bob), completes); }); + + /** - * test status opens + * test DataChannel's readyState opens */ - test('webrtc datachannel open', (){ + test('webrtc datachannel', (){ JsWebRtcPeer alice = new JsWebRtcPeer("alice"); JsWebRtcPeer bob = new JsWebRtcPeer("bob"); - alice.readyStateEvent.stream.listen((String status){ - if(status == "open") - expectAsync0((){}); - }); + _callback(String status) => expect(status, "open"); + + alice.readyStateEvent.stream.listen(expectAsync1(_callback)); alice.connect(bob);