Replace tabs with spaces

This commit is contained in:
2015-09-01 13:19:18 +01:00
parent b422958f5d
commit b6bfa4e7c1
13 changed files with 316 additions and 316 deletions

View File

@@ -4,7 +4,7 @@ import base.Control;
import base.work.Listen;
public interface Lirc extends Control {
public void register(Listen<Object> listen);
public void remove(Listen<Object> listen);
//public void send(String remote, String code);
public void register(Listen<Object> listen);
public void remove(Listen<Object> listen);
//public void send(String remote, String code);
}

View File

@@ -20,12 +20,12 @@ public class LircButton {
public String remote;
public String code;
public LircButton(String remote, String code) {
this.remote = remote;
this.code = code;
}
public LircButton(String remote, String code) {
this.remote = remote;
this.code = code;
}
public lirc.Lirc.LircButton getProto() {
return lirc.Lirc.LircButton.newBuilder().setRemote(remote).setCode(code).build();
}
public lirc.Lirc.LircButton getProto() {
return lirc.Lirc.LircButton.newBuilder().setRemote(remote).setCode(code).build();
}
}

View File

@@ -22,19 +22,19 @@ import base.server.channel.TcpClient;
import base.work.Listen;
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 int PORT = 8765;
protected String send;
protected Listen<Object> listen;
protected Listen<Object> listen;
public LircClient(Listen<Object> listen) {
super(IP, PORT, BUFFER_SIZE);
super(IP, PORT, BUFFER_SIZE);
//send = Native.getValue(Registry.CURRENT_USER, "Software\\LIRC", "password");
this.listen = listen;
}
this.listen = listen;
}
public void send(LircButton button) {
public void send(LircButton button) {
send(button, 0);
}
@@ -44,13 +44,13 @@ public class LircClient extends TcpClient {
}
String command = String.format("%s %s %s \n", send, lircButton.remote, lircButton.code);
try {
send(command.getBytes());
} catch (IOException e) {
logger.error("", e);
}
send(command.getBytes());
} catch (IOException e) {
logger.error("", e);
}
}
public void input(byte[] buffer) {
listen.add(buffer);
listen.add(buffer);
}
}

View File

