Skip to content

Commit

Permalink
Merge branch 'network' of https://github.com/d3scomp/JDEECo.git into …
Browse files Browse the repository at this point in the history
…network
  • Loading branch information
vladamatena committed Feb 12, 2015
2 parents c025f6a + 81daa8b commit 090ef12
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import cz.cuni.mff.d3s.deeco.runtime.DEECoContainer;
import cz.cuni.mff.d3s.deeco.runtime.DEECoPlugin;
import cz.cuni.mff.d3s.jdeeco.network.l1.DataIDSource;
import cz.cuni.mff.d3s.jdeeco.network.l1.DefaultDataIDSource;
import cz.cuni.mff.d3s.jdeeco.network.l1.Layer1;
import cz.cuni.mff.d3s.jdeeco.network.l2.Layer2;
import cz.cuni.mff.d3s.jdeeco.network.marshaller.MarshallerRegistry;
Expand Down Expand Up @@ -39,14 +39,7 @@ public List<Class<? extends DEECoPlugin>> getDependencies() {
public void init(DEECoContainer container) {
// Initialize Layer 1
// TODO: Data id source and node id should have been set properly
l1 = new Layer1(l2, 0, new DataIDSource() {
int nextId = 0;

@Override
public int createDataID() {
return nextId++;
}
});
l1 = new Layer1(l2, (byte) 0, DefaultDataIDSource.getInstance());

// Initialize Layer 2
l2 = new Layer2(l1, registery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DefaultDataIDSource implements DataIDSource {
private static DefaultDataIDSource instance;
private static int COUNTER = 0;

public synchronized DefaultDataIDSource getInstance() {
public static synchronized DefaultDataIDSource getInstance() {
if (instance == null) {
instance = new DefaultDataIDSource();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.cuni.mff.d3s.jdeeco.network.l1;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
* L1 packet
Expand All @@ -10,25 +11,25 @@
*/
public class L1Packet {

public static int HEADER_SIZE = 20; // TotalSize + PayloadSize + SrcNode + StartPos + DataID
public static int HEADER_SIZE = 9; // TotalSize + PayloadSize + SrcNode + StartPos + DataID

public final byte[] payload;
/** payload carried by this packet */
public final int srcNode;
public final byte srcNode;
/** ID of the source that the data originates from */
public final int dataId;
/** data ID this packet (fragment) is part of */
/** data ID this packet (fragment) is part of - 2 bytes*/
public final int startPos;
/** this packet payload start position - in bytes */
/** this packet payload start position - in bytes - 2 bytes*/
public final int payloadSize;
/** this packet payload size - in bytes */
/** this packet payload size - in bytes - 2 bytes*/
public final int totalSize;
/** payload total size - in bytes */
/** payload total size - in bytes - 2 bytes*/
public ReceivedInfo receivedInfo;

/** receival additaional information */

public L1Packet(byte[] payload, int srcNode, int dataId, int startPos, int totalSize, ReceivedInfo receivedInfo) {
public L1Packet(byte[] payload, byte srcNode, int dataId, int startPos, int totalSize, ReceivedInfo receivedInfo) {
this.payload = payload;
this.srcNode = srcNode;
this.dataId = dataId;
Expand All @@ -38,7 +39,7 @@ public L1Packet(byte[] payload, int srcNode, int dataId, int startPos, int total
this.payloadSize = payload.length;
}

public L1Packet(byte[] payload, int srcNode, int dataId, int startPos, int totalSize) {
public L1Packet(byte[] payload, byte srcNode, int dataId, int startPos, int totalSize) {
this(payload, srcNode, dataId, startPos, totalSize, null);
}

Expand All @@ -52,11 +53,11 @@ public L1Packet(byte[] payload, int srcNode, int dataId, int startPos, int total
*/
public byte[] getBytes() {
ByteBuffer byteBuffer = ByteBuffer.allocate(HEADER_SIZE + payloadSize);
byteBuffer.putInt(totalSize);
byteBuffer.putInt(payloadSize);
byteBuffer.putInt(startPos);
byteBuffer.putInt(dataId);
byteBuffer.putInt(srcNode);
byteBuffer.put(encodeIntegerInto2Bytes(totalSize));
byteBuffer.put(encodeIntegerInto2Bytes(payloadSize));
byteBuffer.put(encodeIntegerInto2Bytes(startPos));
byteBuffer.put(encodeIntegerInto2Bytes(dataId));
byteBuffer.put(srcNode);
byteBuffer.put(payload);
return byteBuffer.array();
}
Expand All @@ -69,16 +70,12 @@ public byte[] getBytes() {
* @return L1 packet
*/
public static L1Packet fromBytes(byte[] bytes, int offset) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
byteBuffer.position(offset);
int totalSize = byteBuffer.getInt();
int payloadSize = byteBuffer.getInt();
int startPos = byteBuffer.getInt();
int dataId = byteBuffer.getInt();
int srcNode = byteBuffer.getInt();
byte[] payload = new byte[payloadSize];
byteBuffer.get(payload, byteBuffer.position(), payloadSize);
return new L1Packet(payload, srcNode, dataId, startPos, totalSize);
int totalSize = decodeIntegerFrom2Bytes(Arrays.copyOfRange(bytes, offset, offset+2));
int payloadSize = decodeIntegerFrom2Bytes(Arrays.copyOfRange(bytes, offset+2, offset+4));
int startPos = decodeIntegerFrom2Bytes(Arrays.copyOfRange(bytes, offset+4, offset+6));
int dataId = decodeIntegerFrom2Bytes(Arrays.copyOfRange(bytes, offset+6, offset+8));
byte srcNode = bytes[offset+8];
return new L1Packet(Arrays.copyOfRange(bytes, offset+9, offset + payloadSize + 9), srcNode, dataId, startPos, totalSize);
}

/**
Expand All @@ -89,4 +86,17 @@ public static L1Packet fromBytes(byte[] bytes, int offset) {
public int getByteSize() {
return HEADER_SIZE + payloadSize;
}

private static int decodeIntegerFrom2Bytes(byte [] value) {
int high = value[1] >= 0 ? value[1] : 256 + value[1];
int low = value[0] >= 0 ? value[0] : 256 + value[0];
return low | (high << 8);
}

private static byte [] encodeIntegerInto2Bytes(int value) {
byte[] result = new byte[2];
result[0] = (byte)(value & 0xFF);
result[1] = (byte)((value >> 8) & 0xFF);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import java.util.Set;

import cz.cuni.mff.d3s.deeco.logging.Log;
import cz.cuni.mff.d3s.jdeeco.network.Address;
import cz.cuni.mff.d3s.jdeeco.network.Device;
import cz.cuni.mff.d3s.jdeeco.network.L1DataProcessor;
Expand All @@ -29,15 +30,15 @@ public class Layer1 implements L2PacketSender, L1StrategyManager {
protected final static int MINIMUM_DATA_TRANSMISSION_SIZE = MINIMUM_PAYLOAD + L1Packet.HEADER_SIZE;

private final Set<L1Strategy> strategies; // registered strategies
private final int nodeId; // node ID
private final byte nodeId; // node ID
private final DataIDSource dataIdSource; // data ID source
private final Set<Device> devices; // registered devices
private final Map<Address, DeviceOutputQueue> outputQueues;
private final Map<CollectorKey, Collector> collectors; // collectors that store incoming L1 packets. Grouped by data
// ID and Node ID
private final L1DataProcessor l1DataProcessor; // reference to the upper layer

public Layer1(L1DataProcessor l1DataProcessor, int nodeId, DataIDSource dataIdSource) {
public Layer1(L1DataProcessor l1DataProcessor, byte nodeId, DataIDSource dataIdSource) {
this.outputQueues = new HashMap<Address, DeviceOutputQueue>();
this.strategies = new HashSet<L1Strategy>();
this.collectors = new HashMap<CollectorKey, Collector>();
Expand Down Expand Up @@ -88,12 +89,11 @@ public boolean unregisterL1Strategy(L1Strategy strategy) {
* @param device
* device to be registered
*/
public boolean registerDevice(Device device) {
public void registerDevice(Device device) {
if (device.getMTU() >= MINIMUM_DATA_TRANSMISSION_SIZE) {
devices.add(device);
return true;
} else {
return false;
Log.e("The device MTU is too small for the needs of jDEECo data transmission - minimum trasmission size is " + MINIMUM_DATA_TRANSMISSION_SIZE);
}
}

Expand All @@ -114,7 +114,8 @@ public boolean sendL2Packet(L2Packet l2Packet, Address address) {
*/
L2ReceivedInfo receivedInfo = l2Packet.receivedInfo;
int totalSize = l2Packet.getData().length;
int srcNode, dataId;
byte srcNode;
int dataId;
if (receivedInfo == null) {
srcNode = nodeId;
dataId = dataIdSource.createDataID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class L2ReceivedInfo {
/**
* Source jDEECo node
*/
public final int srcNode;
public final byte srcNode;

/**
* L2 Packet identification
Expand All @@ -39,7 +39,7 @@ public class L2ReceivedInfo {
* @param dataId
* Source data identification (Unique to L2 packet)
*/
public L2ReceivedInfo(Collection<L1Packet> srcFragments, int srcNode, int dataId) {
public L2ReceivedInfo(Collection<L1Packet> srcFragments, byte srcNode, int dataId) {
this.srcFragments = srcFragments;
this.srcNode = srcNode;
this.dataId = dataId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testL2PacketMarshalling() {
assertPayload(srcPacket.getObject());

// Create destination packet from source packet binary data
L2ReceivedInfo info = new L2ReceivedInfo(new LinkedList<L1Packet>(), 1, 1);
L2ReceivedInfo info = new L2ReceivedInfo(new LinkedList<L1Packet>(), (byte) 1, 1);
L2Packet dstPacket = new L2Packet(srcPacket.getData(), info);
dstPacket.setLayer(l2Layer);
assertPayload(dstPacket.getObject());
Expand Down Expand Up @@ -102,7 +102,7 @@ public void testL2PacketProcessing() {
L2Strategy strategy = Mockito.mock(L2Strategy.class);
l2Layer.registerL2Strategy(strategy);

L2ReceivedInfo info = new L2ReceivedInfo(new LinkedList<L1Packet>(), 1, 1);
L2ReceivedInfo info = new L2ReceivedInfo(new LinkedList<L1Packet>(), (byte) 1, 1);

// Create source packet (created with data and received packet info)
byte[] data = registry.marshall(L2PacketType.KNOWLEDGE, PAYLOAD);
Expand Down

0 comments on commit 090ef12

Please sign in to comment.