diff --git a/src/main/java/com/github/boukefalos/ibuddy/Loader.java b/src/main/java/com/github/boukefalos/ibuddy/Loader.java index b36c87e..6457da7 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/Loader.java +++ b/src/main/java/com/github/boukefalos/ibuddy/Loader.java @@ -10,9 +10,15 @@ import org.picocontainer.parameters.ConstantParameter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.github.boukefalos.ibuddy.implementation.Local; -import com.github.boukefalos.ibuddy.implementation.Remote; -import com.github.boukefalos.ibuddy.server.Server; +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.iBuddyTcpServer; +import com.github.boukefalos.ibuddy.server.iBuddyUdpServer; +import com.github.boukefalos.ibuddy.server.iBuddyServer; public class Loader { protected static final String PROPERTIES_FILE = "ibuddy.properties"; @@ -24,21 +30,46 @@ public class Loader { pico = new DefaultPicoContainer(); /* Add implementation */ - if (properties.getProperty("implementation").equals("local")) { - /* Local */ - pico.addComponent(Local.class); - } else { - /* Remote */ - pico.addComponent(Remote.class, Remote.class, new Parameter[]{ - new ConstantParameter(properties.getProperty("remote.host")), - new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))}); + switch (properties.getProperty("implementation")) { + case "local": + pico.addComponent(LocalImplementation.class); + break; + case "remote": + //pico.addComponent(Remote.class); + break; } - + + /* 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 server */ if (properties.getProperty("server") != null) { - pico.addComponent(Server.class, Server.class, new Parameter[]{ - new ConstantParameter(getiBuddy()), - new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))}); + 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")))}); + } + } } @@ -59,8 +90,8 @@ public class Loader { return pico.getComponent(iBuddy.class); } - public Server getServer() { - return pico.getComponent(Server.class); + public Work getServer() { + return (Work) pico.getComponent(iBuddyServer.class); } } diff --git a/src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java b/src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java new file mode 100644 index 0000000..32012f9 --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/client/iBuddyTcpClient.java @@ -0,0 +1,22 @@ +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/helper/SenderHelper.java b/src/main/java/com/github/boukefalos/ibuddy/helper/SenderHelper.java new file mode 100644 index 0000000..c283e84 --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/helper/SenderHelper.java @@ -0,0 +1,186 @@ +package com.github.boukefalos.ibuddy.helper; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.jraf.jlibibuddy.IBuddyException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import proto.Ibuddy.Blink; +import proto.Ibuddy.Color; +import proto.Ibuddy.Command; +import proto.Ibuddy.Direction; +import proto.Ibuddy.Flap; +import proto.Ibuddy.Head; +import proto.Ibuddy.Heart; +import proto.Ibuddy.Nudge; +import proto.Ibuddy.Rotate; +import proto.Ibuddy.State; +import proto.Ibuddy.Type; +import proto.Ibuddy.Wings; +import base.sender.Sender; + + +public class SenderHelper { + protected final static int BUFFER_SIZE = 1024; + protected static Logger logger = LoggerFactory.getLogger(SenderHelper.class); + + + public static void setHeart(Sender sender, boolean on) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.HEART) + .setHeart(Heart.newBuilder() + .setState(mapState(on))).build()); + } + + public static void setHeadRed(Sender sender, boolean on) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.HEAD) + .setHead(Head.newBuilder() + .setState(mapState(on)) + .setSingle(true) + .setColor(Color.RED)).build()); + } + + + public static void setHeadBlue(Sender sender, boolean on) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.HEAD) + .setHead(Head.newBuilder() + .setState(mapState(on)) + .setSingle(true) + .setColor(Color.BLUE)).build()); + } + + + public static void setHeadGreen(Sender sender, boolean on) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.HEAD) + .setHead(Head.newBuilder() + .setState(mapState(on)) + .setSingle(true) + .setColor(Color.GREEN)).build()); + } + + + public static void setHead(Sender sender, Color color) throws IBuddyException { + State state = mapState(!color.equals(Color.NONE)); + send( + sender, + 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 static void setWingsDown(Sender sender) throws IBuddyException { + setWings(sender, Direction.DOWN); + } + + + public static void setWingsCenter(Sender sender) throws IBuddyException { + setWings(sender, Direction.CENTER); + } + + public static void setWings(Sender sender, Direction direction) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.WINGS) + .setWings(Wings.newBuilder() + .setDirection(direction)).build()); + + } + + public static void setRotateLeft(Sender sender) throws IBuddyException { + setRotate(sender, Direction.LEFT); + } + + public static void setRotateRight(Sender sender) throws IBuddyException { + setRotate(sender, Direction.RIGHT); + } + + public static void setRotateCenter(Sender sender) throws IBuddyException { + setRotate(sender, Direction.CENTER); + } + + public static void setRotate(Sender sender, Direction direction) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.ROTATE) + .setRotate(Rotate.newBuilder() + .setDirection(direction)).build()); + } + + + public static void off(Sender sender) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.STATE).build()); + } + + public static void blink(Sender sender, Color color, int onTime, int offTime, int times) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.BLINK) + .setBlink(Blink.newBuilder() + .setOnTime(onTime) + .setOffTime(offTime) + .setTimes(times)).build()); + } + + public static void nudge(Sender sender, int delay, int times) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.NUDGE) + .setNudge(Nudge.newBuilder() + .setDelay(delay) + .setTimes(times)).build()); + } + + + public static void flap(Sender sender, int delay, int times) throws IBuddyException { + send( + sender, + Command.newBuilder() + .setType(Type.FLAP) + .setFlap(Flap.newBuilder() + .setDelay(delay) + .setTimes(times)).build()); + } + + protected static State mapState(boolean on) { + return on ? State.ON : State.OFF; + } + + protected static void send(Sender sender, Command command) { + ByteArrayOutputStream output = new ByteArrayOutputStream(BUFFER_SIZE); + try { + command.writeDelimitedTo(output); + sender.send(output.toByteArray()); + } catch (IOException e) { + logger.error("Failed to send command"); + } + } +} diff --git a/src/main/java/com/github/boukefalos/ibuddy/helper/ServerHelper.java b/src/main/java/com/github/boukefalos/ibuddy/helper/ServerHelper.java new file mode 100644 index 0000000..b9c37d4 --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/helper/ServerHelper.java @@ -0,0 +1,80 @@ +package com.github.boukefalos.ibuddy.helper; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.jraf.jlibibuddy.IBuddyException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import proto.Ibuddy.Blink; +import proto.Ibuddy.Color; +import proto.Ibuddy.Command; +import proto.Ibuddy.Flap; +import proto.Ibuddy.Head; +import proto.Ibuddy.Nudge; +import proto.Ibuddy.State; + +import com.github.boukefalos.ibuddy.iBuddy; + +public class ServerHelper { + protected static Logger logger = LoggerFactory.getLogger(ServerHelper.class); + + public static void receive(iBuddy iBuddy, byte[] buffer) { + ByteArrayInputStream input = new ByteArrayInputStream(buffer); + logger.debug("Received input"); + try { + Command command = Command.parseDelimitedFrom(input); + logger.debug("Command type = " + command.getType().name()); + switch (command.getType()) { + case HEAD: + Head head = command.getHead(); + Color color = head.getColor(); + if (head.getSingle()) { + boolean state = head.getState().equals(State.ON); + switch (color) { + case RED: + iBuddy.setHeadRed(state); + case GREEN: + iBuddy.setHeadGreen(state); + case BLUE: + iBuddy.setHeadBlue(state); + default: + break; + } + } else { + iBuddy.setHead(color); + } + break; + case WINGS: + iBuddy.setWings(command.getWings().getDirection()); + break; + case ROTATE: + iBuddy.setRotate(command.getRotate().getDirection()); + break; + case HEART: + iBuddy.setHeart(command.getHeart().getState().equals(State.ON)); + break; + case BLINK: + Blink blink = command.getBlink(); + iBuddy.blink(blink.getColor(), blink.getOnTime(), blink.getOffTime(), blink.getTimes()); + break; + case NUDGE: + Nudge nudge = command.getNudge(); + iBuddy.nudge(nudge.getDelay(), nudge.getTimes()); + break; + case FLAP: + Flap flap = command.getFlap(); + iBuddy.flap(flap.getDelay(), flap.getTimes()); + break; + default: + iBuddy.off(); + } + } catch (IOException e) { + logger.error("Failed to parse input"); + return; + } catch (IBuddyException e) { + logger.error("Failed to send command to iBuddy", e); + } + } +} diff --git a/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java b/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java index 76cabd8..e57a74d 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java +++ b/src/main/java/com/github/boukefalos/ibuddy/iBuddy.java @@ -1,12 +1,27 @@ package com.github.boukefalos.ibuddy; + import org.jraf.jlibibuddy.IBuddyException; -import com.github.boukefalos.ibuddy.exception.iBuddyException; +import proto.Ibuddy.Color; +import proto.Ibuddy.Direction; public interface iBuddy { - public void setHeadRed(boolean headRed) throws iBuddyException; - public void setHeadGreen(boolean headGreen) throws iBuddyException; - public void setHeadBlue(boolean headBlue) throws iBuddyException; - public void test() throws IBuddyException; + public void setHeart(boolean on) throws IBuddyException; + public void setHeadRed(boolean on) throws IBuddyException; + public void setHeadBlue(boolean on) throws IBuddyException; + public void setHeadGreen(boolean on) throws IBuddyException; + public void setHead(Color color) throws IBuddyException; + public void setWingsUp() throws IBuddyException; + public void setWingsDown() throws IBuddyException; + public void setWingsCenter() throws IBuddyException; + public void setWings(Direction direction) throws IBuddyException; + public void setRotateLeft() throws IBuddyException; + public void setRotateRight() throws IBuddyException; + 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; } diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java deleted file mode 100644 index e7a7f5b..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.github.boukefalos.ibuddy.implementation; - -import org.jraf.jlibibuddy.IBuddy; -import org.jraf.jlibibuddy.IBuddyException; - -import com.github.boukefalos.ibuddy.iBuddy; -import com.github.boukefalos.ibuddy.exception.iBuddyException; - -public class Local implements iBuddy { - IBuddy IBuddy; - - @SuppressWarnings("static-access") - public Local() { - IBuddy = IBuddy.getIBuddy(); - } - - public void sendHeadRed(boolean headRed) throws iBuddyException { - try { - IBuddy.sendHeadRed(headRed); - } catch (IBuddyException e) { - throw new iBuddyException(); - } - } - - public void setHeadGreen(boolean headGreen) throws iBuddyException { - try { - IBuddy.sendHeadGreen(headGreen); - } catch (IBuddyException e) { - throw new iBuddyException(); - } - - } - - public void setHeadBlue(boolean headBlue) throws iBuddyException { - try { - IBuddy.sendHeadBlue(headBlue); - } catch (IBuddyException e) { - throw new iBuddyException(); - } - - } - - public void setHeadRed(boolean headRed) throws iBuddyException { - try { - IBuddy.sendHeadRed(headRed); - } catch (IBuddyException e) { - throw new iBuddyException(); - } - - } - - public void test() throws IBuddyException { - IBuddy.sendAllOff(); - } -} diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/LocalImplementation.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/LocalImplementation.java new file mode 100644 index 0000000..87e5051 --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/LocalImplementation.java @@ -0,0 +1,112 @@ +package com.github.boukefalos.ibuddy.implementation; + +import org.jraf.jlibibuddy.IBuddy; +import org.jraf.jlibibuddy.IBuddyException; +import org.jraf.jlibibuddy.IBuddyUtils; + +import proto.Ibuddy.Color; +import proto.Ibuddy.Direction; + +import com.github.boukefalos.ibuddy.iBuddy; + +public class LocalImplementation implements iBuddy { + IBuddy IBuddy; + + @SuppressWarnings("static-access") + public LocalImplementation() { + IBuddy = IBuddy.getIBuddy(); + } + + public void setHeadRed(boolean on) throws IBuddyException { + IBuddy.sendHeadRed(on); + } + + public void setHeadBlue(boolean on) throws IBuddyException { + IBuddy.sendHeadBlue(on); + } + + public void setHeadGreen(boolean on) throws IBuddyException { + IBuddy.sendHeadGreen(on); + } + + public void setHeart(boolean on) throws IBuddyException { + IBuddy.sendHeart(on); + } + + public void setHead(Color color) throws IBuddyException { + IBuddy.sendHeadColor(mapColor(color)); + } + + public void setWingsUp() throws IBuddyException { + IBuddy.sendWings(false, true); + } + + public void setWingsDown() throws IBuddyException { + IBuddy.sendWings(true, false); + } + + public void setWingsCenter() throws IBuddyException { + IBuddy.sendWings(false, false); + } + + public void setWings(Direction direction) throws IBuddyException { + switch (direction) { + case UP: + setWingsUp(); + break; + case DOWN: + setWingsDown(); + break; + default: + setWingsCenter(); + break; + } + } + + public void setRotateLeft() throws IBuddyException { + IBuddy.sendRotate(false, true); + } + + + public void setRotateRight() throws IBuddyException { + IBuddy.sendRotate(true, false); + } + + public void setRotateCenter() throws IBuddyException { + IBuddy.sendRotate(false, false); + } + + public void setRotate(Direction direction) throws IBuddyException { + switch (direction) { + case LEFT: + setRotateLeft(); + break; + case RIGHT: + setRotateRight(); + default: + setRotateCenter(); + } + } + + public void off() throws IBuddyException { + IBuddy.sendAllOff(); + } + + public void blink(Color color, long onTime, long offTime, int times) throws IBuddyException { + IBuddyUtils.blink(IBuddy, mapColor(color), onTime, offTime, times); + } + + public void nudge(long delay, int times) throws IBuddyException { + IBuddyUtils.nudge(IBuddy, delay, times); + } + + public void flap(long delay, int times) throws IBuddyException { + IBuddyUtils.flap(IBuddy, delay, times); + } + + protected org.jraf.jlibibuddy.IBuddy.Color mapColor(Color color) { + return color.equals(Color.NONE) + ? org.jraf.jlibibuddy.IBuddy.Color.OFF + : org.jraf.jlibibuddy.IBuddy.Color.valueOf(color.name()); + } +} diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java deleted file mode 100644 index 6e58bce..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.github.boukefalos.ibuddy.implementation; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.SocketException; -import java.net.UnknownHostException; - -import org.jraf.jlibibuddy.IBuddyException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - - - - - -import com.github.boukefalos.ibuddy.iBuddy; -import com.github.boukefalos.ibuddy.exception.iBuddyException; - -import ibuddy.Ibuddy.Color; -import ibuddy.Ibuddy.Command; -import ibuddy.Ibuddy.Command.Type; -import ibuddy.Ibuddy.SetLed; - -public class Remote implements iBuddy { - protected Logger logger = LoggerFactory.getLogger(getClass()); - - protected DatagramSocket udpSocket; - protected InetAddress inetAddress; - protected int port; - - public Remote(String host, int port) throws UnknownHostException{ - inetAddress = InetAddress.getByName(host); - logger.debug(host); - logger.debug(String.valueOf(port)); - this.port = port; - } - - public void setHeadRed(boolean headRed) { - Command command = Command.newBuilder() - .setType(Type.SET_LED) - .setSetLed( - SetLed.newBuilder() - .setColor(Color.RED) - .setPos(0).build()).build(); - - ByteArrayOutputStream output = new ByteArrayOutputStream(1024); - try { - command.writeDelimitedTo(output); - send(output.toByteArray()); - } catch (IOException e) { - logger.error("Failed to send command"); - } - } - - public void setHeadGreen(boolean headGreen) { - System.out.println("oki"); - send("GREEN"); - } - - public void setHeadBlue(boolean headBlue) { - send("BLUE"); - } - - protected void send(String request) { - send(request.getBytes()); - } - - protected void send(byte[] buffer) { - try { - setup(); - DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length, inetAddress, port); - udpSocket.send(datagramPacket); - } catch (IOException e) { - logger.error("Failed to send buffer", e); - } - } - - protected boolean setup() { - if (udpSocket == null) { - try { - udpSocket = new DatagramSocket(); - } catch (SocketException e) { - logger.error("Failed to create socket", e); - return false; - } - Runtime.getRuntime().addShutdownHook(new Thread() { - public void run() { - udpSocket.close(); - } - }); - } - return true; - } - - - @Override - public void test() throws IBuddyException { - // TODO Auto-generated method stub - - } -} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java new file mode 100644 index 0000000..403301d --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/TcpImplementation.java @@ -0,0 +1,84 @@ +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 new file mode 100644 index 0000000..3e521cc --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/UdpImplementation.java @@ -0,0 +1,86 @@ +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/Server.java b/src/main/java/com/github/boukefalos/ibuddy/server/Server.java deleted file mode 100644 index f6fe8f9..0000000 --- a/src/main/java/com/github/boukefalos/ibuddy/server/Server.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.github.boukefalos.ibuddy.server; - -import ibuddy.Ibuddy.Command; -import ibuddy.Ibuddy.Command.Type; -import ibuddy.Ibuddy.SetLed; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.SocketException; - -import org.jraf.jlibibuddy.IBuddy; -import org.jraf.jlibibuddy.IBuddy.Color; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.github.boukefalos.ibuddy.iBuddy; -import com.github.boukefalos.ibuddy.exception.ServerException; -import com.github.boukefalos.ibuddy.exception.iBuddyException; - -public class Server extends Thread { - protected Logger logger = LoggerFactory.getLogger(getClass()); - protected iBuddy iBuddy; - protected DatagramSocket diagramSocket; - - public Server(iBuddy iBuddy, int port) throws ServerException { - logger.debug(String.valueOf(port)); - this.iBuddy = iBuddy; - try { - diagramSocket = new DatagramSocket(port); - return; - } catch (SocketException e) { - logger.error("Failed to initialize socket", e); - } - throw new ServerException(); - } - - @SuppressWarnings("incomplete-switch") - public void run() { - while (true) { - logger.debug("Wait for input"); - byte[] buffer = new byte[1024]; - DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length); - try { - diagramSocket.receive(datagramPacket); - } catch (IOException e) { - logger.error("Failed to receive packet", e); - } - ByteArrayInputStream input = new ByteArrayInputStream(buffer); - logger.debug("Received input"); - try { - Command command = Command.parseDelimitedFrom(input); - logger.debug("Command type = " + command.getType().name()); - switch (command.getType()) { - case SET_LED: - SetLed setLed = command.getSetLed(); - logger.debug("Color = " + setLed.getColor().name()); - switch (setLed.getColor()) { - case RED: - iBuddy.setHeadRed(true); - } - break; - } - } catch (IOException e) { - logger.error("Failed to parse input"); - return; - } catch (iBuddyException e) { - logger.error("Failed to send command to iBuddy", e); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java new file mode 100644 index 0000000..9cb91d3 --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyServer.java @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..fd41bf6 --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyTcpServer.java @@ -0,0 +1,19 @@ +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 new file mode 100644 index 0000000..00fb15e --- /dev/null +++ b/src/main/java/com/github/boukefalos/ibuddy/server/iBuddyUdpServer.java @@ -0,0 +1,24 @@ +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/proto/ibuddy.proto b/src/main/proto/ibuddy.proto index 6c23aa2..10da2a8 100644 --- a/src/main/proto/ibuddy.proto +++ b/src/main/proto/ibuddy.proto @@ -1,52 +1,82 @@ -package ibuddy; +package proto; -enum Color { - GREEN = 1; - RED = 2; - BOTH = 3; - NONE = 4; +enum Type { + STATE = 1; + HEAD = 2; + WINGS = 3; + ROTATE = 4; + HEART = 5; + BLINK = 6; + NUDGE = 7; + FLAP = 8; } -enum Module { - TM1638 = 1; - InvertedTM1638 = 2; - TM1640 = 3; +enum State { + ON = 1; + OFF = 2; +} + +enum Direction { + CENTER = 1; + DOWN = 2; + UP = 3; + LEFT = 4; + RIGHT = 5; +} + +enum Color { + NONE = 1; + RED = 2; + GREEN = 3; + BLUE = 4; + YELLOW = 5; + PURPLE = 6; + CYAN = 7; + WHITE = 8; +} + +message Head { + required State state = 1; + required bool single = 2; + required Color color = 3; +} + +message Wings { + required Direction direction = 1; +} + +message Rotate { + required Direction direction = 1; +} + +message Heart { + required State state = 1; +} + +message Blink { + required int32 onTime = 1; + required int32 offTime = 2; + required int32 times = 3; + optional Color color = 4; +} + +message Nudge { + required int32 delay = 1; + required int32 times = 2; +} + +message Flap { + required int32 delay = 1; + required int32 times = 2; } message Command { - enum Type { - PING = 1; - CONSTRUCT = 2; - SET_LED = 10; - } - required Type type = 1; - optional Ping ping = 2; - optional Construct construct = 3; - optional SetLed setLed = 10; -} - -message Ping { - required int32 id = 1; -} - -message Echo { - required int32 id = 1; - optional string message = 2; -} - -message Construct { - required int32 dataPin = 1; - required int32 clockPin = 2; - optional int32 strobePin = 3; - optional bool activateDisplay = 4 [default = true]; - optional int32 intensity = 5 [default = 7]; - optional Module module = 6 [default = TM1638]; - optional int32 id = 7 [default = 0]; -} - -message SetLed { - required Color color = 1; - required int32 pos = 2; - optional int32 id = 3 [default = 1]; + optional Head head = 2; + optional Wings wings = 3; + optional Rotate rotate = 4; + optional Heart heart = 5; + optional Blink blink = 6; + optional Nudge nudge = 7; + optional Flap flap = 8; } \ No newline at end of file diff --git a/src/main/java/test/TestOriginal.java b/src/test/java/test/TestOriginal.java similarity index 100% rename from src/main/java/test/TestOriginal.java rename to src/test/java/test/TestOriginal.java diff --git a/src/main/java/test/TestProperties.java b/src/test/java/test/TestProperties.java similarity index 99% rename from src/main/java/test/TestProperties.java rename to src/test/java/test/TestProperties.java index 928dbaf..6453b28 100644 --- a/src/main/java/test/TestProperties.java +++ b/src/test/java/test/TestProperties.java @@ -1,4 +1,5 @@ package test; + import com.github.boukefalos.ibuddy.Loader; public class TestProperties { diff --git a/src/main/java/test/TestCommunication.java b/src/test/java/test/TestTcpCommunication.java similarity index 74% rename from src/main/java/test/TestCommunication.java rename to src/test/java/test/TestTcpCommunication.java index 2c12304..8418f75 100644 --- a/src/main/java/test/TestCommunication.java +++ b/src/test/java/test/TestTcpCommunication.java @@ -2,20 +2,24 @@ 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.iBuddy; -import com.github.boukefalos.ibuddy.server.Server; -public class TestCommunication { +public class TestTcpCommunication { public static void main(String[] args) { try { Properties localProperties = new Properties(); localProperties.setProperty("implementation", "local"); localProperties.setProperty("server", "true"); localProperties.setProperty("server.port", "8883"); - + localProperties.setProperty("server.protocol", "tcp"); + Properties remoteProperties = new Properties(); remoteProperties.setProperty("implementation", "remote"); + remoteProperties.setProperty("protocol", "tcp"); remoteProperties.setProperty("remote.host", "localhost"); remoteProperties.setProperty("remote.port", "8883"); @@ -24,14 +28,13 @@ public class TestCommunication { iBuddy localiBuddy = localLoader.getiBuddy(); iBuddy remoteiBuddy = remoteLoader.getiBuddy(); - - localiBuddy.test(); - - Server server = localLoader.getServer(); - + //localiBuddy.setHead(Color.WHITE); + + Work server = localLoader.getServer(); server.start(); remoteiBuddy.setHeadRed(true); - Thread.sleep(10000); + Thread.sleep(1000); + //server.exit(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/test/TestUdpCommunication.java b/src/test/java/test/TestUdpCommunication.java new file mode 100644 index 0000000..9ccda50 --- /dev/null +++ b/src/test/java/test/TestUdpCommunication.java @@ -0,0 +1,43 @@ +package test; + +import java.util.Properties; + +import base.work.Work; + +import com.github.boukefalos.ibuddy.Loader; +import com.github.boukefalos.ibuddy.iBuddy; + +public class TestUdpCommunication { + public static void main(String[] args) { + try { + Properties localProperties = new Properties(); + localProperties.setProperty("implementation", "local"); + localProperties.setProperty("server", "true"); + localProperties.setProperty("server.port", "8883"); + localProperties.setProperty("server.protocol", "udp"); + + Properties remoteProperties = new Properties(); + remoteProperties.setProperty("implementation", "remote"); + remoteProperties.setProperty("protocol", "udp"); + remoteProperties.setProperty("remote.host", "255.255.255.255"); + remoteProperties.setProperty("remote.port", "8883"); + + 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(); + + server.start(); + remoteiBuddy.setHeadRed(true); + Thread.sleep(1000); + server.exit(); + } catch (Exception e) { + e.printStackTrace(); + } + } +}