📄 groupnetwork.java
字号:
/* * @(#)$Id: GroupNetwork.java,v 1.2 2005/01/17 22:36:31 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.group;import java.io.BufferedReader;import java.io.FileReader;import java.net.InetAddress;import java.util.HashMap;import java.util.Random;import java.util.StringTokenizer;import simulator.schedulers.network.NetworkTopology;/** * Class HomogenousNetwork * */public class GroupNetwork implements NetworkTopology { private Random random; private BufferedReader groupFile; private GroupDefinition[] groups; private int totalAssignSpace; private int[] assignSpace; private HashMap nodes; private double nextLatency; private double nextLoss; private double nextBandwidth; private InetAddress nextHop; /** * Constructor HomogenousNetwork * * * @param random * @param filename */ public GroupNetwork(Random random, String filename) { this.random = random; this.nodes = new HashMap(); readFile(filename); } private void readFile(String filename) { boolean inited = false; boolean symmetric = true; try { groupFile = new BufferedReader(new FileReader(filename)); while (groupFile.ready()) { StringTokenizer fileTokens = new StringTokenizer(groupFile.readLine(), ":"); int numTokens = fileTokens.countTokens(); String type = fileTokens.nextToken(); switch (type.charAt(0)) { case '#': break; case 'D': if (inited) { throw new RuntimeException( "Graph definition already processed"); } if (numTokens == 3) { int numGroups = Integer.parseInt(fileTokens.nextToken()); symmetric = Boolean.valueOf( fileTokens.nextToken()).booleanValue(); assignSpace = new int[numGroups]; groups = new GroupDefinition[numGroups]; inited = true; } else { throw new RuntimeException( "Incorrect number of tokens for graph definition. Found " + numTokens); } break; case 'G': if ( !inited) { throw new RuntimeException( "Group definition prior to graph definition"); } if (numTokens == 6) { int groupNum = Integer.parseInt(fileTokens.nextToken()); int numNodes = Integer.parseInt(fileTokens.nextToken()); double internalLatency = Double.parseDouble(fileTokens.nextToken()); double internalLoss = Double.parseDouble(fileTokens.nextToken()); double internalMaxBandwidth = Double.parseDouble(fileTokens.nextToken()); if ((groupNum < 0) || (groupNum > groups.length)) { throw new RuntimeException( "Group number out of range. Found " + groupNum); } if (groups[groupNum] != null) { throw new RuntimeException( "Duplicate group definition, Found " + groupNum); } groups[groupNum] = new GroupDefinition(groupNum, numNodes, internalLatency, internalLoss, internalMaxBandwidth); assignSpace[groupNum] = numNodes; totalAssignSpace += numNodes; } else { throw new RuntimeException( "Incorrect number of tokens for group definition. Found " + numTokens); } break; case 'E': if ( !inited) { throw new RuntimeException( "Edge definition prior to graph definition"); } if (numTokens == 6) { int group1 = Integer.parseInt(fileTokens.nextToken()); int group2 = Integer.parseInt(fileTokens.nextToken()); double latency = Double.parseDouble(fileTokens.nextToken()); double loss = Double.parseDouble(fileTokens.nextToken()); double maxBandwidth = Double.parseDouble(fileTokens.nextToken()); if ((group1 < 0) || (group1 > groups.length) || (groups[group1] == null)) { throw new RuntimeException( "Group 1 number out of range. Found " + group1); } if ((group2 < 0) || (group2 > groups.length) || (groups[group2] == null)) { throw new RuntimeException( "Group 2 number out of range. Found " + group2); } if (group1 == group2) { throw new RuntimeException( "Group 1 and 2 numbers equal. Found " + group1); } EdgeDefinition edge = new EdgeDefinition(latency, loss, maxBandwidth); if (groups[group1].edges.get(new Integer(group2)) != null) { throw new RuntimeException( "Duplicate edge definition, Found " + group1 + " to " + group2); } groups[group1].addEdge(group2, edge); if (symmetric) { if (groups[group2].edges.get(new Integer(group1)) != null) { throw new RuntimeException( "Duplicate edge definition, Found " + group2 + " to " + group1); } groups[group2].addEdge(group1, edge); } } break; } } } catch (Exception error) { throw new RuntimeException("Error creating group topology", error); } } /** * Method getRandomGroup * @return */ public int getRandomGroup() { if (totalAssignSpace == 0) { throw new RuntimeException("Run out of stub nodes to assign"); } int desiredLocation = random.nextInt(totalAssignSpace) + 1; int curLocation = 0; int groupNum = 0; while ( !((desiredLocation > curLocation) && (desiredLocation <= (curLocation + assignSpace[groupNum])))) { curLocation += assignSpace[groupNum]; groupNum++; } assignSpace[groupNum]--; totalAssignSpace--; return groupNum; } /** * Method linkNodeToTopology * * @param node */ public void linkNodeToTopology(InetAddress node) { int groupNum = getRandomGroup(); nodes.put(node, new Integer(groupNum)); } /** * Method computeNextHop * * @param source * @param destination */ public void computeNextHop(InetAddress source, InetAddress destination) { nextHop = destination; int sourceGroup = ((Integer) nodes.get(source)).intValue(); int destinationGroup = ((Integer) nodes.get(destination)).intValue(); if (sourceGroup == destinationGroup) { GroupDefinition group = groups[sourceGroup]; nextLatency = group.internalLatency; nextLoss = group.internalLoss; nextBandwidth = group.internalMaxBandwidth; } else { EdgeDefinition edge = (EdgeDefinition) groups[sourceGroup].edges.get( new Integer(destinationGroup)); if (edge == null) { throw new RuntimeException("No edge connecting group " + sourceGroup + " with " + destinationGroup); } else { nextLatency = edge.latency; nextLoss = edge.loss; nextBandwidth = edge.maxBandwidth; } } } /** * Method getNextHop * @return */ public InetAddress getNextHop() { return nextHop; } /** * Method getLatency * @return */ public double getLatency() { return nextLatency; } /** * Method getLoss * @return */ public double getLoss() { return nextLoss; } /** * Method getMaxBandwidth * @return */ public double getMaxBandwidth() { return nextBandwidth; } /** * Class GroupDefinition * */ class GroupDefinition { public int groupNum; public int numNodes; public double internalLatency; public double internalLoss; public double internalMaxBandwidth; public HashMap edges; /** * Constructor GroupDefinition * * @param groupNum * @param numNodes * @param internalLatency * @param internalLoss * @param internalMaxBandwidth */ public GroupDefinition(int groupNum, int numNodes, double internalLatency, double internalLoss, double internalMaxBandwidth) { this.groupNum = groupNum; this.numNodes = numNodes; this.internalLatency = internalLatency; this.internalLoss = internalLoss; this.internalMaxBandwidth = internalMaxBandwidth; edges = new HashMap(); } /** * Method addEdge * * @param destination * @param edge */ public void addEdge(int destination, EdgeDefinition edge) { edges.put(new Integer(destination), edge); } /** * Method equals * * @param other * @return */ public boolean equals(Object other) { if (other instanceof GroupDefinition) { if (groupNum == ((GroupDefinition) other).groupNum) { return true; } else { return false; } } else { return false; } } } /** * Class EdgeDefinition * */ class EdgeDefinition { public double latency; public double loss; public double maxBandwidth; /** * Constructor EdgeDefinition * * @param latency * @param loss * @param maxBandwidth */ public EdgeDefinition(double latency, double loss, double maxBandwidth) { this.latency = latency; this.loss = loss; this.maxBandwidth = maxBandwidth; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -