📄 etherswitch.java
字号:
}
}
private void sw_my_receive(SimEvent e) {
EtherFrame frame=(EtherFrame)e.getParams();
ForwardEntry fe;
SimComponent src=(SimComponent)e.getSource();
Port voport=null;
long ticks;
fe=(ForwardEntry)sw_forward_table.get(new Long(frame.dest_mac));
if(fe==null) { //no forwarding info, must broadcast!
sw_broadcast(frame,src);
}
else {
voport=(Port)voports.get(fe.to_link);
voport.spq.add(frame);
voport.spq_size+= (frame.length()>>3);
if(voport.spq.size()==1) { //must schedule a processing slot if spq previously empty
ticks=SimClock.USec2Tick(frame.length() / (double)sw_speed.getValue());
theSim.enqueue(new SimEvent(MY_SLOT_TIME,this,this,theSim.now()+ticks,voport));
}
}
//test source mac
fe=(ForwardEntry)sw_forward_table.get(new Long(frame.src_mac));
if(fe==null) { //no entry yet, than add one
fe=new ForwardEntry();
fe.dest_mac=frame.src_mac;
fe.to_link=src;
sw_forward_table.put(new Long(fe.dest_mac),fe);
}
}
//////////////////// SimCommand services //////////////////////////
private transient SimGUI theGUI=null;
public SimCommand querySimCommand(int selectedCount) {
if(selectedCount==1) return this; //only allow this when exactly one
// switch is selected
else return null;
}
public void analyzerUp(Analyzer a) {
analyzer=a;
}
public void analyzerDown(Analyzer a) {
analyzer=null;
}
public java.util.List getCommand(Sim aSim,SimGUI aGUI) {
java.util.List cmd=new java.util.ArrayList();
JMenuItem menuItem=new JMenuItem("Set IP Subnet");
menuItem.addActionListener(this);
cmd.add(menuItem);
//analyzer
if(analyzer!=null) cmd.addAll(analyzer.getCommand(aSim,aGUI));
else cmd.addAll((new Analyzer(this,this)).getCommand(aSim,aGUI));
theGUI=aGUI;
return cmd;
}
private boolean checkRunning() {
if(theSim.isRunning()) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Simulation is running!",
"Command not allowed",JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
private class SubnetDialogListener implements ActionListener,FocusListener {
JDialog dialog=null;
JTextField txtIP=null, txtMask=null;
JComboBox comps=null;
JButton btnOK=null, btnCancel=null;
java.util.List links=null;
java.util.List routers=null;
int newIP,newMask,subnet_size=0;
private boolean ipOK() {
String ip=txtIP.getText()+".";
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()+".";
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);
int lastOne=mask.lastIndexOf('1');
int firstZero=mask.indexOf('0');
if(lastOne != firstZero-1) return false;
subnet_size=(1<<(31-lastOne)) - 2;
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 actionPerformed(ActionEvent e) {
if(e.getSource() == btnOK) {
if(!ipOK()) {
JOptionPane.showMessageDialog(null,"Not a valid IP address!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtIP.requestFocus();
return;
}
if(!maskOK()) {
JOptionPane.showMessageDialog(null,"Not a valid net mask!","Error",
JOptionPane.INFORMATION_MESSAGE);
txtMask.requestFocus();
return;
}
//check ip/mask combo
if((newIP & newMask) != newIP) {
JOptionPane.showMessageDialog(null,"Subnet IP and mask do not match!","Error",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//check subnet size
if(subnet_size < routers.size()) {
JOptionPane.showMessageDialog(null,"Subnet size too small!","Error",
JOptionPane.INFORMATION_MESSAGE);
return;
}
//Good, now everyone is assigned an IP for this link
int i;
int gateway_index=comps.getSelectedIndex();
int curIP=1;
for(i=0;i<routers.size();i++) {
SimComponent thiscomp=(SimComponent)routers.get(i);
SimComponent thelink=(SimComponent)links.get(i);
Object [] paramlist=new Object[3];
if(i==gateway_index)
paramlist[0]=new Integer(newIP | subnet_size);
else
paramlist[0]=new Integer(newIP | curIP++);
paramlist[1]=new Integer(newMask);
paramlist[2]=thelink;
thiscomp.compInfo(IPRouter.SET_PORT_IP,null,paramlist);
}
//now add route entry to this subnet
for(i=0;i<routers.size();i++) {
SimComponent thiscomp=(SimComponent)routers.get(i);
SimComponent thelink=(SimComponent)links.get(i);
Object [] paramlist=new Object[5];
paramlist[0]=new Integer(newIP);
paramlist[1]=new Integer(newMask);
paramlist[2]=new Integer(0);
paramlist[3]=thelink;
paramlist[4]="Static";
thiscomp.compInfo(IPRouter.ADD_ROUTE_ENTRY,null,paramlist);
}
//if a gateway is selected, add the default route for everyone
// except the gateway itself
if(gateway_index<routers.size()) {
for(i=0;i<routers.size();i++) {
if(i==gateway_index) continue; //skip the gateway itself
SimComponent thiscomp=(SimComponent)routers.get(i);
SimComponent thelink=(SimComponent)links.get(i);
Object [] paramlist=new Object[5];
paramlist[0]=new Integer(0);
paramlist[1]=new Integer(0);
paramlist[2]=new Integer(newIP | subnet_size);
paramlist[3]=thelink;
paramlist[4]="Static";
thiscomp.compInfo(IPRouter.ADD_ROUTE_ENTRY,null,paramlist);
}
}
//Done!
dialog.dispose();
return;
}
else {
dialog.dispose();
return;
}
}
}
private void getConnectedRouters(java.util.List links,java.util.List comps) {
java.util.Iterator i=voports.values().iterator();
while(i.hasNext()) {
Port voport=(Port)i.next();
SimComponent otherend=(SimComponent)voport.to_link.compInfo(GenericLink.GET_OTHER_END,this,this);
if(otherend==null) continue;
if(!otherend.getCompClass().equals("Router")) continue;
links.add(voport.to_link);
comps.add(otherend);
}
}
private String [] getRouterNames(java.util.List routers) {
String [] names=new String[routers.size()+1];
int i;
for(i=0;i<routers.size();i++) {
names[i]=((SimComponent)routers.get(i)).getName();
}
names[i]="None";
return names;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Set IP Subnet")) {
if(!checkRunning()) return;
SubnetDialogListener listener=new SubnetDialogListener();
listener.links=new java.util.ArrayList();
listener.routers=new java.util.ArrayList();
getConnectedRouters(listener.links,listener.routers);
if(listener.links.size()<2) {
JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Subnet must have at least two hosts/routers.",
"Error",JOptionPane.INFORMATION_MESSAGE);
return;
}
JDialog dialog=new JDialog(theGUI.getMainFrame(),"Set IP Subnet",true);
dialog.getContentPane().setLayout(new GridLayout(4,1));
JPanel aPanel=new JPanel();
aPanel.add(new JLabel("Subnet IP:"));
aPanel.add(listener.txtIP=new JTextField(10));
dialog.getContentPane().add(aPanel);
aPanel=new JPanel();
aPanel.add(new JLabel("Subnet Mask:"));
aPanel.add(listener.txtMask=new JTextField(10));
dialog.getContentPane().add(aPanel);
aPanel=new JPanel();
aPanel.add(new JLabel("Gateway:"));
JComboBox comps=new JComboBox(getRouterNames(listener.routers));
comps.setSelectedIndex(listener.routers.size()); //select last item "None" by default
aPanel.add(listener.comps=comps);
dialog.getContentPane().add(aPanel);
aPanel=new JPanel();
aPanel.add(listener.btnOK=new JButton("OK"));
aPanel.add(listener.btnCancel=new JButton("Cancel"));
dialog.getContentPane().add(aPanel);
dialog.getRootPane().setDefaultButton(listener.btnOK);
listener.btnOK.addActionListener(listener);
listener.btnCancel.addActionListener(listener);
listener.txtMask.addFocusListener(listener);
listener.dialog=dialog;
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.pack();
Dimension ddim=dialog.getPreferredSize();
Dimension sdim=Toolkit.getDefaultToolkit().getScreenSize();
dialog.setLocation((sdim.width-ddim.width)/2,(sdim.height-ddim.height)/2);
dialog.show();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -