📄 tcpmon.java
字号:
delayTimeLabel = new JLabel(getMessage("delay02", "Delay in Milliseconds")); opts.add(delayTimeLabel, c); delayTimeLabel.setForeground(Color.gray); c.anchor = GridBagConstraints.WEST; c.gridwidth = GridBagConstraints.REMAINDER; opts.add(delayTime = new NumberField(6), c); delayTime.setEnabled(false); //enabler callback delayBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (delaySupport.equals(event.getActionCommand())) { boolean b = delayBox.isSelected(); Color color = b ? Color.black : Color.gray; delayBytes.setEnabled(b); delayTime.setEnabled(b); delayBytesLabel.setForeground(color); delayTimeLabel.setForeground(color); } } } ); // Spacer ////////////////////////////////////////////////////////////////// mainPane.add( Box.createRigidArea(new Dimension(1, 10)), c ); // ADD Button /////////////////////////////////////////////////////////////////// c.anchor = GridBagConstraints.WEST ; c.gridwidth = GridBagConstraints.REMAINDER ; final String add = getMessage("add00", "Add"); mainPane.add( addButton = new JButton( add ), c ); this.add( new JScrollPane( mainPane ), BorderLayout.CENTER ); // addButton.setEnabled( false ); addButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { if ( add.equals(event.getActionCommand()) ) { String text ; Listener l = null ; int lPort; lPort=port.getValue(0); if(lPort==0) { //no port, button does nothing return; } String tHost = host.getText(); int tPort = 0 ; tPort=tport.getValue(0); SlowLinkSimulator slowLink=null; if(delayBox.isSelected()) { int bytes= delayBytes.getValue(0); int time = delayTime.getValue(0); slowLink=new SlowLinkSimulator(bytes,time); } try { l = new Listener( noteb, null, lPort, tHost, tPort, proxyButton.isSelected(), slowLink); } catch (Exception e){ e.printStackTrace(); } // Pick-up the HTTP Proxy settings /////////////////////////////////////////////////// text = HTTPProxyHost.getText(); if ( "".equals(text) ) { text = null ; } l.HTTPProxyHost = text ; text = HTTPProxyPort.getText(); int proxyPort=HTTPProxyPort.getValue(-1); if(proxyPort!=-1) { l.HTTPProxyPort = Integer.parseInt(text); } //reset the port port.setText(null); /* but not, any more, the target port and host values host.setText(null); tport.setText(null); */ } } } ); notebook.addTab( name, this ); notebook.repaint(); notebook.setSelectedIndex( notebook.getTabCount() - 1 ); } } /** * wait for incoming connections, spawn a connection thread when * stuff comes in. */ class SocketWaiter extends Thread { ServerSocket sSocket = null ; Listener listener ; int port ; boolean pleaseStop = false ; public SocketWaiter(Listener l, int p) { listener = l ; port = p ; start(); } public void run() { try { listener.setLeft( new JLabel(getMessage("wait00", " Waiting for Connection...") ) ); listener.repaint(); sSocket = new ServerSocket( port ); for (; ; ) { Socket inSocket = sSocket.accept(); if ( pleaseStop ) { break ; } new Connection( listener, inSocket ); inSocket = null ; } } catch ( Exception exp ) { if ( !"socket closed".equals(exp.getMessage()) ) { JLabel tmp = new JLabel( exp.toString() ); tmp.setForeground( Color.red ); listener.setLeft( tmp ); listener.setRight( new JLabel("") ); listener.stop(); } } } /** * force a halt by connecting to self and then closing the server socket */ public void halt() { try { pleaseStop = true ; new Socket( "127.0.0.1", port ); if ( sSocket != null ) { sSocket.close(); } } catch ( Exception e ) { e.printStackTrace(); } } } /** * class to simulate slow connections by slowing down the system */ static class SlowLinkSimulator { private int delayBytes; private int delayTime; private int currentBytes; private int totalBytes; /** * construct * @param delayBytes bytes per delay; set to 0 for no delay * @param delayTime delay time per delay in milliseconds */ public SlowLinkSimulator(int delayBytes, int delayTime) { this.delayBytes = delayBytes; this.delayTime = delayTime; } /** * construct by copying delay bytes and time, but not current * count of bytes * @param that source of data */ public SlowLinkSimulator(SlowLinkSimulator that) { this.delayBytes=that.delayBytes; this.delayTime=that.delayTime; } /** * how many bytes have gone past? * @return */ public int getTotalBytes() { return totalBytes; } /** * log #of bytes pumped. Will pause when necessary. This method is not * synchronized * @param bytes */ public void pump(int bytes) { totalBytes+=bytes; if(delayBytes==0) { //when not delaying, we are just a byte counter return; } currentBytes += bytes; if(currentBytes>delayBytes) { //we have overshot. lets find out how far int delaysize=currentBytes/delayBytes; long delay=delaysize*(long)delayTime; //move byte counter down to the remainder of bytes currentBytes=currentBytes%delayBytes; //now wait try { Thread.sleep(delay); } catch (InterruptedException e) { ; //ignore the exception } } } /** * get the current byte count * @return */ public int getCurrentBytes() { return currentBytes; } /** * set the current byte count * @param currentBytes */ public void setCurrentBytes(int currentBytes) { this.currentBytes = currentBytes; } } /** * this class handles the pumping of data from the incoming socket to the * outgoing socket */ class SocketRR extends Thread { Socket inSocket = null ; Socket outSocket = null ; JTextArea textArea ; InputStream in = null ; OutputStream out = null ; boolean xmlFormat ; boolean numericEnc ; volatile boolean done = false ; TableModel tmodel = null ; int tableIndex = 0 ; String type = null; Connection myConnection = null; SlowLinkSimulator slowLink; public SocketRR(Connection c, Socket inputSocket, InputStream inputStream, Socket outputSocket, OutputStream outputStream, JTextArea _textArea, boolean format, boolean numeric, TableModel tModel, int index, final String type, SlowLinkSimulator slowLink) { inSocket = inputSocket ; in = inputStream ; outSocket = outputSocket ; out = outputStream ; textArea = _textArea ; xmlFormat = format ; numericEnc= numeric ; tmodel = tModel ; tableIndex = index ; this.type = type; myConnection = c; this.slowLink= slowLink; start(); } public boolean isDone() { return ( done ); } public void run() { try { byte[] buffer = new byte[4096]; byte[] tmpbuffer = new byte[8192]; String message = null; int saved = 0 ; int len ; int i1, i2 ; int i ; int reqSaved = 0 ; int tabWidth = 3 ; boolean atMargin = true ; int thisIndent = -1, nextIndent = -1, previousIndent = -1; //if ( inSocket != null ) inSocket.setSoTimeout( 10 ); //if ( outSocket != null ) outSocket.setSoTimeout( 10 ); if ( tmodel != null ) { String tmpStr = (String) tmodel.getValueAt(tableIndex, REQ_COLUMN); if ( !"".equals(tmpStr) ) { reqSaved = tmpStr.length(); } } a: for ( ; ; ) { if ( done ) { break; } //try{ //len = in.available(); //}catch(Exception e){len=0;} len = buffer.length ; // Used to be 1, but if we block it doesn't matter // however 1 will break with some servers, including apache if ( len == 0 ) { len = buffer.length; } if ( saved + len > buffer.length) { len = buffer.length - saved ; } int len1 = 0; while ( len1 == 0 ) { try { len1 = in.read(buffer, saved, len); } catch ( Exception ex ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -