Skip to content
Snippets Groups Projects
Commit 1e07fe93 authored by jkerdreu's avatar jkerdreu
Browse files

- Remove Config() in Engine constructor.

- Fix aLiveTimer using the wrong timeout value
- Get Engine.init() back to allow to change configuration at runtime

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Java/branches/0.7@2613 b32b6428-25c9-4566-ad07-03861ab6144f
parent 17debc98
Branches
No related tags found
No related merge requests found
......@@ -157,11 +157,20 @@ public abstract class Device {
return alivePeriod;
}
/**
* return the period timeout announce
*
* @return period value
*/
public int getTimeOutPeriod() {
return alivePeriod *2;
}
/**
* ask the device to refresh its internal timeout value.
*/
public void updateAliveTimeOut() {
aliveTimeOut = System.currentTimeMillis() / 1000 + getAlivePeriod()/2;
aliveTimeOut = System.currentTimeMillis() / 1000 + getAlivePeriod();
}
/**
......
......@@ -27,12 +27,11 @@ public class Engine extends Thread {
private ConcurrentLinkedQueue<Attribute> attrChgQueue = new ConcurrentLinkedQueue<Attribute>();
private boolean pause = false;
public Engine(Config cfg) throws IOException {
public Engine() throws IOException {
Config cfg = Config.getInstance();
factory = new MessageFactory(BinTools.unhexlify(cfg.getKey()));
networkConnector = new NetworkConnector(cfg.getAddress(), cfg.getPort(), cfg.getHops());
networkConnector.setOUTFifo(fifoIN);
byte[] key = BinTools.unhexlify(cfg.getKey());
factory = new MessageFactory(key);
init();
}
public NetworkConnector getNetworkConnector() {
......@@ -152,7 +151,7 @@ public class Engine extends Thread {
for (Device dev : getDevices()) {
if (dev.getAliveTimeOut() < now) {
Message msg = factory.buildAlive(dev, new ArrayList<UUID>(), dev.getAlivePeriod());
Message msg = factory.buildAlive(dev, new ArrayList<UUID>(), dev.getTimeOutPeriod());
LOGGER.info("Sending Alive for " + dev);
queueMessage(msg);
dev.updateAliveTimeOut();
......@@ -213,7 +212,7 @@ public class Engine extends Thread {
* @throws Exception
*/
public void handleIsAlive(Device dev, Message msg) {
Message reply = factory.buildAlive(dev, Arrays.asList(msg.getSource()), 60);
Message reply = factory.buildAlive(dev, Arrays.asList(msg.getSource()), dev.getTimeOutPeriod());
queueMessage(reply);
}
......@@ -315,8 +314,10 @@ public class Engine extends Thread {
/**
* Start the network Thread.
* @throws IOException
*/
public void init() {
public void init() throws IOException {
networkConnector.connect();
networkConnector.start();
}
......
......@@ -26,7 +26,9 @@ public final class NetworkConnector extends Thread {
public NetworkConnector(String addr, int port, int ttl) throws IOException {
msocket = new MulticastSocket();
this.connect(InetAddress.getByName(addr), port, ttl);
setAddr(InetAddress.getByName(addr));
setPort(port);
setHops(ttl);
}
/**
......@@ -88,10 +90,9 @@ public final class NetworkConnector extends Thread {
}
}
public void connect(InetAddress addr, int port, int ttl) throws IOException {
this.setAddr(addr);
this.setPort(port);
this.setHops(ttl);
public void connect() throws IOException {
InetAddress addr = getAddr();
int port = getPort();
this.msocket = new MulticastSocket(port);
this.msocket.joinGroup(addr);
LOGGER.info("Connnecting " + addr.toString() + ":" + port);
......@@ -99,7 +100,7 @@ public final class NetworkConnector extends Thread {
public void disconnect() {
try {
this.msocket.leaveGroup(this.groupAddr);
this.msocket.leaveGroup(getAddr());
} catch (IOException e) {
LOGGER.severe(e.getMessage());
}
......@@ -108,7 +109,7 @@ public final class NetworkConnector extends Thread {
public void send(byte[] data) {
try {
DatagramPacket msgTx = new DatagramPacket(data, data.length, groupAddr, port);
DatagramPacket msgTx = new DatagramPacket(data, data.length, getAddr(), port);
this.msocket.send(msgTx);
} catch (IOException e) {
LOGGER.severe(e.getMessage());
......
......@@ -2,7 +2,6 @@ package org.imta.xaal.tests;
import org.imta.xaal.lib.Engine;
import org.imta.xaal.lib.Message;
import org.imta.xaal.lib.Config;
import java.io.IOException;
......@@ -10,8 +9,8 @@ import java.io.IOException;
public class Dumper extends Engine {
public Dumper(Config cfg) throws IOException {
super(cfg);
public Dumper() throws IOException {
super();
}
@Override
......@@ -20,7 +19,8 @@ public class Dumper extends Engine {
}
public static void main (String[] args) throws IOException {
Dumper dumper = new Dumper(Config.getInstance());
dumper.run();
Dumper dumper = new Dumper();
dumper.init();
dumper.start();
}
}
......@@ -4,8 +4,6 @@ import java.util.Arrays;
import java.util.UUID;
import org.imta.xaal.lib.Engine;
import org.imta.xaal.lib.Config;
public class TestLamp {
......@@ -13,7 +11,8 @@ public class TestLamp {
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
Engine eng = new Engine(Config.getInstance());
Engine eng = new Engine();
Lamp l1 = new Lamp(UUID.fromString("6265eb30-8c59-11e9-98b1-b827ebe99201"));
Lamp l2 = new Lamp(UUID.fromString("6265eb30-8c59-11e9-98b1-b827ebe99202"));
UUID grp = UUID.randomUUID();
......@@ -21,6 +20,7 @@ public class TestLamp {
l2.setGroupID(grp);
eng.registerDevices(Arrays.asList(l1,l2));
eng.init();
eng.start();
l1.toggle();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment