Snapshot using preliminary client/server model

This commit is contained in:
2015-06-21 14:54:01 +01:00
parent 75a5bf262e
commit 931c219ebe
13 changed files with 85 additions and 191 deletions

View File

@@ -3,10 +3,8 @@ package com.github.boukefalos.lirc;
import base.work.Listen; import base.work.Listen;
public interface Lirc { public interface Lirc {
public void start(); public void start();
public void register(Listen<Object> listen);
public void add(Object object);
// Required for Client / ClientListen to forward from separate Thread // Required for Client / ClientListen to forward from separate Thread
public void input(Object object); public void input(Object object);
public void register(Listen<Object> listen);
} }

View File

@@ -17,37 +17,24 @@
package com.github.boukefalos.lirc; package com.github.boukefalos.lirc;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import base.receiver.Receiver;
import base.server.channel.TcpClient; import base.server.channel.TcpClient;
import base.work.Listen; 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 int BUFFER_SIZE = 1024;
public static final String IP = "localhost"; public static final String IP = "localhost";
public static final int PORT = 8765; public static final int PORT = 8765;
protected ArrayList<Listen<Object>> listenList;
protected String send; protected String send;
protected Listen<Object> listen;
public LircClient() { public LircClient(Listen<Object> listen) {
super(IP, PORT, BUFFER_SIZE); super(IP, PORT, BUFFER_SIZE);
//send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password"); //send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password");
listenList = new ArrayList<Listen<Object>>(); this.listen = listen;
register(this); }
}
public void register(Listen<Object> listen) { public void send(LircButton button) {
listenList.add(listen);
}
public void remove(Listen<String[]> listen) {
listenList.remove(listen);
}
public void send(LircButton button) {
send(button, 0); send(button, 0);
} }
@@ -63,26 +50,7 @@ public class LircClient extends TcpClient implements Receiver {
} }
} }
public void receive(byte[] buffer) { public void input(byte[] buffer) {
receive(new String(buffer).trim()); listen.add(buffer);
} }
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<Object> lircbuttonListener : listenList) {
// Need to pass as String to assure consistent hash
lircbuttonListener.add(remote + " " + code);
}
}
} }

View File

