Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /****************************************************************
- *
- * Class: Switch
- * Author: Robert Taracha
- * purpose: This class models the activity of a layer 2 switch in a network
- * data members: camTable: SwitchEntry[] - holds the camTable of switch
- * numEntries: int - number of entries currently in camTable
- * maxEntries: int - size of camTable
- * methods: processFrame(Frame): boolean - uses info in Frame parameter to update camTable
- * based on source address, then send frame on its way based on destination
- * find (MACAddress):int - searches for parameter in camTable - returns index in camTable or -1 if not found
- * displayTable() - displays current elements in table
- * addInOrder(Frame) - add Frame parameter source address to table
- */
- public class Switch {
- private ArrayList<LinkedList> camTable;
- private int numEntries;
- private int maxEntries;
- LinkedList<SwitchEntry> node;
- public Switch() {
- camTable = new ArrayList<LinkedList>(100); // default value
- numEntries = 0;
- maxEntries = 100;
- /*Initialize the switch*/
- for(int i = 0;i<maxEntries;i++){
- LinkedList<SwitchEntry> node = new LinkedList<SwitchEntry>();
- node.add(new SwitchEntry());
- camTable.add(i,node);
- }
- }
- public Switch(int maxEntries) {
- camTable = new ArrayList<LinkedList>(maxEntries);
- numEntries = 0;
- this.maxEntries = maxEntries;
- /*Initialize the switch*/
- for(int i = 0;i<maxEntries;i++){
- LinkedList<SwitchEntry> node = new LinkedList<SwitchEntry>();
- node.add(new SwitchEntry());
- camTable.add(i, node);
- }
- }
- public void processFrame(Frame inFrame) {
- // first, add source MAC to camTable (in order) if not already there
- if (find(inFrame.getSource()) == -1) {
- if (numEntries >= maxEntries) {
- System.out.println ("Error...camTable is full - cannot add " + inFrame.getSource());
- } else {
- addInOrder(inFrame);
- System.out.println ("Adding " + inFrame.getSource() + " to camTable");
- }
- }
- //process frame
- int index = find(inFrame.getDestination());
- if (index != -1){
- System.out.print ("Sending frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
- System.out.println (" out port " + camTable.get(index).getFirst() );
- } else {
- System.out.print ("Flooding frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
- System.out.println (" out all ports" );
- }
- }
- // return of -1 indicates not found, otherwise returns index of where found
- // uses binary search for efficiency
- public int find (MACAddress source) {
- int low = 0;
- int high = numEntries-1;
- int found = -1;
- int mid = 0;
- if (numEntries == 0)
- return found;
- while (low <= high) {
- mid = (high+low)/2;
- if (source.isEqual (camTable.get(mid).getAddress())){
- found = mid;
- break;
- }
- else if (source.isGreaterThan(camTable.get(mid).getAddress()))
- low = mid+1;
- else high = mid-1;
- }
- return found;
- }
- public void displayTable() {
- System.out.println ("\nCam Table is : ");
- for (int i=0; i < camTable.size(); i++)
- if(camTable.get(i).get(i). != "no"){
- System.out.println ("["+i+"]"+camTable.get(i));
- }
- }
- private int hashIndex(Frame inFrame){
- int index = 0;
- String temp = inFrame.getSource().getAddress();
- for(int i = 0;i<temp.length();i++){
- index += (int)temp.charAt(i);
- }
- return index%100;
- }
- public void addInOrder(Frame inFrame) {
- int index = hashIndex(inFrame);
- camTable.set(index, new SwitchEntry(inFrame.getSource(),inFrame.getPort()));
- System.out.println("ADDED THESE VALUES -------->" + camTable.get(index) + " @ " + index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement