diff --git a/src/main/java/com/github/boukefalos/lirc/Lirc.java b/src/main/java/com/github/boukefalos/lirc/Lirc.java index c6aff46..23ba9c2 100644 --- a/src/main/java/com/github/boukefalos/lirc/Lirc.java +++ b/src/main/java/com/github/boukefalos/lirc/Lirc.java @@ -3,10 +3,8 @@ package com.github.boukefalos.lirc; import base.work.Listen; public interface Lirc { - public void start(); - public void register(Listen listen); - public void add(Object object); - + public void start(); // Required for Client / ClientListen to forward from separate Thread public void input(Object object); + public void register(Listen listen); } diff --git a/src/main/java/com/github/boukefalos/lirc/LircClient.java b/src/main/java/com/github/boukefalos/lirc/LircClient.java index 4598c0e..19ca4ed 100644 --- a/src/main/java/com/github/boukefalos/lirc/LircClient.java +++ b/src/main/java/com/github/boukefalos/lirc/LircClient.java @@ -17,37 +17,24 @@ package com.github.boukefalos.lirc; import java.io.IOException; -import java.util.ArrayList; -import java.util.Scanner; -import base.receiver.Receiver; import base.server.channel.TcpClient; import base.work.Listen; -public class LircClient extends TcpClient implements Receiver { +public class LircClient extends TcpClient { public static final int BUFFER_SIZE = 1024; public static final String IP = "localhost"; public static final int PORT = 8765; - - protected ArrayList> listenList; protected String send; + protected Listen listen; - public LircClient() { - super(IP, PORT, BUFFER_SIZE); + public LircClient(Listen listen) { + super(IP, PORT, BUFFER_SIZE); //send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password"); - listenList = new ArrayList>(); - register(this); - } + this.listen = listen; + } - public void register(Listen listen) { - listenList.add(listen); - } - - public void remove(Listen listen) { - listenList.remove(listen); - } - - public void send(LircButton button) { + public void send(LircButton button) { send(button, 0); } @@ -63,26 +50,7 @@ public class LircClient extends TcpClient implements Receiver { } } - public void receive(byte[] buffer) { - receive(new String(buffer).trim()); - } - - protected void receive(String line) { - if (!line.startsWith("BEGIN")) { - Scanner scanner = new Scanner(line); - receive(scanner); - scanner.close(); - } - } - - protected void receive(Scanner scanner) { - scanner.next(); - scanner.next(); - String code = scanner.next(); - String remote = scanner.next(); - for (Listen lircbuttonListener : listenList) { - // Need to pass as String to assure consistent hash - lircbuttonListener.add(remote + " " + code); - } - } + public void input(byte[] buffer) { + listen.add(buffer); + } } diff --git a/src/main/java/com/github/boukefalos/lirc/Loader.java b/src/main/java/com/github/boukefalos/lirc/Loader.java index 79c5b3d..d9297be 100644 --- a/src/main/java/com/github/boukefalos/lirc/Loader.java +++ b/src/main/java/com/github/boukefalos/lirc/Loader.java @@ -1,15 +1,11 @@ package com.github.boukefalos.lirc; -import java.io.IOException; import java.util.Properties; -import org.picocontainer.DefaultPicoContainer; -import org.picocontainer.MutablePicoContainer; import org.picocontainer.Parameter; import org.picocontainer.parameters.ConstantParameter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import base.loader.AbstractLoader; import base.work.Work; import com.github.boukefalos.lirc.client.LircTcpClient; @@ -20,14 +16,11 @@ import com.github.boukefalos.lirc.server.LircServer; import com.github.boukefalos.lirc.server.LircTcpServer; import com.github.boukefalos.lirc.server.LircUdpServer; -public class Loader { - protected static final String PROPERTIES_FILE = "Lirc.properties"; - protected Logger logger = LoggerFactory.getLogger(Loader.class); - protected MutablePicoContainer pico; +public class Loader extends AbstractLoader { + protected static final String PROPERTIES_FILE = "lirc.properties"; public Loader(Properties properties) { - /* Initialise container */ - pico = new DefaultPicoContainer(); + super(); /* Add implementation */ switch (properties.getProperty("implementation")) { @@ -73,19 +66,6 @@ public class Loader { } } - public static Loader getLoader() throws IOException { - return getLoader(PROPERTIES_FILE); - } - - public static Loader getLoader(String propertiesFile) throws IOException { - /* Read properties file */ - Properties properties = new Properties(); - properties.load(Loader.class.getClassLoader().getResourceAsStream(propertiesFile)); - - /* Initialise loader */ - return new Loader(properties); - } - public Lirc getLirc() { return pico.getComponent(Lirc.class); } diff --git a/src/main/java/com/github/boukefalos/lirc/button/ColorButton.java b/src/main/java/com/github/boukefalos/lirc/button/ColorButton.java deleted file mode 100644 index 6ab131a..0000000 --- a/src/main/java/com/github/boukefalos/lirc/button/ColorButton.java +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Copyright (C) 2015 Rik Veenboer - * - * 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 . - */ -package com.github.boukefalos.lirc.button; - -public enum ColorButton { - RED, GREEN, YELLOW, BLUE; -} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/lirc/button/NumberButton.java b/src/main/java/com/github/boukefalos/lirc/button/NumberButton.java deleted file mode 100644 index 9389343..0000000 --- a/src/main/java/com/github/boukefalos/lirc/button/NumberButton.java +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Copyright (C) 2015 Rik Veenboer - * - * 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 . - */ -package com.github.boukefalos.lirc.button; - -public enum NumberButton { - ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE; -} diff --git a/src/main/java/com/github/boukefalos/lirc/button/RemoteButton.java b/src/main/java/com/github/boukefalos/lirc/button/RemoteButton.java deleted file mode 100644 index cbedb13..0000000 --- a/src/main/java/com/github/boukefalos/lirc/button/RemoteButton.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.boukefalos.lirc.button; - -public interface RemoteButton { - -} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/lirc/implementation/LocalImplementation.java b/src/main/java/com/github/boukefalos/lirc/implementation/LocalImplementation.java index 3e403ae..119fc70 100644 --- a/src/main/java/com/github/boukefalos/lirc/implementation/LocalImplementation.java +++ b/src/main/java/com/github/boukefalos/lirc/implementation/LocalImplementation.java @@ -17,6 +17,7 @@ package com.github.boukefalos.lirc.implementation; import java.util.ArrayList; +import java.util.Scanner; import lirc.Lirc.Color; import lirc.Lirc.Direction; @@ -35,12 +36,11 @@ import com.github.boukefalos.lirc.util.SignalObject; public class LocalImplementation extends Listen implements Lirc { protected ArrayList> listenList; protected Multiplexer multiplexer; - protected LircClient lircService; + protected LircClient lircClient; public LocalImplementation() { listenList = new ArrayList>(); - lircService = new LircClient(); - lircService.register(this); + lircClient = new LircClient(this); multiplexer = new Multiplexer(); multiplexer.register(this); } @@ -54,27 +54,35 @@ public class LocalImplementation extends Listen implements Lirc { } public void activate() throws ActivateException { - logger.debug("Activate " + getClass().getSimpleName()); - lircService.start(); + lircClient.start(); super.activate(); } public void deactivate() throws DeactivateException { - logger.debug("Deactivate LircDevice"); super.deactivate(); - lircService.stop(); + lircClient.stop(); multiplexer.stop(); } public void exit() { - logger.debug("Exit LircDevice"); super.exit(); - lircService.exit(); + lircClient.exit(); multiplexer.exit(); } - public void input(String input) { - multiplexer.add(input); + public void input(byte[] buffer) { + String line = new String(buffer).trim(); + if (!line.startsWith("BEGIN")) { + Scanner scanner = new Scanner(line); + scanner.next(); + scanner.next(); + String code = scanner.next(); + String remote = scanner.next(); + + // Need to pass as String to assure consistent hash + multiplexer.add(remote + " " + code); + scanner.close(); + } } public void input(SignalObject signalObject) { diff --git a/src/main/java/com/github/boukefalos/lirc/implementation/TcpImplementation.java b/src/main/java/com/github/boukefalos/lirc/implementation/TcpImplementation.java index 6835fdf..a5a89b7 100644 --- a/src/main/java/com/github/boukefalos/lirc/implementation/TcpImplementation.java +++ b/src/main/java/com/github/boukefalos/lirc/implementation/TcpImplementation.java @@ -3,7 +3,6 @@ package com.github.boukefalos.lirc.implementation; import java.util.ArrayList; import base.exception.worker.ActivateException; -import base.receiver.Receiver; import base.server.socket.TcpClient; // Change to channel? import base.work.Listen; @@ -12,15 +11,16 @@ import com.github.boukefalos.lirc.listen.ClientListen; import com.github.boukefalos.server.helper.ServerHelper; // Fix dual Receiver and Sender roles -public class TcpImplementation extends TcpClient implements Lirc, Receiver { - protected ArrayList> listenList; +public class TcpImplementation extends Listen implements Lirc { + protected TcpClient tcpClient; protected ClientListen listen; + protected ArrayList> listenList; public TcpImplementation(String host, int port) { - super(host, port); + tcpClient = new TcpClient(host, port); + tcpClient.register(this); listenList = new ArrayList>(); listen = new ClientListen(this); - register(this); } public void activate() throws ActivateException { @@ -28,24 +28,16 @@ public class TcpImplementation extends TcpClient implements Lirc, Receiver { super.activate(); } - public void receive(byte[] buffer) { - // Decode byte array - ServerHelper.receive(this, buffer); + public void input(byte[] buffer) { + Object object = ServerHelper.decode(this, buffer); + if (object != null) { + for (Listen listen : listenList) { + listen.add(object); + } + } } public void register(Listen listen) { listenList.add(listen); } - - public void add(Object object) { - // Receive decoded object from ServerHelper - listen.add(object); - } - - public void input(Object object) { - // Forward to all listens - for (Listen listen : listenList) { - listen.add(object); - } - } } diff --git a/src/main/java/com/github/boukefalos/lirc/implementation/UdpImplementation.java b/src/main/java/com/github/boukefalos/lirc/implementation/UdpImplementation.java index 28848f6..ada6be7 100644 --- a/src/main/java/com/github/boukefalos/lirc/implementation/UdpImplementation.java +++ b/src/main/java/com/github/boukefalos/lirc/implementation/UdpImplementation.java @@ -18,23 +18,17 @@ public class UdpImplementation extends UdpSender implements Lirc { } - @Override - public void register(Listen listen) { - // TODO Auto-generated method stub - - } - - @Override - public void add(Object object) { - // TODO Auto-generated method stub - - } - @Override public void input(Object object) { // TODO Auto-generated method stub } + + @Override + public void register(Listen listen) { + // TODO Auto-generated method stub + + } // add way to receive udp packets } diff --git a/src/main/java/com/github/boukefalos/lirc/server/LircTcpServer.java b/src/main/java/com/github/boukefalos/lirc/server/LircTcpServer.java index ebcebf4..953453a 100644 --- a/src/main/java/com/github/boukefalos/lirc/server/LircTcpServer.java +++ b/src/main/java/com/github/boukefalos/lirc/server/LircTcpServer.java @@ -25,6 +25,6 @@ public class LircTcpServer extends TcpServer implements LircServer { } public void receive(byte[] buffer) { - ServerHelper.receive(lirc, buffer); + ServerHelper.decode(lirc, buffer); } } diff --git a/src/main/java/com/github/boukefalos/lirc/server/LircUdpServer.java b/src/main/java/com/github/boukefalos/lirc/server/LircUdpServer.java index 1da91d7..5782afc 100644 --- a/src/main/java/com/github/boukefalos/lirc/server/LircUdpServer.java +++ b/src/main/java/com/github/boukefalos/lirc/server/LircUdpServer.java @@ -19,6 +19,6 @@ public class LircUdpServer extends UdpServer implements LircServer { } protected void receive(byte[] buffer) { - ServerHelper.receive(lirc, buffer); + ServerHelper.decode(lirc, buffer); } } \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/server/helper/ServerHelper.java b/src/main/java/com/github/boukefalos/server/helper/ServerHelper.java index 1256832..160273a 100644 --- a/src/main/java/com/github/boukefalos/server/helper/ServerHelper.java +++ b/src/main/java/com/github/boukefalos/server/helper/ServerHelper.java @@ -20,34 +20,31 @@ import com.github.boukefalos.lirc.util.SignalObject; public class ServerHelper { protected static Logger logger = LoggerFactory.getLogger(ServerHelper.class); - public static void receive(Lirc lirc, byte[] buffer) { + public static SignalObject decode(Lirc lirc, byte[] buffer) { ByteArrayInputStream input = new ByteArrayInputStream(buffer); try { Button button = Button.parseDelimitedFrom(input); Type type = button.getType(); Signal signal = button.getSignal(); - // Use switch-case statements - if (button.hasColorButton()) { - Color color = button.getColorButton().getColor(); - lirc.add(new SignalObject(signal, color)); - } - if (button.hasDirectionButton()) { - Direction direction = button.getDirectionButton().getDirection(); - lirc.add(new SignalObject(signal, direction)); - } - if (button.hasNumberButton()) { - Number number = button.getNumberButton().getNumber(); - lirc.add(new SignalObject(signal, number)); - } - if (button.hasLircButton()) { - String remote = button.getLircButton().getRemote(); - String code = button.getLircButton().getCode(); - LircButton lircButton = new LircButton(remote, code); - lirc.add(new SignalObject(signal, lircButton)); + switch (type) { + case COLOR: + Color color = button.getColorButton().getColor(); + return new SignalObject(signal, color); + case DIRECTION: + Direction direction = button.getDirectionButton().getDirection(); + return new SignalObject(signal, direction); + case NUMBER: + Number number = button.getNumberButton().getNumber(); + return new SignalObject(signal, number); + case LIRC: + String remote = button.getLircButton().getRemote(); + String code = button.getLircButton().getCode(); + LircButton lircButton = new LircButton(remote, code); + return new SignalObject(signal, lircButton); } } catch (IOException e) { logger.error("Failed to parse input"); - return; } + return null; } } diff --git a/src/test/java/test/Test.java b/src/test/java/test/TestLocal.java similarity index 56% rename from src/test/java/test/Test.java rename to src/test/java/test/TestLocal.java index 64600ec..1cd28a1 100644 --- a/src/test/java/test/Test.java +++ b/src/test/java/test/TestLocal.java @@ -4,22 +4,23 @@ import lirc.Lirc.Signal; import base.exception.worker.ActivateException; import base.work.Listen; +import com.github.boukefalos.lirc.Lirc; import com.github.boukefalos.lirc.LircButton; import com.github.boukefalos.lirc.implementation.LocalImplementation; import com.github.boukefalos.lirc.util.SignalObject; -public class Test extends Listen { +public class TestLocal extends Listen { public static void main(String[] args) { - new Test().start(); + new TestLocal().start(); try { Thread.sleep(1000000); } catch (InterruptedException e) {} } - protected LocalImplementation lirc; + protected Lirc lirc; - public Test() { + public TestLocal() { lirc = new LocalImplementation(); lirc.register(this); } @@ -34,10 +35,13 @@ public class Test extends Listen { super.start(); } - public void input(SignalObject lircButtonSignal) { - Signal signal = lircButtonSignal.signal; - LircButton lircButton = lircButtonSignal.object; - String code = lircButton.code; - logger.error(signal.name() + " : " + code + " @ " + lircButton.remote); + public void input(SignalObject lircButtonSignal) { + Object object = lircButtonSignal.object; + if (object instanceof LircButton) { + Signal signal = lircButtonSignal.signal; + LircButton lircButton = lircButtonSignal.object; + String code = lircButton.code; + logger.error(signal.name() + " : " + code + " @ " + lircButton.remote); + } } }