package multipath; import multipath.*; import java.text.DecimalFormat; public class Multipath { public static int maxNode;//max number of nodes public static int maxPath;//max number of pathes public static int maxServer;//max number of servers public static int selected=2;//number of selected servers public static double maxSpeed=0;//max downloading speed public static Node [] nodes;//store all nodes public static Path [] pathes;//store all pathes public static Server [] servers;//store all servers public static double [] actSpeed;//store actual speed from each server public Multipath() { } public static void main(String[] args) { LoadData.loadData(); Multipath m = new Multipath(); m.path(); m.output(); } //final output public void output(){ for(int i=1;i<=maxServer;i++){ maxSpeed += actSpeed[i]; } System.out.print("Final Max Speed: "+formatNumber(maxSpeed)); System.out.print("\nServers Selected: "); for(int i=1;i<=maxServer;i++){ if(actSpeed[i]!=0) System.out.print("Server"+i+" : ("+formatNumber(actSpeed[i])+") , "); } } //format double number for output private static String formatNumber(double number) { DecimalFormat myFormatter = new DecimalFormat("#0.00#"); return myFormatter.format(number); } //calc path avail speed for each server public void path(){ for(int sid=1;sid<=maxServer;sid++){ updatePath(sid); } } //update available speed of path for each server private void updatePath(int sid){ boolean pathEnd=false; double speed=servers[sid].speed; Node fNode=servers[sid].fatherNode; Path fPath=findPath(servers[sid],fNode); if((fPath.availSpeed - speed)>=0) fPath.availSpeed -= speed; else speed=fPath.availSpeed; Node cNode; while(!pathEnd){ cNode=fNode; fNode=cNode.fatherNode; fPath=findPath(cNode,fNode); if((fPath.availSpeed - speed)>0) fPath.availSpeed -= speed; else{ speed=fPath.availSpeed; fPath.availSpeed=0; } actSpeed[sid]=speed; if(fNode.isEqual(nodes[maxNode])) pathEnd=true; } } //find a path with start node and end node private Path findPath(Node sNode, Node fNode){ for(int i=1;i<=maxPath;i++){ if((pathes[i].startNode.isEqual(sNode))&&(pathes[i].endNode.isEqual(fNode))) return pathes[i]; } return null; } }