Commit 67681394 authored by Marc Schiller's avatar Marc Schiller
Browse files

More advances in models.

parent 421a101d
......@@ -24,7 +24,10 @@ import java.util.HashMap;
import de.tud.kom.p2psim.api.linklayer.mac.MacAddress;
import de.tud.kom.p2psim.api.linklayer.mac.PhyType;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.impl.linklayer.ModularLinkLayer;
import de.tud.kom.p2psim.impl.topology.views.fiveg.models.AbstractModel;
import de.tud.kom.p2psim.impl.topology.views.fiveg.models.ParameterType;
import de.tud.kom.p2psim.impl.util.IncrementableHashMap;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Time;
......@@ -34,10 +37,14 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationLi
public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
implements LocationListener {
final Integer DEFAULT_SEGMENT_ID = new Integer(-1);
private HashMap<MacAddress, Integer> currentPositions = new HashMap<>();
private IncrementableHashMap<Integer> segmentSize = new IncrementableHashMap<>();
IncrementableHashMap<Integer> usersPerSegment = new IncrementableHashMap<>();
HashMap<ParameterType, HashMap<Integer, AbstractModel>> models = new HashMap<>();
public ModelBasedSegmentDatabase() {
super(100, true);
......@@ -50,6 +57,30 @@ public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
return new ModelBasedEntry(segmentID);
}
public void setModel(AbstractModel model) {
ParameterType type = model.getParameterType();
if (type == null) {
throw new ConfigurationException(
"Model is not defined for any Parameter Type. Please specify the Type for this Model.");
}
Integer segmentID = new Integer(model.getSegmentID());
if (models.containsKey(type)) {
if (models.get(type).containsKey(segmentID)) {
throw new ConfigurationException(
"Conflicting models for " + type.toString()
+ " and SegementID " + segmentID.toString());
} else {
models.get(type).put(segmentID, model);
}
} else {
HashMap<Integer, AbstractModel> tmp = new HashMap<>();
tmp.put(segmentID, model);
models.put(type, tmp);
}
}
@Override
public void onLocationChanged(Host host, Location location) {
try {
......@@ -62,17 +93,16 @@ public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
if (currentPositions.containsKey(mac)) {
Integer oldSegmentID = currentPositions.get(mac);
// Host changed segment
if(oldSegmentID.compareTo(segmentID) != 0) {
segmentSize.decrement(oldSegmentID);
segmentSize.increment(segmentID);
if (oldSegmentID.compareTo(segmentID) != 0) {
usersPerSegment.decrement(oldSegmentID);
usersPerSegment.increment(segmentID);
currentPositions.put(mac, segmentID);
}
} else {
// New host
currentPositions.put(mac, segmentID);
segmentSize.increment(segmentID);
usersPerSegment.increment(segmentID);
}
System.out.println(segmentSize);
} catch (ComponentNotAvailableException e) {
// TODO Handle unknown host type
}
......@@ -98,8 +128,23 @@ public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
if (!getConnectivity()) {
return 1;
}
// TODO Return Drop Probability based on Model
return 0;
// TODO Up / Download?
Integer segmentID = new Integer(getSegmentID());
int usersInSegment = usersPerSegment.get(segmentID);
// Is there a Model for Droprate?
if (models.containsKey(ParameterType.DROPRATE)) {
// Is there a specific model for this segment
if(models.get(ParameterType.DROPRATE).containsKey(segmentID)) {
return models.get(ParameterType.DROPRATE).get(segmentID).getDouble(usersInSegment);
} else {
return models.get(ParameterType.DROPRATE).get(DEFAULT_SEGMENT_ID).getDouble(usersInSegment);
}
} else {
throw new ConfigurationException(
"No Model is defined for Droprate.");
}
}
@Override
......
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.views.fiveg.models;
import de.tud.kom.p2psim.api.scenario.ConfigurationException;
public abstract class AbstractModel {
private int segmentID = -1;
private ParameterType type;
public void setSegmentID(int segmentid) {
assert segmentid > 0;
this.segmentID = segmentid;
}
public int getSegmentID() {
return this.segmentID;
}
public void setParameterType(String param) {
param = param.toUpperCase();
try {
this.type = ParameterType.valueOf(param);
} catch (IllegalArgumentException e) {
throw new ConfigurationException(
"The Parameter " + param + " is unknown. Please select one of "
+ ParameterType.printTypes());
}
if(this.type == null) {
throw new ConfigurationException(
"The Parameter " + param + " is unknown. Please select one of "
+ ParameterType.printTypes());
}
}
public ParameterType getParameterType() {
return type;
}
public abstract long getLong(int users);
public abstract double getDouble(int users);
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.views.fiveg.models;
public class LinearModel extends AbstractModel {
private long a = 0;
private long b = 0;
public void setA(long a) {
this.a = a;
}
public void setB(long b) {
this.b = b;
}
@Override
public long getLong(int users) {
return this.a * users + this.b;
}
@Override
public double getDouble(int users) {
return (double) this.a * users + this.b;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.views.fiveg.models;
public enum ParameterType {
BANDWIDTH(),
DROPRATE(),
LATENCY();
public static String printTypes() {
ParameterType[] types = values();
String out = "";
for (int i = 0; i < types.length; i++) {
if (i > 0) {
out += ", ";
}
out += types[i].name();
}
return out;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment