📄 simparamiprtable.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 javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SimParamIPRTable extends SimParameter implements ActionListener,java.io.Serializable {
private java.util.List rtable;
private transient JComponent jcomp=null;
private transient SimDialog rtableDialog=null;
private transient JTable table=null;
public SimParamIPRTable(String aName,SimComponent comp,long creationTick) {
super(aName,comp,creationTick,false);
rtable=new java.util.ArrayList();
}
////////////// possible overrides //////////////////////////
public JComponent getJComponent() {
if(jcomp==null) {
jcomp=new JButton("Manage...");
((JButton)jcomp).setHorizontalAlignment(JButton.LEFT);
((JButton)jcomp).addActionListener(this);
}
return jcomp;
}
////////////// services /////////////////////////////////////
//return the correct outgoing component (well, should be a Link),
//return null if no match and no default gateway,
//NOTE: Current implementation is VERY slow for large number of route entries
public IPRouteEntry lookup(int IP) {
//perform longest match (the list is already sorted)
int i;
for(i=0;i<rtable.size();i++) {
IPRouteEntry r=(IPRouteEntry)rtable.get(i);
if((IP & r.mask) == r.ip) return r;
}
return null;
}
//add an entry into the route table
//return the insert point of the new entry (according to the descending sort)
//Warning: NO checking here (all check must be performed before insertion)
public int addNewEntry(int newIP,int newMask,int newNextIP,
SimComponent newNextHop,String type) {
//perform binary search to insert it (sort descending by mask)
int left=0;
int right=rtable.size()-1;
int insertpoint=0;
while(left<=right) {
int middle=(left+right)/2;
if(newMask==((IPRouteEntry)rtable.get(middle)).mask) {
do {
middle++;
if(middle==rtable.size()) break;
} while(newMask==((IPRouteEntry)rtable.get(middle)).mask);
insertpoint=middle;
break;
}
if(longerMask(((IPRouteEntry)rtable.get(middle)).mask,newMask)) {
left=middle+1;
insertpoint=left;
}
else {
insertpoint=middle;
right=middle-1;
}
}
IPRouteEntry newroute=new IPRouteEntry();
newroute.ip=newIP;
newroute.mask=newMask;
newroute.nexthopIP=newNextIP;
newroute.nexthop=newNextHop;
newroute.type=type;
rtable.add(insertpoint,newroute);
//finally, refresh the table
if(rtableDialog!=null) {
table.revalidate();
rtableDialog.repaint();
}
return insertpoint;
}
public void clearTable() {
clearTable(null);
}
public void clearTable(String type) {
if(type==null) rtable.clear();
else {
java.util.Iterator i=rtable.iterator();
while(i.hasNext()) {
IPRouteEntry aroute=(IPRouteEntry)i.next();
if(aroute.type.equals(type)) i.remove();
}
}
if(rtableDialog!=null) {
table.revalidate();
rtableDialog.repaint();
}
}
////////////// private /////////////////////////////////////
//helper, compare two ip mask
//return true if mask1 is longer than mask2
private boolean longerMask(int mask1,int mask2) {
if(mask1==0) return false;
return (mask1>mask2 || (mask1!=0 && mask2==0));
}
private class DialogListener implements ActionListener,FocusListener,
ListSelectionListener {
private JTextField txtIP,txtMask,txtNextIP;
private JComboBox comps;
private java.util.List neighbors;
private int newIP,newMask,newNextIP;
DialogListener(JTextField i,JTextField m,JTextField n,
JComboBox c,java.util.List nbors) {
txtIP=i;
txtMask=m;
txtNextIP=n;
comps=c;
neighbors=nbors;
txtMask.addFocusListener(this);
}
private boolean ipOK() {
String ip=txtIP.getText()+".";
if(ip.equals(".") || ip.equals("0.")) {
newIP=0;
return true;
}
int thisIndex,lastIndex=0;
int quad;
newIP=0;
for(int i=0;i<4;i++) {
thisIndex=ip.indexOf('.',lastIndex);
if(thisIndex==-1) return false;
try {
quad=Integer.parseInt(ip.substring(lastIndex,thisIndex));
if(quad<0 || quad>255) return false;
} catch(NumberFormatException ex) {
return false;
}
newIP<<=8;
newIP|=quad;
lastIndex=thisIndex+1;
}
return true;
}
private boolean maskOK() {
String mask=txtMask.getText().trim()+".";
if(mask.equals(".") || mask.equals("0.")) {
newMask=0;
return true;
}
int thisIndex,lastIndex=0;
int quad;
newMask=0;
for(int i=0;i<4;i++) {
thisIndex=mask.indexOf('.',lastIndex);
if(thisIndex==-1) return false;
try {
quad=Integer.parseInt(mask.substring(lastIndex,thisIndex));
if(quad<0 || quad>255) return false;
} catch(NumberFormatException ex) {
return false;
}
newMask<<=8;
newMask|=quad;
lastIndex=thisIndex+1;
}
//verify mask, is there a better way?
if(newMask==0 || newMask==0xffffffff) return true;
if(newMask>0) return false; //must have leading one!
mask=Integer.toBinaryString(newMask);
if(mask.lastIndexOf('1')!=(mask.indexOf('0')-1)) return false;
return true;
}
private boolean nextIPOK() {
String ip=txtNextIP.getText().trim()+".";
if(ip.equals(".")) {
newNextIP=0;
return true;
}
int thisIndex,lastIndex=0;
int quad;
newNextIP=0;
for(int i=0;i<4;i++) {
thisIndex=ip.indexOf('.',lastIndex);
if(thisIndex==-1) return false;
try {
quad=Integer.parseInt(ip.substring(lastIndex,thisIndex));
if(quad<0 || quad>255) return false;
} catch(NumberFormatException ex) {
return false;
}
newNextIP<<=8;
newNextIP|=quad;
lastIndex=thisIndex+1;
}
return true;
}
public void focusGained(FocusEvent e) {
if(!txtIP.getText().equals("") && txtMask.getText().equals("")) {
if(ipOK()) {
int quad=(newIP>>24)&0xff;
if(quad<128) txtMask.setText("255.0.0.0");
else if(quad<192) txtMask.setText("255.255.0.0");
else txtMask.setText("255.255.255.0");
txtMask.setCaretPosition(0);
txtMask.moveCaretPosition(txtMask.getText().length());
}
}
}
public void focusLost(FocusEvent e) {
}
public void valueChanged(ListSelectionEvent e) {
int [] sels=table.getSelectedRows();
if(sels.length==1) {
//fill in field values for this entry
IPRouteEntry entry=(IPRouteEntry)rtable.get(sels[0]);
txtIP.setText(getStringIP(entry.ip));
txtMask.setText(getStringIP(entry.mask));
txtNextIP.setText(getStringIP(entry.nexthopIP));
for(int i=0;i<neighbors.size();i++) {
SimComponent acomp=(SimComponent)neighbors.get(i);
if(acomp==entry.nexthop) {
comps.setSelectedIndex(i);
break;
}
}
txtNextIP.requestFocus();
txtNextIP.setCaretPosition(0);
txtNextIP.moveCaretPosition(txtNextIP.getText().length());
}
else {
//clear all fields
txtIP.setText("");
txtMask.setText("");
txtNextIP.setText("");
txtIP.requestFocus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -