Commit 9d378ff6 authored by Jeffrey Schiller's avatar Jeffrey Schiller Committed by Jeffrey I. Schiller

Simplify the WebRTC Poller

Eliminate the single threaded executor because it is not needed. Instead
use Timer.scheduleAtFixedRate() to trigger the Poller once per
second. This approach is more correct and simpler.

Also, ignore an empty response from the Rendezvous server (which means
that it hasn’t received any ICE Candidates from the browser
yet). Previously we attempted to JSON parse it and threw an
exception (which was logged and ignored).

Change-Id: If0bc95c754a3fd052ac32cfa966fcbcfe658f55d
parent c0cabd21
......@@ -17,17 +17,15 @@ import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharacterCodingException;
import java.util.Collections;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
......@@ -67,8 +65,6 @@ public class WebRTCNativeMgr {
private ReplForm form;
private PeerConnection peerConnection;
/* We use a single threaded executor to read from the rendezvous server */
private volatile ExecutorService background = Executors.newSingleThreadExecutor();
/* We need to keep track of whether or not we have processed an element */
/* Received from the rendezvous server. */
private TreeSet<String> seenNonces = new TreeSet();
......@@ -133,6 +129,8 @@ public class WebRTCNativeMgr {
WebRTCNativeMgr.this.dataChannel = dataChannel;
dataChannel.registerObserver(dataObserver);
keepPolling = false; // Turn off talking to the rendezvous server
timer.cancel();
Log.d(LOG_TAG, "Poller() Canceled");
seenNonces.clear();
}
......@@ -220,8 +218,12 @@ public class WebRTCNativeMgr {
peerConnection = factory.createPeerConnection(Collections.singletonList(iceServer), new MediaConstraints(),
observer);
// peerConnection.createOffer(sdpObserver, new MediaConstraints()); // Let's see what happens :-)
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Poller();
}
}, 0, 1000); // Start the Poller now and then every second
}
/*
......@@ -230,27 +232,48 @@ public class WebRTCNativeMgr {
* is in the sender roll.
*/
private void Poller() {
background.submit(new Runnable() {
@Override public void run() {
try {
if (!keepPolling) {
return;
}
Log.d(LOG_TAG, "Poller() Called");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://" + rendezvousServer + "/rendezvous2/" + rCode + "-s");
HttpResponse response = client.execute(request);
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader
BufferedReader rd = null;
try {
rd = new BufferedReader
(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
sb.append(line);
}
} finally {
if (rd != null) {
rd.close();
}
}
if (!keepPolling) {
Log.d(LOG_TAG, "keepPolling is false, we're done!");
return;
}
Log.d(LOG_TAG, "response = " + sb.toString());
JSONArray jsonArray = new JSONArray(sb.toString());
String responseText = sb.toString();
Log.d(LOG_TAG, "response = " + responseText);
if (responseText.equals("")) {
Log.d(LOG_TAG, "Received an empty response");
// Empty Response
return;
}
JSONArray jsonArray = new JSONArray(responseText);
Log.d(LOG_TAG, "jsonArray.length() = " + jsonArray.length());
int i = 0;
while (i < jsonArray.length()) {
......@@ -274,14 +297,13 @@ public class WebRTCNativeMgr {
peerConnection.createAnswer(sdpObserver, new MediaConstraints());
Log.d(LOG_TAG, "createAnswer returned");
i = -1;
} else {
if (element.has("nonce")) {
String nonce = element.optString("nonce");
} else if (element.has("nonce")) {
if (element.isNull("candidate")) {
Log.d(LOG_TAG, "Received a null candidate, skipping...");
i++;
continue;
}
String nonce = element.optString("nonce");
JSONObject candidate = (JSONObject) element.get("candidate");
String sdpcandidate = candidate.optString("candidate");
String sdpMid = candidate.optString("sdpMid");
......@@ -295,7 +317,6 @@ public class WebRTCNativeMgr {
Log.d(LOG_TAG, "addIceCandidate returned");
}
}
}
i++;
}
Log.d(LOG_TAG, "exited loop");
......@@ -306,16 +327,6 @@ public class WebRTCNativeMgr {
} catch (Exception e) {
Log.e(LOG_TAG, "Caught Exception: " + e.toString(), e);
}
if (keepPolling) {
timer.schedule(new TimerTask() {
@Override
public void run() {
WebRTCNativeMgr.this.Poller();
}
}, 1000);
}
}
});
}
private void sendRendezvous(JSONObject data) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment