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;
public interface Lirc {
public void start();
public void register(Listen<Object> 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<Object> listen);
}

View File

@@ -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<Listen<Object>> listenList;
protected String send;
protected Listen<Object> listen;
public LircClient() {
super(IP, PORT, BUFFER_SIZE);
public LircClient(Listen<Object> listen) {
super(IP, PORT, BUFFER_SIZE);
//send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password");
listenList = new ArrayList<Listen<Object>>();
register(this);
}
this.listen = listen;
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void remove(Listen<String[]> 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<Object> lircbuttonListener : listenList) {
// Need to pass as String to assure consistent hash
lircbuttonListener.add(remote + " " + code);
}
}
public void input(byte[] buffer) {
listen.add(buffer);
}
}

View File

@@ -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);
}

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;
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<Object> implements Lirc {
protected ArrayList<Listen<Object>> listenList;
protected Multiplexer<String> multiplexer;
protected LircClient lircService;
protected LircClient lircClient;
public LocalImplementation() {
listenList = new ArrayList<Listen<Object>>();
lircService = new LircClient();
lircService.register(this);
lircClient = new LircClient(this);
multiplexer = new Multiplexer<String>();
multiplexer.register(this);
}
@@ -54,27 +54,35 @@ public class LocalImplementation extends Listen<Object> 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<Object> signalObject) {

View File

@@ -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<Listen<Object>> listenList;
public class TcpImplementation extends Listen<byte[]> implements Lirc {
protected TcpClient tcpClient;
protected ClientListen listen;
protected ArrayList<Listen<Object>> listenList;
public TcpImplementation(String host, int port) {
super(host, port);
tcpClient = new TcpClient(host, port);
tcpClient.register(this);
listenList = new ArrayList<Listen<Object>>();
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<Object> listen : listenList) {
listen.add(object);
}
}
}
public void register(Listen<Object> 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
public void input(Object object) {
// TODO Auto-generated method stub
}
@Override
public void register(Listen<Object> listen) {
// TODO Auto-generated method stub
}
// add way to receive udp packets
}

View File

@@ -25,6 +25,6 @@ public class LircTcpServer extends TcpServer implements LircServer {
}
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) {
ServerHelper.receive(lirc, buffer);
ServerHelper.decode(lirc, buffer);
}
}

View File

@@ -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<Object>(signal, color));
}
if (button.hasDirectionButton()) {
Direction direction = button.getDirectionButton().getDirection();
lirc.add(new SignalObject<Direction>(signal, direction));
}
if (button.hasNumberButton()) {
Number number = button.getNumberButton().getNumber();
lirc.add(new SignalObject<Number>(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<LircButton>(signal, lircButton));
switch (type) {
case COLOR:
Color color = button.getColorButton().getColor();
return new SignalObject<Color>(signal, color);
case DIRECTION:
Direction direction = button.getDirectionButton().getDirection();
return new SignalObject<Direction>(signal, direction);
case NUMBER:
Number number = button.getNumberButton().getNumber();
return new SignalObject<Number>(signal, number);
case LIRC:
String remote = button.getLircButton().getRemote();
String code = button.getLircButton().getCode();
LircButton lircButton = new LircButton(remote, code);
return new SignalObject<LircButton>(signal, lircButton);
}
} catch (IOException e) {
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.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<Object> {
public class TestLocal extends Listen<Object> {
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<Object> {
super.start();
}
public void input(SignalObject<LircButton> lircButtonSignal) {
Signal signal = lircButtonSignal.signal;
LircButton lircButton = lircButtonSignal.object;
String code = lircButton.code;
logger.error(signal.name() + " : " + code + " @ " + lircButton.remote);
public void input(SignalObject<LircButton> 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);
}
}
}