Some refactoring
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package base.server;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class AbstractClient extends Work {
|
||||
protected Socket socket;
|
||||
protected InputStream inputStream;
|
||||
protected OutputStream outputStream;
|
||||
|
||||
public AbstractClient(Socket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package base.server;
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.receiver.Receiver;
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class UdpServer extends Work {
|
||||
@@ -15,6 +17,7 @@ public abstract class UdpServer extends Work {
|
||||
protected int port;
|
||||
protected int bufferSize;
|
||||
protected DatagramSocket diagramSocket;
|
||||
protected ArrayList<Receiver> receiverList = new ArrayList<Receiver>();
|
||||
|
||||
public UdpServer(int port) {
|
||||
this(port, BUFFER_SIZE);
|
||||
@@ -50,8 +53,16 @@ public abstract class UdpServer extends Work {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
receive(buffer);
|
||||
for (Receiver receiver : receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected void receive(byte[] buffer);
|
||||
public void addReceiver(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void removeReceiver(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,32 @@
|
||||
package base.server;
|
||||
package base.server.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.receiver.Receiver;
|
||||
|
||||
public abstract class TcpClient extends AbstractClient {
|
||||
protected static final int BUFFER = 2048;
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected int bufferSize;
|
||||
protected ArrayList<Receiver> receiverList = new ArrayList<Receiver>();
|
||||
|
||||
public TcpClient(String host, int port) {
|
||||
this(host, port, BUFFER);
|
||||
}
|
||||
|
||||
public TcpClient(String host, int port, int bufferSize) {
|
||||
super(null);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
@@ -25,7 +34,7 @@ public abstract class TcpClient extends AbstractClient {
|
||||
socket = new Socket(host, port);
|
||||
inputStream = socket.getInputStream();
|
||||
outputStream = socket.getOutputStream();
|
||||
send("Incoming client!".getBytes());
|
||||
//send("Incoming client!".getBytes());
|
||||
} catch (UnknownHostException e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
@@ -52,10 +61,12 @@ public abstract class TcpClient extends AbstractClient {
|
||||
}
|
||||
|
||||
public final void work() {
|
||||
byte[] buffer = new byte[BUFFER];
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
try {
|
||||
while (inputStream.read(buffer) > 0) {
|
||||
receive(buffer);
|
||||
for (Receiver receiver : receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
stop();
|
||||
@@ -67,5 +78,11 @@ public abstract class TcpClient extends AbstractClient {
|
||||
outputStream.write(buffer);
|
||||
}
|
||||
|
||||
public abstract void receive(byte[] buffer);
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package base.server;
|
||||
package base.server.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -7,14 +7,17 @@ import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.receiver.Receiver;
|
||||
import base.sender.Sender;
|
||||
import base.work.Work;
|
||||
|
||||
public class TcpServer extends Work {
|
||||
public class TcpServer extends Work implements Sender {
|
||||
protected int port;
|
||||
protected Socket socket;
|
||||
protected Constructor<?> clientConstructor;
|
||||
protected ArrayList<Client> clientList;
|
||||
protected ServerSocket serverSocket;
|
||||
protected ArrayList<Receiver> receiverList = new ArrayList<Receiver>();
|
||||
|
||||
public TcpServer(int port, Class<?> clientClass) {
|
||||
this.port = port;
|
||||
@@ -63,6 +66,22 @@ public class TcpServer extends Work {
|
||||
|
||||
public void work() {
|
||||
}
|
||||
|
||||
//public send(byte[] )
|
||||
}
|
||||
|
||||
public void addReceiver(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void removeReceiver(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
for (Client client : clientList) {
|
||||
// Should be dealt with in clients own thread
|
||||
client.send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2015 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package mimis.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ArrayCycle<E> extends ArrayList<E> {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
protected int index = 0;
|
||||
|
||||
public ArrayCycle(E... elementArray) {
|
||||
if (elementArray != null) {
|
||||
for (E element : elementArray) {
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public E current() {
|
||||
return this.get(index);
|
||||
}
|
||||
|
||||
public E previous() {
|
||||
if (--index < 0) {
|
||||
index = Math.max(0, size() - 1);
|
||||
}
|
||||
return get(index);
|
||||
}
|
||||
|
||||
public E next() {
|
||||
if (++index >= size()) {
|
||||
index = 0;
|
||||
}
|
||||
return size() == 0 ? null : get(index);
|
||||
}
|
||||
|
||||
public E reset() {
|
||||
return get(index = 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user