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

📄 simparamftable.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.*;
import java.io.Serializable;

class ForwardKey implements Serializable {
  short vpi,vci;
  SimComponent link;

  ForwardKey(int avpi,int avci,SimComponent comp) {
    vpi=(short)avpi; vci=(short)avci; link=comp;
  }
  public boolean equals(Object o) {
    if(!(o instanceof ForwardKey)) return false;
    ForwardKey k=(ForwardKey)o;
    if(vpi==k.vpi && vci==k.vci && link==k.link) return true;
    return false;
  }
  public int hashCode() {
    return ((vpi<<16) | vci);
  }
}

class ForwardEntry implements Serializable {
  ForwardKey fromkey=null; //this is the key for the forward table
  ForwardKey tokey=null; //this is the key for reverse table
  int contype;
  boolean svc=false;
}

public class SimParamFTable extends SimParameter implements ActionListener,java.io.Serializable {
  private java.util.Map ftable; //the forward table
  private java.util.Map rtable; //the reverse table
  private transient JComponent jcomp=null;
  private transient SimDialog ftableDialog=null;
  private transient JTable table=null;

  public SimParamFTable(String aName,SimComponent comp,long creationTick) {
    super(aName,comp,creationTick,false);

    ftable=new java.util.HashMap();
    rtable=new java.util.HashMap();
  }

////////////// possible overrides //////////////////////////

  public JComponent getJComponent() {
    if(jcomp==null) {
      jcomp=new JButton("Manage...");
      ((JButton)jcomp).setHorizontalAlignment(JButton.LEFT);
      ((JButton)jcomp).addActionListener(this);
    }
    return jcomp;
  }

////////////// services /////////////////////////////////////

  public ForwardEntry lookup(int vpi,int vci,SimComponent from_link) {
    return (ForwardEntry)ftable.get(new ForwardKey(vpi,vci,from_link));
  }

  public boolean fromkeyExist(ForwardKey fk) {
    return (ftable.get(fk) != null);
  }

  public boolean tokeyExist(ForwardKey fk) {
    return (rtable.get(fk) != null);
  }

  public void addNewEntry(ForwardEntry fe) {
    ftable.put(fe.fromkey,fe);
    rtable.put(fe.tokey,fe);

    if(ftableDialog!=null) {
      table.revalidate();
      ftableDialog.repaint();
    }
  }

  public void removeEntry(ForwardEntry fe) {
    ftable.remove(fe.fromkey);
    rtable.remove(fe.tokey);

    if(ftableDialog!=null) {
      table.revalidate();
      ftableDialog.repaint();
    }
  }

  public void clearAllSVC() {
    java.util.Iterator it=ftable.values().iterator();
    while(it.hasNext()) {
      ForwardEntry fe=(ForwardEntry)it.next();
      if(fe.svc==true) {
        rtable.remove(fe.tokey);
        it.remove();
      }
    }

    if(ftableDialog!=null) {
      table.revalidate();
      ftableDialog.repaint();
    }
  }

////////////// private /////////////////////////////////////

  private class DialogListener implements ActionListener {
    private JComboBox inlinks,outlinks;
    private JTextField invpi,outvpi,invci,outvci;
    private java.util.List neighbors;

    DialogListener(JComboBox inl,JTextField ivpi,JTextField ivci,
                    JComboBox outl,JTextField ovpi,JTextField ovci,
                    java.util.List nbors) {
      inlinks=inl; outlinks=outl;
      invpi=ivpi; invci=ivci;
      outvpi=ovpi; outvci=ovci;
      neighbors=nbors;
    }

    private boolean checkRunning() {
      if(getOwner().getSim().isRunning()) {
        JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Simulation is running!",
          "Command not allowed",JOptionPane.WARNING_MESSAGE);
        return false;
      }
      return true;
    }

