📄 iprouter.java
字号:
/*
JaNetSim --- Java Network Simulator
-------------------------------------
This software was developed at the Network Research Lab, Faculty of
Computer Science and Information Technology (FCSIT), University of Malaya.
This software may be used and distributed freely. FCSIT assumes no responsibility
whatsoever for its use by other parties, and makes no guarantees, expressed or
implied, about its quality, reliability, or any other characteristic.
We would appreciate acknowledgement if the software is used.
FCSIT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
FROM THE USE OF THIS SOFTWARE.
*/
package janetsim.component;
import janetsim.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
public class IPRouter extends SimComponent implements SimCommand,AnalyzerUser,
ActionListener,Serializable {
protected class ARPEntry implements Serializable {
int IP;
long mac;
}
protected class Port implements Serializable {
SimComponent to_link=null;
boolean link_busy=false;
java.util.List outQ=null;
SimParamInt outQ_size; //in bytes
java.util.List spq=null;
int spq_size; //in bytes
//RED variables
SimParamDouble red_avg;
long red_q_time=0;
int red_count=-1;
//end RED variables
long mac_addr; //6 bytes, obtained from ARP
SimParamString str_mac=null;
java.util.Map arp_entries=null; //ARP cache (no aging yet...)
java.util.List arp_q=null; //queued EtherFrame (unresolved mac IP packets)
SimParamIP ip=null; //ip address of this interface
}
protected class AppInfo implements Serializable {
Object comp; //either SimComponent or IPProtocol
int port;
int protocol;
}
protected class TCPListener implements TCPUser,java.io.Serializable {
Object key=null;
public void notify(int id,Object msg) {
switch(id) {
case TCPUser.CON_ESTABLISHED:
TCP tcp=sw_tcp.getTCP();
tcp.receive(key,-1);
//spawn a new listener
TCPListener newlistener=new TCPListener();
newlistener.key=tcp.open(getMyIP(),TCP_LISTEN_PORT,0,0,true,newlistener);
break;
case TCPUser.RECEIVE_BUF:
//don't care what is received, just continue receiving...
sw_tcp.getTCP().receive(key,-1);
break;
case TCPUser.CON_CLOSING:
sw_tcp.getTCP().close(key);
break;
case TCPUser.CON_RESET:
System.out.println("Warning: TCP listener get connection reset!");
break;
case TCPUser.CON_REFUSED:
System.out.println("Warning: TCP listener get connection refused!");
break;
}
}
}
protected java.util.Random randgen;
protected SimParamDouble sw_delay;
protected SimParamInt sw_oqsize;
//RED parameter set
protected SimParamBool sw_red;
protected SimParamDouble sw_red_wq;
protected SimParamInt sw_red_minth;
protected SimParamInt sw_red_maxth;
protected SimParamDouble sw_red_maxp;
protected SimParamDouble sw_red_s;
//end RED parameter set
protected SimParamInt sw_spqsize;
protected SimParamInt sw_speed;
protected SimParamDouble sw_ai;
protected SimParamBool sw_arp_q;
protected SimParamBool sw_name_seed;
protected SimParamInt sw_log_factor;
protected SimParamInt sw_frames_received;
protected SimParamInt sw_dropped;
protected SimParamInt sw_dropped_clas;
protected SimParamBool sw_cpucong;
protected SimParamTCP sw_tcp;
protected SimParamIPRTable sw_route_table=null;
protected java.util.Map voports;
protected java.util.Map apps;
protected transient Analyzer analyzer=null;
//public constants
public static final int TCP_LISTEN_PORT = 80; //must be in range [0,1023]
//private events
protected static final int MY_RECEIVE = SimProvider.EV_PRIVATE + 1;
protected static final int MY_SLOT_TIME = SimProvider.EV_PRIVATE + 2;
protected static final int EV_AVERAGING_INTERVAL = SimProvider.EV_PRIVATE + 3;
//compInfo IDs:
//SimProvider.CI_TRANSPORT_SEND implemented
//SimProvider.CI_TRANSPORT_SEND2LINK implemented
//SimProvider.CI_GET_MAC implementated
public static final int GET_PORT = SimProvider.CI_PRIVATE + 1;
//Ask for a port number (>=1024)
// parameter:
// protocol number (Integer)
//Returns an integer as
// the port number
public static final int GET_PRV_PORT = SimProvider.CI_PRIVATE + 2;
//Register a port number (<1024)
// parameter (array of Object):
// the port number (1st element) (Integer)
// protocol number (2nd element) (Integer)
// the IPProtocol object (3rd element)
//Returns an integer as
// the port number (null if already
// occupied)
public static final int REMOVE_PORT = SimProvider.CI_PRIVATE + 3;
//Release a port number
// parameter (array of Integer):
// the port number (1st element)
// protocol number (2nd element)
public static final int GET_IP = SimProvider.CI_PRIVATE + 4;
//get the smallest ip of this router,
// or the localhost ip (if no ip is
// defined for any interface).
//Returns an Integer
public static final int GET_ALL_IP = SimProvider.CI_PRIVATE + 5;
//get all ip as java.util.List of Integer
public static final int GET_ALL_PORT = SimProvider.CI_PRIVATE + 6;
//get all port/protocol pairs
//as java.util.List of Integer (in pairs)
public static final int ADD_ROUTE_ENTRY = SimProvider.CI_PRIVATE + 7;
//add a route entry for this router
//Parameter (Object []):
// IP (Integer) (1st element)
// mask (Integer) (2nd element)
// nexthop (Integer) IP (3rd element)
// nexthop (SimComponent) (4th element)
// type (String) (5th element)
public static final int CLEAR_ROUTE = SimProvider.CI_PRIVATE + 8;
//clear route entries for this router
//Parameter (String): route_type
//(if null, then clear all routes)
public static final int SET_PORT_IP = SimProvider.CI_PRIVATE + 9;
//set IP for a port of this router
//Parameter (Object []):
// IP (1st element)
// mask (2nd element)
// link (3rd element)
public static final int GET_ALL_IP_LINK = SimProvider.CI_PRIVATE + 10;
//get all IP addresses and the associated
//link, Parameter is Object []
//(caller must instantiate both List)
// First element: java.util.List (IPs)
// (Integer)
// Second element: java.util.List (links)
// (SimComponent)
public IPRouter(String aName,String aClass,Sim aSim,int locx,int locy) {
super(aName,aClass,aSim,locx,locy);
randgen=new java.util.Random();
sw_create();
}
public boolean isConnectable(SimComponent comp) {
if(!super.isConnectable(comp)) return false;
if(comp.getCompClass().equals("Link") ||
comp.getCompClass().equals("Application")) return true;
return false;
}
protected String getMACString(long mac) {
String str=Long.toHexString((mac>>40)&0xffL);
str+=":"+Long.toHexString((mac>>32)&0xffL);
str+=":"+Long.toHexString((mac>>24)&0xffL);
str+=":"+Long.toHexString((mac>>16)&0xffL);
str+=":"+Long.toHexString((mac>>8)&0xffL);
str+=":"+Long.toHexString(mac&0xffL);
return str;
}
protected void neighborAdded(SimComponent comp) {
if(comp.getCompClass().equals("Link")) {
if(sw_route_table==null) { //create route table if necessary
sw_route_table=new SimParamIPRTable("Route Table",this,theSim.now());
addParameter(sw_route_table);
}
Port voport=new Port();
voport=new Port();
voport.link_busy=false;
voport.to_link=comp;
voport.outQ=new java.util.LinkedList();
voport.outQ_size=new SimParamInt("Current Q size (bytes) to "+comp.getName(),this,theSim.now(),true,false,0);
voport.spq=new java.util.LinkedList();
voport.spq_size=0;
//RED variables
voport.red_avg=new SimParamDouble("RED avg Q size (bytes) to "+comp.getName(),this,theSim.now(),true,false,0);
voport.red_q_time=0;
voport.red_count=-1;
//end RED variables
voport.mac_addr=EtherFrame.newMAC(theSim);
voport.str_mac=new SimParamString("MAC address to "+comp.getName(),this,theSim.now(),false,false,getMACString(voport.mac_addr));
voport.arp_entries=new java.util.HashMap();
voport.arp_q=new java.util.LinkedList();
voport.ip=new SimParamIP("IP address to "+comp.getName(),this,theSim.now(),false,true,0,0);
addParameter(voport.str_mac);
addParameter(voport.ip);
addParameter(voport.outQ_size);
addParameter(voport.red_avg);
voports.put(voport.to_link,voport);
}
}
protected void neighborRemoved(SimComponent comp) {
if(comp.getCompClass().equals("Link")) {
Port voport=(Port)voports.remove(comp);
if(voport==null) return;
removeParameter(voport.ip);
removeParameter(voport.str_mac);
removeParameter(voport.outQ_size);
removeParameter(voport.red_avg);
voport=null;
//check whether still need the route table
if(voports.isEmpty()) {
removeParameter(sw_route_table);
sw_route_table=null;
}
}
}
public void copy(SimComponent comp) {
if(comp instanceof IPRouter) {
IPRouter theComp=(IPRouter)comp;
sw_delay.setValue(theComp.sw_delay.getValue());
sw_oqsize.setValue(theComp.sw_oqsize.getValue());
sw_red.setValue(theComp.sw_red.getValue());
sw_red_wq.setValue(theComp.sw_red_wq.getValue());
sw_red_minth.setValue(theComp.sw_red_minth.getValue());
sw_red_maxth.setValue(theComp.sw_red_maxth.getValue());
sw_red_maxp.setValue(theComp.sw_red_maxp.getValue());
sw_red_s.setValue(theComp.sw_red_s.getValue());
sw_spqsize.setValue(theComp.sw_spqsize.getValue());
sw_speed.setValue(theComp.sw_speed.getValue());
sw_ai.setValue(theComp.sw_ai.getValue());
sw_arp_q.setValue(theComp.sw_arp_q.getValue());
sw_name_seed.setValue(theComp.sw_name_seed.getValue());
sw_log_factor.setValue(theComp.sw_log_factor.getValue());
}
}
public Image getImage(Component refcomp) {
if(image==null) {
image=Toolkit.getDefaultToolkit().getImage(
"images"+System.getProperty("file.separator")+"iprouter.gif");
}
return image;
}
public void reset() {
sw_frames_received.setValue(0);
sw_frames_received.update(theSim.now());
sw_dropped.setValue(0);
sw_dropped.update(theSim.now());
sw_dropped_clas.setValue(0);
sw_dropped_clas.update(theSim.now());
sw_cpucong.setValue(false);
sw_cpucong.update(theSim.now());
sw_tcp.reset();
java.util.Iterator i=voports.values().iterator();
while(i.hasNext()) {
Port voport=(Port)i.next();
voport.link_busy=false;
voport.outQ.clear();
voport.outQ_size.setValue(0);
voport.outQ_size.update(theSim.now());
voport.spq.clear();
voport.spq_size=0;
voport.arp_entries.clear();
voport.arp_q.clear();
//RED variables;
voport.red_avg.setValue(0);
voport.red_avg.update(theSim.now());
voport.red_q_time=0;
voport.red_count=-1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -