📄 simpletransitstubnetwork.java
字号:
/* * @(#)$Id: SimpleTransitStubNetwork.java,v 1.24 2004/07/02 23:59:22 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package simulator.schedulers.network.topology.transitstub;import java.net.InetAddress;import java.util.HashMap;import java.util.Random;import org.apache.log4j.Logger;import services.Output;import simulator.schedulers.network.NetworkTopology;import util.logging.LogMessage;/** * Class SimpleTransitStubNetwork * */public class SimpleTransitStubNetwork implements NetworkTopology { private static Logger logger = Logger.getLogger(SimpleTransitStubNetwork.class); private HashMap inetToVertex = new HashMap(); private HashMap bandwidthMap = new HashMap(); /** Precomputed latency */ private HashMap precomputedLatency = new HashMap(); /** Precomputed bandwidth */ private HashMap precomputedBandwidth = new HashMap(); private double intraTransitLatency, stubTransitLatency, intraStubLatency; private double loss; private InetAddress nextHop; private Path nextPath; private TransitStub ts; private String filename; private boolean topologyInitialize = false; /** Computed latency */ private double computedLatency; /** Computed bandwidth */ private double computedBandwidth; /** Default bandwidth */ private double defaultBandwidth; private boolean cacheAllPaths; /** * Constructor SimpleTransitStubNetwork * * @param filename * @param intraTransitLatency * @param stubTransitLatency * @param intraStubLatency * @param loss * @param random * @param defaultBandwidth * @param cacheAllPaths */ public SimpleTransitStubNetwork(String filename, double intraTransitLatency, double stubTransitLatency, double intraStubLatency, double loss, Random random, double defaultBandwidth, boolean cacheAllPaths) { this.cacheAllPaths = cacheAllPaths; this.loss = loss; this.filename = filename; this.intraTransitLatency = intraTransitLatency; this.stubTransitLatency = stubTransitLatency; this.intraStubLatency = intraStubLatency; // initialize the transit stub ts = new TransitStub(filename, false, false, random); this.defaultBandwidth = defaultBandwidth; } /** * Method computeNextHop * * @param source * @param destination */ public void computeNextHop(InetAddress source, InetAddress destination) { Vertex sourceVertex = (Vertex) inetToVertex.get(source); Vertex destinationVertex = (Vertex) inetToVertex.get(destination); if ((sourceVertex == null) || (destinationVertex == null)) { if (Output.debuggingEnabled) { logger.debug(new LogMessage(new Object[]{ "Vertices are not assigned. ", String.valueOf(sourceVertex), " ", String.valueOf(destinationVertex)})); } return; } nextHop = destination; if (sourceVertex.equals(destinationVertex)) { // special case if (source.equals(destination)) { computedLatency = 0; } else { computedLatency = intraStubLatency; } Float curBandwidth = (Float) bandwidthMap.get(sourceVertex.getVertexNumber()); if (curBandwidth != null) { computedBandwidth = curBandwidth.floatValue(); } else { computedBandwidth = (float) defaultBandwidth; } return; } // computed the latency and bandwidth bandwidth nextPath = ts.getPath(sourceVertex, destinationVertex, cacheAllPaths); // ArrayList Object[] pathList = nextPath.getPath(); Vertex prevVertex = sourceVertex; if (precomputedLatency.get(nextPath) == null) { computedLatency = 0; for (int k = 1; k < pathList.length; k++) { Vertex nextVertex = (Vertex) pathList[k]; // figure out what link type // are we in the same stub? if ((nextVertex.isStub() == true) && (prevVertex.isStub() == true)) { computedLatency += intraStubLatency; } else if ((nextVertex.isStub() == false) && (prevVertex.isStub() == false)) { computedLatency += intraTransitLatency; } else { computedLatency += stubTransitLatency; } prevVertex = nextVertex; } precomputedLatency.put(nextPath, new Float(computedLatency)); } else { computedLatency = ((Float) precomputedLatency.get(nextPath)).floatValue(); } if (precomputedBandwidth.get(nextPath) == null) { Vertex firstNode = (Vertex) pathList[0]; Float sourceBandwidth = (Float) bandwidthMap.get(firstNode.getVertexNumber()); computedBandwidth = sourceBandwidth.floatValue(); for (int k = 1; k < pathList.length; k++) { Vertex v = (Vertex) pathList[k]; Float curBandwidth = (Float) bandwidthMap.get(v.getVertexNumber()); float bandwidthOfVertex; if (curBandwidth != null) { bandwidthOfVertex = curBandwidth.floatValue(); } else { bandwidthOfVertex = (float) defaultBandwidth; } computedBandwidth = Math.min(computedBandwidth, bandwidthOfVertex); } precomputedBandwidth.put(nextPath, new Float(computedBandwidth)); } else { computedBandwidth = ((Float) precomputedBandwidth.get(nextPath)).floatValue(); } } /** * Method initializeTopology */ public void initializeTopology() { ts.generateShortestPaths(); topologyInitialize = true; } /** * Method linkNodeToTopology * * @param node * @param bandwidth * @param multipleNodes */ public void linkNodeToTopology(InetAddress node, double bandwidth, boolean multipleNodes) { // assign to a random node Vertex v = ts.getRandomVertex(multipleNodes); if (v == null) { logger.error(new LogMessage(new Object[]{ "Run out of stub nodes to assign"})); return; } // get the mappings inetToVertex.put(node, v); bandwidthMap.put(v.getVertexNumber(), new Float(bandwidth)); } /** * Method getNextHop * @return */ public InetAddress getNextHop() { return nextHop; } /** * Method getNextPath * @return */ public Path getNextPath() { return nextPath; } /** * Method getLatency * @return */ public double getLatency() { return computedLatency; } /** * Method getLoss * @return */ public double getLoss() { return loss; } /** * Method getMaxBandwidth * @return */ public double getMaxBandwidth() { // return the narrowest link return computedBandwidth; } /** * Method numVertices * @return */ public short numVertices() { return ts.numVertices(); } /** * Method main * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Random random = new Random(0); SimpleTransitStubNetwork stsn = new SimpleTransitStubNetwork(args[0], 50, 10, 2, 0, random, 10, true); double fract = 1; int numVertices = (int) (fract * stsn.numVertices()); InetAddress[] inetAddresses = new InetAddress[numVertices]; for (int k = 0; k < numVertices; k++) { byte[] quads = new byte[4]; random.nextBytes(quads); // Attempt to create the actual address inetAddresses[k] = InetAddress.getByAddress(quads); stsn.linkNodeToTopology(inetAddresses[k], 10, true); } double totalLatency = 0, totalCount = 0; for (int k = 0; k < numVertices; k++) { for (int j = 0; j < numVertices; j++) { int randomVal = k; // (int) (random.nextDouble() * numVertices); int randomVal1 = j; // (int) (random.nextDouble() * numVertices); totalCount += 1; stsn.computeNextHop(inetAddresses[randomVal], inetAddresses[randomVal1]); totalLatency += stsn.getLatency(); } } logger.info("Average latency " + totalLatency / totalCount); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -