📄 udp_cbr.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 UDP_CBR extends SimComponent implements java.io.Serializable {
private SimParamDouble cn_bit_rate;
private SimParamInt cn_start_time;
private SimParamInt cn_packet_size;
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 SimParamIP cn_destip;
private SimParamInt cn_destport;
private SimParamInt cn_conattempt;
private int cn_status;
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 CON_NULL = 0;
private static final int CON_ACTIVE = 1;
private static final int MY_PROTOCOL = IPPacket.PRO_UDP;
//private events
private static final int MY_SENDCELL = SimProvider.EV_PRIVATE + 1;
private static final int MY_START = SimProvider.EV_PRIVATE + 2;
public UDP_CBR(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("Router"))
return true;
return false;
}
protected void neighborAdded(SimComponent comp) {
Integer port=(Integer)comp.compInfo(IPRouter.GET_PORT,this,new Integer(MY_PROTOCOL));
cn_thisport=new SimParamInt("Port number",this,theSim.now(),false,false,port.intValue());
cn_destip=new SimParamIP("Destination IP",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);
addParameter(cn_thisport);
addParameter(cn_destip);
addParameter(cn_destport);
addParameter(cn_conattempt);
}
protected void neighborRemoved(SimComponent comp) {
if(cn_thisport!=null) {
Object [] paramlist=new Object[2];
paramlist[0]=new Integer(cn_thisport.getValue());
paramlist[1]=new Integer(MY_PROTOCOL);
comp.compInfo(IPRouter.REMOVE_PORT,this,paramlist);
removeParameter(cn_thisport);
removeParameter(cn_destip);
removeParameter(cn_destport);
removeParameter(cn_conattempt);
cn_thisport=null;
}
}
public void copy(SimComponent comp) {
if(comp instanceof UDP_CBR) {
UDP_CBR theComp=(UDP_CBR)comp;
cn_bit_rate.setValue(theComp.cn_bit_rate.getValue());
cn_start_time.setValue(theComp.cn_start_time.getValue());
cn_packet_size.setValue(theComp.cn_packet_size.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=CON_NULL;
cn_con_done=0;
if(neighborCount()!=0) {
cn_conattempt.setValue(0);
cn_conattempt.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;
}
}
///////////////////// 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_packet_size=new SimParamInt("Packet size (bytes, 46-1500)",this,ctick,false,true,576);
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_packet_size);
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 CON_NULL: //null, so start a new transmission round
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...
}
cn_conattempt.setValue(cn_conattempt.getValue()+1);
cn_conattempt.update(theSim.now());
cn_cur_trans_size = getTransSize();
cn_num_sent=0;
cn_status = CON_ACTIVE;
theSim.enqueue(new SimEvent(MY_SENDCELL,this,this,theSim.now(),null));
}
break;
case CON_ACTIVE: //active, so it's time to send data cells
if(cn_num_sent < cn_cur_trans_size) {
UDPPacket udppacket=new UDPPacket();
udppacket.sourceport=cn_thisport.getValue();
udppacket.destport=cn_destport.getValue();
udppacket.len=cn_packet_size.getValue()-28; //minus IP & UDP header size
IPPacket packet=new IPPacket();
//the router/host will fill in the source IP
packet.destIP=cn_destip.getValue();
packet.protocol=MY_PROTOCOL;
packet.len=cn_packet_size.getValue();
packet.payload=udppacket;
theSim.enqueue(new SimEvent(SimProvider.EV_RECEIVE,this,
neighbor(0),theSim.now(),packet));
long interval=SimClock.USec2Tick(cn_packet_size.getValue()*8/cn_bit_rate.getValue());
theSim.enqueue(new SimEvent(MY_SENDCELL,this,this,theSim.now()+interval,null));
cn_num_sent++;
}
else { //it's time for next transmission round...
cn_con_done++;
cn_status = CON_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 ips=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 host
if(comp.getCompClass().equals("Router")) {
java.util.List thisips=(java.util.List)comp.compInfo(IPRouter.GET_ALL_IP,this,null);
java.util.List thisports=(java.util.List)comp.compInfo(IPRouter.GET_ALL_PORT,this,null);
for(j=0;j<thisips.size();j++) {
for(k=0;k<thisports.size();k+=2) {
int thisprotocol=((Integer)thisports.get(k+1)).intValue();
if(thisprotocol==MY_PROTOCOL) {
ips.add(thisips.get(j));
ports.add(thisports.get(k));
}
}
}
}
}
if(ips.isEmpty()) return false;
int choice=randgen.nextInt(ips.size());
cn_destip.setValue(((Integer)ips.get(choice)).intValue());
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 number of packets
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 / (cn_packet_size.getValue() * 8));
}
private void cn_receive(SimEvent e) {
//ignore all incoming packets...
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -