[Javanetsim-cvs] javaNetSim/core MultilayerSwitch.java,NONE,1.1
Status: Beta
Brought to you by:
darkkey
From: Alexander B. <da...@us...> - 2008-10-17 18:59:21
|
Update of /cvsroot/javanetsim/javaNetSim/core In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv31175/core Added Files: MultilayerSwitch.java Log Message: --- NEW FILE: MultilayerSwitch.java --- package core; import java.util.Enumeration; import java.util.Hashtable; public class MultilayerSwitch extends NetworkLayerDevice { int sz = 0; int buffSize = 255; Hashtable<String, Hashtable<String, Long>> IntCaches; Hashtable<Integer, String> Vlans; public MultilayerSwitch(String inName, boolean inOn) { super(inName, 3, inOn); // pass name and protocolstack layer IntCaches = new Hashtable<String, Hashtable<String, Long>>(); Vlans = new Hashtable<Integer, String>(); } public Enumeration<Integer> getVlans(){ return Vlans.keys(); } public void addVlan(Integer v, String name){ Vlans.put(v, name); } public void removeVlan(Integer v){ Vlans.remove(v); } public void setVlanName(Integer v, String name){ Vlans.remove(v); Vlans.put(v, name); } public String getAllData(){ String file = ""; file += "vlan.dat\n"; file += "1\n"; Enumeration<Integer> it; Integer v; it = Vlans.keys(); while (it.hasMoreElements()) { v = it.nextElement(); file = file + v + "," + Vlans.get(v) + "|"; } file += "\n"; return file; } public void loadDataFile(String name, int lines, String Data){ if(name.equals("vlan.dat")){ Vlans.clear(); String[] vlans = Data.split("\\|"); for(int i = 0; i<vlans.length; i++){ if(vlans[i].length() > 1){ String[] vlan = vlans[i].split(","); Vlans.put(Integer.valueOf(vlan[0]), vlan[1]); } } } return; } public void addNetworkInterface(String name, int type, boolean active) { if (!active) { super.addNetworkInterface(name, type, false, 0); IntCaches.put(name, new Hashtable<String, Long>()); } } public void Reset() { sz = 0; Enumeration it; String nic = ""; it = NetworkInterfacetable.elements(); while (it.hasMoreElements()) { NetworkInterface tempInterface = (NetworkInterface) it .nextElement(); nic = tempInterface.getName(); Hashtable outInt = IntCaches.get(nic); outInt.clear(); } } public int getState() { return sz; } public String getCache() { Enumeration it, it2; String nic = ""; String result = ""; it = NetworkInterfacetable.elements(); while (it.hasMoreElements()) { NetworkInterface tempInterface = (NetworkInterface) it .nextElement(); nic = tempInterface.getName(); Hashtable outInt = IntCaches.get(nic); result = result + nic + ": "; it2 = outInt.keys(); while (it2.hasMoreElements()) { String mac = (String) it2.nextElement(); result = result + mac + "\t"; } result = result + "\n"; } return result; } /** * This method will recieve a packet from any of the connected links and the * copy the Packet and distribute a copy to each of the other connected * links. * * @author bevan_calliess * @param inPacket - * The packet to be transported * @param inLinkName - * The name of the link that sent the packet eg: eth0 */ public void receivePacket(Packet inPacket, String inInterfaceName) throws LowLinkException { if (sz != 1) { Ethernet_packet tempPacket = (Ethernet_packet) inPacket; Enumeration it; boolean intFound = false; String nic = ""; try { Hashtable<String, Long> inInt = IntCaches.get(inInterfaceName); inInt.put(tempPacket.getSourceMACAddress(), Long.valueOf(System.currentTimeMillis())); Ethernet_packet copyPacket = new Ethernet_packet(tempPacket .getData(), tempPacket.getDestinationMACAddress(), tempPacket.getSourceMACAddress()); it = NetworkInterfacetable.elements(); while (it.hasMoreElements()) { EthernetNetworkInterface tempInterface = (EthernetNetworkInterface) it .nextElement(); nic = tempInterface.getName(); Hashtable<String, Long> outInt = IntCaches.get(nic); if (outInt.get(tempPacket.getDestinationMACAddress()) != null) { intFound = true; try { if(tempInterface.mode == EthernetNetworkInterface.MODE_TRUNK || tempInterface.vlan == copyPacket.vlan_id) tempInterface.sendPacket(copyPacket); } catch (NullPointerException e) { System.out.println("MultilayerSwitch.java: " + e.toString()); } } } it = NetworkInterfacetable.elements(); while (it.hasMoreElements() && !intFound) { // Test to see if the current Interface is the Interface // that sent in the packet // if it is skip that interface EthernetNetworkInterface tempInterface = (EthernetNetworkInterface) it .nextElement(); if (!tempInterface.getName().equals(inInterfaceName)) { try { if(tempInterface.mode == EthernetNetworkInterface.MODE_TRUNK || tempInterface.vlan == copyPacket.vlan_id) tempInterface.sendPacket(copyPacket); } catch (NullPointerException e) { System.out.println("Switch.java: " + e.toString()); } } } } catch (Throwable th) { if (th.toString().contains( "Packet lost due to physical link problems!")) { throw new LowLinkException(th.toString()); } else { sz = 1; System.out.println(th.toString()); throw new LowLinkException( "Switch buffer overflow (packet loop flood?)."); } } } } public void turnOn(){ init(ProtocolStackLayers); initApplications(); ifacesLinkUP(); // up all before loading config config.load(); } } |