Skip to content
Snippets Groups Projects
Commit 2dda121d authored by Britta Heymann's avatar Britta Heymann
Browse files

Merge remote-tracking branch 'remotes/origin/master' into evaluate-warmup-time

parents 6cee0d8a 906e60e7
No related branches found
No related tags found
No related merge requests found
Pipeline #
Showing
with 199 additions and 58 deletions
# Add this file as argument if you want to use ProphetRouter
Group.router = ProphetRouter
ProphetRouter.secondsInTimeUnit = 30
\ No newline at end of file
......@@ -303,7 +303,7 @@ public class DatabaseApplication extends Application implements DisasterDataCrea
List<Tuple<DisasterData, Double>> subsetToSent = data.subList(i, firstIndexNotToSent);
DataMessage message = new DataMessage(
this.host, unknownReceiver,
this.createMessageId(subsetToSent), subsetToSent,
DatabaseApplication.createMessageId(subsetToSent), subsetToSent,
DATA_MESSAGE_PRIORITY);
message.setAppID(APP_ID);
messages.add(message);
......@@ -311,7 +311,7 @@ public class DatabaseApplication extends Application implements DisasterDataCrea
return messages;
}
private String createMessageId(List<Tuple<DisasterData, Double>> subsetToSent) {
private static String createMessageId(List<Tuple<DisasterData, Double>> subsetToSent) {
List<DisasterData> dataList = new ArrayList<>(subsetToSent.size());
for (Tuple<DisasterData, Double> item : subsetToSent) {
dataList.add(item.getKey());
......
......@@ -10,6 +10,8 @@ import core.World;
* Created by Britta Heymann on 15.02.2017.
*/
public class BroadcastCreateEvent extends MessageEvent {
private static final long serialVersionUID = 1;
private int size;
private int responseSize;
......
......@@ -271,6 +271,7 @@ public class DisasterRouter extends ActiveRouter {
* Subclasses that are interested of the event may want to override this.
* @param con The connection whose transfer was finalized
*/
@Override
protected void transferDone(Connection con) {
super.transferDone(con);
addMessageAndHostToHistory(con.getMessage(), con.getOtherNode(getHost()));
......
......@@ -12,6 +12,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import core.BroadcastMessage;
import core.DataMessage;
import core.MulticastMessage;
import routing.util.DatabaseApplicationUtil;
import routing.util.RoutingInfo;
import util.Tuple;
......@@ -55,7 +59,7 @@ public class ProphetRouter extends ActiveRouter {
private double beta;
/** delivery predictabilities */
private Map<DTNHost, Double> preds;
private Map<Integer, Double> preds;
/** last delivery predictability update (sim)time */
private double lastAgeUpdate;
......@@ -93,7 +97,7 @@ public class ProphetRouter extends ActiveRouter {
* Initializes predictability hash
*/
private void initPreds() {
this.preds = new HashMap<DTNHost, Double>();
this.preds = new HashMap<>();
}
@Override
......@@ -115,7 +119,43 @@ public class ProphetRouter extends ActiveRouter {
private void updateDeliveryPredFor(DTNHost host) {
double oldValue = getPredFor(host);
double newValue = oldValue + (1 - oldValue) * P_INIT;
preds.put(host, newValue);
preds.put(host.getAddress(), newValue);
}
/**
* Returns the current prediction (P) value for a message or 0 if no entry for any recipient exists.
* @param message Message to find P for. Either a one-to-one message or a multicast.
* @return The current P value. In case of multicast, the maximum P value of remaining recipients.
*/
private double getPredFor(Message message) {
switch (message.getType()) {
case ONE_TO_ONE:
return this.getPredFor(message.getTo());
case MULTICAST:
MulticastMessage multicast = (MulticastMessage)message;
return this.getMaxPredFor(multicast.getRemainingRecipients());
default:
throw new IllegalArgumentException(
"No delivery predictability for messages of type " + message.getType() + " defined!");
}
}
/**
* Returns the maximum prediction (P) value for all hosts matching the provided addresses. If no such P values
* exist, returns 0.
* @param addresses The addresses to check.
* @return The maximum P value.
*/
private double getMaxPredFor(Collection<Integer> addresses) {
// Make sure preds are updated once before getting.
this.ageDeliveryPreds();
double maxPred = 0;
for (int address : addresses) {
double predForAddress = this.preds.getOrDefault(address, 0D);
maxPred = Math.max(maxPred, predForAddress);
}
return maxPred;
}
/**
......@@ -125,13 +165,19 @@ public class ProphetRouter extends ActiveRouter {
* @return the current P value
*/
public double getPredFor(DTNHost host) {
ageDeliveryPreds(); // make sure preds are updated before getting
if (preds.containsKey(host)) {
return preds.get(host);
}
else {
return 0;
return this.getPredFor(host.getAddress());
}
/**
* Returns the current prediction (P) value for a host address or 0 if entry for
* the host doesn't exist.
* @param address The host address to look the P for
* @return the current P value
*/
private double getPredFor(Integer address) {
// make sure preds are updated before getting
this.ageDeliveryPreds();
return preds.getOrDefault(address, 0D);
}
/**
......@@ -146,11 +192,11 @@ public class ProphetRouter extends ActiveRouter {
" with other routers of same type";
double pForHost = getPredFor(host); // P(a,b)
Map<DTNHost, Double> othersPreds =
Map<Integer, Double> othersPreds =
((ProphetRouter)otherRouter).getDeliveryPreds();
for (Map.Entry<DTNHost, Double> e : othersPreds.entrySet()) {
if (e.getKey() == getHost()) {
for (Map.Entry<Integer, Double> e : othersPreds.entrySet()) {
if (e.getKey().equals(getHost().getAddress())) {
continue; // don't add yourself
}
......@@ -175,7 +221,7 @@ public class ProphetRouter extends ActiveRouter {
}
double mult = Math.pow(GAMMA, timeDiff);
for (Map.Entry<DTNHost, Double> e : preds.entrySet()) {
for (Map.Entry<Integer, Double> e : preds.entrySet()) {
e.setValue(e.getValue()*mult);
}
......@@ -186,7 +232,7 @@ public class ProphetRouter extends ActiveRouter {
* Returns a map of this router's delivery predictions
* @return a map of this router's delivery predictions
*/
private Map<DTNHost, Double> getDeliveryPreds() {
private Map<Integer, Double> getDeliveryPreds() {
ageDeliveryPreds(); // make sure the aging is done
return this.preds;
}
......@@ -206,6 +252,16 @@ public class ProphetRouter extends ActiveRouter {
tryOtherMessages();
}
/**
* Checks whether this router has anything to send out.
*
* @return Whether or not the router has anything to send out.
*/
@Override
protected boolean hasNothingToSend() {
return DatabaseApplicationUtil.hasNoMessagesToSend(this);
}
/**
* Tries to send all other messages to all connected hosts ordered by
* their delivery probability
......@@ -219,6 +275,7 @@ public class ProphetRouter extends ActiveRouter {
/* for all connected hosts collect all messages that have a higher
probability of delivery by the other host */
List<Connection> availableConnections = new ArrayList<>();
for (Connection con : getConnections()) {
DTNHost other = con.getOtherNode(getHost());
ProphetRouter othRouter = (ProphetRouter)other.getRouter();
......@@ -226,18 +283,27 @@ public class ProphetRouter extends ActiveRouter {
if (othRouter.isTransferring()) {
continue; // skip hosts that are transferring
}
availableConnections.add(con);
for (Message m : msgCollection) {
if (othRouter.hasMessage(m.getId())) {
continue; // skip messages that the other one has
}
if (othRouter.getPredFor(m.getTo()) > getPredFor(m.getTo())) {
if (othRouter.hasMessage(m.getId()) || m instanceof BroadcastMessage) {
// Ignore both messages that the other one has and all broadcast messages.
// (Broadcasts should be sent via exchangeDeliverableMessages.)
// The latter check may not be caught by the former because of caching (direct messages may be
// sent belatedly; see explanation at ActiveRouter#cachedMessagesForConnected.).
continue;
}
if (othRouter.getPredFor(m) > getPredFor(m)) {
// the other node has higher probability of delivery
messages.add(new Tuple<Message, Connection>(m,con));
}
}
}
/* For all available connections, add useful data messages. */
messages.addAll(DatabaseApplicationUtil.wrapUsefulDataIntoMessages(
this, this.getHost(), availableConnections));
if (messages.size() == 0) {
return null;
}
......@@ -248,35 +314,42 @@ public class ProphetRouter extends ActiveRouter {
}
/**
* Comparator for Message-Connection-Tuples that orders the tuples by
* their delivery probability by the host on the other side of the
* connection (GRTRMax)
* Comparator for Message-Connection-Tuples that orders the tuples by the utility computed by
* {@link #computeUtility(Tuple)}, higher utilities first.
*/
private class TupleComparator implements Comparator
<Tuple<Message, Connection>> {
public int compare(Tuple<Message, Connection> tuple1,
Tuple<Message, Connection> tuple2) {
// delivery probability of tuple1's message with tuple1's connection
double p1 = ((ProphetRouter)tuple1.getValue().
getOtherNode(getHost()).getRouter()).getPredFor(
tuple1.getKey().getTo());
// -"- tuple2...
double p2 = ((ProphetRouter)tuple2.getValue().
getOtherNode(getHost()).getRouter()).getPredFor(
tuple2.getKey().getTo());
// bigger probability should come first
if (p2-p1 == 0) {
/* equal probabilities -> let queue mode decide */
double utility1 = this.computeUtility(tuple1);
double utility2 = this.computeUtility(tuple2);
// bigger utility should come first
int utilityComparison = (-1) * Double.compare(utility1, utility2);
if (utilityComparison == 0) {
/* equal utilities -> let queue mode decide */
return compareByQueueMode(tuple1.getKey(), tuple2.getKey());
}
else if (p2-p1 < 0) {
return -1;
return utilityComparison;
}
else {
return 1;
/**
* Computes a utility value for a Message-Connection tuple. This is
* - either the delivery probability by the host on the other side of the connection (GRTRMax) if the message is
* not a data message, or
* - the data message's utility.
* @param tuple Tuple to compute utility for.
* @return The tuple's utility.
*/
private double computeUtility(Tuple<Message, Connection> tuple) {
Message message = tuple.getKey();
if (message instanceof DataMessage) {
return ((DataMessage) message).getUtility();
}
DTNHost neighbor = tuple.getValue().getOtherNode(getHost());
return ((ProphetRouter)neighbor.getRouter()).getPredFor(message);
}
}
......@@ -287,12 +360,12 @@ public class ProphetRouter extends ActiveRouter {
RoutingInfo ri = new RoutingInfo(preds.size() +
" delivery prediction(s)");
for (Map.Entry<DTNHost, Double> e : preds.entrySet()) {
DTNHost host = e.getKey();
for (Map.Entry<Integer, Double> e : preds.entrySet()) {
Integer hostAddress = e.getKey();
Double value = e.getValue();
ri.addMoreInfo(new RoutingInfo(String.format("%s : %.6f",
host, value)));
hostAddress, value)));
}
top.addMoreInfo(ri);
......
package test;
import core.Group;
import core.SimClock;
import org.junit.After;
import org.junit.Before;
......@@ -53,6 +54,16 @@ public abstract class AbstractReportTest {
this.outputFile.delete();
}
@After
public void resetGroups() {
Group.clearGroups();
}
@After
public void resetSimClock() {
SimClock.reset();
}
/**
* Checks that the report correctly handles the warm up time as set by the {@link Report#WARMUP_S} setting.
*/
......
......@@ -84,6 +84,7 @@ public abstract class AbstractRouterTest extends TestCase {
protected void tearDown() throws Exception {
super.tearDown();
Settings.init(null);
SimClock.reset();
}
protected void setRouterProto(MessageRouter r) {
......
......@@ -24,6 +24,11 @@ public class ActivenessHandlerTest extends TestCase {
moreTimes);
ah = new ActivenessHandler(ts);
clock = SimClock.getInstance();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
SimClock.reset();
}
......
......@@ -32,7 +32,6 @@ public class ConnectionTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
SimClock.reset();
clock.setTime(START_TIME);
TestSettings testSettings = new TestSettings();
testSettings.setNameSpace(TestUtils.IFACE_NS);
......@@ -65,6 +64,12 @@ public class ConnectionTest extends TestCase {
conCount = 3;
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
SimClock.reset();
}
private void con(DTNHost from, DTNHost to) {
c[index] = new CBRConnection(from, from.getInterfaces().get(0), to, to.getInterfaces().get(0), speed[index]);
index++;
......
......@@ -72,6 +72,12 @@ public class ContactTimesReportTest extends TestCase {
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
SimClock.reset();
}
private void generateConnections(TestUtils utils) {
Coord c1 = new Coord(0,0);
Coord c2 = new Coord(1,0);
......
......@@ -44,6 +44,12 @@ public class DistanceDelayReportTest extends TestCase {
this.utils = new TestUtils(null, ml, ts);
}
@Override
protected void tearDown() throws Exception{
super.tearDown();
SimClock.reset();
}
@Test
public void testMessageTransferred() throws IOException {
DTNHost h1 = utils.createHost(new Coord(0,0));
......
......@@ -79,6 +79,12 @@ public class ExternalMovementTest extends TestCase {
clock.setTime(0);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
SimClock.reset();
}
public void testMovement() throws Exception {
setUpUsing(INPUT);
......
......@@ -5,7 +5,7 @@ import core.DTNHost;
import core.Group;
import core.MessageListener;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import java.util.ArrayList;
......@@ -21,8 +21,8 @@ public class GroupTest {
private TestUtils utils = new TestUtils(new ArrayList<ConnectionListener>(),
new ArrayList<MessageListener>(), new TestSettings());
@Before
public void setUp(){
@After
public void tearDown(){
Group.clearGroups();
DTNHost.reset();
}
......
......@@ -86,11 +86,12 @@ public class ImmediateMessageDelayReportTest extends AbstractReportTest {
}
/**
* Resets the clock to 0.
* Resets the clock to 0 and clears all groups.
*/
@After
public void resetClock() {
public void tearDown() {
this.clock.setTime(0);
Group.clearGroups();
}
/***
......
......@@ -8,6 +8,7 @@ import core.Group;
import core.Message;
import core.MessageListener;
import core.MulticastMessage;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -47,7 +48,6 @@ public class MessageRouterTest {
@Before
public void setUp() {
Group.clearGroups();
TestSettings ts = new TestSettings();
ts.putSetting(MessageRouter.B_SIZE_S, ""+BUFFER_SIZE);
List<MessageListener> mListener = new ArrayList<>(1);
......@@ -69,6 +69,11 @@ public class MessageRouterTest {
this.multicast = new MulticastMessage(sender, g,"G",DEFAULT_MESSAGE_SIZE);
}
@After
public void tearDown() {
Group.clearGroups();
}
@Test
public void testMessageTransferredForNonRecipientPutsMessageIntoBufferButNotIntoDelivered() {
DTNHost nonRecipient = this.utils.createHost();
......
......@@ -41,7 +41,6 @@ public class MessageStatsReportTest {
public void init() throws IOException{
SimScenario.reset();
DTNHost.reset();
Group.clearGroups();
Settings.init(null);
java.util.Locale.setDefault(java.util.Locale.US);
......@@ -68,6 +67,7 @@ public class MessageStatsReportTest {
// Cleanup to make further tests with other settings possible.
SimScenario.reset();
DTNHost.reset();
Group.clearGroups();
}
private void playScenario() {
......
......@@ -48,6 +48,12 @@ public class MessageTest extends TestCase {
msg.setTtl(10);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
SimClock.reset();
}
@Test
public void testGetTtl() {
assertEquals(10, msg.getTtl());
......
......@@ -8,6 +8,7 @@ import core.DisasterData;
import core.Group;
import core.Message;
import core.MulticastMessage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import routing.util.MessageTransferAcceptPolicy;
......@@ -53,7 +54,6 @@ public class MessageTransferAcceptPolicyTest {
@Before
public void init() {
Group.clearGroups();
DTNHost.reset();
this.settings = new TestSettings();
TestUtils utils = new TestUtils(
......@@ -83,6 +83,11 @@ public class MessageTransferAcceptPolicyTest {
this.setAcceptableRecipientAddress(this.recipient.getAddress());
}
@After
public void tearDown() {
Group.clearGroups();
}
@Test
public void testAcceptSendingReturnsTrueForMessageAdheringToSimplePolicies() {
MessageTransferAcceptPolicy policy = new MessageTransferAcceptPolicy(this.settings);
......
......@@ -6,6 +6,7 @@ import core.Message;
import core.MulticastMessage;
import input.ExternalEvent;
import input.MulticastCreateEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -20,11 +21,15 @@ public class MulticastCreateEventTest extends AbstractMessageCreateEventTest {
@Before
public void createGroup(){
Group.clearGroups();
Group g = Group.createGroup(0);
g.addHost(creator);
}
@After
public void tearDown() {
Group.clearGroups();
}
@Test
public void testProcessEventCreatesMulticastMessageWithCorrectGroupAddress() {
int groupAddress = 0;
......
......@@ -68,8 +68,6 @@ public class MulticastDeliveryReportTest extends AbstractReportTest {
// Let base do the basic report setup.
super.setUp();
Group.clearGroups();
// Set clock to 0.
this.clock.setTime(0);
......@@ -84,8 +82,9 @@ public class MulticastDeliveryReportTest extends AbstractReportTest {
}
@After
public void resetClock(){
public void tearDown(){
clock.setTime(0);
Group.clearGroups();
}
@Test
......
  • Contributor

    SonarQube analysis reported no issues.

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment