From 50567a9846b0c9750ab3860bb1fa5e8e4eaa154f Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Sun, 21 Jun 2015 14:52:18 +0100 Subject: [PATCH] Migrate to simplified implementation using improved client/server model and proper dependency injection --- build.gradle | 2 - .../com/github/boukefalos/ibuddy/Loader.java | 65 +++------- .../{helper/ServerHelper.java => Server.java} | 31 ++++- .../ibuddy/client/iBuddyTcpClient.java | 22 ---- .../ibuddy/exception/LoaderException.java | 6 - .../ibuddy/exception/ServerException.java | 6 - .../ibuddy/exception/iBuddyException.java | 1 - .../com/github/boukefalos/ibuddy/iBuddy.java | 10 +- .../{LocalImplementation.java => Local.java} | 14 ++- .../Remote.java} | 114 +++++++++--------- .../implementation/TcpImplementation.java | 84 ------------- .../implementation/UdpImplementation.java | 86 ------------- .../ibuddy/server/iBuddyServer.java | 5 - .../ibuddy/server/iBuddyTcpServer.java | 19 --- .../ibuddy/server/iBuddyUdpServer.java | 24 ---- src/main/resources/ibuddy.properties | 3 +- src/test/java/test/TestProperties.java | 13 -- src/test/java/test/TestTcpCommunication.java | 20 +-- src/test/java/test/TestUdpCommunication.java | 18 +-- 19 files changed, 136 insertions(+), 407 deletions(-) rename src/main/java/com/github/boukefalos/ibuddy/{helper/ServerHelper.java => Server.java} (76%) delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/exception/LoaderException.java delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/exception/ServerException.java rename src/main/java/com/github/boukefalos/ibuddy/implementation/{LocalImplementation.java => Local.java} (86%) rename src/main/java/com/github/boukefalos/ibuddy/{helper/SenderHelper.java => implementation/Remote.java} (57%) delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/implementation/UdpImplementation.java delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/server/iBuddyTcpServer.java delete mode 100644 src/main/java/com/github/boukefalos/ibuddy/server/iBuddyUdpServer.java delete mode 100644 src/test/java/test/TestProperties.java diff --git a/build.gradle b/build.gradle index f50ef05..d37b947 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/com/github/boukefalos/ibuddy/Loader.java b/src/main/java/com/github/boukefalos/ibuddy/Loader.java index 5b78936..57a1076 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/Loader.java +++ b/src/main/java/com/github/boukefalos/ibuddy/Loader.java @@ -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 { 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); } - } diff --git a/src/main/java/com/github/boukefalos/ibuddy/helper/ServerHelper.java b/src/main/java/com/github/boukefalos/ibuddy/Server.java similarity index 76% rename from src/main/java/com/github/boukefalos/ibuddy/helper/ServerHelper.java rename to src/main/java/com/github/boukefalos/ibuddy/Server.java index b9c37d4..d03e91e 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/helper/ServerHelper.java +++ b/src/main/java/com/github/boukefalos/ibuddy/Server.java @@ -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 { diff --git a/src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java b/src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java deleted file mode 100644 index 32012f9..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java +++ /dev/null @@ -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 - } -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/exception/LoaderException.java b/src/main/java/com/github/boukefalos/ibuddy/exception/LoaderException.java deleted file mode 100644 index 2157b9a..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/exception/LoaderException.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.github.boukefalos.ibuddy.exception; - -public class LoaderException extends Exception { - private static final long serialVersionUID = 1L; - -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/exception/ServerException.java b/src/main/java/com/github/boukefalos/ibuddy/exception/ServerException.java deleted file mode 100644 index 3ca48f0..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/exception/ServerException.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.github.boukefalos.ibuddy.exception; - -public class ServerException extends Exception { - private static final long serialVersionUID = 1L; - -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/exception/iBuddyException.java b/src/main/java/com/github/boukefalos/ibuddy/exception/iBuddyException.java index 20b155b..9814a41 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/exception/iBuddyException.java +++ b/src/main/java/com/github/boukefalos/ibuddy/exception/iBuddyException.java @@ -1,6 +1,5 @@ package com.github.boukefalos.ibuddy.exception; - public class iBuddyException extends Exception { private static final long serialVersionUID = 1L; diff --git a/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java b/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java index e57a74d..7552907 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java +++ b/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java @@ -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; } diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/LocalImplementation.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java similarity index 86% rename from src/main/java/com/github/boukefalos/ibuddy/implementation/LocalImplementation.java rename to src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java index 87e5051..c081ff0 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/LocalImplementation.java +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java @@ -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); } diff --git a/src/main/java/com/github/boukefalos/ibuddy/helper/SenderHelper.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java similarity index 57% rename from src/main/java/com/github/boukefalos/ibuddy/helper/SenderHelper.java rename to src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java index c283e84..40fd849 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/helper/SenderHelper.java +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java @@ -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); diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java deleted file mode 100644 index 403301d..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/UdpImplementation.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/UdpImplementation.java deleted file mode 100644 index 3e521cc..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/UdpImplementation.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java deleted file mode 100644 index 9cb91d3..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.boukefalos.ibuddy.server; - -public interface iBuddyServer { - -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyTcpServer.java b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyTcpServer.java deleted file mode 100644 index fd41bf6..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyTcpServer.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyUdpServer.java b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyUdpServer.java deleted file mode 100644 index 00fb15e..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyUdpServer.java +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/main/resources/ibuddy.properties b/src/main/resources/ibuddy.properties index e881371..5ccb617 100644 --- a/src/main/resources/ibuddy.properties +++ b/src/main/resources/ibuddy.properties @@ -2,4 +2,5 @@ implementation=local remote.host=localhost remote.port=8883 server.port=8883 -server=true \ No newline at end of file +server=true +server.protocol=tcp \ No newline at end of file diff --git a/src/test/java/test/TestProperties.java b/src/test/java/test/TestProperties.java deleted file mode 100644 index 6453b28..0000000 --- a/src/test/java/test/TestProperties.java +++ /dev/null @@ -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(); - } - } -} diff --git a/src/test/java/test/TestTcpCommunication.java b/src/test/java/test/TestTcpCommunication.java index 8418f75..04ed507 100644 --- a/src/test/java/test/TestTcpCommunication.java +++ b/src/test/java/test/TestTcpCommunication.java @@ -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(); } diff --git a/src/test/java/test/TestUdpCommunication.java b/src/test/java/test/TestUdpCommunication.java index 9ccda50..37021bf 100644 --- a/src/test/java/test/TestUdpCommunication.java +++ b/src/test/java/test/TestUdpCommunication.java @@ -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(); }