Skip to content

Commit

Permalink
Merged public and private code:
Browse files Browse the repository at this point in the history
  • Loading branch information
Alain Pimentel committed Mar 30, 2018
1 parent 807b2ba commit 601477f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
4 changes: 2 additions & 2 deletions android-wsbridge/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ android {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.2.3"
versionName "1.2.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -36,7 +36,7 @@ android {

dependencies {
compile 'com.amitshekhar.android:android-networking:0.3.0'
compile 'org.java-websocket:java-websocket:1.3.1'
compile 'org.java-websocket:java-websocket:1.3.3'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup:otto:1.3.8'
compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class BridgeActivity extends Activity {
private AtomicBoolean isUSBConnected = new AtomicBoolean(false);
private AtomicBoolean isRCConnected = new AtomicBoolean(false);
private AtomicBoolean isWiFiConnected = new AtomicBoolean(false);
private AtomicBoolean isWSTrafficSlow = new AtomicBoolean(false);
private AtomicBoolean isStreamRunnerActive = new AtomicBoolean(false);
private InputStream usbInputStream;
private OutputStream usbOutputStream;
Expand Down Expand Up @@ -325,6 +326,25 @@ public void onWSConnectionEvent(WSConnectionManager.WSConnectionEvent event) {
}
showToast("Network ", isWiFiConnected.get(), event.getMessage());
}

@Subscribe
public void onWSTrafficEvent(WSConnectionManager.WSTrafficEvent event) {

boolean shouldRefresh = false;
if (event.isSlowConnection()) {
if (isWSTrafficSlow.compareAndSet(false, true)) {
shouldRefresh = true;
showToast("Bad Network Connection: " + event.getMessage() + "!", isWiFiConnected.get(), event.getMessage());
}
} else {
if (isWSTrafficSlow.compareAndSet(true, false)) {
shouldRefresh = true;
}
}
if (shouldRefresh) {
refresh();
}
}
//endregion -----------------------------------------------------------------------------------------------------

//region Internal Utility Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DJILogger extends Thread {
public static final int CONNECTION_BRIDGE = 2;
private static final String TAG = "RemoteLogger";
// Add your remote server IP
private static final String REMOTE_LOGGER_URL = "";
private static final String REMOTE_LOGGER_URL = "your_url_goes_here.com";
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
private static DJILogger instance = new DJILogger();
private String serverURL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public void onError(WebSocket conn, Exception ex) {
}
}

@Override
public void onStart() {

}

@Override
public void onWebsocketPing(WebSocket webSocket, Framedata framedata) {
ByteBuffer buffer = framedata.getPayloadData();
Expand Down Expand Up @@ -252,13 +257,25 @@ public void send(byte[] b) {
if (activeConnectionCount() > 0) {
final int maxBufferSizePerConnection = MAX_BUFFER_SIZE / con.size();
synchronized (con) {
for (WebSocket c : con) {
if (c.isOpen()) {
if (c instanceof WebSocketImpl && ((WebSocketImpl) c).outQueue.size() > maxBufferSizePerConnection) {
for (WebSocket conn : con) {
if (conn.isOpen()) {
final boolean isSlowTraffic;
if (conn instanceof WebSocketImpl && ((WebSocketImpl) conn).outQueue.size() > maxBufferSizePerConnection) {
// Do nothing because we don't want to over flow the internal buffer of WebSocket
// This could happen when usb is fast but the connection is slow ( producer/consumer problem)
isSlowTraffic = true;
} else {
c.send(b);
conn.send(b);
isSlowTraffic = false;
}
if (isSlowTraffic) {
String hostName = "";
if (conn != null && conn.getRemoteSocketAddress() != null) {
hostName = conn.getRemoteSocketAddress().getHostName();
}
BridgeApplication.getInstance()
.getBus()
.post(new WSTrafficEvent(isSlowTraffic, hostName));
}
//DJILogger.d("SOURCE", DJILogger.sha1Hash(b) + " -- " + DJILogger.bytesToHex(b));
}
Expand Down Expand Up @@ -303,6 +320,27 @@ public int getActiveConnectionCount() {
}
}

/**
* Event to notify changes in WebSocket Traffic
*/
public static final class WSTrafficEvent {
final boolean isSlowConnection;
final String message;

public WSTrafficEvent(boolean isSlowConnection, String message) {
this.isSlowConnection = isSlowConnection;
this.message = message;
}

public boolean isSlowConnection() {
return isSlowConnection;
}

public String getMessage() {
return message;
}
}

private class ByteStatCounter {

public long recentByteCount = 0;
Expand Down

0 comments on commit 601477f

Please sign in to comment.