@@ -11,50 +11,50 @@ import com.github.boukefalos.lirc.implementation.Remote;
public class Loader extends AbstractLoader<Loader> {
protected static final String PROPERTIES_FILE = "lirc.properties";
public Loader(Properties properties) throws LoaderException {
super();
public Loader(Properties properties) throws LoaderException {
super();
/* Add implementation */
switch (properties.getProperty("implementation")) {
case "local":
pico.addComponent(Local.class);
break;
case "remote":
pico.addComponent(Remote.class);
/* Add implementation */
switch (properties.getProperty("implementation")) {
case "local":
pico.addComponent(Local.class);
break;
case "remote":
pico.addComponent(Remote.class);
/* Add remote forwarder implementation */
try {
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
int port = Integer.valueOf(properties.getProperty("remote.port"));
addServerForwarder(protocol, implementation, port);
} catch (NumberFormatException e) {
throw new LoaderException("Failed to parse remote.port");
}
break;
}
/* Add remote forwarder implementation */
try {
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
int port = Integer.valueOf(properties.getProperty("remote.port"));
addServerForwarder(protocol, implementation, port);
} catch (NumberFormatException e) {
throw new LoaderException("Failed to parse remote.port");
}
break;
}
/* Add server */
if (properties.getProperty("server") != null) {
pico.addComponent(Server.class);
/* Add server */
if (properties.getProperty("server") != null) {
pico.addComponent(Server.class);
/* Add sender implementation */
try {
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
int port = Integer.valueOf(properties.getProperty("server.port"));
addServerSender(protocol, implementation, port);
} catch (NumberFormatException e) {
throw new LoaderException("Failed to parse server.port");
}
}
}
/* Add sender implementation */
try {
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
int port = Integer.valueOf(properties.getProperty("server.port"));
addServerSender(protocol, implementation, port);
} catch (NumberFormatException e) {
throw new LoaderException("Failed to parse server.port");
}
}
}
public Lirc getLirc() {
return pico.getComponent(Lirc.class);
public Lirc getLirc() {
return pico.getComponent(Lirc.class);
}
public Server getServer() {
return pico.getComponent(Server.class);
return pico.getComponent(Server.class);
}
}

View File

@@ -22,59 +22,59 @@ import base.work.Listen;
import com.github.boukefalos.lirc.util.SignalObject;
public class Server extends Listen<Object> implements Control {
protected Lirc lirc;
protected Sender sender;
protected Lirc lirc;
protected Sender sender;
public Server(Lirc lirc, Sender sender) {
this.lirc = lirc;
this.sender = sender;
lirc.register(this);
}
public Server(Lirc lirc, Sender sender) {
this.lirc = lirc;
this.sender = sender;
lirc.register(this);
}
public void activate() throws ActivateException {
lirc.start();
sender.start();
super.activate();
}
public void activate() throws ActivateException {
lirc.start();
sender.start();
super.activate();
}
public void deactivate() throws DeactivateException {
super.deactivate();
lirc.start();
sender.stop();
}
public void deactivate() throws DeactivateException {
super.deactivate();
lirc.start();
sender.stop();
}
public void exit() {
lirc.exit();
sender.exit();
}
public void exit() {
lirc.exit();
sender.exit();
}
public void input(SignalObject<Object> signalObject) {
Signal signal = signalObject.signal;
Object object = signalObject.object;
Builder builder = Button.newBuilder().setSignal(signal);
if (object instanceof LircButton) {
builder.setType(Type.LIRC);
LircButton lircButton = (LircButton) object;
builder.setLircButton(lircButton.getProto());
} else if (object instanceof Color) {
builder.setType(Type.COLOR);
Color color = (Color) object;
builder.setColorButton(ColorButton.newBuilder().setColor(color));
} else if (object instanceof Number) {
builder.setType(Type.NUMBER);
Number number = (Number) object;
builder.setNumberButton(NumberButton.newBuilder().setNumber(number));
} else if (object instanceof Direction) {
builder.setType(Type.DIRECTION);
Direction direction = (Direction) object;
builder.setDirectionButton(DirectionButton.newBuilder().setDirection(direction));
}
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
try {
builder.build().writeDelimitedTo(output);
sender.send(output.toByteArray());
} catch (IOException e) {
logger.error("Failed to send command");
}
}
public void input(SignalObject<Object> signalObject) {
Signal signal = signalObject.signal;
Object object = signalObject.object;
Builder builder = Button.newBuilder().setSignal(signal);
if (object instanceof LircButton) {
builder.setType(Type.LIRC);
LircButton lircButton = (LircButton) object;
builder.setLircButton(lircButton.getProto());
} else if (object instanceof Color) {
builder.setType(Type.COLOR);
Color color = (Color) object;
builder.setColorButton(ColorButton.newBuilder().setColor(color));
} else if (object instanceof Number) {
builder.setType(Type.NUMBER);
Number number = (Number) object;
builder.setNumberButton(NumberButton.newBuilder().setNumber(number));
} else if (object instanceof Direction) {
builder.setType(Type.DIRECTION);
Direction direction = (Direction) object;
builder.setDirectionButton(DirectionButton.newBuilder().setDirection(direction));
}
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
try {
builder.build().writeDelimitedTo(output);
sender.send(output.toByteArray());
} catch (IOException e) {
logger.error("Failed to send command");
}
}
}

View File

@@ -34,27 +34,27 @@ import com.github.boukefalos.lirc.util.Multiplexer;
import com.github.boukefalos.lirc.util.SignalObject;
public class Local extends Listen<Object> implements Lirc {
protected ArrayList<Listen<Object>> listenList;
protected ArrayList<Listen<Object>> listenList;
protected Multiplexer<String> multiplexer;
protected LircClient lircClient;
public Local() {
listenList = new ArrayList<Listen<Object>>();
listenList = new ArrayList<Listen<Object>>();
lircClient = new LircClient(this);
multiplexer = new Multiplexer<String>();
multiplexer.register(this);
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void remove(Listen<Object> listen) {
listenList.remove(listen);
}
public void activate() throws ActivateException {
lircClient.start();
lircClient.start();
super.activate();
}
@@ -70,54 +70,54 @@ public class Local extends Listen<Object> implements Lirc {
multiplexer.exit();
}
public void input(byte[] buffer) {
String line = new String(buffer).trim();
if (!line.startsWith("BEGIN")) {
Scanner scanner = new Scanner(line);
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();
// Need to pass as String to assure consistent hash
multiplexer.add(remote + " " + code);
scanner.close();
}
}
}
public void input(SignalObject<Object> signalObject) {
Object object = signalObject.object;
if (object instanceof String) {
// Translate String signal objects to LircButton signalObjects
String[] input = ((String) object).split(" ");
LircButton lircButton = new LircButton(input[0], input[1]);
signalObject = new SignalObject<Object>(signalObject.signal, lircButton);
otherParsings(signalObject.signal, input[1].toUpperCase());
}
// Pass signalObject to listens
public void input(SignalObject<Object> signalObject) {
Object object = signalObject.object;
if (object instanceof String) {
// Translate String signal objects to LircButton signalObjects
String[] input = ((String) object).split(" ");
LircButton lircButton = new LircButton(input[0], input[1]);
signalObject = new SignalObject<Object>(signalObject.signal, lircButton);
otherParsings(signalObject.signal, input[1].toUpperCase());
}
// Pass signalObject to listens
for (Listen<Object> listen : listenList) {
listen.add(signalObject);
listen.add(signalObject);
}
}
}
protected void otherParsings(Signal signal, String code) {
protected void otherParsings(Signal signal, String code) {
for (Color color : Color.values()) {
if (color.toString().equals(code)) {
add(new SignalObject<Color>(signal, color));
add(new SignalObject<Color>(signal, color));
}
}
for (Number number : Number.values()) {
if (number.toString().equals(code)) {
add(new SignalObject<Number>(signal, number));
add(new SignalObject<Number>(signal, number));
}
}
for (Direction direction : Direction.values()) {
if (direction.toString().equals(code)) {
add(new SignalObject<Direction>(signal, direction));
add(new SignalObject<Direction>(signal, direction));
}
}
try {
add(new SignalObject<Number>(signal, Number.valueOf(Integer.valueOf(code))));
add(new SignalObject<Number>(signal, Number.valueOf(Integer.valueOf(code))));
} catch (NumberFormatException e) {}
}
}
}

View File

@@ -19,55 +19,55 @@ import com.github.boukefalos.lirc.LircButton;
import com.github.boukefalos.lirc.util.SignalObject;
public class Remote extends AbstractReceiver implements Lirc {
protected ArrayList<Listen<Object>> listenList;
protected ArrayList<Listen<Object>> listenList;
public Remote(Forwarder forwarder) {
super(forwarder);
listenList = new ArrayList<Listen<Object>>();
}
public Remote(Forwarder forwarder) {
super(forwarder);
listenList = new ArrayList<Listen<Object>>();
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void register(Listen<Object> listen) {
listenList.add(listen);
}
public void remove(Listen<Object> listen) {
listenList.remove(listen);
}
public void remove(Listen<Object> listen) {
listenList.remove(listen);
}
public void receive(byte[] buffer) {
Object object = decode(buffer);
if (object != null) {
for (Listen<Object> listen : listenList) {
listen.add(object);
}
}
}
public void receive(byte[] buffer) {
Object object = decode(buffer);
if (object != null) {
for (Listen<Object> listen : listenList) {
listen.add(object);
}
}
}
public SignalObject<?> decode(byte[] buffer) {
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
try {
Button button = Button.parseDelimitedFrom(input);
Type type = button.getType();
Signal signal = button.getSignal();
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 null;
}
public SignalObject<?> decode(byte[] buffer) {
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
try {
Button button = Button.parseDelimitedFrom(input);
Type type = button.getType();
Signal signal = button.getSignal();
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 null;
}
}

View File

@@ -30,7 +30,7 @@ public class Multiplexer<T> {
protected int timeout;
protected ArrayList<Listen<Object>> listenList;
protected ScheduledExecutorService executor;
protected ScheduledExecutorService executor;
protected HashMap<T,Integer> counterMap;
public Multiplexer() {
@@ -38,63 +38,63 @@ public class Multiplexer<T> {
}
public Multiplexer(int timeout) {
this.timeout = timeout;
listenList = new ArrayList<Listen<Object>>();
this.timeout = timeout;
listenList = new ArrayList<Listen<Object>>();
executor = Executors.newSingleThreadScheduledExecutor();
counterMap = new HashMap<T,Integer>();
}
public void register(Listen<Object> listen) {
listenList.add(listen);
listenList.add(listen);
}
public void remove(Listen<SignalObject<T>> listen) {
listenList.remove(listen);
listenList.remove(listen);
}
public synchronized void add(T object) {
Expire expire = new Expire(this, object);
executor.schedule(expire, (long) timeout, TimeUnit.MILLISECONDS);
int counter = counterMap.getOrDefault(object, 0);
if (counter == 0) {
for (Listen<Object> listen : listenList) {
listen.add(new SignalObject<T>(Signal.BEGIN, object));
}
}
counterMap.put(object, counter + 1);
public synchronized void add(T object) {
Expire expire = new Expire(this, object);
executor.schedule(expire, (long) timeout, TimeUnit.MILLISECONDS);
int counter = counterMap.getOrDefault(object, 0);
if (counter == 0) {
for (Listen<Object> listen : listenList) {
listen.add(new SignalObject<T>(Signal.BEGIN, object));
}
}
counterMap.put(object, counter + 1);
}
protected synchronized void expire(T object) {
int counter = counterMap.get(object);
counterMap.put(object, counter - 1);
if (counter == 1) {
for (Listen<Object> listen : listenList) {
listen.add(new SignalObject<T>(Signal.END, object));
}
}
}
protected synchronized void expire(T object) {
int counter = counterMap.get(object);
counterMap.put(object, counter - 1);
if (counter == 1) {
for (Listen<Object> listen : listenList) {
listen.add(new SignalObject<T>(Signal.END, object));
}
}
}
public class Expire implements Runnable {
protected Multiplexer<T> multiplexer;
protected T object;
public class Expire implements Runnable {
protected Multiplexer<T> multiplexer;
protected T object;
public Expire(Multiplexer<T> multiplexer, T object) {
this.multiplexer = multiplexer;
this.object = object;
}
public Expire(Multiplexer<T> multiplexer, T object) {
this.multiplexer = multiplexer;
this.object = object;
}
public void run() {
multiplexer.expire(object);
}
}
public void run() {
multiplexer.expire(object);
}
}
public void stop() {
executor.shutdown();
}
public void exit() {
stop();
// Should cancel all scheduled Runnables
}
public void stop() {
executor.shutdown();
}
public void exit() {
stop();
// Should cancel all scheduled Runnables
}
}

View File

@@ -3,16 +3,16 @@ package com.github.boukefalos.lirc.util;
import lirc.Lirc.Signal;
public class SignalObject<T> {
public Signal signal;
public T object;
public Signal signal;
public T object;
public SignalObject(Signal signal, T object) {
this.signal = signal;
this.object = object;
}
public SignalObject(Signal signal, T object) {
this.signal = signal;
this.object = object;
}
public String bla() {
return signal + " " + object;
}
public String bla() {
return signal + " " + object;
}
}

View File

@@ -9,37 +9,37 @@ import com.github.boukefalos.lirc.LircButton;
import com.github.boukefalos.lirc.implementation.Local;
import com.github.boukefalos.lirc.util.SignalObject;
public class TestLocal extends Listen<Object> {
public static void main(String[] args) {
new TestLocal().start();
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {}
}
public class TestLocal extends Listen<Object> {
public static void main(String[] args) {
new TestLocal().start();
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {}
}
protected Lirc lirc;
protected Lirc lirc;
public TestLocal() {
lirc = new Local();
lirc.register(this);
}
public TestLocal() {
lirc = new Local();
lirc.register(this);
}
public void activate() throws ActivateException {
lirc.start();
lirc.start();
super.activate();
}
public void start() {
super.start();
}
public void start() {
super.start();
}
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);
}
}
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);
}
}
}

View File

@@ -14,39 +14,39 @@ import com.github.boukefalos.lirc.Server;
import com.github.boukefalos.lirc.util.SignalObject;
public class TestRemoteImplementation extends Listen<Object> {
protected Lirc lirc;
protected Lirc lirc;
public TestRemoteImplementation(Loader loader) {
lirc = loader.getLirc();
lirc.register(this);
}
public TestRemoteImplementation(Loader loader) {
lirc = loader.getLirc();
lirc.register(this);
}
public void activate() throws ActivateException {
lirc.start();
lirc.start();
super.activate();
}
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);
}
}
public static void main(Properties localProperties, Properties remoteProperties) throws LoaderException {
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
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);
}
}
public static void main(Properties localProperties, Properties remoteProperties) throws LoaderException {
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
Server server = localLoader.getServer();
Server server = localLoader.getServer();
server.start();
new TestRemoteImplementation(remoteLoader).start();
server.start();
new TestRemoteImplementation(remoteLoader).start();
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {}
}
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {}
}
}

View File

@@ -5,19 +5,19 @@ import java.util.Properties;
import base.exception.LoaderException;
public class TestTcpImplementation {
public static void main(String[] args) throws LoaderException {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "tcp");
public static void main(String[] args) throws LoaderException {
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");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("protocol", "tcp");
remoteProperties.setProperty("remote.host", "localhost");
remoteProperties.setProperty("remote.port", "8883");
TestRemoteImplementation.main(localProperties, remoteProperties);
}
TestRemoteImplementation.main(localProperties, remoteProperties);
}
}

View File

@@ -5,19 +5,19 @@ import java.util.Properties;
import base.exception.LoaderException;
public class TestUdpImplementation {
public static void main(String[] args) throws LoaderException {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.port", "8883");
localProperties.setProperty("server.protocol", "udp");
public static void main(String[] args) throws LoaderException {
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", "localhost");
remoteProperties.setProperty("remote.port", "8883");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("protocol", "udp");
remoteProperties.setProperty("remote.host", "localhost");
remoteProperties.setProperty("remote.port", "8883");
TestRemoteImplementation.main(localProperties, remoteProperties);
}
TestRemoteImplementation.main(localProperties, remoteProperties);
}
}