📄 genericatmswitch.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 java.awt.*;
import java.io.Serializable;
public class GenericATMSwitch extends SimComponent implements Serializable {
private class Queue implements Serializable {
java.util.List ptr=null;
SimParamInt iq=null;
SimParamInt dq=null;
}
private class Port implements Serializable {
SimComponent to_link=null;
boolean link_busy=false;
Queue vbr=null;
Queue abr=null;
java.util.List sigQueue=null;
java.util.List spq=null;
AAL5 aal=null; //AAL for the UNI signaling channel (0/5)
}
private class CallRecordKey implements Serializable {
SimComponent link;
int callref;
CallRecordKey(int cref,SimComponent comp) {
callref=cref; link=comp;
}
public boolean equals(Object o) {
if(!(o instanceof CallRecordKey)) return false;
CallRecordKey k=(CallRecordKey)o;
if(callref==k.callref && link==k.link) return true;
return false;
}
public int hashCode() {
return (callref ^ link.hashCode());
}
}
private class CallRecord implements Serializable {
ForwardEntry fe;
CallRecordKey fromkey;
CallRecordKey tokey;
}
private java.util.Random randgen;
private SimParamInt sw_delay;
private SimParamInt sw_oqsize;
private SimParamInt sw_speed;
private SimParamInt sw_speedup;
private SimParamDouble sw_ai;
private SimParamInt sw_log_factor;
private SimParamInt sw_cells_received;
private SimParamDouble sw_dropped;
private SimParamBool sw_cpucong;
private int num_dropped,total_cell;
private SimParamFTable sw_forward_table=null;
private SimParamRTable sw_route_table=null;
private java.util.Map voports;
private java.util.Map records,r_records;
//private events
static final int MY_RECEIVE = SimProvider.EV_PRIVATE + 1;
static final int MY_SLOT_TIME = SimProvider.EV_PRIVATE + 2;
static final int EV_AVERAGING_INTERVAL = SimProvider.EV_PRIVATE + 3;
public GenericATMSwitch(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")) return true;
return false;
}
protected void neighborAdded(SimComponent comp) {
if(sw_route_table==null) { //create route table if necessary
sw_route_table=new SimParamRTable("Route Table",this,theSim.now(),comp);
addParameter(sw_route_table);
}
if(sw_forward_table==null) { //create forward table if necessary
sw_forward_table=new SimParamFTable("Forwarding Table",this,theSim.now());
addParameter(sw_forward_table);
}
Port voport=new Port();
voport=new Port();
voport.link_busy=false;
voport.to_link=comp;
voport.spq=new java.util.LinkedList();
voport.vbr=new Queue();
voport.vbr.ptr=new java.util.LinkedList();
voport.vbr.iq=new SimParamInt("Cells in VBR Q to "+comp.getName(),this,
theSim.now(),true,false,0);
voport.vbr.iq.update(theSim.now());
voport.vbr.dq=new SimParamInt("Cells dropped in VBR Q to "+comp.getName(),this,
theSim.now(),true,false,0);
voport.vbr.dq.update(theSim.now());
voport.abr=new Queue();
voport.abr.ptr=new java.util.LinkedList();
voport.abr.iq=new SimParamInt("Cells in ABR Q to "+comp.getName(),this,
theSim.now(),true,false,0);
voport.abr.iq.update(theSim.now());
voport.abr.dq=new SimParamInt("Cells dropped in ABR Q to "+comp.getName(),this,
theSim.now(),true,false,0);
voport.abr.dq.update(theSim.now());
voport.sigQueue=new java.util.LinkedList();
voport.aal=new AAL5();
addParameter(voport.vbr.iq);
addParameter(voport.vbr.dq);
addParameter(voport.abr.iq);
addParameter(voport.abr.dq);
voports.put(voport.to_link,voport);
}
protected void neighborRemoved(SimComponent comp) {
Port voport=(Port)voports.remove(comp);
if(voport==null) return;
removeParameter(voport.vbr.iq);
removeParameter(voport.vbr.dq);
removeParameter(voport.abr.iq);
removeParameter(voport.abr.dq);
voport=null; //optional but good
//check whether still need the forward table
if(voports.isEmpty()) {
removeParameter(sw_forward_table);
sw_forward_table=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 GenericATMSwitch) {
GenericATMSwitch theComp=(GenericATMSwitch)comp;
sw_delay.setValue(theComp.sw_delay.getValue());
sw_oqsize.setValue(theComp.sw_oqsize.getValue());
sw_speed.setValue(theComp.sw_speed.getValue());
sw_speedup.setValue(theComp.sw_speedup.getValue());
sw_ai.setValue(theComp.sw_ai.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")+"switch.gif");
}
return image;
}
public void reset() {
sw_cells_received.setValue(0);
sw_cells_received.update(theSim.now());
sw_dropped.setValue(0);
sw_dropped.update(theSim.now());
sw_cpucong.setValue(false);
sw_cpucong.update(theSim.now());
num_dropped=0;
total_cell=0;
records.clear();
r_records.clear();
if (sw_forward_table!=null) sw_forward_table.clearAllSVC();
java.util.Iterator i=voports.values().iterator();
while(i.hasNext()) {
Port voport=(Port)i.next();
voport.spq.clear();
voport.link_busy=false;
voport.vbr.iq.setValue(0);
voport.vbr.iq.update(theSim.now());
voport.vbr.dq.setValue(0);
voport.vbr.dq.update(theSim.now());
voport.vbr.ptr.clear();
voport.abr.iq.setValue(0);
voport.abr.iq.update(theSim.now());
voport.abr.dq.setValue(0);
voport.abr.dq.update(theSim.now());
voport.abr.ptr.clear();
voport.sigQueue.clear();
voport.aal.reset();
}
}
public void start() {
long ticks=SimClock.USec2Tick(424.0/sw_speed.getValue());
theSim.enqueue(new SimEvent(MY_SLOT_TIME,this,this,theSim.now()+ticks,null));
double aiusec=sw_ai.getValue();
aiusec+=randgen.nextDouble()*aiusec;
theSim.enqueue(new SimEvent(EV_AVERAGING_INTERVAL,this,this,
theSim.now()+SimClock.USec2Tick(aiusec),null));
}
public void action(SimEvent e) {
switch(e.getType()) {
case MY_SLOT_TIME:
sw_proc_slot_time();
long ticks=SimClock.USec2Tick(424.0/sw_speed.getValue());
theSim.enqueue(new SimEvent(MY_SLOT_TIME,this,this,theSim.now()+ticks,null));
break;
case SimProvider.EV_RECEIVE:
sw_receive(e);
break;
case MY_RECEIVE:
sw_my_receive(e);
break;
case SimProvider.EV_READY:
sw_ready(e);
break;
case EV_AVERAGING_INTERVAL:
sw_averaging_interval();
theSim.enqueue(new SimEvent(EV_AVERAGING_INTERVAL,this,this,
theSim.now()+SimClock.USec2Tick(sw_ai.getValue()),null));
break;
}
}
////////////////////////// private methods ////////////////////////////////
private void sw_create() {
voports=new java.util.HashMap();
records=new java.util.HashMap();
r_records=new java.util.HashMap();
//Initialize the parameters
long ctick=theSim.now();
sw_delay=new SimParamInt("Delay to process a cell (uSec)",this,ctick,false,true,0);
sw_speed=new SimParamInt("Switching Slot time (Mbit/s)",this,ctick,false,true,155);
sw_speedup=new SimParamInt("No. of Port (speedup)",this,ctick,false,true,24);
sw_oqsize=new SimParamInt("Output q_size (cells, -1=inf)",this,ctick,false,true,1000);
sw_ai=new SimParamDouble("Averaging Interval (usec)",this,ctick,false,true,100000.0);
sw_log_factor=new SimParamInt("Logging every (ticks) (e.g. 1, 100)",this,ctick,false,true,0);
sw_cells_received=new SimParamInt("Cells Received",this,ctick,true,false,0);
sw_cells_received.update(ctick);
sw_dropped=new SimParamDouble("Cell Drop %",this,ctick,true,false,0);
sw_dropped.update(ctick);
sw_cpucong=new SimParamBool("CPU Slow Triggered",this,ctick,true,false,false);
sw_cpucong.update(ctick);
addParameter(sw_delay);
addParameter(sw_speed);
addParameter(sw_oqsize);
addParameter(sw_ai);
addParameter(sw_log_factor);
addParameter(sw_cells_received);
addParameter(sw_dropped);
addParameter(sw_cpucong);
num_dropped=0;
total_cell=0;
}
private void sw_ready(SimEvent e) {
Port voport=(Port)voports.get(e.getSource());
if(voport!=null) {
if(voport.link_busy) {
voport.link_busy=false;
if(!voport.sigQueue.isEmpty() || !voport.abr.ptr.isEmpty()
|| !voport.vbr.ptr.isEmpty()) {
voport.link_busy=true;
sw_schedule_output(voport);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -