Commit 5e41d97a authored by Björn Richerzhagen's avatar Björn Richerzhagen
Browse files

Added getTopology() helpers to the GlobalOracle

parent ecd21692
......@@ -22,16 +22,26 @@ package de.tud.kom.p2psim.impl.util.oracle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tudarmstadt.maki.simonstrator.api.Graphs;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.common.graph.DirectedEdge;
import de.tudarmstadt.maki.simonstrator.api.common.graph.Graph;
import de.tudarmstadt.maki.simonstrator.api.common.graph.Node;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.core.OracleComponent;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetID;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.topology.TopologyID;
import de.tudarmstadt.maki.simonstrator.api.component.topology.TopologyProvider;
/**
* This class gives access to the hosts of the scenario. To work, it has to be
......@@ -172,6 +182,92 @@ public class GlobalOracle implements OracleComponent {
public boolean isSimulation() {
return true;
}
/**
* Returns a global view of the topology for the specified mechanism. The
* mechanism must be a HostComponent that is registered at the local host.
* Otherwise, this method will not be able to find the local mechanism
* objects.
*
* @param component
* @param identifier
* @return
*/
public static <T extends TopologyProvider> Graph getTopology(
Class<T> component, TopologyID identifier) {
HashSet<DirectedEdge> edges = new LinkedHashSet<>();
HashSet<Node> nodes = new LinkedHashSet<Node>();
LinkedList<TopologyProvider> allProviders = new LinkedList<>();
for (SimHost host : getHosts()) {
try {
TopologyProvider topologyProvider = host
.getComponent(component);
Node providerNode = topologyProvider.getNode(identifier);
nodes.add(providerNode);
allProviders.add(topologyProvider);
} catch (ComponentNotAvailableException e) {
// if the component is not available on the host, we can't do
// anything about it
// no reason to crash the simulation as this might be the case
// in various scenarios
}
}
for (TopologyProvider topologyProvider : allProviders) {
Set<de.tudarmstadt.maki.simonstrator.api.common.graph.DirectedEdge> neighbors = topologyProvider
.getNeighbors(identifier);
edges.addAll(neighbors);
}
Graph graph = Graphs.createGraph();
for(Node node : nodes){
graph.add(node);
}
for(DirectedEdge edge : edges) {
graph.add(edge);
}
return graph;
}
/**
* Returns available topology identifiers for the given component. Throws an
* {@link ComponentNotAvailableException} if the component is not available
* on any node in the network. Assumes that all instances of a given
* component class provide the same topology identifiers.
*
* @throws ComponentNotAvailableException
*/
public static <T extends TopologyProvider> Iterable<TopologyID> getTopologyIdentifiers(
Class<T> component) throws ComponentNotAvailableException {
// iterate over all the hosts, find one host that contains the given
// component class and ask this component about available topologies
for (SimHost host : getHosts()) {
try {
TopologyProvider topologyProvider = host
.getComponent(component);
return topologyProvider.getTopologyIdentifiers();
} catch (ComponentNotAvailableException e) {
// if the component is not available on the host, we can't do
// anything about it
// no reason to crash the simulation as this might be the case
// in various scenarios
}
}
throw new ComponentNotAvailableException();
}
/**
* Checks whether the host with the given NetID is online using a global
......
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