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

📄 echo.java

📁 一个小型网络仿真器的实现
💻 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.*;

//Implements the UDP version of the Echo protocol.
//Purpose: to demonstrate a lightweight, standalone UDP user as
//          an IPProtocol attached to an IPRouter

public class Echo implements UDPUser,UDPProvider,IPProtocol,java.io.Serializable {
  private UDP udp;
  private Object udp_key;
  private IPRouter owner;

  public static int ECHO_PORT = 7;

  public Echo(IPRouter owner) {
    this.owner = owner;
    udp=new UDP(owner,this);

    //register the Echo port
    Object [] parms=new Object[3];
    parms[0]=new Integer(ECHO_PORT);
    parms[1]=new Integer(IPPacket.PRO_UDP);
    parms[2]=this;
    if(owner.compInfo(IPRouter.GET_PRV_PORT,null,parms)==null) {
      System.out.println("Warning: ECHO PORT occupied!");
    }
  }

////////////////// IPProtocol methods //////////////////
  public void reset() {
    udp.reset();
  }
  public void start() {
    int srcip=((Integer)owner.compInfo(IPRouter.GET_IP,null,null)).intValue();
    udp_key=udp.open(srcip,ECHO_PORT,0,0,this);
  }
  public void receive_ip(IPPacket packet,SimComponent fromlink) {
    udp.receive_ip(packet,fromlink);
  }

///////////////////// UDPUser methods ///////////////////////

  public void receive_packet(int src_ip,int src_port,SimComponent fromlink,
                                UDPBuffer buf) {
    //simply echo anything received back
    udp.send(udp_key,buf,src_ip,src_port);
  }

///////////////////// UDPProvider methods ////////////////////

  public double getDelay() {
    return 0;
  }
  public double getDelayVar() {
    return 0;
  }
  public void sendPacket(IPPacket packet) {
    owner.compInfo(SimProvider.CI_TRANSPORT_SEND,null,packet);
  }
  public void sendPacket(IPPacket packet,SimComponent tolink) {
    Object [] parms=new Object[2];
    parms[0]=packet;
    parms[1]=tolink;
    owner.compInfo(SimProvider.CI_TRANSPORT_SEND2LINK,null,parms);
  }
  public void statusChanged() {
  }

///////////////////// For echo test ////////////////////////////

  private class TestEcho implements UDPUser,java.io.Serializable {
    Object test_key=null;
    int id=0;

    public void receive_packet(int src_ip,int src_port,SimComponent fromlink,
                                UDPBuffer buf) {
      int backid=((Integer)buf.payload).intValue();
      System.out.println("Echo ("+owner.getName()+"): Test datagram ID "+backid+
                    " received at "+SimClock.Tick2Sec(owner.getSim().now())+
                    " from "+SimParamIP.IP2String(src_ip));
    }
  }

  public void test(int dest_ip) {
    TestEcho t=new TestEcho();
    t.id=(int)(Math.random()*10000);
    System.out.println("Echo ("+owner.getName()+"): Test datagram ID "+t.id+
              " sent at "+SimClock.Tick2Sec(owner.getSim().now())+
              " to "+SimParamIP.IP2String(dest_ip));

    int srcip=((Integer)owner.compInfo(IPRouter.GET_IP,null,null)).intValue();
    t.test_key=udp.open(srcip,ECHO_PORT,dest_ip,ECHO_PORT,t);

    UDPBuffer buf=new UDPBuffer(50,new Integer(t.id));
    udp.send(t.test_key,buf);
  }
}

⌨️ 快捷键说明

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