    public void actionPerformed(ActionEvent e) {
      String command=e.getActionCommand();

      if(command.equals("Add")) {
        if(!checkRunning()) return;

        try {
          int vpi1,vci1,vpi2,vci2;
          SimComponent comp1,comp2;
          ForwardKey fk1,fk2;

          vpi1=Integer.parseInt(invpi.getText().trim());
          vci1=Integer.parseInt(invci.getText().trim());
          comp1=(SimComponent)neighbors.get(inlinks.getSelectedIndex());
          vpi2=Integer.parseInt(outvpi.getText().trim());
          vci2=Integer.parseInt(outvci.getText().trim());
          comp2=(SimComponent)neighbors.get(outlinks.getSelectedIndex());

          fk1=new ForwardKey(vpi1,vci1,comp1);
          fk2=new ForwardKey(vpi2,vci2,comp2);
          if(fromkeyExist(fk1) || tokeyExist(fk2)) {
            JOptionPane.showMessageDialog(ftableDialog,"VPI/VCI already used.",
              "Error",JOptionPane.INFORMATION_MESSAGE);
            return;
          }

          if(vpi1<1 || vpi2<1 || vpi1>255 || vpi2>255 ||
             vci1<1 || vci2<1 || vci1>65535 || vci2>65535) {
            JOptionPane.showMessageDialog(ftableDialog,"VPI/VCI not in valid range.",
              "Error",JOptionPane.INFORMATION_MESSAGE);
            return;
          }

          ForwardEntry fe=new ForwardEntry();
          fe.fromkey=fk1;
          fe.tokey=fk2;
          fe.contype=UNIInfo.CON_UBR;
          fe.svc=false;
          ftable.put(fk1,fe);
          rtable.put(fk2,fe);
        } catch(NumberFormatException ex) {
          JOptionPane.showMessageDialog(ftableDialog,ex.toString(),
            "Error",JOptionPane.INFORMATION_MESSAGE);
          return;
        }

      //finally, refresh the table
        table.revalidate();
        ftableDialog.repaint();
      }
      else if(command.equals("Remove")) {
        if(!checkRunning()) return;

        int [] sels=table.getSelectedRows();
        ForwardEntry [] fearray=(ForwardEntry [])ftable.values().toArray(new ForwardEntry[0]);
        java.util.List selcomps=new java.util.ArrayList();
        for(int i=0;i<sels.length;i++) {
          if(fearray[sels[i]].svc) {
            JOptionPane.showMessageDialog(ftableDialog,"Not removing SVC entry.",
              "Error",JOptionPane.INFORMATION_MESSAGE);
          }
          else selcomps.add(fearray[sels[i]]);
        }
        ftable.values().removeAll(selcomps);
        rtable.values().removeAll(selcomps);
        table.getSelectionModel().clearSelection();
        table.revalidate();
        ftableDialog.repaint();
      }
      else if(command.equals("Close")) {
        ftableDialog.dispose();
      }
    }
  }

//helper for generating the combobox contents
// and fill in the neighbor list
  private String [] getLinks(java.util.List links) {
    int i;
    for(i=0;i<getOwner().neighborCount();i++) {
      if(getOwner().neighbor(i).getCompClass().equals("Link"))
        links.add(getOwner().neighbor(i));
    }
    String [] names=new String[links.size()];
    for(i=0;i<names.length;i++) {
      names[i]=((SimComponent)links.get(i)).getName();
    }
    return names;
  }

//helper for monitoring changes in the simulation (e.g. to refresh table)
  private void checkLinks() {
    int i;

    //check if there are still neighbors...
    if(getOwner().neighborCount()==0) {
      //remove all entries
      ftable.clear();
      rtable.clear();
      if(ftableDialog!=null) ftableDialog.dispose();
      return;
    }

    //check for removal of neighbors...
    java.util.List removed=new java.util.ArrayList();
    java.util.Iterator it=ftable.values().iterator();
    while(it.hasNext()) {
      ForwardEntry fe=(ForwardEntry)it.next();
      for(i=0;i<getOwner().neighborCount();i++) {
        if(getOwner().neighbor(i)==fe.fromkey.link) break;
      }
      if(i==getOwner().neighborCount()) {
        removed.add(fe);
        it.remove();
        continue;
      }
      for(i=0;i<getOwner().neighborCount();i++) {
        if(getOwner().neighbor(i)==fe.tokey.link) break;
      }
      if(i==getOwner().neighborCount()) {
        removed.add(fe);
        it.remove();
      }
    }
    rtable.values().removeAll(removed);

    //refresh dialog box if it's there...
    if(ftableDialog!=null) {
      ftableDialog.getContentPane().removeAll();
      JComponent newpane=createNewFTableDialog();
      ftableDialog.getContentPane().add(newpane);
      newpane.revalidate();
    }
  }

