⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpapp.java

📁 一个小型网络仿真器的实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
   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 TCPApp extends SimComponent implements java.io.Serializable {
  private class TCPCon implements TCPUser,java.io.Serializable {
    Object key=null;
    SimParamIP con_sourceip=null, con_destip=null;
    SimParamInt con_sourceport=null, con_destport=null;
    java.util.List paramset=null;
    SimParamInt cn_conattempt=null;
    SimParamInt cn_throughput=null;
    long last_throughput_mark;
    long total_bytes=0;
    long total_throughput_period=0;
    int cn_cur_trans_size=0; //in bytes
    int cn_con_done=0;
    int cn_num_sent=0;
    java.util.List oldsourceport=null;

    public void notify(int id,Object msg) {
      switch(id) {
        case TCPUser.SENT_ACK:
          cn_num_sent+=((Integer)msg).intValue();

          //update throughput
          cn_throughput.setValue((int)((total_bytes+cn_num_sent)/
                                  SimClock.Tick2Sec(total_throughput_period+
                                    (theSim.now()-last_throughput_mark))),
                                  theSim.now(),cn_log_factor.getValue());

          if(cn_num_sent>=cn_cur_trans_size) {
            total_bytes+=cn_num_sent;
            total_throughput_period+=theSim.now()-last_throughput_mark;
            cn_num_sent=0;

            cn_tcp.getTCP().close(key);
          }
          break;
        case TCPUser.CON_CLOSED:
          cn_con_done++;
          theSim.enqueue(new SimEvent(MY_START,me(),me(),theSim.now()+getDelay(),this));
          break;
        case TCPUser.CON_ESTABLISHED:
          if(cn_cur_trans_size>0) { //allow no data connection (open only)
            TCPBuffer buf=new TCPBuffer();
            buf.len=cn_cur_trans_size;
            cn_tcp.getTCP().send(key,buf);
            last_throughput_mark=theSim.now();
          }
          break;
        case TCPUser.CON_RESET:
        case TCPUser.CON_REFUSED:
          theSim.enqueue(new SimEvent(MY_START,me(),me(),theSim.now()+getDelay(),this));
          break;
        case TCPUser.CON_TIMEWAIT_TIMEOUT:
          Integer srcport=(Integer)msg;
          if(srcport.intValue()!=con_sourceport.getValue()) { //it's an old one
            if(!cn_tcp.getTCP().isSrcportInUse(srcport.intValue())) {
              //not used anymore, unregister it
              Integer [] parms=new Integer[2];
              parms[0]=srcport;
              parms[1]=new Integer(MY_PROTOCOL);
              neighbor(0).compInfo(IPRouter.REMOVE_PORT,me(),parms);
              //remove from oldsourceport list as well
              java.util.Iterator it=oldsourceport.iterator();
              while(it.hasNext()) {
                Integer oldport=(Integer)it.next();
                if(oldport.intValue()==srcport.intValue()) it.remove();
              }
            }
          }
          break;
      }
    }
  }

  private SimParamInt cn_start_time;
  private SimParamDouble cn_trans_size1;
  private SimParamDouble cn_trans_size2;
  private SimParamInt cn_repeat;
  private SimParamInt cn_delay;
  private SimParamInt cn_start_delay;
  private SimParamBool cn_random_size;
  private SimParamBool cn_random_delay;
  private SimParamBool cn_random_target;
  private SimParamBool cn_name_seed;
  private SimParamDouble cn_ai;
  private SimParamInt cn_log_factor;
  private SimParamIP cn_destip;
  private SimParamInt cn_destport;
  private SimParamInt cn_throughput;
  private SimParamTCP cn_tcp;
  private SimPFInt cn_con_num;
  private SimPFInt cn_cur_con;

  private int cur_showing=0;

  private java.util.Random randgen;

  private java.util.List cons;

  private static final int MY_PROTOCOL = IPPacket.PRO_TCP;

//private events
  private static final int MY_START = SimProvider.EV_PRIVATE + 1;
  private static final int AVERAGING_INTERVAL = SimProvider.EV_PRIVATE + 2;

//compInfo IDs
  //SimProvider.CI_TRANSPORT_SEND implemented (forwarded ;)
  //SimProvider.CI_PF_INT implemented


  public TCPApp(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;
  }

  public void copy(SimComponent comp) {
    if(comp instanceof TCPApp) {
      TCPApp theComp=(TCPApp)comp;
      cn_start_time.setValue(theComp.cn_start_time.getValue());
      cn_trans_size1.setValue(theComp.cn_trans_size1.getValue());
      cn_trans_size2.setValue(theComp.cn_trans_size2.getValue());
      cn_repeat.setValue(theComp.cn_repeat.getValue());
      cn_delay.setValue(theComp.cn_delay.getValue());
      cn_start_delay.setValue(theComp.cn_start_delay.getValue());
      cn_random_size.setValue(theComp.cn_random_size.getValue());
      cn_random_delay.setValue(theComp.cn_random_delay.getValue());
      cn_random_target.setValue(theComp.cn_random_target.getValue());
      cn_name_seed.setValue(theComp.cn_name_seed.getValue());
      cn_ai.setValue(theComp.cn_ai.getValue());
      cn_log_factor.setValue(theComp.cn_log_factor.getValue());
      cn_destip.setValue(theComp.cn_destip.getValue());
      cn_destport.setValue(theComp.cn_destport.getValue());
      cn_con_num.setValue(theComp.cn_con_num.getValue());
    }
  }

  public void reset() {
    cn_throughput.setValue(0);
    cn_throughput.update(theSim.now());
    cn_tcp.reset();

    for(int i=0;i<cons.size();i++) {
      TCPCon con=(TCPCon)cons.get(i);
      if(con.con_sourceport.getValue()!=0 && neighborCount()>0) {
        //release port number to host
        Integer [] parms=new Integer[2];
        parms[0]=new Integer(con.con_sourceport.getValue());
        parms[1]=new Integer(MY_PROTOCOL);
        neighbor(0).compInfo(IPRouter.REMOVE_PORT,this,parms);
        con.con_sourceport.setValue(0);
      }
      con.cn_conattempt.setValue(0);
      con.cn_conattempt.update(theSim.now());
      con.cn_throughput.setValue(0);
      con.cn_throughput.update(theSim.now());
      con.cn_con_done=0;
      con.cn_num_sent=0;
      con.total_bytes=0;
      con.total_throughput_period=0;

      //unregister old source ports as well
      if(neighborCount()>0) {
        for(int j=0;j<con.oldsourceport.size();j++) {
          Integer [] parms=new Integer[2];
          parms[0]=(Integer)con.oldsourceport.get(j);
          parms[1]=new Integer(MY_PROTOCOL);
          neighbor(0).compInfo(IPRouter.REMOVE_PORT,this,parms);
        }
      }
      con.oldsourceport.clear();
    }
  }

  public void start() {
    if(neighborCount()==0) return; //must not send without a neighbor!

    if(cn_name_seed.getValue()==true)
      randgen.setSeed(getName().hashCode());

    int i;
    for(i=0;i<randgen.nextInt(100);i++) randgen.nextDouble();

    theSim.enqueue(new SimEvent(AVERAGING_INTERVAL,this,this,
                                theSim.now()+SimClock.USec2Tick(cn_ai.getValue()),null));

    fill_connection();
    for(i=0;i<cn_con_num.getValue();i++) {
      TCPCon con=(TCPCon)cons.get(i);
      con.con_sourceip.setValue(((Integer)neighbor(0).compInfo(IPRouter.GET_IP,this,null)).intValue());
      if(con.con_sourceport.getValue()==0) {
        con.con_sourceport.setValue(((Integer)neighbor(0).compInfo(IPRouter.GET_PORT,this,new Integer(MY_PROTOCOL))).intValue());
      }
      myStart(con);
    }
  }

  public void action(SimEvent e) {
    switch(e.getType()) {
      case MY_START:
        myStart((TCPCon)e.getParams());
        break;
      case SimProvider.EV_RECEIVE:
        cn_receive(e);
        break;
      case AVERAGING_INTERVAL:
        cn_averaging_interval();
        theSim.enqueue(new SimEvent(AVERAGING_INTERVAL,this,this,
                                    theSim.now()+SimClock.USec2Tick(cn_ai.getValue()),null));
        break;
    }
  }

////compInfo of TCPApp:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -