📄 cbrapp.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.*;
public class CBRApp extends SimComponent implements java.io.Serializable {
private SimParamDouble cn_bit_rate;
private SimParamInt cn_start_time;
private SimParamDouble cn_trans_size;
private SimParamInt cn_repeat;
private SimParamInt cn_delay;
private SimParamBool cn_random_size;
private SimParamBool cn_random_delay;
private SimParamBool cn_start_delay;
private SimParamBool cn_random_target;
private SimParamBool cn_name_seed;
private SimParamInt cn_thisport=null;
private SimParamNSAP cn_destnsap;
private SimParamInt cn_destport;
private SimParamInt cn_conattempt;
private SimParamInt cn_conaccept;
private SimParamInt cn_curincome;
private SimParamInt cn_totalincome;
private int cn_status;
private int cn_vpi,cn_vci;
private long cn_cur_trans_size;
private int cn_con_done;
private long cn_num_sent;
private java.util.Random randgen;
//connection status constants
private static final int UNI_NULL = 0;
private static final int UNI_CALL_INIT = 1;
private static final int UNI_CALL_PROC = 2;
private static final int UNI_ACTIVE = 3;
//private events
private static final int MY_SENDCELL = SimProvider.EV_PRIVATE + 1;
private static final int MY_START = SimProvider.EV_PRIVATE + 2;
public CBRApp(String name,String aClass,Sim aSim,int locx,int locy) {
super(name,aClass,aSim,locx,locy);
randgen=new java.util.Random();
cn_create();
}
public boolean isConnectable(SimComponent comp) {
if(!super.isConnectable(comp)) return false;
if(neighborCount()==0 && comp.getCompClass().equals("BTE"))
return true;
return false;
}
protected void neighborAdded(SimComponent comp) {
Integer port=(Integer)comp.compInfo(GenericBTE.GET_PORT,this,null);
cn_thisport=new SimParamInt("Port number",this,theSim.now(),false,false,port.intValue());
cn_destnsap=new SimParamNSAP("Destination NSAP",this,theSim.now(),false,true,"0");
cn_destport=new SimParamInt("Destination port number",this,theSim.now(),false,true,0);
cn_conattempt=new SimParamInt("Calls attempted",this,theSim.now(),true,false,0);
cn_conaccept=new SimParamInt("Calls accepted",this,theSim.now(),true,false,0);
cn_curincome=new SimParamInt("Incoming Calls",this,theSim.now(),true,false,0);
cn_totalincome=new SimParamInt("Total Incoming Calls",this,theSim.now(),true,false,0);
addParameter(cn_thisport);
addParameter(cn_destnsap);
addParameter(cn_destport);
addParameter(cn_conattempt);
addParameter(cn_conaccept);
addParameter(cn_curincome);
addParameter(cn_totalincome);
}
protected void neighborRemoved(SimComponent comp) {
if(cn_thisport!=null) {
comp.compInfo(GenericBTE.REMOVE_PORT,this,new Integer(cn_thisport.getValue()));
removeParameter(cn_thisport);
removeParameter(cn_destnsap);
removeParameter(cn_destport);
removeParameter(cn_conattempt);
removeParameter(cn_conaccept);
removeParameter(cn_curincome);
removeParameter(cn_totalincome);
cn_thisport=null;
}
}
public void copy(SimComponent comp) {
if(comp instanceof CBRApp) {
CBRApp theComp=(CBRApp)comp;
cn_bit_rate.setValue(theComp.cn_bit_rate.getValue());
cn_start_time.setValue(theComp.cn_start_time.getValue());
cn_trans_size.setValue(theComp.cn_trans_size.getValue());
cn_repeat.setValue(theComp.cn_repeat.getValue());
cn_delay.setValue(theComp.cn_delay.getValue());
cn_random_size.setValue(theComp.cn_random_size.getValue());
cn_random_delay.setValue(theComp.cn_random_delay.getValue());
cn_start_delay.setValue(theComp.cn_start_delay.getValue());
cn_random_target.setValue(theComp.cn_random_target.getValue());
cn_name_seed.setValue(theComp.cn_name_seed.getValue());
}
}
public void reset() {
cn_status=UNI_NULL;
cn_con_done=0;
if(neighborCount()!=0) {
cn_conattempt.setValue(0);
cn_conattempt.update(theSim.now());
cn_conaccept.setValue(0);
cn_conaccept.update(theSim.now());
cn_curincome.setValue(0);
cn_curincome.update(theSim.now());
cn_totalincome.setValue(0);
cn_totalincome.update(theSim.now());
}
}
public void start() {
if(neighborCount()==0) return; //must not send without a neighbor!
if(cn_name_seed.getValue()==true)
randgen.setSeed(getName().hashCode());
for(int i=0;i<randgen.nextInt(100);i++) randgen.nextDouble();
myStart();
}
public void action(SimEvent e) {
switch(e.getType()) {
case MY_START:
myStart();
break;
case MY_SENDCELL:
myStart();
break;
case SimProvider.EV_RECEIVE:
cn_receive(e);
break;
case SimProvider.EV_RECEIVE_UNI:
cn_receive_uni(e);
break;
}
}
///////////////////// private methods ////////////////////////////////
private void cn_create() {
//Initialize the parameters
long ctick=theSim.now();
cn_bit_rate=new SimParamDouble("Bit Rate (MBits/s)",this,ctick,false,true,0);
cn_start_time=new SimParamInt("Start time (usecs)",this,ctick,false,true,0);
cn_trans_size=new SimParamDouble("Number of MBits to be sent",this,ctick,false,true,0);
cn_repeat=new SimParamInt("Repeat count (-1=inf)",this,ctick,false,true,0);
cn_delay=new SimParamInt("Delay between calls (usecs)",this,ctick,false,true,1000000);
cn_random_size=new SimParamBool("Random data size",this,ctick,false,true,false);
cn_random_delay=new SimParamBool("Random delay bet. calls",this,ctick,false,true,false);
cn_start_delay=new SimParamBool("Enable starting delay",this,ctick,false,true,false);
cn_random_target=new SimParamBool("Random destination",this,ctick,false,true,true);
cn_name_seed=new SimParamBool("Use name as seed",this,ctick,false,true,false);
addParameter(cn_bit_rate);
addParameter(cn_start_time);
addParameter(cn_trans_size);
addParameter(cn_repeat);
addParameter(cn_delay);
addParameter(cn_random_size);
addParameter(cn_random_delay);
addParameter(cn_start_delay);
addParameter(cn_random_target);
addParameter(cn_name_seed);
reset();
}
private void myStart() {
if(SimClock.USec2Tick(cn_start_time.getValue()) <= theSim.now()) {
switch(cn_status) {
case UNI_NULL: //null, so setup a new connection
if(cn_repeat.getValue()>cn_con_done || cn_repeat.getValue()==-1) {
if(cn_random_target.getValue()) {
if(!setRandomTarget()) break; //bye bye if no targets at all...
}
//send SETUP
UNIInfo uni=new UNIInfo();
uni.callref=cn_thisport.getValue();
uni.msgtype=UNIInfo.UNI_SETUP;
uni.contype=UNIInfo.CON_CBR;
uni.calledNSAP=cn_destnsap.getValue();
uni.calledport=cn_destport.getValue();
uni.newvpi= -1; //let the other decide
uni.newvci= -1; //let the other decide
theSim.enqueue(new SimEvent(SimProvider.EV_RECEIVE_UNI,this,
neighbor(0),theSim.now(),uni));
cn_conattempt.setValue(cn_conattempt.getValue()+1);
cn_conattempt.update(theSim.now());
cn_status = UNI_CALL_INIT;
}
break;
case UNI_ACTIVE: //active, so it's time to send data cells
if(cn_num_sent < cn_cur_trans_size) {
Cell cell=new Cell();
cell.vpi=cn_vpi;
cell.vci=cn_vci;
theSim.enqueue(new SimEvent(SimProvider.EV_RECEIVE,this,
neighbor(0),theSim.now(),cell));
long interval=SimClock.USec2Tick(424.0/cn_bit_rate.getValue());
theSim.enqueue(new SimEvent(MY_SENDCELL,this,this,theSim.now()+interval,null));
cn_num_sent++;
}
else { //it's time to release the connection...
//send RELEASE
UNIInfo uni=new UNIInfo();
uni.callref=cn_thisport.getValue();
uni.msgtype=UNIInfo.UNI_RELEASE;
theSim.enqueue(new SimEvent(SimProvider.EV_RECEIVE_UNI,this,
neighbor(0),theSim.now(),uni));
cn_con_done++;
cn_status = UNI_NULL;
theSim.enqueue(new SimEvent(MY_SENDCELL,this,this,theSim.now()+getDelay(),null));
}
break;
}
}
else {
long interval=SimClock.USec2Tick(cn_start_time.getValue());
if(cn_start_delay.getValue()) interval+=getDelay();
theSim.enqueue(new SimEvent(MY_START,this,this,theSim.now()+interval,null));
}
}
//helper function to set a random destination
//return false if can't find a target
private boolean setRandomTarget() {
java.util.List allcomps=theSim.getSimComponents();
java.util.List nsaps=new java.util.ArrayList();
java.util.List ports=new java.util.ArrayList();
int i,j,k;
for(i=0;i<allcomps.size();i++) {
SimComponent comp=(SimComponent)allcomps.get(i);
if(comp==neighbor(0)) continue; //skip it's own BTE
if(comp.getCompClass().equals("BTE")) {
String [] strnsaps=(String [])comp.compInfo(GenericBTE.GET_ALL_NSAP,this,null);
Integer [] intports=(Integer [])comp.compInfo(GenericBTE.GET_ALL_PORT,this,null);
if(strnsaps==null || intports==null) continue;
for(j=0;j<strnsaps.length;j++) {
for(k=0;k<intports.length;k++) {
nsaps.add(strnsaps[j]);
ports.add(intports[k]);
}
}
}
}
if(nsaps.isEmpty()) return false;
int choice=randgen.nextInt(nsaps.size());
cn_destnsap.setValue((String)nsaps.get(choice));
cn_destport.setValue(((Integer)ports.get(choice)).intValue());
return true;
}
//helper function to get the random delay (if specified by user)
//return in ticks
private long getDelay() {
double delay=cn_delay.getValue();
if(cn_random_delay.getValue()) {
delay= -delay * Math.log(1.0 - randgen.nextDouble());
}
return SimClock.USec2Tick(delay);
}
//helper function to get the random transmission size (if specified by user)
//return in cells
private long getTransSize() {
double size=cn_trans_size.getValue();
if(cn_random_size.getValue()) {
size= -size * Math.log(1.0 - randgen.nextDouble());
}
return (long)(size * 1000000 / 424);
}
private void cn_receive_uni(SimEvent e) {
UNIInfo uni=(UNIInfo)e.getParams();
switch(uni.msgtype) {
case UNIInfo.UNI_SETUP:
//call setup from others
cn_curincome.setValue(cn_curincome.getValue()+1);
cn_curincome.update(theSim.now());
cn_totalincome.setValue(cn_totalincome.getValue()+1);
cn_totalincome.update(theSim.now());
break;
case UNIInfo.UNI_RELEASE:
//call release from others
cn_curincome.setValue(cn_curincome.getValue()-1);
cn_curincome.update(theSim.now());
break;
case UNIInfo.UNI_CALL_PROCEEDING:
if(cn_status==UNI_CALL_INIT && (uni.callref-65535)==cn_thisport.getValue()) {
//good, grab vpi/vci and update status
cn_vpi=uni.newvpi;
cn_vci=uni.newvci;
cn_status=UNI_CALL_PROC;
}
break;
case UNIInfo.UNI_CONNECT:
if(cn_status==UNI_CALL_PROC && (uni.callref-65535)==cn_thisport.getValue()) {
//good, call accepted!
cn_conaccept.setValue(cn_conaccept.getValue()+1);
cn_conaccept.update(theSim.now());
cn_cur_trans_size = getTransSize();
cn_num_sent = 0;
cn_status=UNI_ACTIVE;
theSim.enqueue(new SimEvent(MY_SENDCELL,this,this,theSim.now(),null));
}
break;
case UNIInfo.UNI_RELEASE_COMPLETE:
if((cn_status==UNI_CALL_PROC || cn_status==UNI_CALL_INIT) &&
(uni.callref-65535)==cn_thisport.getValue()) {
//bad, call rejected!
//perform retry after delay
theSim.enqueue(new SimEvent(MY_SENDCELL,this,this,theSim.now()+getDelay(),null));
cn_status=UNI_NULL;
}
break;
}
}
private void cn_receive(SimEvent e) {
Cell cell=(Cell)e.getParams();
cell=null; //ignore all incoming data cells...
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -