From 676813949ae95a505bdfd7d5bd265449cc637248 Mon Sep 17 00:00:00 2001 From: Marc Schiller Date: Mon, 5 Dec 2016 20:17:41 +0100 Subject: [PATCH] More advances in models. --- .../fiveg/ModelBasedSegmentDatabase.java | 61 +++++++++++++++--- .../views/fiveg/models/AbstractModel.java | 62 +++++++++++++++++++ .../views/fiveg/models/LinearModel.java | 46 ++++++++++++++ .../views/fiveg/models/ParameterType.java | 39 ++++++++++++ 4 files changed, 200 insertions(+), 8 deletions(-) create mode 100644 src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/AbstractModel.java create mode 100644 src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/LinearModel.java create mode 100644 src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/ParameterType.java diff --git a/src/de/tud/kom/p2psim/impl/topology/views/fiveg/ModelBasedSegmentDatabase.java b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/ModelBasedSegmentDatabase.java index 9e01b406..ab814b6a 100644 --- a/src/de/tud/kom/p2psim/impl/topology/views/fiveg/ModelBasedSegmentDatabase.java +++ b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/ModelBasedSegmentDatabase.java @@ -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 currentPositions = new HashMap<>(); - private IncrementableHashMap segmentSize = new IncrementableHashMap<>(); + IncrementableHashMap usersPerSegment = new IncrementableHashMap<>(); + + HashMap> 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 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 diff --git a/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/AbstractModel.java b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/AbstractModel.java new file mode 100644 index 00000000..3264d554 --- /dev/null +++ b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/AbstractModel.java @@ -0,0 +1,62 @@ +/* + * 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 . + * + */ + +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); +} diff --git a/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/LinearModel.java b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/LinearModel.java new file mode 100644 index 00000000..64b3b5eb --- /dev/null +++ b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/LinearModel.java @@ -0,0 +1,46 @@ +/* + * 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 . + * + */ + +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; + } + +} diff --git a/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/ParameterType.java b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/ParameterType.java new file mode 100644 index 00000000..7816074a --- /dev/null +++ b/src/de/tud/kom/p2psim/impl/topology/views/fiveg/models/ParameterType.java @@ -0,0 +1,39 @@ +/* + * 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 . + * + */ + +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; + } +} -- GitLab