Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
[INSTORE-424] A small refactoring (#4)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* trying to get it to user java 1.7

* fixed log message level

* cleanup

* updating exception message

* removed extraneous action parameter from call to opensocket
  • Loading branch information
gareth-ivatt-fivestars authored and darran-kelinske-fivestars committed Oct 11, 2019
1 parent 48778f1 commit 9205e4a
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 165 deletions.
5 changes: 4 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
<param name="android-package" value="org.apache.cordova.dgram.Dgram"/>
</feature>
</config-file>
<source-file src="src/android/Dgram.java" target-dir="src/org/apache/cordova/dgram/" />
<source-file src="src/android/Dgram.java" target-dir="src/org/apache/cordova/dgram/" />
<source-file src="src/android/CallbackUtil.java" target-dir="src/org/apache/cordova/dgram/" />
<source-file src="src/android/DatagramSocketListener.java" target-dir="src/org/apache/cordova/dgram/" />
<source-file src="src/android/DatagramSocketSend.java" target-dir="src/org/apache/cordova/dgram/" />
<framework src="src/build-extras.gradle" custom="true" type="gradleReference" />
</platform>

</plugin>
7 changes: 5 additions & 2 deletions src/android/CallbackUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.apache.cordova.dgram;

import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

Expand All @@ -8,10 +10,11 @@ public class CallbackUtil {
/**
* Helper function to make sure JavaScript side of cordova keeps the callback reference
* alive. This allows the same JavaScript function to be used mutliple times via the
* CallbackContext stored in this plugin
* CallbackContext stored in this plugin.
*/
public static void sendPluginResult(CallbackContext callback, PluginResult result) {
public static void emitPluginResult(CallbackContext callback, PluginResult result) {
if (callback == null || result == null) {
Log.d(Dgram.TAG, "The emitPluginResult helper was called with null parameter(s).");
return;
}
result.setKeepCallback(true);
Expand Down
79 changes: 79 additions & 0 deletions src/android/DatagramSocketListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.apache.cordova.dgram;

import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

import org.json.JSONObject;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import java.nio.charset.StandardCharsets;

class DatagramSocketListener extends Thread {
private DatagramSocket datagramSocket;
private CallbackContext callbackContext;

DatagramSocketListener(
final DatagramSocket datagramSocket,
final CallbackContext callbackContext
) {
this.datagramSocket = datagramSocket;
this.callbackContext = callbackContext;
}

public void run() {
// The field size sets a theoretical limit of 65,535 bytes (8 byte header + 65,527 bytes of data)
// for a UDP datagram. However the actual limit for the data length, which is imposed by the
// underlying IPv4 protocol, is 65,507 bytes (65,535 − 8 byte UDP header − 20 byte IP header).
// For now allowing 10 megabytes which seems plenty large.
byte[] data = new byte[1024*10];
DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
emitMessageResult("listener started", "", 0);

while (true) {
try {
if (this.datagramSocket == null || this.datagramSocket.isClosed()) {
Log.d(Dgram.TAG, "Exiting message loop because socket is closed.");
return;
}

// Reset the length in case we receive an incomplete DatagramPacket
datagramPacket.setLength(data.length);
this.datagramSocket.receive(datagramPacket);
String message = new String(data, 0, datagramPacket.getLength(), StandardCharsets.UTF_8);
String address = datagramPacket.getAddress().getHostAddress();
int port = datagramPacket.getPort();
Log.d(Dgram.TAG, "Received message " + message + " from " + address + " and port " + port);
emitMessageResult(message, address, port);
} catch (Exception e) {
Log.e(Dgram.TAG, "Exception in listener:" + e.toString());
emitMessageErrorResult(e);
}
}
}

private void emitMessageResult(String message, String address, int port) {
try {
JSONObject payload = new JSONObject();
payload.put("message", message);
payload.put("address", address);
payload.put("port", port);
CallbackUtil.emitPluginResult(this.callbackContext, new PluginResult(PluginResult.Status.OK, payload));
} catch (Exception e) {
Log.e(Dgram.TAG, "Exception emitting message:" + e.toString());
}
}

private void emitMessageErrorResult(Exception e) {
try {
JSONObject payload = new JSONObject();
payload.put("error", e.toString());
CallbackUtil.emitPluginResult(this.callbackContext, new PluginResult(PluginResult.Status.ERROR, payload));
} catch (Exception exception) {
Log.e(Dgram.TAG, "Exception emitting message:" + e.toString());
}
}
}
56 changes: 56 additions & 0 deletions src/android/DatagramSocketSend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.apache.cordova.dgram;

import android.util.Log;

import org.apache.cordova.CallbackContext;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import java.nio.charset.StandardCharsets;

class DatagramSocketSend implements Runnable {
private DatagramSocket datagramSocket;
private CallbackContext callbackContext;
private String message;
private String address;
private int port;

public DatagramSocketSend(
final DatagramSocket datagramSocket,
final CallbackContext callbackContext,
final String message,
final String address,
final int port
) {
this.datagramSocket = datagramSocket;
this.callbackContext = callbackContext;
this.message = message;
this.address = address;
this.port = port;
}

public void run() {
try {
if (this.datagramSocket == null || this.datagramSocket.isClosed()) {
Log.d(Dgram.TAG, "Attempted to send message but socket is closed.");
return;
}

final byte[] bytes = this.message.getBytes(StandardCharsets.UTF_8);
final DatagramPacket packet = new DatagramPacket(
bytes,
bytes.length,
InetAddress.getByName(this.address),
this.port
);
// The send is wrapped in this thread to prevent NetworkOnMainThreadException
this.datagramSocket.send(packet);
this.callbackContext.success();
} catch (Exception e) {
Log.e(Dgram.TAG, "Send exception: " + e.toString(), e);
this.callbackContext.error("Exception sending message: " + e.toString());
}
}
}
Loading

0 comments on commit 9205e4a

Please sign in to comment.