Skip to content
Snippets Groups Projects
Commit 4127124f authored by jkerdreu's avatar jkerdreu
Browse files

Better error handling, remove unusefull Exceptions

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Java/branches/0.7@2607 b32b6428-25c9-4566-ad07-03861ab6144f
parent dea3cc8d
Branches
Tags
No related merge requests found
......@@ -120,7 +120,7 @@ public class Engine extends Thread {
* Queue the attributesChange message for each device.
* @throws Exception
*/
public void processAttributesChange() throws Exception {
public void processAttributesChange() {
HashMap<Device, HashMap<String, Object>> map = new HashMap<Device, HashMap<String, Object>>();
// group attributeChange by device, and store it in a HashMap
......@@ -146,7 +146,7 @@ public class Engine extends Thread {
* Queue Alive message for devices.
* @throws Exception
*/
public void processAlives() throws Exception {
public void processAlives() {
long now = System.currentTimeMillis()/1000;
for (Device dev : getDevices()) {
......@@ -167,7 +167,7 @@ public class Engine extends Thread {
* @param msg the incoming request
* @throws Exception
*/
public void handleGetDescription(Device dev, Message msg) throws Exception {
public void handleGetDescription(Device dev, Message msg) {
HashMap<String,Object> body = new HashMap<String,Object>();
if (dev.getVendorID()!=null) body.put("vendor_id", dev.getVendorID());
if (dev.getProductID()!=null) body.put("product_id", dev.getProductID());
......@@ -194,7 +194,7 @@ public class Engine extends Thread {
* @param msg the incoming request
* @throws Exception
*/
public void handleGetAttributes(Device dev, Message msg) throws Exception {
public void handleGetAttributes(Device dev, Message msg) {
HashMap<String,Object> body = new HashMap<String,Object>();
for (Attribute attr : dev.getAttributes()) {
body.put(attr.getName(), attr.getValue());
......@@ -211,7 +211,7 @@ public class Engine extends Thread {
* @param msg the incoming request
* @throws Exception
*/
public void handleIsAlive(Device dev, Message msg) throws Exception {
public void handleIsAlive(Device dev, Message msg) {
Message reply = factory.buildAlive(dev, Arrays.asList(msg.getSource()), 60);
queueMessage(reply);
}
......@@ -223,7 +223,7 @@ public class Engine extends Thread {
* @param msg the incoming request
* @throws Exception
*/
public void handleTDB(Device dev, Message msg) throws Exception {
public void handleTDB(Device dev, Message msg) {
Message error = factory.buildError(dev, Arrays.asList(msg.getSource()), 500, "Not implemented yet");
queueMessage(error);
}
......@@ -235,10 +235,10 @@ public class Engine extends Thread {
* @param msg the message to handle.
* @throws Exception
*/
public void handleRequest(List<Device> targets, Message msg) throws Exception {
public void handleRequest(List<Device> targets, Message msg) {
for (Device dev : targets) {
System.out.println("Handling request " + msg.getAction() + " for : " + dev.toString());
LOGGER.info("REQUEST " + msg.getAction() + " for : " + dev.toString());
switch (msg.getAction()) {
case "is_alive":
......@@ -273,9 +273,14 @@ public class Engine extends Thread {
* @param msg the message to queue.
* @throws Exception
*/
public void queueMessage(Message msg) throws Exception {
byte[] buf = factory.encodeMessage(msg);
fifoOUT.add(buf);
public void queueMessage(Message msg) {
try {
byte[] buf = factory.encodeMessage(msg);
if (buf != null)
fifoOUT.add(buf);
} catch (IOException e) {
LOGGER.warning("Unable to encode msg: " + e.toString());
}
}
/**
......@@ -296,7 +301,7 @@ public class Engine extends Thread {
* @param msg the message to process
* @throws Exception
*/
public void processRxMSG(Message msg) throws Exception {
public void processRxMSG(Message msg) {
if (msg.getMsgType() == MessageType.REQUEST) {
List<Device> targets = filterDevicesForMessage(msg);
handleRequest(targets, msg);
......@@ -334,7 +339,8 @@ public class Engine extends Thread {
if (!fifoIN.isEmpty()) {
byte[] data = fifoIN.poll();
Message msg = factory.decodeMessage(data);
processRxMSG(msg);
if (msg!=null)
processRxMSG(msg);
}
// AttributesChange ?
......
......@@ -19,6 +19,7 @@ public class Lamp extends Device {
setVersion("35 rev2");
setProductID("Dummy Java Lamp");
setHwID("6b2");
setInfo(getProductID()+ " " + getHwID());
setSchema( new URI("http://foobar.com"));
setUrl(new URI("http://dev.foobar.org"));
registerAttributes(Arrays.asList(light,brightness));
......
......@@ -11,16 +11,18 @@ import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.UUID;
//import java.util.logging.Logger;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.LookAndFeel;
import com.upokecenter.cbor.CBORObject;
public class MessageFactory {
//private final static Logger LOGGER = Logger.getLogger(Device.class.getName());
private final static Logger LOGGER = Logger.getLogger(Device.class.getName());
static Random random = new Random();
private byte[] key;
......@@ -29,7 +31,7 @@ public class MessageFactory {
this.key = key;
}
public Message decodeMessage(byte[] data) throws Exception {
public Message decodeMessage(byte[] data) {
Message msg = new Message();
CBORObject cbor = bytesToCbor(data);
......@@ -55,7 +57,14 @@ public class MessageFactory {
// ------- Payload --------------------
byte[] cyph = cbor.get(4).GetByteString();
byte[] clear = decrypt(cyph, key, nonce, ad);
byte[] clear;
try {
clear = decrypt(cyph, key, nonce, ad);
} catch (Exception e) {
LOGGER.warning("Unable to decrypt msg: " + e.toString());
return null;
}
CBORObject payload = bytesToCbor(clear);
// source
UUID source = UUIDUtils.getUUIDFromBytes(payload.get(0).GetByteString());
......@@ -77,7 +86,7 @@ public class MessageFactory {
}
public byte[] encodeMessage(Message message) throws Exception {
public byte[] encodeMessage(Message message) throws IOException {
ArrayList<Object> result = new ArrayList<Object>();
// ------- Headers ---------------------
result.add(message.getVersion());
......@@ -89,6 +98,7 @@ public class MessageFactory {
for (UUID k:message.getTargets()){
targets.add(UUIDUtils.getBytesFromUUID(k));
}
byte[] ad = cborToBytes(CBORObject.FromObject(targets));
result.add(ad);
......@@ -102,15 +112,19 @@ public class MessageFactory {
// cyphering
byte[] clear = cborToBytes(CBORObject.FromObject(buf));
byte[] nonce = buildNonce(ts);
byte[] payload = encrypt(clear, key, nonce, ad);
result.add(payload);
try {
byte[] payload = encrypt(clear, key, nonce, ad);
result.add(payload);
} catch (Exception e) {
LOGGER.warning("Unable to encrypt message: " + e.toString());
return null;
}
// final Cbor packing.
CBORObject cbor = CBORObject.FromObject(result);
return cborToBytes(cbor);
}
public Message buildMSG(Device dev, List<UUID> targets, MessageType msgType,String action, CBORObject body) throws Exception {
public Message buildMSG(Device dev, List<UUID> targets, MessageType msgType,String action, CBORObject body) {
Message message = new Message();
if (dev!=null) {
message.setSource(dev.getAddress());
......@@ -130,7 +144,7 @@ public class MessageFactory {
return null;
}
public Message buildAlive(Device dev, List<UUID> targets, int timeout) throws Exception {
public Message buildAlive(Device dev, List<UUID> targets, int timeout) {
Message message = buildMSG(dev, targets, MessageType.NOTIFY, "alive", null);
HashMap<String,Object> body = new HashMap<String,Object>();
body.put("timeout", timeout);
......
......@@ -103,7 +103,7 @@ public final class NetworkConnector extends Thread {
} catch (IOException e) {
LOGGER.severe(e.getMessage());
}
System.out.println("Leave multicast group");
LOGGER.info("Leave multicast group");
}
public void send(byte[] data) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment