Alter Loader to allow configuration from Properties object. Server can now be obtained from Loader.

This commit is contained in:
2015-03-21 21:09:34 +00:00
parent 54cc7dc65f
commit 5dc25b8858
9 changed files with 137 additions and 75 deletions

View File

@@ -10,59 +10,57 @@ import org.picocontainer.parameters.ConstantParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.boukefalos.ibuddy.exception.LoaderException;
import com.github.boukefalos.ibuddy.implementation.Local;
import com.github.boukefalos.ibuddy.implementation.Remote;
import com.github.boukefalos.ibuddy.server.Server;
public class Loader {
protected static Logger logger = LoggerFactory.getLogger(Loader.class);
protected static MutablePicoContainer pico;
protected static final String PROPERTIES_FILE = "ibuddy.properties";
protected Logger logger = LoggerFactory.getLogger(Loader.class);
protected MutablePicoContainer pico;
public static iBuddy getiBuddy() throws LoaderException {
if (!setup()) {
throw new LoaderException();
}
public Loader(Properties properties) {
/* Initialise container */
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")))});
}
/* 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")))});
}
}
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 iBuddy getiBuddy() {
return pico.getComponent(iBuddy.class);
}
public static Server getServer() throws LoaderException {
if (!setup()) {
throw new LoaderException();
}
public Server getServer() {
return pico.getComponent(Server.class);
}
protected static boolean setup() {
if (pico == null) {
/* Read properties file */
Properties properties = new Properties();
try {
properties.load(Loader.class.getClassLoader().getResourceAsStream("ibuddy.properties"));
} catch (IOException e) {
e.printStackTrace();
logger.error("Failed to load properties file", e);
return false;
}
/* Initialise container */
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")))});
}
/* Add server */
pico.addComponent(Server.class, Server.class, new Parameter[]{
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
}
return true;
}
}

View File

@@ -1,9 +1,12 @@
package com.github.boukefalos.ibuddy;
import org.jraf.jlibibuddy.IBuddyException;
import com.github.boukefalos.ibuddy.exception.iBuddyException;
public interface iBuddy {
public void sendHeadRed(boolean headRed) throws iBuddyException;
public void sendHeadGreen(boolean headGreen) throws iBuddyException;
public void sendHeadBlue(boolean headBlue) throws iBuddyException;
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;
}

View File

@@ -22,7 +22,7 @@ public class Local implements iBuddy {
}
}
public void sendHeadGreen(boolean headGreen) throws iBuddyException {
public void setHeadGreen(boolean headGreen) throws iBuddyException {
try {
IBuddy.sendHeadGreen(headGreen);
} catch (IBuddyException e) {
@@ -31,7 +31,7 @@ public class Local implements iBuddy {
}
public void sendHeadBlue(boolean headBlue) throws iBuddyException {
public void setHeadBlue(boolean headBlue) throws iBuddyException {
try {
IBuddy.sendHeadBlue(headBlue);
} catch (IBuddyException e) {
@@ -39,4 +39,16 @@ public class Local implements iBuddy {
}
}
@Override
public void setHeadRed(boolean headRed) throws iBuddyException {
// TODO Auto-generated method stub
}
@Override
public void test() throws IBuddyException {
IBuddy.sendAllOff();
}
}

View File

@@ -7,10 +7,12 @@ 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;
public class Remote implements iBuddy {
protected Logger logger = LoggerFactory.getLogger(getClass());
@@ -21,6 +23,8 @@ public class Remote implements iBuddy {
public Remote(String host, int port) throws UnknownHostException{
inetAddress = InetAddress.getByName(host);
logger.debug(host);
logger.debug(String.valueOf(port));
this.port = port;
}
@@ -28,11 +32,12 @@ public class Remote implements iBuddy {
send("RED");
}
public void sendHeadGreen(boolean headGreen) {
public void setHeadGreen(boolean headGreen) {
System.out.println("oki");
send("GREEN");
}
public void sendHeadBlue(boolean headBlue) {
public void setHeadBlue(boolean headBlue) {
send("BLUE");
}
@@ -66,4 +71,16 @@ public class Remote implements iBuddy {
}
return true;
}
@Override
public void setHeadRed(boolean headRed) throws iBuddyException {
// TODO Auto-generated method stub
}
@Override
public void test() throws IBuddyException {
// TODO Auto-generated method stub
}
}

View File

@@ -10,23 +10,21 @@ import org.jraf.jlibibuddy.IBuddy.Color;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.boukefalos.ibuddy.Loader;
import com.github.boukefalos.ibuddy.exception.LoaderException;
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 com.github.boukefalos.ibuddy.iBuddy iBuddy;
protected iBuddy iBuddy;
protected DatagramSocket diagramSocket;
public Server(int port) throws ServerException {
public Server(iBuddy iBuddy, int port) throws ServerException {
logger.debug(String.valueOf(port));
this.iBuddy = iBuddy;
try {
iBuddy = Loader.getiBuddy();
diagramSocket = new DatagramSocket(port);
return;
} catch (LoaderException e) {
logger.error("Failed to load iBuddy", e);
} catch (SocketException e) {
logger.error("Failed to initialize socket", e);
}
@@ -36,6 +34,7 @@ public class Server extends Thread {
@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 {
@@ -43,16 +42,21 @@ public class Server extends Thread {
} catch (IOException e) {
logger.error("Failed to receive packet", e);
}
// Rewrite with protocol buffers!
String received = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength());
try {
Color color = IBuddy.Color.valueOf(received);
switch (color) {
case RED:
iBuddy.sendHeadRed(true);
iBuddy.setHeadRed(true);
break;
case GREEN:
iBuddy.sendHeadGreen(true);
iBuddy.setHeadGreen(true);
break;
case BLUE:
iBuddy.sendHeadBlue(true);
iBuddy.setHeadBlue(true);
break;
}
} catch (IllegalArgumentException e) {
logger.error("No such command", e);

View File

@@ -0,0 +1,39 @@
package test;
import java.util.Properties;
import com.github.boukefalos.ibuddy.Loader;
import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.server.Server;
public class TestCommunication {
public static void main(String[] args) {
try {
Properties localProperties = new Properties();
localProperties.setProperty("implementation", "local");
localProperties.setProperty("server", "true");
localProperties.setProperty("server.port", "8883");
Properties remoteProperties = new Properties();
remoteProperties.setProperty("implementation", "remote");
remoteProperties.setProperty("remote.host", "localhost");
remoteProperties.setProperty("remote.port", "8883");
Loader localLoader = new Loader(localProperties);
Loader remoteLoader = new Loader(remoteProperties);
iBuddy localiBuddy = localLoader.getiBuddy();
iBuddy remoteiBuddy = remoteLoader.getiBuddy();
localiBuddy.test();
Server server = localLoader.getServer();
server.start();
remoteiBuddy.setHeadGreen(true);
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -1,10 +1,10 @@
package test;
import com.github.boukefalos.ibuddy.Loader;
public class TestClient {
public class TestProperties {
public static void main(String[] args) {
try {
Loader.getiBuddy().sendHeadGreen(true);
Loader.getLoader().getServer().start();
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -1,12 +0,0 @@
package test;
import com.github.boukefalos.ibuddy.Loader;
public class TestServer {
public static void main(String[] args) {
try {
Loader.getServer().start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -1,4 +1,5 @@
implementation=local
remote.host=localhost
remote.port=8883
server.port=8883
implementation=remote
server=true