This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
forked from vomatec/cordova-plugin-dgram
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INSTORE-424] A small refactoring (#4)
* 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
1 parent
48778f1
commit 9205e4a
Showing
6 changed files
with
216 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.