aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/cycling74/net/UdpReceiver.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/java/com/cycling74/net/UdpReceiver.java')
-rw-r--r--src/java/com/cycling74/net/UdpReceiver.java64
1 files changed, 39 insertions, 25 deletions
diff --git a/src/java/com/cycling74/net/UdpReceiver.java b/src/java/com/cycling74/net/UdpReceiver.java
index fd47e25..f7423fd 100644
--- a/src/java/com/cycling74/net/UdpReceiver.java
+++ b/src/java/com/cycling74/net/UdpReceiver.java
@@ -5,24 +5,32 @@ import java.net.DatagramSocket;
import com.cycling74.max.Atom;
import com.cycling74.max.MaxRuntimeException;
-import com.cycling74.max.MaxSystem;
+//import com.cycling74.max.MaxSystem;
import com.cycling74.max.Callback;
-/**
- * This portion of code is scheduled for pdj-0.8.5
- * IT IS NOT FUNCTIONAL
+/**
+ * Class wrapper to receive atoms via UDP/IP using the class
+ * UdpSender.
+ *
+ * This class is a work in progress and have been lightly tested.
*/
public class UdpReceiver implements Runnable {
DatagramSocket receiver;
DatagramPacket packet;
- Callback callback;
-
+ Callback callback = null;
String debugString = null;
- int port;
boolean runnable = true;
+ int port = -1;
- public void close() {
+ public UdpReceiver() {
+ }
+
+ public UdpReceiver(int port) {
+ this.port = port;
+ }
+
+ public void close() {
if ( receiver == null )
return;
runnable = false;
@@ -34,17 +42,23 @@ public class UdpReceiver implements Runnable {
}
public void setActive(boolean active) {
- if ( active == false ) {
- runnable = true;
- new Thread(this).start();
+ if ( active == true ) {
+ if ( port == -1 )
+ throw new MaxRuntimeException("No UDP port specified");
+ try {
+ receiver = new DatagramSocket(port);
+ } catch ( Exception e ) {
+ throw new MaxRuntimeException(e);
+ }
+ runnable = true;
+ new Thread(this, "UdpReceiver[" + port + "]").start();
} else {
close();
}
-
}
public void setCallback(Object caller, String methodName) {
- callback = new Callback(caller, methodName);
+ callback = new Callback(caller, methodName, new Object[] { new Atom[0] });
}
public void setPort(int port) {
@@ -57,22 +71,22 @@ public class UdpReceiver implements Runnable {
}
public void run() {
- DatagramPacket packet = new DatagramPacket(new byte[4096], 4096);
- Object callerArgs[] = new Object[1];
-
try {
while(runnable) {
+ DatagramPacket packet = new DatagramPacket(new byte[4096], 4096);
receiver.receive(packet);
String msg = new String(packet.getData(), 0, packet.getLength());
- if ( debugString != null )
- MaxSystem.post(debugString + " " + msg);
- callerArgs[0] = Atom.parse(msg);
-
- /* try {
- callback.invoke(instance, callerArgs);
- } catch( Exception e ) {
- e.printStackTrace();
- } */
+ //if ( debugString != null )
+ // MaxSystem.post(debugString + " " + msg);
+
+ if ( callback != null ) {
+ callback.setArgs(Atom.parse(msg));
+ try {
+ callback.execute();
+ } catch( Exception e ) {
+ e.printStackTrace();
+ }
+ }
}
} catch (Exception e) {
if ( runnable != false) {