Thread: [Javanetsim-cvs] javaNetSim/guiUI Terminal.java, NONE, 1.1 MainScreen.java, 1.66, 1.67 RunCMD.java,
Status: Beta
Brought to you by:
darkkey
From: QweR <qw...@us...> - 2007-10-12 21:06:56
|
Update of /cvsroot/javanetsim/javaNetSim/guiUI In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv6934/guiUI Modified Files: MainScreen.java Added Files: Terminal.java Removed Files: RunCMD.java Log Message: new cisco like console have been written Index: MainScreen.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/guiUI/MainScreen.java,v retrieving revision 1.66 retrieving revision 1.67 diff -C2 -d -r1.66 -r1.67 *** MainScreen.java 29 Sep 2007 20:33:36 -0000 1.66 --- MainScreen.java 12 Oct 2007 21:06:39 -0000 1.67 *************** *** 95,99 **** import core.protocolsuite.tcp_ip.Telnet_server; import core.protocolsuite.tcp_ip.Telnet_client; ! import guiUI.RunCMD; import javax.swing.JViewport; import javax.swing.table.*; --- 95,99 ---- import core.protocolsuite.tcp_ip.Telnet_server; import core.protocolsuite.tcp_ip.Telnet_client; ! import guiUI.Terminal; import javax.swing.JViewport; import javax.swing.table.*; *************** *** 4142,4146 **** public void RunCmd(String inNodeName){ try{ ! RunCMD r = new RunCMD(this, (core.NetworkLayerDevice)Sim.getNode(inNodeName)); r.pack(); r.setVisible(true); --- 4142,4146 ---- public void RunCmd(String inNodeName){ try{ ! Terminal r = new Terminal(this, (core.NetworkLayerDevice)Sim.getNode(inNodeName)); r.pack(); r.setVisible(true); --- NEW FILE: Terminal.java --- /* * TelnetEmulator.java * * Created on 22 Feb 2006, 16:38 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package guiUI; import core.CommunicationException; import core.DeviceConfig; import core.LowLinkException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.InputVerifier; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.WindowEvent; import core.TransportLayerException; import java.awt.Font; import java.awt.event.KeyListener; import java.util.regex.*; import core.protocolsuite.tcp_ip.*; import java.util.Vector; import core.CommandsTree; import core.CommandInterface; import core.Version; import java.awt.event.ActionEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JComponent; import core.Pair; import core.CommandProcessor; /** * * @author qwer */ public class Terminal extends JFrame { private JPanel panel; private JScrollPane scrollpane; private JTextArea terminal; private JTextField cmdline; private String text = ""; private core.NetworkLayerDevice device; MainScreen parent; private Vector<String> history = new Vector<String>(0); private int pos_history=0; private final static int max_history = 128; CmdVerifier cmdverifier = new CmdVerifier(); private int current_mode; private int device_type; private int current_config; private CommandProcessor cmdproc; private clear_terminal_CommandClass clear_terminal_Command = new clear_terminal_CommandClass(); private configure_replace_CommandClass configure_replace_Command = new configure_replace_CommandClass(); private configure_memory_CommandClass configure_memory_Command = new configure_memory_CommandClass(); private configure_terminal_CommandClass configure_terminal_Command = new configure_terminal_CommandClass(); private exit_CommandClass exit_Command = new exit_CommandClass(); private logout_CommandClass logout_Command = new logout_CommandClass(); private ping_CommandClass ping_Command = new ping_CommandClass(); private show_history_CommandClass show_history_Command = new show_history_CommandClass(); // Creates a new instance of TelnetEmulator public Terminal(MainScreen parent, core.NetworkLayerDevice dev) { super("Console: " + dev.NodeProtocolStack.getParentNodeName()); device = dev; this.parent = parent; cmdproc = new CommandProcessor(dev); current_mode = CommandsTree.STD_MODE; device_type = CommandsTree.APPLICATION_LAYER; current_config = DeviceConfig.RUNNING_CONFIG; cmdproc.add("clear terminal", clear_terminal_Command, cmdproc.STD_CONF_MODE | cmdproc.NETWORK_LAYER, "<cr>", "Clear screen"); cmdproc.addDescription("configure","Enter configuration mode"); cmdproc.add("configure replace", configure_replace_Command, cmdproc.STD_MODE | cmdproc.NO_LAYER, "<cr>", "Replace configure from the terminal"); cmdproc.add("configure memory", configure_memory_Command, cmdproc.STD_MODE | cmdproc.NO_LAYER, "<cr>", "Configure from memory"); cmdproc.add("configure terminal", configure_terminal_Command, cmdproc.STD_MODE | cmdproc.NO_LAYER, "<cr>", "Configure from the terminal"); cmdproc.add("exit", exit_Command, cmdproc.STD_CONF_MODE | cmdproc.NO_LAYER, "<cr>", "Exit from current mode"); cmdproc.add("logout", logout_Command, cmdproc.STD_MODE | cmdproc.NO_LAYER, "<cr>", "Exit from the console"); cmdproc.add("ping", ping_Command, cmdproc.STD_CONF_MODE | cmdproc.NETWORK_LAYER, "<ip>", "Send echo messages"); cmdproc.add("show history", show_history_Command, cmdproc.STD_CONF_MODE | cmdproc.NO_LAYER, "<cr>", "Display the session command history"); panel = new JPanel(); scrollpane = new JScrollPane(); terminal = new JTextArea(text); cmdline = new JTextField(); this.setContentPane(panel); panel.setLayout(new java.awt.BorderLayout()); panel.add(cmdline, BorderLayout.SOUTH); panel.add(scrollpane, BorderLayout.CENTER); panel.setPreferredSize(new java.awt.Dimension(600,500)); scrollpane.setViewportView(terminal); terminal.setEnabled(true); terminal.setEditable(false); terminal.setFocusable(false); terminal.setBackground(Color.BLACK); terminal.setForeground(Color.WHITE); terminal.setFont(new Font("Courier New", Font.PLAIN, 12)); addToTerminal("javaNetSim console "+Version.CORE_VERSION+", "+Version.YEARS); cmdline.setVisible(true); cmdline.setEnabled(true); cmdline.setFocusable(true); cmdline.setFocusCycleRoot(true); cmdline.setFocusTraversalKeysEnabled(false); cmdline.setInputVerifier(cmdverifier); cmdline.addKeyListener(cmdverifier); this.addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { printInfo(); } }); } public void exitWindow() { printInfo(); this.dispose(); } public void printInfo(){ parent.printLayerInfo(false); //!!!!!: add more headers here } private String runcmd(String cmd) { String out=""; if(cmd.compareTo("")==0) { //nothing } else if(cmd.compareTo("?")==0 || cmd.compareToIgnoreCase("help")==0) { out += " route \t show/edit route table\n"; out += " arp \t show/edit arp table\n"; out += " snmp \t on/off snmp agent\n"; out += " counters\t show network counters\n"; out += " quit \t close terminal session\n"; out += " ? or help\tshow this screen\n"; } else if(cmd.compareToIgnoreCase("quit")==0) { this.dispose(); } else { String tokens[]=cmd.split(" "); Matcher m; if(tokens[0].compareTo("route")==0){ if((m=Pattern.compile(" +print$").matcher(cmd)).find()) { String routes[] = device.NodeProtocolStack.getRouteTableEntries(); out += "IP routing table:\n" + "Destination" + "\t" + "Gateway" + "\t" + "Genmask" + "\t" + "Type" + "\t" + "Iface\n"; for(int i=0; i<routes.length - 1; i++){ Route_entry r = device.NodeProtocolStack.getRouteTableEntry(routes[i]); out += routes[i] + "\t" + r.gateway + "\t" + r.genMask + "\t" + r.Type + "\t" + r.iFace + "\n"; } } else if((m=Pattern.compile(" +add +([^ ]+) +([^ ]+) +([^ ]+)( +([^ ]+|\\*))?$").matcher(cmd)).find()) { if(m.group(5)!=null) { device.NodeProtocolStack.addRoute(new Route_entry(m.group(1), m.group(5), m.group(3), m.group(2), 0)); } else { device.NodeProtocolStack.addRoute(new Route_entry(m.group(1), "", m.group(3), m.group(2), 0)); } out+="Route added.\n"; } else if((m=Pattern.compile(" +del +([^ ]+)$").matcher(cmd)).find()) { device.NodeProtocolStack.removeRoute(m.group(1)); out+="Route to " + tokens[2] + " removed.\n"; } else{ out+="Unknown route command. Usage:\n"; out+=" route add (<host ip>|<network ip>) <target interface> <netmask> [<gateway>|*]\n" + " add new route record\n"; out+=" route del (<host ip>|<network ip>) delete route record\n"; out+=" route print print route table\n"; } } else if(tokens[0].compareTo("snmp")==0){ if(device instanceof core.ApplicationLayerDevice) { if((m=Pattern.compile(" +(on|\\d+)( +([^ ]+))?$").matcher(cmd)).find()) { int l_port=161; String l_pass="public"; if(m.group(1).compareTo("on")!=0) l_port = Integer.parseInt(m.group(1)); if(m.group(3)!=null) { if(m.group(3).compareTo("")!=0) l_pass = m.group(3); } SNMP snmpa = (SNMP) ((core.ApplicationLayerDevice)device).getApp(161); try{ snmpa.Close(); snmpa.Disconnect(); out+="SNMP agent stoped\n"; snmpa.setPassword(l_pass); snmpa.setPort(l_port); try{ snmpa.Listen(); out+="Now SNMP agent listen on port " + String.valueOf(l_port) + "\n"; } catch(TransportLayerException e) { out+="Unable to open connection on SNMP agent\n"; } } catch(TransportLayerException e) { out+="Unable to close connection on SNMP agent\n"; } } else if((m=Pattern.compile(" +off$").matcher(cmd)).find()) { SNMP snmpa = (SNMP) ((core.ApplicationLayerDevice)device).getApp(161); try{ snmpa.Close(); out+="SNMP agent stoped\n"; } catch(TransportLayerException e) { out+="Unable to close connection on SNMP agent\n"; } } else { out+="Unknown snmp command. Usage:\n"; out+=" snmp (on|<port number>) [community name] Start SNMP agent\n"; out+=" snmp off Stop SNMP agent\n"; } } else { out += "Sorry, but this device not supported SNMP.\n"; } } else if(tokens[0].compareTo("counters")==0){ out += " Recieved IP Packets: " + Integer.valueOf(device.NodeProtocolStack.getinputIPCount()).toString() + "\n Sent IP Packets: " + Integer.valueOf(device.NodeProtocolStack.getoutputIPCount()).toString() + "\n ARP Packets: " + Integer.valueOf(device.NodeProtocolStack.getARPCount()).toString() + "\n Recieved TCP segments: " + Integer.valueOf(device.NodeProtocolStack.getTCPinputCount()).toString() + "\n Sent TCP segments: " + Integer.valueOf(device.NodeProtocolStack.getTCPoutputCount()).toString() + "\n Sent TCP ACK's: " + Integer.valueOf(device.NodeProtocolStack.getTCPACKCount()).toString() + "\n Sent TCP Dublicates: " + Integer.valueOf(device.NodeProtocolStack.getTCPSDCount()).toString() + "\n Recieved TCP Dublicates: " + Integer.valueOf(device.NodeProtocolStack.getTCPRDCount()).toString() + "\n Recieved UDP segments: " + Integer.valueOf(device.NodeProtocolStack.getUDPinputCount()).toString() + "\n Sent UDP segments: " + Integer.valueOf(device.NodeProtocolStack.getUDPoutputCount()).toString() + "\n"; } else if(tokens[0].compareTo("arp")==0){ if((m=Pattern.compile(" +-a$").matcher(cmd)).find()) { try{ String ArpTable[] = device.NodeProtocolStack.getARPTable(); for(int i=0;i<ArpTable.length;i++) { out += ArpTable[i] + "\n"; } } catch(Exception e) { //Should never get here. } } else if((m=Pattern.compile(" +-d +([^ ]+)$").matcher(cmd)).find()) { device.NodeProtocolStack.removeARP(m.group(1)); out += "Removed ARP entry for ip " + m.group(1) + "\n"; } else if((m=Pattern.compile(" +-s +([^ ]+) +([^ ]+)$").matcher(cmd)).find()) { device.NodeProtocolStack.addToARPStatic(m.group(1), m.group(2)); out += "Created new static ARP entry: " + m.group(1) + " is " + m.group(2) + "\n"; } else { out+="Unknown arp command. Usage:\n"; out+=" arp -a print ARP table\n"; out+=" arp -d <ip address> delete record from ARP table\n"; out+=" arp -s <ip address> <MAC address> add new ARP record\n"; } } else out = tokens[0] + ": command not found\n"; } addToTerminal(out); return out; } // // private String removeSpaces(String s) { // while(s.startsWith(" ")) s = s.substring(1); // while(s.endsWith(" ")) s = s.substring(0, s.length()-1); // return s; // } /** Add text and prompt to terminal * */ private void addToTerminal(String data){ text += data+"\n"+device.NodeProtocolStack.getParentNodeName(); if(current_mode == cmdproc.CONF_MODE){ text += "(config)"; } text += "# "; terminal.setText(text); } /** Add text to terminal * */ private void appendToTerminal(String data){ text += data; terminal.setText(text); } private void clearTerminal(){ text = ""; } public void sendSymbol(String symbol){ if(symbol.length()==1) switch(symbol.charAt(0)) { case 0x0A: { if(cmdline.getText().compareTo("")!=0) { if(history.size()>=max_history) history.remove(0); history.add(cmdline.getText()); pos_history = history.size(); } appendToTerminal(cmdline.getText() + "\n"); String tmptext = cmdproc.call(cmdline.getText(), current_mode | device_type); if(tmptext==null){ tmptext = "% Incomplete command.\n"; } addToTerminal(tmptext); cmdline.setText(""); printInfo(); break; } case 0x04: { exitWindow(); break; } case 0x1B: { cmdline.setText(""); break; } case '\t': { addToTerminal(cmdline.getText()); int caretPos = cmdline.getCaretPosition(); String compl_str = cmdproc.complete(cmdline.getText().substring(0,caretPos), current_mode | device_type); //String last_str = cmdline.getText().substring(caretPos); if(compl_str!=null){ cmdline.setText(compl_str+" "); //cmdline.setText(compl_str+last_str); } break; } case '?': { String tmptext = cmdline.getText() + "?\n"; Vector<Pair> tmpv = cmdproc.help(cmdline.getText().substring(0,cmdline.getCaretPosition()), current_mode | device_type); if(tmpv!=null){ for(int i=0; i<tmpv.size(); i++){ String first = (String)tmpv.get(i).getFirst(); for(int j=20-first.length(); j>0; j--){ first += " "; } tmptext += " "+first+(String)tmpv.get(i).getSecond()+"\n"; } } addToTerminal(tmptext); break; } default: { pos_history = history.size(); } } else if(symbol.length()==3 && symbol.startsWith("^[")) switch(symbol.charAt(2)){ case 0x26: { if(history.size()>0) { if(pos_history == history.size() && cmdline.getText().compareTo("")!=0) { if(history.size()>=max_history) history.remove(0); history.add(cmdline.getText()); pos_history = history.size()-1; } if(pos_history>0) { cmdline.setText(history.get(--pos_history)); } else if(pos_history>0) { cmdline.setText(history.get(pos_history-1)); } } break; } case 0x28: { if(history.size()>0) { if(pos_history+1<history.size()) { cmdline.setText(history.get(++pos_history)); } } break; } default: { pos_history = history.size(); //terminal.setText(text+"\n"+Integer.toHexString(i)); } } else{ pos_history = history.size(); } } class CmdVerifier extends InputVerifier implements KeyListener{ public boolean verify(JComponent input){ String text = ((JTextField)input).getText(); boolean res = !text.contains("?"); return res; } public boolean shouldYieldFocus(JComponent input) { if(!verify(input)) { String text = ((JTextField)input).getText(); ((JTextField)input).setText(text.replace("?","")); } return true; } public void keyTyped(KeyEvent e) { JTextField source = (JTextField)e.getSource(); shouldYieldFocus(source); } public void keyReleased(KeyEvent e) { JTextField source = (JTextField)e.getSource(); shouldYieldFocus(source); } public void keyPressed(KeyEvent e) { JTextField source = (JTextField)e.getSource(); shouldYieldFocus(source); char c = e.getKeyChar(); if(c>=65535) { sendSymbol("^["+(char)e.getKeyCode()); } else { sendSymbol(String.valueOf(c)); } } } class clear_terminal_CommandClass implements CommandInterface{ public String call(Vector<String> params){ clearTerminal(); return ""; } }; class configure_terminal_CommandClass implements CommandInterface{ public String call(Vector<String> params){ current_config = DeviceConfig.RUNNING_CONFIG; current_mode = cmdproc.CONF_MODE; return ""; } }; class configure_replace_CommandClass implements CommandInterface{ public String call(Vector<String> params){ current_config = DeviceConfig.RUNNING_CONFIG; current_mode = cmdproc.CONF_MODE; return "Running-config was erased"; } }; class configure_memory_CommandClass implements CommandInterface{ public String call(Vector<String> params){ current_config = DeviceConfig.STARTUP_CONFIG; current_mode = cmdproc.CONF_MODE; return "Starting-config editing"; } }; class exit_CommandClass implements CommandInterface{ public String call(Vector<String> params){ if(current_mode == cmdproc.CONF_MODE){ current_mode = cmdproc.STD_MODE; } else if(current_mode == cmdproc.STD_MODE){ exitWindow(); } return ""; } }; class logout_CommandClass implements CommandInterface{ public String call(Vector<String> params){ exitWindow(); return ""; } }; class ping_CommandClass implements CommandInterface{ public String call(Vector<String> params){ if(params.size()==1){ appendToTerminal("Type escape sequence to abort.\n"); appendToTerminal("Sending 5, 4-byte ICMP Echos to "+params.get(0)+", timeout is 2 seconds:\n"); int success = 0; for(int i=0; i<5; i++){ try { device.sendPing(params.get(0)); if(false){ success++; } }catch (LowLinkException ex) { ex.printStackTrace(); } catch (CommunicationException ex) { ex.printStackTrace(); } appendToTerminal("."); try{ Thread.sleep(1000); }catch (InterruptedException ex) { ex.printStackTrace(); } } appendToTerminal("\nSuccess rate is " +(int)(success*100/5)+ " percent ("+success+"/5)\n"); } return "Command not supported yet.\n"; } }; class show_history_CommandClass implements CommandInterface{ public String call(Vector<String> params){ String out = ""; for(int i=0; i<params.size(); i++){ out += params.get(i) + "\n"; } return out; } }; } --- RunCMD.java DELETED --- |