Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/network' into jDEECo-3
Browse files Browse the repository at this point in the history
  • Loading branch information
vladamatena committed Mar 13, 2015
2 parents e176156 + 6a3171f commit 7a98115
Show file tree
Hide file tree
Showing 24 changed files with 1,144 additions and 227 deletions.
1 change: 1 addition & 0 deletions jdeeco-network-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target/
/.classpath
/logs/
15 changes: 15 additions & 0 deletions jdeeco-network-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Expand All @@ -48,5 +57,11 @@
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ public List<Class<? extends DEECoPlugin>> getDependencies() {
@Override
public void init(DEECoContainer container) {
// Initialize Layer 1
// TODO: Data id source and node id should have been set properly
l1 = new Layer1(l2, (byte) 0, DefaultDataIDSource.getInstance());
l1 = new Layer1((byte) container.getId(), DefaultDataIDSource.getInstance(), container.getRuntimeFramework()
.getScheduler());

// Initialize Layer 2
l2 = new Layer2(l1, registery);
l2 = new Layer2(registery);

l1.setL1DataProcessor(l2);
l2.setL2PacketSender(l1);

// Add default marshaler for knowledge
registery.registerMarshaller(L2PacketType.KNOWLEDGE, new SerializingMarshaller());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*
*/
public class IPAddress extends Address {

public final String ipAddress;

public IPAddress(String ipAddress) {
Expand All @@ -24,4 +23,9 @@ public boolean equals(Object obj) {
IPAddress other = (IPAddress) obj;
return other.ipAddress.equals(ipAddress);
}

@Override
public int hashCode() {
return ipAddress.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@
*
*/
public class MANETBroadcastAddress extends Address {
public final static MANETBroadcastAddress BROADCAST = new MANETBroadcastAddress("broadcast");

private String address;

public final static MANETBroadcastAddress INSTANCE = new MANETBroadcastAddress();

private MANETBroadcastAddress() {
// Prevents instantiation from outside the class
public MANETBroadcastAddress(String address) {
this.address = address;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
return getClass() == obj.getClass();
public String getAddress() {
return address;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package cz.cuni.mff.d3s.jdeeco.network.device;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cz.cuni.mff.d3s.deeco.runtime.DEECoContainer;
import cz.cuni.mff.d3s.deeco.runtime.DEECoPlugin;
import cz.cuni.mff.d3s.deeco.scheduler.Scheduler;
import cz.cuni.mff.d3s.deeco.task.CustomStepTask;
import cz.cuni.mff.d3s.deeco.task.Task;
import cz.cuni.mff.d3s.deeco.task.TimerTask;
import cz.cuni.mff.d3s.deeco.task.TimerTaskListener;
import cz.cuni.mff.d3s.jdeeco.network.Network;
import cz.cuni.mff.d3s.jdeeco.network.address.Address;
import cz.cuni.mff.d3s.jdeeco.network.address.MANETBroadcastAddress;
import cz.cuni.mff.d3s.jdeeco.network.l1.Layer1;
import cz.cuni.mff.d3s.jdeeco.network.l1.ReceivedInfo;

/**
* Loop-back broadcast plug-in
*
* Can be initialized by more DEECo run-times at the same time. Packets send are then delivered (instantly or with
* delay) to all of them.
*
* @author Vladimir Matena <matena@d3s.mff.cuni.cz>
*
*/
public class BroadcastLoopback implements DEECoPlugin {
final int PACKET_SIZE = 128;
final long constantDelay;

Scheduler scheduler;

// Loop devices this loop-back network is registered with
private Set<LoopDevice> loops = new HashSet<>();

/**
* Loop device used to provide broadcast device to layer 1
*/
private class LoopDevice extends Device {
public Layer1 layer1;
public MANETBroadcastAddress address;

private String id;

public LoopDevice(String id, Layer1 layer1) {
this.id = id;
this.address = new MANETBroadcastAddress(getId());
this.layer1 = layer1;
}

@Override
public String getId() {
return id;
}

@Override
public int getMTU() {
return PACKET_SIZE;
}

@Override
public boolean canSend(Address address) {
return address instanceof MANETBroadcastAddress;
}

@Override
public void send(byte[] data, Address addressNotUsed) {
Task task = new CustomStepTask(scheduler,
new DeliveryListener(constantDelay, new PacketWrapper(data, this)));
BroadcastLoopback.this.scheduler.addTask(task);
}
}

/**
* Packet package used to carry information about packet
*/
private final class PacketWrapper {
public final byte[] data;
public final LoopDevice source;

PacketWrapper(byte[] data, LoopDevice source) {
this.data = data;
this.source = source;
}
}

/**
* Listener used to delayed delivery of data
*/
private class DeliveryListener implements TimerTaskListener {
final private PacketWrapper packet;

public DeliveryListener(long delay, PacketWrapper packet) {
this.packet = packet;
}

@Override
public void at(long time, Object triger) {
BroadcastLoopback.this.sendToAll(packet);
}

@Override
public TimerTask getInitialTask(Scheduler scheduler) {
return null;
}
}

/**
* Constructs loop-back broadcast
*
* @param constantDelay
* Delay between sending and delivering the packets
*/
public BroadcastLoopback(long constantDelay) {
this.constantDelay = constantDelay;
}

/**
* Constructs loop-back broadcast
*
* Delivers packets immediately
*/
public BroadcastLoopback() {
this(0);
}

/**
* Sends packet to all registered loop devices
*
* @param packet
* Container containing packet data and sender information
*/
public void sendToAll(PacketWrapper packet) {
for (LoopDevice loop : loops) {
loop.layer1.processL0Packet(packet.data, packet.source, new ReceivedInfo(packet.source.address));
}
}

@Override
public List<Class<? extends DEECoPlugin>> getDependencies() {
return Arrays.asList(Network.class);
}

@Override
public void init(DEECoContainer container) {
scheduler = container.getRuntimeFramework().getScheduler();
Layer1 l1 = container.getPluginInstance(Network.class).getL1();
LoopDevice loop = new LoopDevice(String.valueOf(container.getId()), l1);
l1.registerDevice(loop);
loops.add(loop);
}
}
Loading

0 comments on commit 7a98115

Please sign in to comment.