Alter Loader to allow configuration from Properties object. Server can now be obtained from Loader.
This commit is contained in:
@@ -10,41 +10,16 @@ import org.picocontainer.parameters.ConstantParameter;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.github.boukefalos.ibuddy.exception.LoaderException;
|
|
||||||
import com.github.boukefalos.ibuddy.implementation.Local;
|
import com.github.boukefalos.ibuddy.implementation.Local;
|
||||||
import com.github.boukefalos.ibuddy.implementation.Remote;
|
import com.github.boukefalos.ibuddy.implementation.Remote;
|
||||||
import com.github.boukefalos.ibuddy.server.Server;
|
import com.github.boukefalos.ibuddy.server.Server;
|
||||||
|
|
||||||
public class Loader {
|
public class Loader {
|
||||||
protected static Logger logger = LoggerFactory.getLogger(Loader.class);
|
protected static final String PROPERTIES_FILE = "ibuddy.properties";
|
||||||
protected static MutablePicoContainer pico;
|
protected Logger logger = LoggerFactory.getLogger(Loader.class);
|
||||||
|
protected MutablePicoContainer pico;
|
||||||
public static iBuddy getiBuddy() throws LoaderException {
|
|
||||||
if (!setup()) {
|
|
||||||
throw new LoaderException();
|
|
||||||
}
|
|
||||||
return pico.getComponent(iBuddy.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Server getServer() throws LoaderException {
|
|
||||||
if (!setup()) {
|
|
||||||
throw new LoaderException();
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public Loader(Properties properties) {
|
||||||
/* Initialise container */
|
/* Initialise container */
|
||||||
pico = new DefaultPicoContainer();
|
pico = new DefaultPicoContainer();
|
||||||
|
|
||||||
@@ -60,9 +35,32 @@ public class Loader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Add server */
|
/* Add server */
|
||||||
|
if (properties.getProperty("server") != null) {
|
||||||
pico.addComponent(Server.class, Server.class, new Parameter[]{
|
pico.addComponent(Server.class, Server.class, new Parameter[]{
|
||||||
|
new ConstantParameter(getiBuddy()),
|
||||||
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
|
new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))});
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 Server getServer() {
|
||||||
|
return pico.getComponent(Server.class);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package com.github.boukefalos.ibuddy;
|
package com.github.boukefalos.ibuddy;
|
||||||
|
|
||||||
|
import org.jraf.jlibibuddy.IBuddyException;
|
||||||
|
|
||||||
import com.github.boukefalos.ibuddy.exception.iBuddyException;
|
import com.github.boukefalos.ibuddy.exception.iBuddyException;
|
||||||
|
|
||||||
public interface iBuddy {
|
public interface iBuddy {
|
||||||
public void sendHeadRed(boolean headRed) throws iBuddyException;
|
public void setHeadRed(boolean headRed) throws iBuddyException;
|
||||||
public void sendHeadGreen(boolean headGreen) throws iBuddyException;
|
public void setHeadGreen(boolean headGreen) throws iBuddyException;
|
||||||
public void sendHeadBlue(boolean headBlue) throws iBuddyException;
|
public void setHeadBlue(boolean headBlue) throws iBuddyException;
|
||||||
|
public void test() throws IBuddyException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public class Local implements iBuddy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendHeadGreen(boolean headGreen) throws iBuddyException {
|
public void setHeadGreen(boolean headGreen) throws iBuddyException {
|
||||||
try {
|
try {
|
||||||
IBuddy.sendHeadGreen(headGreen);
|
IBuddy.sendHeadGreen(headGreen);
|
||||||
} catch (IBuddyException e) {
|
} 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 {
|
try {
|
||||||
IBuddy.sendHeadBlue(headBlue);
|
IBuddy.sendHeadBlue(headBlue);
|
||||||
} catch (IBuddyException e) {
|
} 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();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import java.net.InetAddress;
|
|||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import org.jraf.jlibibuddy.IBuddyException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.github.boukefalos.ibuddy.iBuddy;
|
import com.github.boukefalos.ibuddy.iBuddy;
|
||||||
|
import com.github.boukefalos.ibuddy.exception.iBuddyException;
|
||||||
|
|
||||||
public class Remote implements iBuddy {
|
public class Remote implements iBuddy {
|
||||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
@@ -21,6 +23,8 @@ public class Remote implements iBuddy {
|
|||||||
|
|
||||||
public Remote(String host, int port) throws UnknownHostException{
|
public Remote(String host, int port) throws UnknownHostException{
|
||||||
inetAddress = InetAddress.getByName(host);
|
inetAddress = InetAddress.getByName(host);
|
||||||
|
logger.debug(host);
|
||||||
|
logger.debug(String.valueOf(port));
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,11 +32,12 @@ public class Remote implements iBuddy {
|
|||||||
send("RED");
|
send("RED");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendHeadGreen(boolean headGreen) {
|
public void setHeadGreen(boolean headGreen) {
|
||||||
|
System.out.println("oki");
|
||||||
send("GREEN");
|
send("GREEN");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendHeadBlue(boolean headBlue) {
|
public void setHeadBlue(boolean headBlue) {
|
||||||
send("BLUE");
|
send("BLUE");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,4 +71,16 @@ public class Remote implements iBuddy {
|
|||||||
}
|
}
|
||||||
return true;
|
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
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,23 +10,21 @@ import org.jraf.jlibibuddy.IBuddy.Color;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.github.boukefalos.ibuddy.Loader;
|
import com.github.boukefalos.ibuddy.iBuddy;
|
||||||
import com.github.boukefalos.ibuddy.exception.LoaderException;
|
|
||||||
import com.github.boukefalos.ibuddy.exception.ServerException;
|
import com.github.boukefalos.ibuddy.exception.ServerException;
|
||||||
import com.github.boukefalos.ibuddy.exception.iBuddyException;
|
import com.github.boukefalos.ibuddy.exception.iBuddyException;
|
||||||
|
|
||||||
public class Server extends Thread {
|
public class Server extends Thread {
|
||||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
protected com.github.boukefalos.ibuddy.iBuddy iBuddy;
|
protected iBuddy iBuddy;
|
||||||
protected DatagramSocket diagramSocket;
|
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 {
|
try {
|
||||||
iBuddy = Loader.getiBuddy();
|
|
||||||
diagramSocket = new DatagramSocket(port);
|
diagramSocket = new DatagramSocket(port);
|
||||||
return;
|
return;
|
||||||
} catch (LoaderException e) {
|
|
||||||
logger.error("Failed to load iBuddy", e);
|
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
logger.error("Failed to initialize socket", e);
|
logger.error("Failed to initialize socket", e);
|
||||||
}
|
}
|
||||||
@@ -36,6 +34,7 @@ public class Server extends Thread {
|
|||||||
@SuppressWarnings("incomplete-switch")
|
@SuppressWarnings("incomplete-switch")
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
logger.debug("Wait for input");
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
|
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
|
||||||
try {
|
try {
|
||||||
@@ -43,16 +42,21 @@ public class Server extends Thread {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error("Failed to receive packet", e);
|
logger.error("Failed to receive packet", e);
|
||||||
}
|
}
|
||||||
|
// Rewrite with protocol buffers!
|
||||||
String received = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength());
|
String received = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Color color = IBuddy.Color.valueOf(received);
|
Color color = IBuddy.Color.valueOf(received);
|
||||||
switch (color) {
|
switch (color) {
|
||||||
case RED:
|
case RED:
|
||||||
iBuddy.sendHeadRed(true);
|
iBuddy.setHeadRed(true);
|
||||||
|
break;
|
||||||
case GREEN:
|
case GREEN:
|
||||||
iBuddy.sendHeadGreen(true);
|
iBuddy.setHeadGreen(true);
|
||||||
|
break;
|
||||||
case BLUE:
|
case BLUE:
|
||||||
iBuddy.sendHeadBlue(true);
|
iBuddy.setHeadBlue(true);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
logger.error("No such command", e);
|
logger.error("No such command", e);
|
||||||
|
|||||||
39
src/main/java/test/TestCommunication.java
Normal file
39
src/main/java/test/TestCommunication.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package test;
|
package test;
|
||||||
import com.github.boukefalos.ibuddy.Loader;
|
import com.github.boukefalos.ibuddy.Loader;
|
||||||
|
|
||||||
public class TestClient {
|
public class TestProperties {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
Loader.getiBuddy().sendHeadGreen(true);
|
Loader.getLoader().getServer().start();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
|
implementation=local
|
||||||
remote.host=localhost
|
remote.host=localhost
|
||||||
remote.port=8883
|
remote.port=8883
|
||||||
server.port=8883
|
server.port=8883
|
||||||
implementation=remote
|
server=true
|
||||||
Reference in New Issue
Block a user