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

📄 tcpapp.java

📁 一个小型网络仿真器的实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
////(refer constant definitions near the beginning of the class declaration)
  public Object compInfo(int infoid,SimComponent src,Object paramlist) {
    switch(infoid) {
      case SimProvider.CI_TRANSPORT_SEND:
        if(neighborCount()==0) break;
        return  neighbor(0).compInfo(infoid,src,paramlist);
      case SimProvider.CI_PF_INT:
        SimPFInt pfint=(SimPFInt)paramlist;
        if(pfint==cn_cur_con) {
          show_connection();
        }
        break;
    }
    return null;
  }

///////////////////// private methods ////////////////////////////////

  private void cn_create() {
    cons=new java.util.ArrayList();

    //Initialize the parameters
    long ctick=theSim.now();
    cn_start_time=new SimParamInt("Start time (usecs)",this,ctick,false,true,0);
    cn_trans_size1=new SimParamDouble("Transmission size (KByte) (from)",this,ctick,false,true,0);
    cn_trans_size2=new SimParamDouble("Transmission size (KByte) (to)",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_start_delay=new SimParamInt("Starting delay (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",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);
    cn_ai=new SimParamDouble("Averaging interval (usec)",this,ctick,false,true,100000.0);
    cn_log_factor=new SimParamInt("Logging every (ticks) (e.g. 1, 100)",this,ctick,false,true,0);
    cn_destip=new SimParamIP("Destination IP (non-random)",this,ctick,false,true,0);
    cn_destport=new SimParamInt("Destination port (non-random)",this,ctick,false,true,0);
    cn_throughput=new SimParamInt("Overall throughput (bytes/sec)",this,ctick,true,false,0);
    cn_tcp=new SimParamTCP("TCP",this,ctick);
    cn_con_num=new SimPFInt("Number of source",this,ctick,false,true,1);
    cn_cur_con=new SimPFInt("Show Connection #",this,ctick,false,true,0);

    addParameter(cn_start_time);
    addParameter(cn_trans_size1);
    addParameter(cn_trans_size2);
    addParameter(cn_repeat);
    addParameter(cn_delay);
    addParameter(cn_start_delay);
    addParameter(cn_random_size);
    addParameter(cn_random_delay);
    addParameter(cn_random_target);
    addParameter(cn_name_seed);
    addParameter(cn_log_factor);
    addParameter(cn_destip);
    addParameter(cn_destport);
    addParameter(cn_throughput);
    addParameter(cn_tcp);
    addParameter(cn_con_num);
    addParameter(cn_cur_con);
  }

  private TCPCon create_connection() {
    TCPCon con=new TCPCon();
    con.con_sourceip=new SimParamIP("Source IP",this,theSim.now(),false,false,0);
    con.con_sourceport=new SimParamInt("Source port",this,theSim.now(),false,false,0);
    con.con_destip=new SimParamIP("Destination IP",this,theSim.now(),false,false,0);
    con.con_destport=new SimParamInt("Destination port",this,theSim.now(),false,false,0);
    con.paramset=cn_tcp.getTCP().createParamset();
    con.cn_conattempt=new SimParamInt("Calls attempted",this,theSim.now(),true,false,0);
    con.cn_conattempt.update(theSim.now());
    con.cn_throughput=new SimParamInt("Average throughput (Bytes/sec)",this,theSim.now(),true,false,0);
    con.cn_throughput.update(theSim.now());
    con.oldsourceport=new java.util.ArrayList();
    return con;
  }

  private void fill_connection() {
    while(cn_con_num.getValue()>cons.size()) {
      cons.add(create_connection());
    }
  }

  private void show_connection() {
    int i;
    TCPCon con;

    if(cn_cur_con.getValue()>cn_con_num.getValue() ||
        cn_cur_con.getValue()<0) {
      cn_cur_con.setValue(0);
    }
    if(cur_showing>0) {
      if(cur_showing!=cn_cur_con.getValue()) {
        con=(TCPCon)cons.get(cur_showing-1);
        java.util.List parms=new java.util.LinkedList();
        parms.add(con.con_sourceip);
        parms.add(con.con_sourceport);
        parms.add(con.con_destip);
        parms.add(con.con_destport);
        for(i=0;i<con.paramset.size();i++) {
          parms.add(con.paramset.get(i));
        }
        parms.add(con.cn_conattempt);
        parms.add(con.cn_throughput);
        removeParameters(parms);
        cur_showing=0;
      }
      else return;
    }
    if(cn_cur_con.getValue()==0) return;

    while(cn_cur_con.getValue()>cons.size()) {
      cons.add(create_connection());
    }

    con=(TCPCon)cons.get(cn_cur_con.getValue()-1);
    java.util.List parms=new java.util.LinkedList();
    parms.add(con.con_sourceip);
    parms.add(con.con_sourceport);
    parms.add(con.con_destip);
    parms.add(con.con_destport);
    for(i=0;i<con.paramset.size();i++) {
      parms.add(con.paramset.get(i));
    }
    parms.add(con.cn_conattempt);
    parms.add(con.cn_throughput);
    addParameters(parms);
    cur_showing=cn_cur_con.getValue();
  }

  private void cn_averaging_interval() {
    //update overall throughput
    long total=0;
    for(int i=0;i<cn_con_num.getValue();i++) {
      TCPCon con=(TCPCon)cons.get(i);
      total+=con.total_bytes+con.cn_num_sent;
    }
    cn_throughput.setValue((int)(total/SimClock.Tick2Sec(theSim.now())),
                            theSim.now(),cn_log_factor.getValue());
  }

  private void myStart(TCPCon con) {
    if(SimClock.USec2Tick(cn_start_time.getValue()) <= theSim.now()) {
      if(cn_repeat.getValue()>con.cn_con_done || cn_repeat.getValue()==-1) {
        if(cn_random_target.getValue()) {
          if(!setRandomTarget(con)) return; //bye bye if no targets at all...
        }
        else { //fix target
          if(cn_destip.getValue()!=0 && cn_destport.getValue()!=0) {
            con.con_destip.setValue(cn_destip.getValue());
            con.con_destport.setValue(cn_destport.getValue());
          }
          else return; //bye byte if a proper target is not set
        }

        con.cn_conattempt.setValue(con.cn_conattempt.getValue()+1);
        con.cn_conattempt.update(theSim.now());

        con.cn_cur_trans_size = getTransSize();

        if(con.key!=null) { //got a possible old connection
          cn_tcp.getTCP().newParamset(con.key);
        }
        while((con.key=cn_tcp.getTCP().open(con.con_sourceip.getValue(),con.con_sourceport.getValue(),
                                            con.con_destip.getValue(),con.con_destport.getValue(),
                                            false,con,con.paramset))==null) {
          //most probably previous connection in TIME_WAIT state,
          //must save the old source port and get a new one
          con.oldsourceport.add(new Integer(con.con_sourceport.getValue()));
          con.con_sourceport.setValue(((Integer)neighbor(0).compInfo(IPRouter.GET_PORT,this,new Integer(MY_PROTOCOL))).intValue());
        }
      }
    }
    else {
      long interval=SimClock.USec2Tick(cn_start_time.getValue());
      interval+=getStartDelay();
      theSim.enqueue(new SimEvent(MY_START,this,this,theSim.now()+interval,con));
    }
  }

//helper function to set a random destination
//return false if can't find a target
  private boolean setRandomTarget(TCPCon con) {
    java.util.List allcomps=theSim.getSimComponents();
    java.util.List ips=new java.util.ArrayList();

    int i;
    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")) {
        Integer ip=(Integer)comp.compInfo(IPRouter.GET_IP,this,null);
        if(ip.intValue()!=0x7f000001) //not adding localhost ip
          ips.add(ip);
      }
    }
    if(ips.isEmpty()) return false;

    int choice=randgen.nextInt(ips.size());
    con.con_destip.setValue(((Integer)ips.get(choice)).intValue());
    con.con_destport.setValue(IPRouter.TCP_LISTEN_PORT);
    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 start delay (if specified by user)
//return in ticks
  private long getStartDelay() {
    double delay=cn_start_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 int getTransSize() {
    double size=cn_trans_size1.getValue();
    if(cn_random_size.getValue()) {
      double delta=cn_trans_size2.getValue()-cn_trans_size1.getValue();
      size+=randgen.nextDouble()*delta;
    }
    if(size<0) size=0;
    return (int)size * 1024;
  }

  private void cn_receive(SimEvent e) {
    cn_tcp.receive_ip((IPPacket)e.getParams(),null);
  }

  private SimComponent me() {
    return this;
  }
}

⌨️ 快捷键说明

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