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

📄 analyzer.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.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*
  Packet Analyzer, needs to be updated whenever new packet types or
    protocol messages are developed in order to be able to
    decode those messages through the analyzer.

  NOTE: This class is currently NOT serializable, so user must declare it
          transient.
*/

public class Analyzer implements SimCommand,ActionListener {
  private AnalyzerUser theUser=null;
  private SimComponent theComp=null;
  private Sim theSim=null;
  private SimGUI theGUI=null;
  private SimDialog azDialog=null;
  private JList lstAnalyzer=null;
  private java.util.List txtZ=null;
  private java.util.List lastMsg=null;
  private boolean isHiding=false;

//constructor
  public Analyzer(SimComponent comp,AnalyzerUser user) {
    theComp=comp;
    theUser=user;
  }

  private String getMACString(long mac) {
    String str=Long.toHexString((mac>>40)&0xffL);
    str+=":"+Long.toHexString((mac>>32)&0xffL);
    str+=":"+Long.toHexString((mac>>24)&0xffL);
    str+=":"+Long.toHexString((mac>>16)&0xffL);
    str+=":"+Long.toHexString((mac>>8)&0xffL);
    str+=":"+Long.toHexString(mac&0xffL);
    return str;
  }

  private String getEtherType(int type) {
    switch(type) {
      case EtherFrame.ETHER_IP:
        return "IPv4";
      case EtherFrame.ETHER_ARP:
        return "ARP";
      case EtherFrame.ETHER_IP6:
        return "IPv6";
    }
    return "Unknown";
  }

  private String getIPString(int valueip) {
    String ip=String.valueOf((valueip>>24)&0xff)+".";
    ip+=String.valueOf((valueip>>16)&0xff)+".";
    ip+=String.valueOf((valueip>>8)&0xff)+".";
    ip+=String.valueOf(valueip&0xff);
    return ip;
  }

  private String getProtocolString(int protocol) {
    switch(protocol) {
      case IPPacket.PRO_UDP:
        return "UDP";
      case IPPacket.PRO_TCP:
        return "TCP";
    }
    return "Unknown";
  }

  private String getARPOpString(int opcode) {
    if(opcode==ARPPacket.ARP_REQUEST) return "REQUEST";
    else return "REPLY";
  }

  private String getUNIMsgString(int msg) {
    switch(msg) {
      case UNIInfo.UNI_SETUP: return "SETUP";
      case UNIInfo.UNI_CALL_PROCEEDING: return "CALL_PROCEEDING";
      case UNIInfo.UNI_CONNECT: return "CONNECT";
      case UNIInfo.UNI_CONNECT_ACK: return "CONNECT_ACK";
      case UNIInfo.UNI_RELEASE: return "RELEASE";
      case UNIInfo.UNI_RELEASE_COMPLETE: return "RELEASE_COMPLETE";
    }
    return "Unknown";
  }

  private String getUNIConString(int contype) {
    switch(contype) {
      case UNIInfo.CON_CBR: return "CBR";
      case UNIInfo.CON_VBR: return "VBR";
      case UNIInfo.CON_ABR: return "ABR";
      case UNIInfo.CON_UBR: return "UBR";
    }
    return "Unknown";
  }

//////////// main update here ////////////////////////////////////

  public void update(Bits bits,SimComponent from_link,SimComponent to_link) {
    String newMsg=null;

    if(bits instanceof EtherFrame) { //Ethernet frame
      EtherFrame frame=(EtherFrame)bits;

      newMsg=SimClock.getClockString(theSim.now())+"("+((long)SimClock.Tick2USec(theSim.now())%1000) +
                    ") - EtherFrame from "+(from_link==null?"?":from_link.getName())+" => "+(to_link==null?"?":to_link.getName());
      newMsg+="   src_mac="+getMACString(frame.src_mac)+"   dest_mac="+getMACString(frame.dest_mac);
      newMsg+="   len="+frame.len+"   type="+getEtherType(frame.type);

      if(frame.type==EtherFrame.ETHER_IP) {
        IPPacket packet=(IPPacket)frame.payload;

        newMsg+="   src_IP="+getIPString(packet.sourceIP)+"   dest_IP="+getIPString(packet.destIP)+
                "   TOS="+packet.TOS+"   Protocol="+getProtocolString(packet.protocol);

        if(packet.protocol==IPPacket.PRO_TCP) {
          TCPSegment seg=(TCPSegment)packet.payload;
          newMsg+="   src_port="+seg.sourceport+"   dest_port="+seg.destport+"   seq="+seg.seg_seq+"   len="+seg.seg_len+"   ";
          if(seg.syn) newMsg+="SYN ";
          if(seg.ack) newMsg+="ACK ("+seg.seg_ack+") ";
          if(seg.rst) newMsg+="RST ";
          if(seg.urg) newMsg+="URG ";
          if(seg.psh) newMsg+="PSH ";
          if(seg.fin) newMsg+="FIN ";
          newMsg+="  wnd="+seg.seg_wnd;
        }
      }
      else if(frame.type==EtherFrame.ETHER_ARP) {
        ARPPacket arp=(ARPPacket)frame.payload;

        newMsg+="   opcode="+getARPOpString(arp.opcode)+"   src_mac="+getMACString(arp.src_mac)+
                "   src_IP="+getIPString(arp.src_IP)+"   dest_mac="+getMACString(arp.dest_mac)+
                "   dest_IP="+getIPString(arp.dest_IP);
      }
    }
    else if(bits instanceof Cell) { //ATM cell
      Cell cell=(Cell)bits;

      newMsg=SimClock.getClockString(theSim.now())+"("+((long)SimClock.Tick2USec(theSim.now())%1000) +
                    ") - Cell from "+(from_link==null?"?":from_link.getName())+" => "+(to_link==null?"?":to_link.getName());
      newMsg+="   vpi="+cell.vpi+"   vci="+cell.vci+"   pti="+cell.pti;

      if(cell.vpi==0 && cell.vci==5 && (cell.pti & 0x01)==0x01) { //UNI signalling
        UNIInfo uni=(UNIInfo)cell.payload;

        newMsg+="   UNI msgtyp="+getUNIMsgString(uni.msgtype)+"   callref="+uni.callref+"   contype="+getUNIConString(uni.contype);
        newMsg+="   calledNSAP="+uni.calledNSAP+"   calledport="+uni.calledport;
      }
    }
    else {
      newMsg=SimClock.getClockString(theSim.now())+
                    " - Unknown bits from "+(from_link==null?"?":from_link.getName())+" => "+(to_link==null?"?":to_link.getName());
    }

    actualupdate(newMsg);
  }