@@ -1,15 +1,11 @@
package com.github.boukefalos.lirc; package com.github.boukefalos.lirc;
import java.io.IOException;
import java.util.Properties; import java.util.Properties;
import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter; import org.picocontainer.Parameter;
import org.picocontainer.parameters.ConstantParameter; import org.picocontainer.parameters.ConstantParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import base.loader.AbstractLoader;
import base.work.Work; import base.work.Work;
import com.github.boukefalos.lirc.client.LircTcpClient; 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.LircTcpServer;
import com.github.boukefalos.lirc.server.LircUdpServer; import com.github.boukefalos.lirc.server.LircUdpServer;
public class Loader { public class Loader extends AbstractLoader {
protected static final String PROPERTIES_FILE = "Lirc.properties"; protected static final String PROPERTIES_FILE = "lirc.properties";
protected Logger logger = LoggerFactory.getLogger(Loader.class);
protected MutablePicoContainer pico;
public Loader(Properties properties) { public Loader(Properties properties) {
/* Initialise container */ super();
pico = new DefaultPicoContainer();
/* Add implementation */ /* Add implementation */
switch (properties.getProperty("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() { public Lirc getLirc() {
return pico.getComponent(Lirc.class); return pico.getComponent(Lirc.class);
} }

View File

@@ -1,21 +0,0 @@
/**
* Copyright (C) 2015 Rik Veenboer <rik.veenboer@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.boukefalos.lirc.button;
public enum ColorButton {
RED, GREEN, YELLOW, BLUE;
}

View File

@@ -1,21 +0,0 @@
/**
* Copyright (C) 2015 Rik Veenboer <rik.veenboer@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.boukefalos.lirc.button;
public enum NumberButton {
ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE;
}

View File

@@ -1,5 +0,0 @@
package com.github.boukefalos.lirc.button;
public interface RemoteButton {
}

View File

@@ -17,6 +17,7 @@
package com.github.boukefalos.lirc.implementation; package com.github.boukefalos.lirc.implementation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner;
import lirc.Lirc.Color; import lirc.Lirc.Color;
import lirc.Lirc.Direction; import lirc.Lirc.Direction;
@@ -35,12 +36,11 @@ import com.github.boukefalos.lirc.util.SignalObject;
public class LocalImplementation extends Listen<Object> implements Lirc { public class LocalImplementation extends Listen<Object> implements Lirc {
protected ArrayList<Listen<Object>> listenList; protected ArrayList<Listen<Object>> listenList;
protected Multiplexer<String> multiplexer; protected Multiplexer<String> multiplexer;
protected LircClient lircService; protected LircClient lircClient;
public LocalImplementation() { public LocalImplementation() {
listenList = new ArrayList<Listen<Object>>(); listenList = new ArrayList<Listen<Object>>();
lircService = new LircClient(); lircClient = new LircClient(this);
lircService.register(this);
multiplexer = new Multiplexer<String>(); multiplexer = new Multiplexer<String>();
multiplexer.register(this); multiplexer.register(this);
} }
@@ -54,27 +54,35 @@ public class LocalImplementation extends Listen<Object> implements Lirc {
} }
public void activate() throws ActivateException { public void activate() throws ActivateException {
logger.debug("Activate " + getClass().getSimpleName()); lircClient.start();
lircService.start();
super.activate(); super.activate();
} }
public void deactivate() throws DeactivateException { public void deactivate() throws DeactivateException {
logger.debug("Deactivate LircDevice");
super.deactivate(); super.deactivate();
lircService.stop(); lircClient.stop();
multiplexer.stop(); multiplexer.stop();
} }
public void exit() { public void exit() {
logger.debug("Exit LircDevice");
super.exit(); super.exit();
lircService.exit(); lircClient.exit();
multiplexer.exit(); multiplexer.exit();
} }
public void input(String input) { public void input(byte[] buffer) {
multiplexer.add(input); 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<Object> signalObject) { public void input(SignalObject<Object> signalObject) {

View File

@@ -3,7 +3,6 @@ package com.github.boukefalos.lirc.implementation;
import java.util.ArrayList; import java.util.ArrayList;
import base.exception.worker.ActivateException; import base.exception.worker.ActivateException;
import base.receiver.Receiver;
import base.server.socket.TcpClient; // Change to channel? import base.server.socket.TcpClient; // Change to channel?
import base.work.Listen; import base.work.Listen;
@@ -12,15 +11,16 @@ import com.github.boukefalos.lirc.listen.ClientListen;
import com.github.boukefalos.server.helper.ServerHelper; import com.github.boukefalos.server.helper.ServerHelper;
// Fix dual Receiver and Sender roles // Fix dual Receiver and Sender roles
public class TcpImplementation extends TcpClient implements Lirc, Receiver { public class TcpImplementation extends Listen<byte[]> implements Lirc {
protected ArrayList<Listen<Object>> listenList; protected TcpClient tcpClient;
protected ClientListen listen; protected ClientListen listen;
protected ArrayList<Listen<Object>> listenList;
public TcpImplementation(String host, int port) { public TcpImplementation(String host, int port) {
super(host, port); tcpClient = new TcpClient(host, port);
tcpClient.register(this);
listenList = new ArrayList<Listen<Object>>(); listenList = new ArrayList<Listen<Object>>();
listen = new ClientListen(this); listen = new ClientListen(this);
register(this);
} }
public void activate() throws ActivateException { public void activate() throws ActivateException {
@@ -28,24 +28,16 @@ public class TcpImplementation extends TcpClient implements Lirc, Receiver {
super.activate(); super.activate();
} }
public void receive(byte[] buffer) { public void input(byte[] buffer) {
// Decode byte array Object object = ServerHelper.decode(this, buffer);
ServerHelper.receive(this, buffer); if (object != null) {
for (Listen<Object> listen : listenList) {
listen.add(object);
}
}
} }
public void register(Listen<Object> listen) { public void register(Listen<Object> listen) {
listenList.add(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<Object> listen : listenList) {
listen.add(object);
}
}
} }

View File

@@ -18,23 +18,17 @@ public class UdpImplementation extends UdpSender implements Lirc {
} }
@Override
public void register(Listen<Object> listen) {
// TODO Auto-generated method stub
}
@Override
public void add(Object object) {
// TODO Auto-generated method stub
}
@Override @Override
public void input(Object object) { public void input(Object object) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override
public void register(Listen<Object> listen) {
// TODO Auto-generated method stub
}
// add way to receive udp packets // add way to receive udp packets
} }

View File

@@ -25,6 +25,6 @@ public class LircTcpServer extends TcpServer implements LircServer {
} }
public void receive(byte[] buffer) { public void receive(byte[] buffer) {
ServerHelper.receive(lirc, buffer); ServerHelper.decode(lirc, buffer);
} }
} }

View File

@@ -19,6 +19,6 @@ public class LircUdpServer extends UdpServer implements LircServer {
} }
protected void receive(byte[] buffer) { protected void receive(byte[] buffer) {
ServerHelper.receive(lirc, buffer); ServerHelper.decode(lirc, buffer);
} }
} }

View File

@@ -20,34 +20,31 @@ import com.github.boukefalos.lirc.util.SignalObject;
public class ServerHelper { public class ServerHelper {
protected static Logger logger = LoggerFactory.getLogger(ServerHelper.class); 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); ByteArrayInputStream input = new ByteArrayInputStream(buffer);
try { try {
Button button = Button.parseDelimitedFrom(input); Button button = Button.parseDelimitedFrom(input);
Type type = button.getType(); Type type = button.getType();
Signal signal = button.getSignal(); Signal signal = button.getSignal();
// Use switch-case statements switch (type) {
if (button.hasColorButton()) { case COLOR:
Color color = button.getColorButton().getColor(); Color color = button.getColorButton().getColor();
lirc.add(new SignalObject<Object>(signal, color)); return new SignalObject<Color>(signal, color);
} case DIRECTION:
if (button.hasDirectionButton()) { Direction direction = button.getDirectionButton().getDirection();
Direction direction = button.getDirectionButton().getDirection(); return new SignalObject<Direction>(signal, direction);
lirc.add(new SignalObject<Direction>(signal, direction)); case NUMBER:
} Number number = button.getNumberButton().getNumber();
if (button.hasNumberButton()) { return new SignalObject<Number>(signal, number);
Number number = button.getNumberButton().getNumber(); case LIRC:
lirc.add(new SignalObject<Number>(signal, number)); String remote = button.getLircButton().getRemote();
} String code = button.getLircButton().getCode();
if (button.hasLircButton()) { LircButton lircButton = new LircButton(remote, code);
String remote = button.getLircButton().getRemote(); return new SignalObject<LircButton>(signal, lircButton);
String code = button.getLircButton().getCode();
LircButton lircButton = new LircButton(remote, code);
lirc.add(new SignalObject<LircButton>(signal, lircButton));
} }
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to parse input"); logger.error("Failed to parse input");
return;
} }
return null;
} }
} }

View File

@@ -4,22 +4,23 @@ import lirc.Lirc.Signal;
import base.exception.worker.ActivateException; import base.exception.worker.ActivateException;
import base.work.Listen; import base.work.Listen;
import com.github.boukefalos.lirc.Lirc;
import com.github.boukefalos.lirc.LircButton; import com.github.boukefalos.lirc.LircButton;
import com.github.boukefalos.lirc.implementation.LocalImplementation; import com.github.boukefalos.lirc.implementation.LocalImplementation;
import com.github.boukefalos.lirc.util.SignalObject; import com.github.boukefalos.lirc.util.SignalObject;
public class Test extends Listen<Object> { public class TestLocal extends Listen<Object> {
public static void main(String[] args) { public static void main(String[] args) {
new Test().start(); new TestLocal().start();
try { try {
Thread.sleep(1000000); Thread.sleep(1000000);
} catch (InterruptedException e) {} } catch (InterruptedException e) {}
} }
protected LocalImplementation lirc; protected Lirc lirc;
public Test() { public TestLocal() {
lirc = new LocalImplementation(); lirc = new LocalImplementation();
lirc.register(this); lirc.register(this);
} }
@@ -34,10 +35,13 @@ public class Test extends Listen<Object> {
super.start(); super.start();
} }
public void input(SignalObject<LircButton> lircButtonSignal) { public void input(SignalObject<LircButton> lircButtonSignal) {
Signal signal = lircButtonSignal.signal; Object object = lircButtonSignal.object;
LircButton lircButton = lircButtonSignal.object; if (object instanceof LircButton) {
String code = lircButton.code; Signal signal = lircButtonSignal.signal;
logger.error(signal.name() + " : " + code + " @ " + lircButton.remote); LircButton lircButton = lircButtonSignal.object;
String code = lircButton.code;
logger.error(signal.name() + " : " + code + " @ " + lircButton.remote);
}
} }
} }