//the table model
  private class MyTableModel extends javax.swing.table.AbstractTableModel {
    public int getRowCount() {
      return ftable.size();
    }
    public int getColumnCount() {
      return 7;
    }
    public Object getValueAt(int row,int col) {
      ForwardEntry [] fe=(ForwardEntry [])ftable.values().toArray(new ForwardEntry[0]);
      switch(col) {
        case 0:
          return fe[row].fromkey.link.getName();
        case 1:
          return String.valueOf(fe[row].fromkey.vpi);
        case 2:
          return String.valueOf(fe[row].fromkey.vci);
        case 3:
          return fe[row].tokey.link.getName();
        case 4:
          return String.valueOf(fe[row].tokey.vpi);
        case 5:
          return String.valueOf(fe[row].tokey.vci);
        case 6:
          return String.valueOf(fe[row].svc);
      }
      return null;
    }
    public String getColumnName(int col) {
      switch(col) {
        case 0: return "In Link";
        case 1: return "In VPI";
        case 2: return "In VCI";
        case 3: return "Out Link";
        case 4: return "Out VPI";
        case 5: return "Out VCI";
        case 6: return "SVC";
      }
      return "N/A";
    }
  }

  private JComponent createNewFTableDialog() {
  //prepare the input panel
    JPanel inputPanel1=new JPanel();
    inputPanel1.add(new JLabel("In Link:"));
    java.util.List links=new java.util.ArrayList();
    String [] linknames=getLinks(links);
    JComboBox inlinks=new JComboBox(linknames);
    inlinks.setSelectedIndex(0);
    inputPanel1.add(inlinks);
    inputPanel1.add(new JLabel("In VPI:"));
    JTextField invpi=new JTextField(5);
    inputPanel1.add(invpi);
    inputPanel1.add(new JLabel("In VCI:"));
    JTextField invci=new JTextField(5);
    inputPanel1.add(invci);
    JPanel inputPanel2=new JPanel();
    inputPanel2.add(new JLabel("Out Link:"));
    JComboBox outlinks=new JComboBox(linknames);
    outlinks.setSelectedIndex(0);
    inputPanel2.add(outlinks);
    inputPanel2.add(new JLabel("Out VPI:"));
    JTextField outvpi=new JTextField(5);
    inputPanel2.add(outvpi);
    inputPanel2.add(new JLabel("Out VCI:"));
    JTextField outvci=new JTextField(5);
    inputPanel2.add(outvci);

  //prepare the table
    table=new JTable(new MyTableModel());
    JScrollPane scrollPane=new JScrollPane(table);
    table.setPreferredScrollableViewportSize(new Dimension(400,100));

  //prepare buttons panel
    DialogListener listener=new DialogListener(inlinks,invpi,invci,
                                               outlinks,outvpi,outvci,links);
    JPanel compPanel=new JPanel();
    JButton btnAdd=new JButton("Add");
    btnAdd.addActionListener(listener);
    compPanel.add(btnAdd);
    JButton btnRemove=new JButton("Remove");
    btnRemove.addActionListener(listener);
    compPanel.add(btnRemove);
    JButton btnClose=new JButton("Close");
    btnClose.addActionListener(listener);
    compPanel.add(btnClose);

  //now the outer panel
    JPanel outerpane=new JPanel();
    GridBagLayout gl=new GridBagLayout();
    outerpane.setLayout(gl);
    GridBagConstraints gc=new GridBagConstraints();
    gc.fill=GridBagConstraints.BOTH;
    gc.gridx=0; gc.gridy=0;
    gl.setConstraints(inputPanel1,gc);
    outerpane.add(inputPanel1);
    gc.gridy=1;
    gl.setConstraints(inputPanel2,gc);
    outerpane.add(inputPanel2);
    gc.gridy=2;
    gl.setConstraints(compPanel,gc);
    outerpane.add(compPanel);
    gc.gridy=3; gc.weightx=1; gc.weighty=1;
    gl.setConstraints(scrollPane,gc);
    outerpane.add(scrollPane);

    return outerpane;
  }

  public void actionPerformed(ActionEvent e) {
    if(ftableDialog==null) {
      ftableDialog=new SimDialog(theGUI.getMainFrame(),getOwner().getName()
                                  +" - "+getName(),getOwner().getSim(),theGUI) {
          public void connectionsChanged() { checkLinks(); }
          public void componentsChanged() { checkLinks(); }
        };

      ftableDialog.getContentPane().add(createNewFTableDialog());
      ftableDialog.addWindowListener(new WindowAdapter() {
          public void windowClosed(WindowEvent e) {
            ftableDialog=null;
          }
        });
      ftableDialog.pack();
      Dimension dsize=ftableDialog.getSize();
      Dimension scrsize=Toolkit.getDefaultToolkit().getScreenSize();
      ftableDialog.setLocation((scrsize.width-dsize.width)/2,(scrsize.height-dsize.height)/2);

      ftableDialog.show();
    }
    else
      ftableDialog.requestFocus();
  }
}

⌨️ 快捷键说明

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