  public synchronized void reset() {
    txtZ.clear();
    String msg="Analyzer started at "+SimClock.getClockString(theSim.now())+".";
    txtZ.add(msg);
    if(lstAnalyzer!=null) {
      ((DefaultListModel)lstAnalyzer.getModel()).clear();
      ((DefaultListModel)lstAnalyzer.getModel()).addElement(msg);
    }
    lastMsg.clear();
  }

  private synchronized void actualupdate(String msg) {
    if(txtZ!=null) {
      txtZ.add(msg);
      if(lstAnalyzer!=null) {
        lastMsg.add(msg);

        if(lastMsg.size()==1) {
          Runnable r=new Runnable() {
            public void run() {
              updateListBox();
            }
          };
          SwingUtilities.invokeLater(r);
        }
      }
    }
  }

  private synchronized void updateListBox() {
    if(lstAnalyzer!=null && lastMsg!=null) {
      while(!lastMsg.isEmpty())
        ((DefaultListModel)lstAnalyzer.getModel()).addElement(lastMsg.remove(0));
      lstAnalyzer.ensureIndexIsVisible(lstAnalyzer.getModel().getSize()-1);
    }
  }


///////////////////////// SimCommand interface ////////////////////////

  public java.util.List getCommand(Sim aSim,SimGUI aGUI) {
    java.util.List cmd=new java.util.ArrayList();

    JMenuItem menuItem=new JMenuItem("Analyzer");
    menuItem.addActionListener(this);
    cmd.add(menuItem);

    theSim=aSim;
    theGUI=aGUI;

    return cmd;
  }

  private void checkLinks() {
    java.util.List curcomps=theSim.getSimComponents();
    if(!curcomps.contains(theComp)) {
      if(azDialog!=null) azDialog.dispose();
    }
  }

  private synchronized void processWindowClosed() {
    azDialog=null;
    lstAnalyzer=null;
    if(!isHiding) {
      theUser.analyzerDown(this);
      txtZ=null;
      lastMsg=null;
    }
  }

  public synchronized void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Analyzer")) {
      if(azDialog==null) {
        azDialog=new SimDialog(theGUI.getMainFrame(),theComp.getName()+ " - Analyzer"
                                ,theSim,theGUI) {
            public void componentsChanged() { checkLinks(); }
          };
        isHiding=false;

        if(txtZ==null) {
          String msg="Analyzer started at "+SimClock.getClockString(theSim.now())+".";
          txtZ=new java.util.ArrayList();
          txtZ.add(msg);
          lastMsg=new java.util.ArrayList();
          theUser.analyzerUp(this);
        }
        lstAnalyzer=new JList(new DefaultListModel());
        for(int i=0;i<txtZ.size();i++)
          ((DefaultListModel)lstAnalyzer.getModel()).addElement(txtZ.get(i));
        lastMsg.clear();
        JScrollPane scrollPane=new JScrollPane(lstAnalyzer);
        JPanel outerpane=new JPanel();
        outerpane.setLayout(new BorderLayout());
        outerpane.add(scrollPane,BorderLayout.CENTER);
        JPanel cmdpane=new JPanel();
        JButton btn=new JButton("Clear");
        cmdpane.add(btn);
        btn.addActionListener(this);
        btn=new JButton("Hide");
        cmdpane.add(btn);
        btn.addActionListener(this);
        btn=new JButton("Remove");
        cmdpane.add(btn);
        btn.addActionListener(this);
        outerpane.add(cmdpane,BorderLayout.SOUTH);
        azDialog.getContentPane().add(outerpane);

        azDialog.addWindowListener(new WindowAdapter() {
            public void windowClosed(WindowEvent e) {
              processWindowClosed();
            }
          });
        azDialog.pack();
        Dimension dsize=new Dimension(400,300); //azDialog.getSize();
        Dimension scrsize=Toolkit.getDefaultToolkit().getScreenSize();
        azDialog.setBounds((scrsize.width-dsize.width)/2,(scrsize.height-dsize.height)/2,dsize.width,dsize.height);
        azDialog.show();
      }
      else {
        azDialog.requestFocus();
      }
    }
    else if(e.getActionCommand().equals("Clear")) {
      txtZ.clear();
      String msg="Analyzer restarted at "+SimClock.getClockString(theSim.now())+".";
      txtZ.add(msg);
      ((DefaultListModel)lstAnalyzer.getModel()).clear();
      ((DefaultListModel)lstAnalyzer.getModel()).addElement(msg);
      lastMsg.clear();
    }
    else if(e.getActionCommand().equals("Hide")) {
      isHiding=true;
      azDialog.dispose();
    }
    else if(e.getActionCommand().equals("Remove")) {
      azDialog.dispose();
    }
  }
}

⌨️ 快捷键说明

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