Advertisement
Broatlas

Untitled

Apr 19th, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. import java.util.*;
  2. /****************************************************************
  3.  *
  4.  * Class: Switch
  5.  * Author:  Robert Taracha
  6.  * purpose:  This class models the activity of a layer 2 switch in a network
  7.  * data members:  camTable: SwitchEntry[] - holds the camTable of switch
  8.  *          numEntries: int - number of entries currently in camTable
  9.  *          maxEntries: int - size of camTable
  10.  * methods:  processFrame(Frame): boolean - uses info in Frame parameter to update camTable
  11.  *                             based on source address, then send frame on its way based on destination
  12.  *        find (MACAddress):int - searches for parameter in camTable - returns index in camTable or -1 if not found
  13.  *        displayTable() - displays current elements in table
  14.  *        addInOrder(Frame) - add Frame parameter source address to table
  15.  */
  16. public class Switch {
  17.     private ArrayList<LinkedList> camTable;
  18.     private int numEntries;
  19.     private int maxEntries;
  20.     LinkedList<SwitchEntry> node;
  21.    
  22.     public Switch() {
  23.         camTable = new ArrayList<LinkedList>(100);  // default value
  24.         numEntries = 0;
  25.         maxEntries = 100;
  26.         /*Initialize the switch*/
  27.         for(int i = 0;i<maxEntries;i++){
  28.             LinkedList<SwitchEntry> node = new LinkedList<SwitchEntry>();
  29.             node.add(new SwitchEntry());
  30.             camTable.add(i,node);
  31.            
  32.         }
  33.  
  34.     }
  35.    
  36.     public Switch(int maxEntries) {
  37.         camTable = new ArrayList<LinkedList>(maxEntries);
  38.         numEntries = 0;
  39.         this.maxEntries = maxEntries;
  40.         /*Initialize the switch*/
  41.         for(int i = 0;i<maxEntries;i++){
  42.             LinkedList<SwitchEntry> node = new LinkedList<SwitchEntry>();
  43.             node.add(new SwitchEntry());
  44.             camTable.add(i, node);
  45.            
  46.         }
  47.     }
  48.    
  49.     public void processFrame(Frame inFrame) {
  50.         // first, add source MAC to camTable (in order) if not already there
  51.         if (find(inFrame.getSource()) == -1) {
  52.             if (numEntries >= maxEntries) {
  53.                 System.out.println ("Error...camTable is full - cannot add " + inFrame.getSource());   
  54.             } else {
  55.                 addInOrder(inFrame);
  56.                 System.out.println ("Adding " + inFrame.getSource() + " to camTable");
  57.             }
  58.         }
  59.        
  60.         //process frame
  61.         int index = find(inFrame.getDestination());
  62.         if (index != -1){
  63.             System.out.print ("Sending frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
  64.             System.out.println (" out port " + camTable.get(index).getFirst() );
  65.         } else {
  66.             System.out.print ("Flooding frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
  67.             System.out.println (" out all ports"  );
  68.            
  69.         }
  70.        
  71.     }
  72.    
  73.     // return of -1 indicates not found, otherwise returns index of where found
  74.     // uses binary search for efficiency
  75.     public int find (MACAddress source) {
  76.         int low = 0;
  77.         int high = numEntries-1;
  78.         int found = -1;
  79.         int mid = 0;
  80.        
  81.         if (numEntries == 0)
  82.             return found;
  83.         while (low <= high) {
  84.             mid = (high+low)/2;
  85.             if (source.isEqual (camTable.get(mid).getAddress())){
  86.                 found = mid;
  87.                 break;
  88.             }
  89.             else if (source.isGreaterThan(camTable.get(mid).getAddress()))
  90.                 low = mid+1;
  91.             else high = mid-1;
  92.         }
  93.         return found;              
  94.     }
  95.    
  96.     public void displayTable() {
  97.         System.out.println ("\nCam Table is : ");
  98.         for (int i=0; i < camTable.size(); i++)
  99.             if(camTable.get(i).get(i). != "no"){
  100.             System.out.println ("["+i+"]"+camTable.get(i));
  101.             }
  102.     }
  103.    
  104.     private int hashIndex(Frame inFrame){
  105.        
  106.             int index = 0;
  107.             String temp = inFrame.getSource().getAddress();
  108.             for(int i = 0;i<temp.length();i++){
  109.                 index += (int)temp.charAt(i);
  110.             }
  111.             return index%100;
  112.        
  113.     }
  114.    
  115.     public void addInOrder(Frame inFrame) {
  116.         int index = hashIndex(inFrame);
  117.    
  118.         camTable.set(index, new SwitchEntry(inFrame.getSource(),inFrame.getPort()));
  119.         System.out.println("ADDED THESE VALUES -------->" + camTable.get(index) + " @ " + index);
  120.         }
  121.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement