Snapshot, replace tabs with spaces

This commit is contained in:
2015-09-01 13:18:43 +01:00
parent 4437891be6
commit da1ff0679a
122 changed files with 4011 additions and 3826 deletions

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -1,22 +1,51 @@
package base.server.datagram; package base.server.datagram;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket;
import java.net.UnknownHostException;
import base.Sender; import base.Sender;
public class UdpDuplexClient extends UdpMulticastClient implements Sender { public class UdpDuplexClient extends UdpMulticastClient implements Sender {
protected int sendPort;
protected Sender sender;
public UdpDuplexClient(int port) { public UdpDuplexClient(int bindPort, String sendHost, int sendPort) throws UnknownHostException {
super(port); 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 { 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) {}
}

View File

@@ -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);
}
}

View File

@@ -1,9 +1,59 @@
package base.server.datagram; 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.exception.worker.DeactivateException;
public class UdpDuplexServer extends UdpMulticastServer { public class UdpDuplexServer extends UdpMulticastServer {
protected int bindPort;
protected UdpDuplexHelper helper;
public UdpDuplexServer(int port) { public UdpDuplexServer(int sendPort, int bindPort) {
super(port); 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);
}
}
} }

View File

@@ -1,22 +1,16 @@
package base.server.datagram; package base.server.datagram;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.MulticastSocket; import java.net.MulticastSocket;
import base.exception.worker.ActivateException; 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 String HOST = "239.255.255.255";
protected static final int BUFFER_SIZE = 2048;
protected String host; protected String host;
protected int port; protected int port;
protected int bufferSize;
protected MulticastSocket socket;
protected InetAddress group;
public UdpMulticastClient(int port) { public UdpMulticastClient(int port) {
this(HOST, port); this(HOST, port);
@@ -30,22 +24,13 @@ public abstract class UdpMulticastClient extends Work {
this.host = host; this.host = host;
this.port = port; this.port = port;
this.bufferSize = BUFFER_SIZE; this.bufferSize = BUFFER_SIZE;
} System.out.println("Client bind: " + host + " " + port);
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) {}
} }
public void activate() throws ActivateException { public void activate() throws ActivateException {
try { try {
socket = new MulticastSocket(port); socket = new MulticastSocket(port);
group = InetAddress.getByName(host); InetAddress group = InetAddress.getByName(host);
socket.joinGroup(group); socket.joinGroup(group);
} catch (IOException e) { } catch (IOException e) {
logger.error("", e); logger.error("", e);
@@ -53,10 +38,5 @@ public abstract class UdpMulticastClient extends Work {
} }
} }
public void stop() { protected void input(byte[] buffer) {}
socket.close();
super.stop();
}
protected abstract void input(byte[] buffer);
} }

View File

@@ -18,7 +18,6 @@ public class UdpMulticastServer extends Listen<byte[]> implements Sender {
protected String host; protected String host;
protected int port; protected int port;
protected MulticastSocket socket; protected MulticastSocket socket;
//private XX x;
public UdpMulticastServer(int port) { public UdpMulticastServer(int port) {
this(HOST, port); this(HOST, port);
@@ -28,16 +27,12 @@ public class UdpMulticastServer extends Listen<byte[]> implements Sender {
super(Worker.Type.BACKGROUND); super(Worker.Type.BACKGROUND);
this.host = host; this.host = host;
this.port = port; this.port = port;
System.out.println("Server send: " + host + " " + port);
} }
public void activate() throws ActivateException { public void activate() throws ActivateException {
try { try {
socket = new MulticastSocket(); // optional, add port and receive as well!! socket = new MulticastSocket();
// 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();
} catch (IOException e) { } catch (IOException e) {
throw new ActivateException(); throw new ActivateException();
} }
@@ -50,9 +45,6 @@ public class UdpMulticastServer extends Listen<byte[]> implements Sender {
} }
public void input(byte[] buffer) { public void input(byte[] buffer) {
if (socket == null) {
return;
}
try { try {
InetAddress group = InetAddress.getByName(host); InetAddress group = InetAddress.getByName(host);
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);

View File

@@ -25,27 +25,21 @@ public class UdpSender implements Sender {
} }
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); inetAddress = InetAddress.getByName(host);
logger.debug(host);
logger.debug(String.valueOf(port));
this.port = port; this.port = port;
}
public void start() {
if (datagramSocket == null) {
try { try {
datagramSocket = new DatagramSocket(); datagramSocket = new DatagramSocket();
} catch (SocketException e) { } catch (SocketException e) {
logger.error("Failed to create socket", e); logger.error("Failed to create socket", e);
} }
} }
}
public void start() {}
public void stop() { public void stop() {
if (datagramSocket != null) {
datagramSocket.close(); datagramSocket.close();
} }
}
public void exit() { public void exit() {
stop(); stop();
@@ -59,5 +53,4 @@ public class UdpSender implements Sender {
logger.error("Failed to send buffer", e); logger.error("Failed to send buffer", e);
} }
} }
} }

View File

@@ -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();
}
}

View File

@@ -1,5 +1,6 @@
package base.server.forwarder; package base.server.forwarder;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import base.Duplex; import base.Duplex;
@@ -9,8 +10,8 @@ import base.server.datagram.UdpDuplexClient;
public class UdpDuplexClientForwarder extends UdpDuplexClient implements Duplex { public class UdpDuplexClientForwarder extends UdpDuplexClient implements Duplex {
protected ArrayList<Receiver> receiverList; protected ArrayList<Receiver> receiverList;
public UdpDuplexClientForwarder(String host, int port) { public UdpDuplexClientForwarder(String bindHost, int bindPort, String sendHost, int sendPort) throws UnknownHostException {
super(port); super(bindHost, bindPort, sendHost, sendPort);
receiverList = new ArrayList<Receiver>(); receiverList = new ArrayList<Receiver>();
} }

View File

@@ -9,8 +9,8 @@ import base.server.datagram.UdpDuplexServer;
public class UdpDuplexServerForwarder extends UdpDuplexServer implements Duplex { public class UdpDuplexServerForwarder extends UdpDuplexServer implements Duplex {
protected ArrayList<Receiver> receiverList; protected ArrayList<Receiver> receiverList;
public UdpDuplexServerForwarder(int port) { public UdpDuplexServerForwarder(int port, int listenPort) {
super(port); super(port, listenPort);
receiverList = new ArrayList<Receiver>(); receiverList = new ArrayList<Receiver>();
} }

View File

@@ -93,7 +93,6 @@ public abstract class Listen<E> extends Work implements Listener<E> {
} }
public void input(Object object) { public void input(Object object) {
// This lookup should be cached
MethodType methodType = MethodType.methodType(void.class, object.getClass()); MethodType methodType = MethodType.methodType(void.class, object.getClass());
MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle methodHandle; MethodHandle methodHandle;

View File

@@ -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();
}
}
}
}

View File

@@ -53,7 +53,7 @@ public class TestUdpMulticastCommunication {
super(port); super(port);
} }
protected void input(byte[] buffer) { public void input(byte[] buffer) {
this.buffer = buffer; this.buffer = buffer;
synchronized (this) { synchronized (this) {
notifyAll(); notifyAll();