Migrate to simplified implementation using improved client/server model and proper dependency injection

This commit is contained in:
2015-06-21 14:52:18 +01:00
parent 20a235781d
commit 50567a9846
19 changed files with 136 additions and 407 deletions

View File

@@ -32,8 +32,6 @@ repositories {
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'org.slf4j:slf4j-log4j12:1.7.12'
compile 'org.picocontainer:picocontainer:2.15'
compile 'com.github.boukefalos:jlibusb:0.5.7'
}

View File

@@ -2,76 +2,45 @@ package com.github.boukefalos.ibuddy;
import java.util.Properties;
import org.picocontainer.Parameter;
import org.picocontainer.parameters.ConstantParameter;
import base.exception.LoaderException;
import base.loader.AbstractLoader;
import base.work.Work;
import com.github.boukefalos.ibuddy.client.iBuddyTcpClient;
import com.github.boukefalos.ibuddy.implementation.LocalImplementation;
import com.github.boukefalos.ibuddy.implementation.TcpImplementation;
import com.github.boukefalos.ibuddy.implementation.UdpImplementation;
import com.github.boukefalos.ibuddy.server.iBuddyServer;
import com.github.boukefalos.ibuddy.server.iBuddyTcpServer;
import com.github.boukefalos.ibuddy.server.iBuddyUdpServer;
import com.github.boukefalos.ibuddy.implementation.Local;
import com.github.boukefalos.ibuddy.implementation.Remote;
public class Loader extends AbstractLoader {
public class Loader extends AbstractLoader<Loader> {
protected static final String PROPERTIES_FILE = "ibuddy.properties";
public Loader(Properties properties) {
public Loader(Properties properties) throws LoaderException {
super();
/* Add implementation */
switch (properties.getProperty("implementation")) {
case "local":
pico.addComponent(LocalImplementation.class);
pico.addComponent(Local.class);
break;
case "remote":
//pico.addComponent(Remote.class);
break;
}
pico.addComponent(Remote.class);
/* Add protocol */
if (properties.getProperty("protocol") != null) {
switch (properties.getProperty("protocol")) {
case "tcp":
pico.addComponent(TcpImplementation.class, TcpImplementation.class, new Parameter[]{
new ConstantParameter(properties.getProperty("remote.host")),
new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))});
break;
case "udp":
pico.addComponent(UdpImplementation.class, UdpImplementation.class, new Parameter[] {
new ConstantParameter(properties.getProperty("remote.host")),
new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))});
break;
}
/* Add sender implementation */
addSender(properties);
break;
}
/* Add server */
if (properties.getProperty("server") != null) {
switch (properties.getProperty("server.protocol")) {
case "tcp":
pico.addComponent(iBuddyTcpServer.class, iBuddyTcpServer.class, new Parameter[]{
new ConstantParameter(getiBuddy()),
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port"))),
new ConstantParameter(iBuddyTcpClient.class)});
break;
case "udp":
pico.addComponent(iBuddyUdpServer.class, iBuddyUdpServer.class, new Parameter[]{
new ConstantParameter(getiBuddy()),
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
}
pico.addComponent(Server.class);
/* Add server forwarder implementation */
addForwarder(properties);
}
}
public iBuddy getiBuddy() {
public iBuddy getiBuddy() {
return pico.getComponent(iBuddy.class);
}
public Work getServer() {
return (Work) pico.getComponent(iBuddyServer.class);
public Server getServer() {
return pico.getComponent(Server.class);
}
}

View File

@@ -1,4 +1,4 @@
package com.github.boukefalos.ibuddy.helper;
package com.github.boukefalos.ibuddy;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -14,13 +14,34 @@ import proto.Ibuddy.Flap;
import proto.Ibuddy.Head;
import proto.Ibuddy.Nudge;
import proto.Ibuddy.State;
import base.Control;
import base.receiver.Forwarder;
import base.receiver.Receiver;
import com.github.boukefalos.ibuddy.iBuddy;
public class Server implements Receiver, Control {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected iBuddy iBuddy;
protected Forwarder forwarder;
public class ServerHelper {
protected static Logger logger = LoggerFactory.getLogger(ServerHelper.class);
public Server(iBuddy iBuddy, Forwarder forwarder) {
this.iBuddy = iBuddy;
this.forwarder = forwarder;
forwarder.register(this);
}
public static void receive(iBuddy iBuddy, byte[] buffer) {
public void start() {
forwarder.start();
}
public void stop() {
forwarder.stop();
}
public void exit() {
forwarder.exit();
}
public void receive(byte[] buffer) {
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
logger.debug("Received input");
try {

View File

@@ -1,22 +0,0 @@
package com.github.boukefalos.ibuddy.client;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import base.server.channel.TcpServer;
import base.server.channel.TcpServerClient;
public class iBuddyTcpClient extends TcpServerClient {
public iBuddyTcpClient(TcpServer server, SocketChannel socketChannel) {
super(server, socketChannel);
// TODO Auto-generated constructor stub
}
/*public iBuddyTcpClient(Socket socket) {
super(socket);
}*/
public void work() {
// Work in progress
}
}

View File

@@ -1,6 +0,0 @@
package com.github.boukefalos.ibuddy.exception;
public class LoaderException extends Exception {
private static final long serialVersionUID = 1L;
}

View File

@@ -1,6 +0,0 @@
package com.github.boukefalos.ibuddy.exception;
public class ServerException extends Exception {
private static final long serialVersionUID = 1L;
}

View File

@@ -1,6 +1,5 @@
package com.github.boukefalos.ibuddy.exception;
public class iBuddyException extends Exception {
private static final long serialVersionUID = 1L;

View File

@@ -1,12 +1,12 @@
package com.github.boukefalos.ibuddy;
import org.jraf.jlibibuddy.IBuddyException;
import proto.Ibuddy.Color;
import proto.Ibuddy.Direction;
import base.Control;
public interface iBuddy {
public interface iBuddy extends Control {
public void setHeart(boolean on) throws IBuddyException;
public void setHeadRed(boolean on) throws IBuddyException;
public void setHeadBlue(boolean on) throws IBuddyException;
@@ -21,7 +21,7 @@ public interface iBuddy {
public void setRotateCenter() throws IBuddyException;
public void setRotate(Direction direction) throws IBuddyException;
public void off() throws IBuddyException;
public void blink(Color color, long onTime, long offTime, int times) throws IBuddyException;
public void nudge(long delay, int times) throws IBuddyException;
public void flap(long delay, int times) throws IBuddyException;
public void blink(Color color, int onTime, int offTime, int times) throws IBuddyException;
public void nudge(int delay, int times) throws IBuddyException;
public void flap(int delay, int times) throws IBuddyException;
}

View File

@@ -9,14 +9,18 @@ import proto.Ibuddy.Direction;
import com.github.boukefalos.ibuddy.iBuddy;
public class LocalImplementation implements iBuddy {
public class Local implements iBuddy {
IBuddy IBuddy;
@SuppressWarnings("static-access")
public LocalImplementation() {
public Local() {
IBuddy = IBuddy.getIBuddy();
}
public void start() {}
public void stop() {}
public void exit() {}
public void setHeadRed(boolean on) throws IBuddyException {
IBuddy.sendHeadRed(on);
}
@@ -92,15 +96,15 @@ public class LocalImplementation implements iBuddy {
IBuddy.sendAllOff();
}
public void blink(Color color, long onTime, long offTime, int times) throws IBuddyException {
public void blink(Color color, int onTime, int offTime, int times) throws IBuddyException {
IBuddyUtils.blink(IBuddy, mapColor(color), onTime, offTime, times);
}
public void nudge(long delay, int times) throws IBuddyException {
public void nudge(int delay, int times) throws IBuddyException {
IBuddyUtils.nudge(IBuddy, delay, times);
}
public void flap(long delay, int times) throws IBuddyException {
public void flap(int delay, int times) throws IBuddyException {
IBuddyUtils.flap(IBuddy, delay, times);
}

View File

@@ -1,4 +1,4 @@
package com.github.boukefalos.ibuddy.helper;
package com.github.boukefalos.ibuddy.implementation;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -19,26 +19,43 @@ import proto.Ibuddy.Rotate;
import proto.Ibuddy.State;
import proto.Ibuddy.Type;
import proto.Ibuddy.Wings;
import base.Control;
import base.sender.Sender;
import com.github.boukefalos.ibuddy.iBuddy;
public class SenderHelper {
public class Remote implements iBuddy, Control {
protected final static int BUFFER_SIZE = 1024;
protected static Logger logger = LoggerFactory.getLogger(SenderHelper.class);
protected Logger logger = LoggerFactory.getLogger(getClass());
protected Sender sender;
public static void setHeart(Sender sender, boolean on) throws IBuddyException {
send(
sender,
public Remote(Sender sender) {
this.sender = sender;
}
public void start() {
sender.start();
}
public void stop() {
sender.stop();
}
public void exit() {
sender.exit();
}
public void setHeart(boolean on) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.HEART)
.setHeart(Heart.newBuilder()
.setState(mapState(on))).build());
}
public static void setHeadRed(Sender sender, boolean on) throws IBuddyException {
send(
sender,
public void setHeadRed(boolean on) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.HEAD)
.setHead(Head.newBuilder()
@@ -47,10 +64,8 @@ public class SenderHelper {
.setColor(Color.RED)).build());
}
public static void setHeadBlue(Sender sender, boolean on) throws IBuddyException {
send(
sender,
public void setHeadBlue(boolean on) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.HEAD)
.setHead(Head.newBuilder()
@@ -60,9 +75,8 @@ public class SenderHelper {
}
public static void setHeadGreen(Sender sender, boolean on) throws IBuddyException {
send(
sender,
public void setHeadGreen(boolean on) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.HEAD)
.setHead(Head.newBuilder()
@@ -71,76 +85,65 @@ public class SenderHelper {
.setColor(Color.GREEN)).build());
}
public static void setHead(Sender sender, Color color) throws IBuddyException {
public void setHead(Color color) throws IBuddyException {
State state = mapState(!color.equals(Color.NONE));
send(
sender,
send(
Command.newBuilder()
.setType(Type.HEAD)
.setHead(Head.newBuilder()
.setState(state)
.setSingle(false)
.setColor(color)).build());
}
public static void setWingsUp(Sender sender) throws IBuddyException {
setWings(sender, Direction.UP);
public void setWingsUp() throws IBuddyException {
setWings(Direction.UP);
}
public static void setWingsDown(Sender sender) throws IBuddyException {
setWings(sender, Direction.DOWN);
public void setWingsDown() throws IBuddyException {
setWings(Direction.DOWN);
}
public static void setWingsCenter(Sender sender) throws IBuddyException {
setWings(sender, Direction.CENTER);
public void setWingsCenter() throws IBuddyException {
setWings(Direction.CENTER);
}
public static void setWings(Sender sender, Direction direction) throws IBuddyException {
send(
sender,
public void setWings(Direction direction) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.WINGS)
.setWings(Wings.newBuilder()
.setDirection(direction)).build());
}
public static void setRotateLeft(Sender sender) throws IBuddyException {
setRotate(sender, Direction.LEFT);
public void setRotateLeft() throws IBuddyException {
setRotate(Direction.LEFT);
}
public static void setRotateRight(Sender sender) throws IBuddyException {
setRotate(sender, Direction.RIGHT);
public void setRotateRight() throws IBuddyException {
setRotate(Direction.RIGHT);
}
public static void setRotateCenter(Sender sender) throws IBuddyException {
setRotate(sender, Direction.CENTER);
public void setRotateCenter() throws IBuddyException {
setRotate(Direction.CENTER);
}
public static void setRotate(Sender sender, Direction direction) throws IBuddyException {
send(
sender,
public void setRotate(Direction direction) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.ROTATE)
.setRotate(Rotate.newBuilder()
.setDirection(direction)).build());
}
public static void off(Sender sender) throws IBuddyException {
send(
sender,
public void off() throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.STATE).build());
}
public static void blink(Sender sender, Color color, int onTime, int offTime, int times) throws IBuddyException {
send(
sender,
public void blink(Color color, int onTime, int offTime, int times) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.BLINK)
.setBlink(Blink.newBuilder()
@@ -149,9 +152,8 @@ public class SenderHelper {
.setTimes(times)).build());
}
public static void nudge(Sender sender, int delay, int times) throws IBuddyException {
send(
sender,
public void nudge(int delay, int times) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.NUDGE)
.setNudge(Nudge.newBuilder()
@@ -159,10 +161,8 @@ public class SenderHelper {
.setTimes(times)).build());
}
public static void flap(Sender sender, int delay, int times) throws IBuddyException {
send(
sender,
public void flap(int delay, int times) throws IBuddyException {
send(
Command.newBuilder()
.setType(Type.FLAP)
.setFlap(Flap.newBuilder()
@@ -174,7 +174,7 @@ public class SenderHelper {
return on ? State.ON : State.OFF;
}
protected static void send(Sender sender, Command command) {
protected void send(Command command) {
ByteArrayOutputStream output = new ByteArrayOutputStream(BUFFER_SIZE);
try {
command.writeDelimitedTo(output);

View File

@@ -1,84 +0,0 @@
package com.github.boukefalos.ibuddy.implementation;
import org.jraf.jlibibuddy.IBuddyException;
import proto.Ibuddy.Color;
import proto.Ibuddy.Direction;
import base.sender.TcpSender;
import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.helper.SenderHelper;
public class TcpImplementation extends TcpSender implements iBuddy {
public TcpImplementation(String host, int port) {
super(host, port);
}
public void setHeart(boolean on) throws IBuddyException {
SenderHelper.setHeart(this, on);
}
public void setHeadRed(boolean on) throws IBuddyException {
SenderHelper.setHeadRed(this, on);
}
public void setHeadBlue(boolean on) throws IBuddyException {
SenderHelper.setHeadBlue(this, on);
}
public void setHeadGreen(boolean on) throws IBuddyException {
SenderHelper.setHeadGreen(this, on);
}
public void setHead(Color color) throws IBuddyException {
SenderHelper.setHead(this, color);
}
public void setWingsUp() throws IBuddyException {
SenderHelper.setWingsUp(this);
}
public void setWingsDown() throws IBuddyException {
SenderHelper.setWingsDown(this);
}
public void setWingsCenter() throws IBuddyException {
SenderHelper.setWingsCenter(this);
}
public void setWings(Direction direction) throws IBuddyException {
SenderHelper.setWings(this, direction);
}
public void setRotateLeft() throws IBuddyException {
SenderHelper.setRotateLeft(this);
}
public void setRotateRight() throws IBuddyException {
SenderHelper.setRotateRight(this);
}
public void setRotateCenter() throws IBuddyException {
SenderHelper.setRotateCenter(this);
}
public void setRotate(Direction direction) throws IBuddyException {
SenderHelper.setRotate(this, direction);
}
public void off() throws IBuddyException {
SenderHelper.off(this);
}
public void blink(Color color, long onTime, long offTime, int times) throws IBuddyException {
SenderHelper.blink(this, color, (int) onTime, (int) offTime, times);
}
public void nudge(long delay, int times) throws IBuddyException {
SenderHelper.nudge(this, (int) delay, (int) times);
}
public void flap(long delay, int times) throws IBuddyException {
SenderHelper.flap(this, (int) delay, times);
}
}

View File

@@ -1,86 +0,0 @@
package com.github.boukefalos.ibuddy.implementation;
import java.net.UnknownHostException;
import org.jraf.jlibibuddy.IBuddyException;
import proto.Ibuddy.Color;
import proto.Ibuddy.Direction;
import base.sender.UdpSender;
import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.helper.SenderHelper;
public class UdpImplementation extends UdpSender implements iBuddy {
public UdpImplementation(String host, int port) throws UnknownHostException {
super(host, port);
}
public void setHeart(boolean on) throws IBuddyException {
SenderHelper.setHeart(this, on);
}
public void setHeadRed(boolean on) throws IBuddyException {
SenderHelper.setHeadRed(this, on);
}
public void setHeadBlue(boolean on) throws IBuddyException {
SenderHelper.setHeadBlue(this, on);
}
public void setHeadGreen(boolean on) throws IBuddyException {
SenderHelper.setHeadGreen(this, on);
}
public void setHead(Color color) throws IBuddyException {
SenderHelper.setHead(this, color);
}
public void setWingsUp() throws IBuddyException {
SenderHelper.setWingsUp(this);
}
public void setWingsDown() throws IBuddyException {
SenderHelper.setWingsDown(this);
}
public void setWingsCenter() throws IBuddyException {
SenderHelper.setWingsCenter(this);
}
public void setWings(Direction direction) throws IBuddyException {
SenderHelper.setWings(this, direction);
}
public void setRotateLeft() throws IBuddyException {
SenderHelper.setRotateLeft(this);
}
public void setRotateRight() throws IBuddyException {
SenderHelper.setRotateRight(this);
}
public void setRotateCenter() throws IBuddyException {
SenderHelper.setRotateCenter(this);
}
public void setRotate(Direction direction) throws IBuddyException {
SenderHelper.setRotate(this, direction);
}
public void off() throws IBuddyException {
SenderHelper.off(this);
}
public void blink(Color color, long onTime, long offTime, int times) throws IBuddyException {
SenderHelper.blink(this, color, (int) onTime, (int) offTime, times);
}
public void nudge(long delay, int times) throws IBuddyException {
SenderHelper.nudge(this, (int) delay, (int) times);
}
public void flap(long delay, int times) throws IBuddyException {
SenderHelper.flap(this, (int) delay, times);
}
}

View File

@@ -1,5 +0,0 @@
package com.github.boukefalos.ibuddy.server;
public interface iBuddyServer {
}

View File

@@ -1,19 +0,0 @@
package com.github.boukefalos.ibuddy.server;
import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.helper.ServerHelper;
import base.server.socket.TcpServer;
public class iBuddyTcpServer extends TcpServer implements iBuddyServer {
protected iBuddy iBuddy;
public iBuddyTcpServer(iBuddy iBuddy, int port, Class<?> clientClass) {
super(port, clientClass);
this.iBuddy = iBuddy;
}
public void receive(byte[] buffer) {
ServerHelper.receive(iBuddy, buffer);
}
}

View File

@@ -1,24 +0,0 @@
package com.github.boukefalos.ibuddy.server;
import base.server.datagram.UdpServer;
import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.helper.ServerHelper;
public class iBuddyUdpServer extends UdpServer implements iBuddyServer {
protected iBuddy iBuddy;
public iBuddyUdpServer(iBuddy iBuddy, int port) {
this(iBuddy, port, BUFFER_SIZE);
}
public iBuddyUdpServer(iBuddy iBuddy, int port, int bufferSize) {
super(port, bufferSize);
this.iBuddy = iBuddy;
this.bufferSize = bufferSize;
}
protected void receive(byte[] buffer) {
ServerHelper.receive(iBuddy, buffer);
}
}

View File

@@ -2,4 +2,5 @@ implementation=local
remote.host=localhost
remote.port=8883
server.port=8883
server=true
server=true
server.protocol=tcp

View File

@@ -1,13 +0,0 @@
package test;
import com.github.boukefalos.ibuddy.Loader;
public class TestProperties {
public static void main(String[] args) {
try {
Loader.getLoader().getServer().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -3,9 +3,9 @@ package test;
import java.util.Properties;
import proto.Ibuddy.Color;
import base.work.Work;
import com.github.boukefalos.ibuddy.Loader;
import com.github.boukefalos.ibuddy.Server;
import com.github.boukefalos.ibuddy.iBuddy;
public class TestTcpCommunication {
@@ -16,6 +16,7 @@ public class TestTcpCommunication {
localProperties.setProperty("server", "true");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "tcp");
localProperties.setProperty("tcp.implementation", "socket");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
@@ -26,15 +27,16 @@ public class TestTcpCommunication {
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
iBuddy localiBuddy = localLoader.getiBuddy();
iBuddy remoteiBuddy = remoteLoader.getiBuddy();
//localiBuddy.setHead(Color.WHITE);
Work server = localLoader.getServer();
Server server = localLoader.getServer();
iBuddy iBuddy = remoteLoader.getiBuddy();
server.start();
remoteiBuddy.setHeadRed(true);
Thread.sleep(1000);
//server.exit();
iBuddy.start();
iBuddy.setHead(Color.BLUE);
server.exit();
iBuddy.exit();
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -2,9 +2,10 @@ package test;
import java.util.Properties;
import base.work.Work;
import proto.Ibuddy.Color;
import com.github.boukefalos.ibuddy.Loader;
import com.github.boukefalos.ibuddy.Server;
import com.github.boukefalos.ibuddy.iBuddy;
public class TestUdpCommunication {
@@ -25,17 +26,16 @@ public class TestUdpCommunication {
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
iBuddy localiBuddy = localLoader.getiBuddy();
iBuddy remoteiBuddy = remoteLoader.getiBuddy();
localiBuddy.setHeadRed(false);
Work server = localLoader.getServer();
iBuddy iBuddy = remoteLoader.getiBuddy();
Server server = localLoader.getServer();
iBuddy.start();
server.start();
remoteiBuddy.setHeadRed(true);
Thread.sleep(1000);
iBuddy.setHead(Color.WHITE);
server.exit();
iBuddy.exit();
} catch (Exception e) {
e.printStackTrace();
}