Skip to content
Snippets Groups Projects
Commit 5b397adb authored by jkerdreu's avatar jkerdreu
Browse files

Split the package

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Java/branches/0.7@2621 b32b6428-25c9-4566-ad07-03861ab6144f
parent a469c549
Branches
No related tags found
No related merge requests found
......@@ -10,6 +10,8 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
import imt.xaal.utils.BinUtils;
/**
*
* @author jkx
......@@ -28,7 +30,7 @@ public class Engine extends Thread {
public Engine() throws IOException {
Config cfg = Config.getInstance();
factory = new MessageFactory(BinTools.unhexlify(cfg.getKey()));
factory = new MessageFactory(BinUtils.unhexlify(cfg.getKey()));
networkConnector = new NetworkConnector(cfg.getAddress(), cfg.getPort(), cfg.getHops());
networkConnector.setOUTFifo(fifoIN);
}
......@@ -134,7 +136,7 @@ public class Engine extends Thread {
// for each device produce a new msg
for (Device key : map.keySet()) {
Message msg = factory.buildMSG(key, Arrays.asList(), MessageType.NOTIFY, "attributes_change", null);
Message msg = factory.buildMSG(key, Arrays.asList(), MessageType.NOTIFY, "attributes_change");
LOGGER.info("AttributesChange : " + key + "=>" + map.get(key));
msg.setBodyAsMap(map.get(key));
queueMessage(msg);
......@@ -181,7 +183,7 @@ public class Engine extends Thread {
body.put("unsupportedAttributes", dev.getUnsupportedAttributes());
body.put("unsupportedNotifications", dev.getUnsupportedNotifications());
Message reply = factory.buildMSG(dev, Arrays.asList(msg.getSource()), MessageType.REPLY, msg.getAction(), null);
Message reply = factory.buildMSG(dev, Arrays.asList(msg.getSource()), MessageType.REPLY, msg.getAction());
reply.setBodyAsMap(body);
queueMessage(reply);
}
......@@ -198,7 +200,7 @@ public class Engine extends Thread {
for (Attribute attr : dev.getAttributes()) {
body.put(attr.getName(), attr.getValue());
}
Message reply = factory.buildMSG(dev, Arrays.asList(msg.getSource()), MessageType.REPLY, msg.getAction(), null);
Message reply = factory.buildMSG(dev, Arrays.asList(msg.getSource()), MessageType.REPLY, msg.getAction());
reply.setBodyAsMap(body);
queueMessage(reply);
}
......
......@@ -19,6 +19,11 @@ import javax.crypto.spec.SecretKeySpec;
import com.upokecenter.cbor.CBORObject;
import imt.xaal.utils.BinUtils;
import imt.xaal.utils.UUIDUtils;
import imt.xaal.utils.CBORUtils;
public class MessageFactory {
private final static Logger LOGGER = Logger.getLogger(Device.class.getName());
......@@ -32,7 +37,7 @@ public class MessageFactory {
public Message decodeMessage(byte[] data) {
Message msg = new Message();
CBORObject cbor = bytesToCbor(data);
CBORObject cbor = CBORUtils.bytesToCbor(data);
// ------- Headers ---------------------
// version
......@@ -47,7 +52,7 @@ public class MessageFactory {
byte[] nonce = buildNonce(ts);
// ad and build target
byte[] ad = cbor.get(3).GetByteString();
CBORObject target = bytesToCbor(ad);
CBORObject target = CBORUtils.bytesToCbor(ad);
ArrayList<UUID> targets = new ArrayList<UUID>();
for (var k: target.getValues()) {
targets.add(UUIDUtils.getUUIDFromBytes(k.GetByteString()));
......@@ -64,7 +69,7 @@ public class MessageFactory {
return null;
}
CBORObject payload = bytesToCbor(clear);
CBORObject payload = CBORUtils.bytesToCbor(clear);
// source
UUID source = UUIDUtils.getUUIDFromBytes(payload.get(0).GetByteString());
msg.setSource(source);
......@@ -98,7 +103,7 @@ public class MessageFactory {
targets.add(UUIDUtils.getBytesFromUUID(k));
}
byte[] ad = cborToBytes(CBORObject.FromObject(targets));
byte[] ad = CBORUtils.cborToBytes(CBORObject.FromObject(targets));
result.add(ad);
// ------- Payload --------------------
......@@ -109,7 +114,7 @@ public class MessageFactory {
buf.add(message.getAction());
if (message.getBody()!=null) { buf.add(message.getBody()); }
// cyphering
byte[] clear = cborToBytes(CBORObject.FromObject(buf));
byte[] clear = CBORUtils.cborToBytes(CBORObject.FromObject(buf));
byte[] nonce = buildNonce(ts);
try {
byte[] payload = encrypt(clear, key, nonce, ad);
......@@ -120,10 +125,10 @@ public class MessageFactory {
}
// final Cbor packing.
CBORObject cbor = CBORObject.FromObject(result);
return cborToBytes(cbor);
return CBORUtils.cborToBytes(cbor);
}
public Message buildMSG(Device dev, List<UUID> targets, MessageType msgType,String action, CBORObject body) {
public Message buildMSG(Device dev, List<UUID> targets, MessageType msgType,String action) {
Message message = new Message();
if (dev!=null) {
message.setSource(dev.getAddress());
......@@ -133,36 +138,28 @@ public class MessageFactory {
message.setTimestamp(buildTimeStamp());
message.setMsgType(msgType);
message.setAction(action);
return message;
}
public Message buildMSG(Device dev, List<UUID> targets, MessageType msgType,String action, CBORObject body) {
Message message = buildMSG(dev, targets, msgType, action);
if (body!=null)
message.setBody(body);
return message;
}
public Message buildError(Device dev, List<UUID> targets, int i, String string) {
return null;
}
public Message buildAlive(Device dev, List<UUID> targets, int timeout) {
Message message = buildMSG(dev, targets, MessageType.NOTIFY, "alive", null);
Message message = buildMSG(dev, targets, MessageType.NOTIFY, "alive");
HashMap<String,Object> body = new HashMap<String,Object>();
body.put("timeout", timeout);
message.setBodyAsMap(body);
return message;
}
public static CBORObject bytesToCbor(byte [] data){
return CBORObject.Read(new ByteArrayInputStream(data));
}
public static byte[] cborToBytes(CBORObject cbor) throws IOException {
var stream = new ByteArrayOutputStream();
cbor.WriteTo(stream);
return stream.toByteArray();
}
public static byte[] decrypt(byte[] cipherText, byte[] key, byte[] nonce, byte[] ad) throws Exception {
Cipher cipher = Cipher.getInstance("ChaCha20-Poly1305/None/NoPadding");
AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(nonce);
......@@ -183,8 +180,8 @@ public class MessageFactory {
public static byte[] buildNonce(long[] timestamp) {
byte[] nonce = new byte[12];
System.arraycopy(BinTools.longToByteArray(timestamp[0],8), 0, nonce, 0, 8);
System.arraycopy(BinTools.longToByteArray(timestamp[1],4), 0, nonce, 8, 4);
System.arraycopy(BinUtils.longToByteArray(timestamp[0],8), 0, nonce, 0, 8);
System.arraycopy(BinUtils.longToByteArray(timestamp[1],4), 0, nonce, 8, 4);
return nonce;
}
......
......@@ -21,7 +21,7 @@ public final class NetworkConnector extends Thread {
private int port;
private int ttl;
private boolean pause = false;
private Queue fifoOUT = null;
private Queue<byte[]> fifoOUT = null;
public NetworkConnector(String addr, int port, int ttl) throws IOException {
msocket = new MulticastSocket();
......@@ -126,7 +126,7 @@ public final class NetworkConnector extends Thread {
return Arrays.copyOfRange(buf, 0, data.getLength());
}
public void setOUTFifo(Queue value) {
public void setOUTFifo(Queue<byte[]> value) {
fifoOUT = value;
}
......
package imt.xaal.utils;
public class BinUtils {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
/**
* Transform a byte array into a it's hexadecimal representation
*/
public static String hexlify(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
String ret = new String(hexChars);
return ret;
}
/**
* Transform a string of hexadecimal chars into a byte array
*/
public static byte[] unhexlify(String argbuf) {
int arglen = argbuf.length();
if (arglen % 2 != 0)
throw new RuntimeException("Odd-length string");
byte[] retbuf = new byte[arglen/2];
for (int i = 0; i < arglen; i += 2) {
int top = Character.digit(argbuf.charAt(i), 16);
int bot = Character.digit(argbuf.charAt(i+1), 16);
if (top == -1 || bot == -1)
throw new RuntimeException("Non-hexadecimal digit found");
retbuf[i / 2] = (byte) ((top << 4) + bot);
}
return retbuf;
}
public static byte[] longToByteArray(long value, int size) {
byte[] result = new byte[size];
for (int i = size-1; i>=0; i--) {
byte r = (byte) (value >>> (i * 8));
result[(size-1)-i] = r;
}
return result;
}
}
\ No newline at end of file
package imt.xaal.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.upokecenter.cbor.CBORObject;
public class CBORUtils {
public static CBORObject bytesToCbor(byte [] data){
return CBORObject.Read(new ByteArrayInputStream(data));
}
public static byte[] cborToBytes(CBORObject cbor) throws IOException {
var stream = new ByteArrayOutputStream();
cbor.WriteTo(stream);
return stream.toByteArray();
}
}
package imt.xaal.utils;
import java.nio.ByteBuffer;
import java.util.UUID;
public class UUIDUtils {
public static byte[] getBytesFromUUID(UUID uuid) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
}
public static UUID getUUIDFromBytes(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
Long high = byteBuffer.getLong();
Long low = byteBuffer.getLong();
return new UUID(high, low);
}
}
\ No newline at end of file
......@@ -4,8 +4,6 @@ import imt.xaal.lib.Engine;
import imt.xaal.lib.Message;
import java.io.IOException;
import java.lang.System.Logger;
public class Dumper extends Engine {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment