/** * title: Network Delay Calculator * author: Matt Rankin, 2007 */ public class NetworkDelay{ static char type; static double packetSize,bitrate,distance,velocity,util; public static void main(String[] argv){ if(argv.length<3 || argv.length>5) type='h'; else type=(argv[0].matches("-[spuUh]"))?argv[0].charAt(1):'h'; switch(type){ case 's': System.out.println("Transmission delay = "+send(argv[1],argv[2])+ " seconds"); break; case 'p': System.out.println("Propagation delay = "+prop(argv[1],argv[2])+ " seconds"); break; case 'u': util=util(send(argv[1],argv[2]),prop(argv[3],argv[4])); System.out.println("Utilisation = "+round(util,2)+ "%\nThroughput = "+through(util*bitrate)); break; case 'U': util=utilSW(send(argv[1],argv[2]),prop(argv[3],argv[4])); System.out.println("Utilisation = "+round(util,2)+ "%\nThroughput = "+through(util*bitrate)); break; case 'h': System.out.println(help()); break; } System.exit(0); } static double send(String p1,String p2){ try{ packetSize=Double.parseDouble(p1); bitrate=Double.parseDouble(p2); }catch(NumberFormatException e){ epicFail(); } return packetSize/bitrate; } static double prop(String p1,String p2){ try{ distance=Double.parseDouble(p1); switch(p2.charAt(0)){ case 'r': velocity=299792458; break; case 'w': velocity=199861638; break; default:epicFail(); } }catch(Exception e){ epicFail(); } return distance/velocity; } static double util(double s,double p){ return 100*s/(s+(2*p)); } static double utilSW(double s,double p){ return 1/(1+(2*(p/s))); } static String through(double ebps){ StringBuffer out=new StringBuffer(1024); double mb=Math.pow(2,20); if(ebps=mb) out.append(" ("+round(ebps/mb,2)+"MB/s)"); }else out.append(round(ebps/mb,2)+"MB/s"); return out.toString(); } static double round(double x,int y){ double z=Math.pow(10,y); return Math.round(x*z)/z; } static String help(){ String h; h="Calculates network transmission and propagation delay, and also the\n"+ "overall utilisation percentage and effective throughput.\n\n"+ " NetworkDelay -[spuUh] [ packet bitrate | distance velocity ]\n\n"+ " -s Transmission delay\n"+ " -p Propagation delay\n"+ " -u Network Utilisation\n"+ " -U Network Utilisation (stop-n-wait protocol)\n"+ " -h Help\n"+ " packet Packet size (in bits)\n"+ " bitrate Bits per second\n"+ " distance In metres\n"+ " velocity Wire or radio [r | w]\n\n"+ "Transmission delay requires the 'packet' and 'bitrate' parameters.\n"+ "Propagation delay requires the 'distance' and 'velocity'. Ensure that\n"+ "'velocity' is 'r' (speed of light) or 'w' (2/3 speed of light).\n\n"+ "Network utilisation requires all four parameters in the order shown\n"+ "in the syntax above."; return h; } static void epicFail(){ System.err.println("Epic fail!"); System.exit(0); } }