Snapshot, replace tabs with spaces
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class AbstractUdpClient extends Work {
|
||||
protected static final int BUFFER_SIZE = 2048;
|
||||
|
||||
protected int bufferSize;
|
||||
protected MulticastSocket socket;
|
||||
protected DatagramPacket datagramPacket;
|
||||
|
||||
public AbstractUdpClient() {}
|
||||
|
||||
public AbstractUdpClient(MulticastSocket socket) {
|
||||
this(socket, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public AbstractUdpClient(MulticastSocket socket, int bufferSize) {
|
||||
this.socket = socket;
|
||||
this.bufferSize = bufferSize;
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
datagramPacket = new DatagramPacket(buffer, buffer.length);
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(packet);
|
||||
System.out.println("iets ontvangen!!!!!");
|
||||
buffer = packet.getData();
|
||||
input(buffer);
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
socket.close();
|
||||
super.stop();
|
||||
}
|
||||
|
||||
protected abstract void input(byte[] buffer);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package base.server.datagram;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UdpDuplexAutoClient extends UdpDuplexClient {
|
||||
public UdpDuplexAutoClient(int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(HOST, bindPort, null, sendPort);
|
||||
}
|
||||
|
||||
public UdpDuplexAutoClient(String bindHost, int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(bindHost, bindPort, null, sendPort);
|
||||
}
|
||||
|
||||
public UdpDuplexAutoClient(String bindHost, int bindPort, int sendPort, int bufferSize) throws UnknownHostException {
|
||||
super(bindHost, bindPort, null, sendPort, bufferSize);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,51 @@
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import base.Sender;
|
||||
|
||||
public class UdpDuplexClient extends UdpMulticastClient implements Sender {
|
||||
protected int sendPort;
|
||||
protected Sender sender;
|
||||
|
||||
public UdpDuplexClient(int port) {
|
||||
super(port);
|
||||
public UdpDuplexClient(int bindPort, String sendHost, int sendPort) throws UnknownHostException {
|
||||
this(HOST, bindPort, sendHost, sendPort);
|
||||
}
|
||||
|
||||
public UdpDuplexClient(String bindHost, int bindPort, String sendHost, int sendPort) throws UnknownHostException {
|
||||
this(bindHost, bindPort, sendHost, sendPort, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public UdpDuplexClient(String bindHost, int bindPort, String sendHost, int sendPort, int bufferSize) throws UnknownHostException {
|
||||
super(bindHost, bindPort, bufferSize);
|
||||
this.sendPort = sendPort;
|
||||
if (sendHost != null) {
|
||||
sender = new UdpSender(sendHost, sendPort);
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(packet);
|
||||
buffer = packet.getData();
|
||||
System.out.println("Receive from " + packet.getAddress().getHostAddress());
|
||||
if (sender == null) {
|
||||
String sendHost = packet.getAddress().getHostAddress();
|
||||
sender = new UdpSender(sendHost, sendPort);
|
||||
}
|
||||
input(buffer);
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
if (sender != null) {
|
||||
sender.send(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
public void input(byte[] buffer) {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package base.server.datagram;
|
||||
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.work.Listen;
|
||||
|
||||
public class UdpDuplexHelper extends AbstractUdpClient {
|
||||
protected Listen<byte[]> listen;
|
||||
|
||||
public UdpDuplexHelper(Listen<byte[]> listen, MulticastSocket socket) {
|
||||
super(socket);
|
||||
this.listen = listen;
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
System.out.println("jajajaja");
|
||||
listen.add(buffer);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,59 @@
|
||||
package base.server.datagram;
|
||||
|
||||
public class UdpDuplexServer extends UdpMulticastServer {
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
public UdpDuplexServer(int port) {
|
||||
super(port);
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
|
||||
public class UdpDuplexServer extends UdpMulticastServer {
|
||||
protected int bindPort;
|
||||
protected UdpDuplexHelper helper;
|
||||
|
||||
public UdpDuplexServer(int sendPort, int bindPort) {
|
||||
super(sendPort);
|
||||
this.bindPort = bindPort;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new MulticastSocket(bindPort);
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
helper = new UdpDuplexHelper(this, socket);
|
||||
helper.start();
|
||||
} catch (IOException e) {
|
||||
throw new ActivateException();
|
||||
}
|
||||
super.activate();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
helper.stop();
|
||||
super.deactivate();
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
if (socket == null) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
InetAddress group = InetAddress.getByName(host);
|
||||
System.out.println("Send to " + host + " " + port);
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
|
||||
socket.send(packet);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,16 @@
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class UdpMulticastClient extends Work {
|
||||
public class UdpMulticastClient extends AbstractUdpClient {
|
||||
protected static final String HOST = "239.255.255.255";
|
||||
protected static final int BUFFER_SIZE = 2048;
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected int bufferSize;
|
||||
protected MulticastSocket socket;
|
||||
protected InetAddress group;
|
||||
|
||||
public UdpMulticastClient(int port) {
|
||||
this(HOST, port);
|
||||
@@ -30,22 +24,13 @@ public abstract class UdpMulticastClient extends Work {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.bufferSize = BUFFER_SIZE;
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(packet);
|
||||
buffer = packet.getData();
|
||||
input(buffer);
|
||||
} catch (IOException e) {}
|
||||
System.out.println("Client bind: " + host + " " + port);
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new MulticastSocket(port);
|
||||
group = InetAddress.getByName(host);
|
||||
InetAddress group = InetAddress.getByName(host);
|
||||
socket.joinGroup(group);
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
@@ -53,10 +38,5 @@ public abstract class UdpMulticastClient extends Work {
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
socket.close();
|
||||
super.stop();
|
||||
}
|
||||
|
||||
protected abstract void input(byte[] buffer);
|
||||
protected void input(byte[] buffer) {}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ public class UdpMulticastServer extends Listen<byte[]> implements Sender {
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected MulticastSocket socket;
|
||||
//private XX x;
|
||||
|
||||
public UdpMulticastServer(int port) {
|
||||
this(HOST, port);
|
||||
@@ -28,16 +27,12 @@ public class UdpMulticastServer extends Listen<byte[]> implements Sender {
|
||||
super(Worker.Type.BACKGROUND);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
System.out.println("Server send: " + host + " " + port);
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new MulticastSocket(); // optional, add port and receive as well!!
|
||||
// pass socket directly to Server to establish bidirectional
|
||||
// couple together capabilities
|
||||
// listen to datagrams and deal with writing using nio?
|
||||
//x = new XX(socket);
|
||||
//x.start();
|
||||
socket = new MulticastSocket();
|
||||
} catch (IOException e) {
|
||||
throw new ActivateException();
|
||||
}
|
||||
@@ -50,9 +45,6 @@ public class UdpMulticastServer extends Listen<byte[]> implements Sender {
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
if (socket == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
InetAddress group = InetAddress.getByName(host);
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
|
||||
|
||||
@@ -24,28 +24,22 @@ public class UdpSender implements Sender {
|
||||
this(HOST, port);
|
||||
}
|
||||
|
||||
public UdpSender(String host, int port) throws UnknownHostException{
|
||||
public UdpSender(String host, int port) throws UnknownHostException {
|
||||
System.out.println("Sender use: " + host + " " + port);
|
||||
inetAddress = InetAddress.getByName(host);
|
||||
logger.debug(host);
|
||||
logger.debug(String.valueOf(port));
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (datagramSocket == null) {
|
||||
try {
|
||||
datagramSocket = new DatagramSocket();
|
||||
} catch (SocketException e) {
|
||||
logger.error("Failed to create socket", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {}
|
||||
|
||||
public void stop() {
|
||||
if (datagramSocket != null) {
|
||||
datagramSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
stop();
|
||||
@@ -59,5 +53,4 @@ public class UdpSender implements Sender {
|
||||
logger.error("Failed to send buffer", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.work.Work;
|
||||
|
||||
public class XX extends Work {
|
||||
|
||||
protected int bufferSize = 1024;
|
||||
private MulticastSocket socket;
|
||||
private DatagramPacket dgram;
|
||||
|
||||
public XX(MulticastSocket socket) {
|
||||
this.socket = socket;
|
||||
byte[] b = new byte[1024];
|
||||
dgram = new DatagramPacket(b, b.length);
|
||||
}
|
||||
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
socket.receive(dgram);
|
||||
} catch (IOException e) {
|
||||
stop();
|
||||
} // blocks until a datagram is received
|
||||
System.err.println("Received " + dgram.getLength() +
|
||||
" bytes from " + dgram.getAddress());
|
||||
dgram.setLength(bufferSize); // must reset length field!
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
super.stop();
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
@@ -9,8 +10,8 @@ import base.server.datagram.UdpDuplexClient;
|
||||
public class UdpDuplexClientForwarder extends UdpDuplexClient implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public UdpDuplexClientForwarder(String host, int port) {
|
||||
super(port);
|
||||
public UdpDuplexClientForwarder(String bindHost, int bindPort, String sendHost, int sendPort) throws UnknownHostException {
|
||||
super(bindHost, bindPort, sendHost, sendPort);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ import base.server.datagram.UdpDuplexServer;
|
||||
public class UdpDuplexServerForwarder extends UdpDuplexServer implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public UdpDuplexServerForwarder(int port) {
|
||||
super(port);
|
||||
public UdpDuplexServerForwarder(int port, int listenPort) {
|
||||
super(port, listenPort);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,6 @@ public abstract class Listen<E> extends Work implements Listener<E> {
|
||||
}
|
||||
|
||||
public void input(Object object) {
|
||||
// This lookup should be cached
|
||||
MethodType methodType = MethodType.methodType(void.class, object.getClass());
|
||||
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
||||
MethodHandle methodHandle;
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.datagram.UdpDuplexAutoClient;
|
||||
import base.server.datagram.UdpDuplexServer;
|
||||
|
||||
public class TestUdpDuplexCommunication {
|
||||
protected TestUdpDuplexServer server;
|
||||
protected TestUdpDuplexClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestUdpDuplexServer(1234, 1235);
|
||||
server.start();
|
||||
client = new TestUdpDuplexClient(1234, 1235);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServerToClientCommunication() throws Exception {
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientToServerCommunication() throws Exception {
|
||||
// Let client discover server address
|
||||
testServerToClientCommunication();
|
||||
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
public class TestUdpDuplexServer extends UdpDuplexServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpDuplexServer(int sendPort, int bindPort) {
|
||||
super(sendPort, bindPort);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestUdpDuplexClient extends UdpDuplexAutoClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpDuplexClient(int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(bindPort, sendPort);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public class TestUdpMulticastCommunication {
|
||||
super(port);
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
|
||||
Reference in New Issue
Block a user