Commit 421a101d authored by Marc Schiller's avatar Marc Schiller
Browse files

Added IncrementableHashMap.

parent b0b1e4bc
......@@ -20,9 +20,12 @@
package de.tud.kom.p2psim.impl.topology.views.fiveg;
import de.tud.kom.p2psim.api.linklayer.mac.MacLayer;
import de.tud.kom.p2psim.impl.linklayer.mac.AbstractMacLayer;
import de.tud.kom.p2psim.impl.linklayer.mac.SimpleMacLayer;
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.impl.linklayer.ModularLinkLayer;
import de.tud.kom.p2psim.impl.util.IncrementableHashMap;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
......@@ -32,6 +35,10 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.LocationLi
public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
implements LocationListener {
private HashMap<MacAddress, Integer> currentPositions = new HashMap<>();
private IncrementableHashMap<Integer> segmentSize = new IncrementableHashMap<>();
public ModelBasedSegmentDatabase() {
super(100, true);
super.setSupportCloudlets(true);
......@@ -46,10 +53,28 @@ public class ModelBasedSegmentDatabase extends AbstractGridBasedTopologyDatabase
@Override
public void onLocationChanged(Host host, Location location) {
try {
System.out.println(host.getComponent(SimpleMacLayer.class).getMacAddress());
// Get MAC Address and the SegementID the user is now in
MacAddress mac = host.getComponent(ModularLinkLayer.class)
.getMac(PhyType.UMTS).getMacAddress();
Integer segmentID = new Integer(this.getSegmentID(
location.getLongitude(), location.getLatitude()));
// Check if it is a new host
if (currentPositions.containsKey(mac)) {
Integer oldSegmentID = currentPositions.get(mac);
// Host changed segment
if(oldSegmentID.compareTo(segmentID) != 0) {
segmentSize.decrement(oldSegmentID);
segmentSize.increment(segmentID);
currentPositions.put(mac, segmentID);
}
} else {
// New host
currentPositions.put(mac, segmentID);
segmentSize.increment(segmentID);
}
System.out.println(segmentSize);
} catch (ComponentNotAvailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// TODO Handle unknown host type
}
}
......
/*
* 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.util;
import java.util.HashMap;
public class IncrementableHashMap<K> {
private HashMap<K, Integer> storage;
public IncrementableHashMap() {
storage = new HashMap<>();
}
public int get(K key) {
return storage.get(key).intValue();
}
public void increment(K key) {
if(storage.containsKey(key)) {
Integer tmp = storage.get(key);
storage.put(key, new Integer(tmp.intValue() + 1));
} else {
storage.put(key, new Integer(1));
}
}
public void decrement(K key) {
if(storage.containsKey(key)) {
Integer tmp = storage.get(key);
if(tmp.compareTo(new Integer(1)) == 0) {
storage.put(key, new Integer(0));
} else {
storage.put(key, new Integer(tmp.intValue() - 1));
}
} else {
storage.put(key, new Integer(0));
}
}
public boolean containsKey(K key) {
return storage.containsKey(key);
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(K key: storage.keySet()) {
sb.append(key.toString() + ": " + storage.get(key).toString() + "\n");
}
return sb.toString();
}
}
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