/* CS 352 helper class * Author: Fan Zhang * Modified by: Tuan Phan * given.java * a class with public method getnetwork to look into the routing * table, and to return the matching entries in the file */ import java.io.*; import java.net.*; public class given{ /* currently, a max of 25 routes are possible */ static public int MAX_ROUTES=25; public target[] getnetwork (String file, int port)throws FileNotFoundException { RandomAccessFile in = new RandomAccessFile(file, "r"); //open file target[] temp = new target[this.MAX_ROUTES]; for(int j=0; j< this.MAX_ROUTES; j++) temp[j] = new target(); int i = 0; try{ InetAddress me = InetAddress.getLocalHost(); //get local machine InetAddress, i.e. "paul.rutgers.edu" do { try{ //read file, line by line String temp1 = new String(in.readLine()); String temp2 = new String(in.readLine()); String temp3 = new String(in.readLine()); String temp4 = new String(in.readLine()); String temp5 = new String(in.readLine()); InetAddress address = InetAddress.getByName(temp1); InetAddress address2 = InetAddress.getByName(temp3); //get source machine InetAddress if(me.equals(address)&&(port==Integer.parseInt(temp2))) //compare source machine name and local machine name, //together with port number, to see if they match { temp[i].set_sMachine(temp1); temp[i].set_sPort(Integer.parseInt(temp2)); temp[i].set_tMachine(temp3); temp[i].set_tPort(Integer.parseInt(temp4)); temp[i].set_Metric(Integer.parseInt(temp5)); i++; } else if(me.equals(address2)&&(port==Integer.parseInt(temp4))) { temp[i].set_sMachine(temp3); temp[i].set_sPort(Integer.parseInt(temp4)); temp[i].set_tMachine(temp1); temp[i].set_tPort(Integer.parseInt(temp2)); temp[i].set_Metric(Integer.parseInt(temp5)); i++; } String temp6 = new String(in.readLine()); //read flag line if( !temp6.equals("") ) break; //blank line seperate effective entries //last line is "End" } catch(UnknownHostException ioex){ System.out.println("Given: Cannot found host \n"); } catch(IOException e){ System.out.println("Error Reading File \n"); } } while(true); } catch(UnknownHostException e){ System.out.println("Local Host not Found !"); } target[] result = new target[i]; //array to hold matching entries for( int j=0; j