diff options
author | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2009-07-19 15:56:13 +0000 |
---|---|---|
committer | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2009-07-19 15:56:13 +0000 |
commit | 5d63b1b2a6968f9c0146e1946b72ca6073370fbe (patch) | |
tree | d8c9ef35e0f68c95aac11ebd1decd345ba1f82a8 /src/java/com/cycling74/net | |
parent | 0f770551cebca90c92b7a39b0e0135a445af51f9 (diff) |
updated to 0.8.5HEADsvn2git-headexternals/loaders/pdj
svn path=/trunk/externals/loaders/pdj/; revision=11874
Diffstat (limited to 'src/java/com/cycling74/net')
-rw-r--r-- | src/java/com/cycling74/net/TcpReceiver.java | 97 | ||||
-rw-r--r-- | src/java/com/cycling74/net/TcpSender.java | 92 | ||||
-rw-r--r-- | src/java/com/cycling74/net/UdpReceiver.java | 64 | ||||
-rw-r--r-- | src/java/com/cycling74/net/UdpSender.java | 45 | ||||
-rw-r--r-- | src/java/com/cycling74/net/package.html | 5 |
5 files changed, 180 insertions, 123 deletions
diff --git a/src/java/com/cycling74/net/TcpReceiver.java b/src/java/com/cycling74/net/TcpReceiver.java index f449517..3de2343 100644 --- a/src/java/com/cycling74/net/TcpReceiver.java +++ b/src/java/com/cycling74/net/TcpReceiver.java @@ -1,33 +1,54 @@ package com.cycling74.net; -import java.lang.reflect.Method; -import java.net.DatagramPacket; -import java.net.DatagramSocket; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; import com.cycling74.max.Atom; +import com.cycling74.max.Callback; import com.cycling74.max.MaxRuntimeException; import com.cycling74.max.MaxSystem; -/** - * This portion of code is scheduled for pdj-0.8.5 - * IT IS NOT FUNCTIONAL +/** + * Class wrapper to receive atoms via TCP/IP using the class + * TcpSender. + * + * This class is a work in progress and have been lightly tested. */ public class TcpReceiver implements Runnable { - DatagramSocket receiver; - DatagramPacket packet; - - Method callback = null; - Object instance; + ServerSocket receiver; + + Callback callback = null; String debugString = null; - int port; + int port = -1; boolean runnable = true; + public TcpReceiver() { + + } + + public TcpReceiver(int port) { + this.port = port; + } + + public TcpReceiver(int port, Object caller, String method) { + this.port = port; + this.callback = new Callback(caller, method, new Object[] { new Atom[0] }); + } + + public void close() { if ( receiver == null ) return; runnable = false; - receiver.close(); + try { + receiver.close(); + } catch (IOException e) { + e.printStackTrace(); + } } public int getPort() { @@ -35,9 +56,17 @@ public class TcpReceiver implements Runnable { } public void setActive(boolean active) { - if ( active == false ) { - runnable = true; - new Thread(this).start(); + if ( port == -1 ) + throw new MaxRuntimeException("No TCP port specified"); + + if ( active == true ) { + try { + receiver = new ServerSocket(port); + } catch (IOException e) { + throw new MaxRuntimeException(e); + } + runnable = true; + new Thread(this, "TcpSender[" + port + "]").start(); } else { close(); } @@ -46,8 +75,7 @@ public class TcpReceiver implements Runnable { public void setCallback(Object caller, String methodName) { try { - callback = caller.getClass().getDeclaredMethod(methodName, new Class[] { Atom.class }); - instance = caller; + this.callback = new Callback(caller, methodName, new Object[] { new Atom[0] }); } catch (Exception e) { throw new MaxRuntimeException(e); } @@ -62,22 +90,29 @@ public class TcpReceiver implements Runnable { this.debugString = debugString; } + private void parseMessage(BufferedReader reader) throws IOException { + while(runnable) { + String msg = reader.readLine(); + if ( debugString != null ) + MaxSystem.post(debugString + " " + msg); + + if ( callback != null ) { + callback.setArgs(Atom.parse(msg)); + try { + callback.execute(); + } catch( Exception e ) { + e.printStackTrace(); + } + } + } + } + public void run() { - DatagramPacket packet = new DatagramPacket(new byte[4096], 4096); - Object callerArgs[] = new Object[1]; - try { while(runnable) { - 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(); - } + Socket socket = receiver.accept(); + BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + parseMessage(reader); } } catch (Exception e) { if ( runnable != false) { diff --git a/src/java/com/cycling74/net/TcpSender.java b/src/java/com/cycling74/net/TcpSender.java index 2ba0f67..a092bfb 100644 --- a/src/java/com/cycling74/net/TcpSender.java +++ b/src/java/com/cycling74/net/TcpSender.java @@ -1,20 +1,21 @@ package com.cycling74.net; import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetSocketAddress; +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; import com.cycling74.max.Atom; import com.cycling74.max.MaxRuntimeException; -/** - * This portion of code is scheduled for pdj-0.8.5 - * IT IS NOT FUNCTIONAL +/** + * Class wrapper to send atoms via TCP/IP. The host on the other side + * must use TcpReceive to read the sended atoms. + * + * This class is a work in progress and have been lightly tested. */ public class TcpSender { - DatagramSocket sender; - DatagramPacket packet; + InetAddress inetAddress; String address = null; int port = -1; @@ -22,58 +23,47 @@ public class TcpSender { } public TcpSender(String address, int port) { - this.address= address; + setAddress(address); this.port = port; } public void send(Atom args[]) { - if ( sender == null ) - initsocket(); - - byte buff[] = Atom.toOneString(args).getBytes(); - packet.setData(buff, 0, buff.length); - try { - sender.send(packet); - } catch (IOException e) { - throw new MaxRuntimeException(e); - } + send(Atom.toOneString(args)); } public void send(int i) { - if ( sender == null ) - initsocket(); - - byte buff[] = Integer.toString(i).getBytes(); - packet.setData(buff, 0, buff.length); - try { - sender.send(packet); - } catch (IOException e) { - throw new MaxRuntimeException(e); - } + send(Integer.toString(i)); } public void send(float f) { - if ( sender == null ) - initsocket(); - - byte buff[] = Float.toString(f).getBytes(); - packet.setData(buff, 0, buff.length); - try { - sender.send(packet); - } catch (IOException e) { - throw new MaxRuntimeException(e); - } + send(Float.toString(f)); } + public void send(String msg) { + if ( address == null ) + throw new MaxRuntimeException("TcpSender has no active hosts"); + if ( port == -1 ) + throw new MaxRuntimeException("TcpSender has no active port"); + + try { + Socket sender = new Socket(inetAddress, port); + sender.getOutputStream().write(msg.getBytes()); + sender.close(); + } catch (IOException e) { + throw new MaxRuntimeException(e); + } + } + public String getAddress() { return address; } public void setAddress(String address) { - if ( sender != null ) { - sender = null; - sender.close(); - } + try { + inetAddress = InetAddress.getByName(address); + } catch (UnknownHostException e) { + throw new MaxRuntimeException(e); + } this.address = address; } @@ -82,22 +72,6 @@ public class TcpSender { } public void setPort(int port) { - if ( sender != null ) { - sender = null; - sender.close(); - } this.port = port; } - - private synchronized void initsocket() { - if ( sender != null ) - return; - try { - sender = new DatagramSocket(); - sender.connect(new InetSocketAddress(address, port)); - packet = new DatagramPacket(new byte[0], 0); - } catch (Exception e) { - throw new MaxRuntimeException(e); - } - } } 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) { diff --git a/src/java/com/cycling74/net/UdpSender.java b/src/java/com/cycling74/net/UdpSender.java index 6ef7e34..e4c2d53 100644 --- a/src/java/com/cycling74/net/UdpSender.java +++ b/src/java/com/cycling74/net/UdpSender.java @@ -8,9 +8,11 @@ import java.net.InetAddress; import com.cycling74.max.Atom; import com.cycling74.max.MaxRuntimeException; -/** - * This portion of code is scheduled for pdj-0.8.5 - * IT IS NOT FUNCTIONAL +/** + * Class wrapper to send atoms via UDP/IP. The host on the other side + * must use UdpReceive to read the sended atoms. + * + * This class is a work in progress and have been lightly tested. */ public class UdpSender { InetAddress inetAddress; @@ -23,6 +25,11 @@ public class UdpSender { public UdpSender() { } + /** + * Create a UpdSender. + * @param address the hostname/ip address of the host to reach + * @param port the UDP port to use + */ public UdpSender(String address, int port) { this.address = address; this.port = port; @@ -41,22 +48,45 @@ public class UdpSender { send(Float.toString(f).getBytes()); } + public void send(String msg, Atom args[]) { + send((msg + " " + Atom.toOneString(args)).getBytes()); + } + + /** + * Returns the hostname/ip address to reach. + * @return hostname/ip address to reach + */ public String getAddress() { return address; } + /** + * Sets hostname/ip address to reach. + * @param address hostname/ip address to reach + */ + public void setAddress(String address) { this.address = address; - initsocket(); + if ( port != -1 ) + initsocket(); } + /** + * Returns the UDP port to use. + * @return the UDP port to use + */ public int getPort() { return port; } + /** + * Sets the UDP port to use. + * @param port the UDP port to use + */ public void setPort(int port) { this.port = port; - initsocket(); + if ( address != null ) + initsocket(); } synchronized void initsocket() { @@ -71,14 +101,13 @@ public class UdpSender { void send(byte buff[]) { if ( sender == null ) - throw new MaxRuntimeException("UdpSender is not initialized"); - + throw new MaxRuntimeException("UdpSender: UPD port or address is missing"); + try { DatagramPacket packet = new DatagramPacket(buff, buff.length, inetAddress, port); sender.send(packet); } catch (IOException e) { throw new MaxRuntimeException(e); } - } } diff --git a/src/java/com/cycling74/net/package.html b/src/java/com/cycling74/net/package.html new file mode 100644 index 0000000..830ac7c --- /dev/null +++ b/src/java/com/cycling74/net/package.html @@ -0,0 +1,5 @@ +<html> +<body> +<p>Network utilities for sending atoms.</p> +</body> +</html>
\ No newline at end of file |