KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > TXRXMonitor


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client;
21
22 import com.sslexplorer.agent.client.util.IOStreamConnector;
23 import com.sslexplorer.agent.client.util.IOStreamConnectorListener;
24
25
26 /**
27  * Thread that monitors traffic between the agent and SSL-Explorer
28  * and updates the current {@link AgentClientGUI} to show when
29  * data is received or sent.
30  *
31  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
32  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
33  */

34 public class TXRXMonitor extends Thread JavaDoc {
35
36     // #ifdef DEBUG
37
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(TXRXMonitor.class);
38
39     // #endif
40

41     // Private instance variables
42
private long lastRx, lastTx;
43     private final Agent client;
44     private IOStreamConnectorListener txIo, rxIo;
45
46     /**
47      * Constructor.
48      *
49      * @param client clirny
50      */

51     public TXRXMonitor(Agent client) {
52         super(Messages.getString("TXRXMonitor.threadName")); //$NON-NLS-1$
53
setDaemon(true);
54         this.client = client;
55     }
56     
57     /**
58      * Get the transmit listener
59      *
60      * @return transmit listener
61      */

62     public IOStreamConnectorListener getTxListener() {
63         if(txIo == null) {
64             txIo = new TXIOStreamConnectorListener();
65         }
66         return txIo;
67     }
68     
69     /**
70      * Get the transmit listener
71      *
72      * @return transmit listener
73      */

74     public IOStreamConnectorListener getRxListener() {
75         if(rxIo == null) {
76             rxIo = new RXIOStreamConnectorListener();
77         }
78         return rxIo;
79     }
80
81     /* (non-Javadoc)
82      * @see java.lang.Thread#run()
83      */

84     public void run() {
85
86         long rx;
87         long tx;
88         try {
89             while (true) {
90
91                 if (this.client.getState() != Agent.STATE_DISCONNECTED) {
92                     rx = System.currentTimeMillis() - lastRx;
93                     tx = System.currentTimeMillis() - lastTx;
94
95                     if (rx < 500 && tx < 500) {
96                         this.client.getGUI().showTxRx();
97                     } else if (rx < 500) {
98                         this.client.getGUI().showRx();
99                     } else if (tx < 500) {
100                         this.client.getGUI().showTx();
101                     } else {
102                         this.client.getGUI().showIdle();
103                     }
104                 }
105                 else {
106                     break;
107                 }
108                 try {
109                     Thread.sleep(500);
110                 } catch (InterruptedException JavaDoc ex) {
111                     // #ifdef DEBUG
112
log.info(Messages.getString("TXRXMonitor.interrupted")); //$NON-NLS-1$
113
// #endif
114
break;
115                 }
116             }
117         } catch (Throwable JavaDoc ex) {
118             // #ifdef DEBUG
119
Agent.log.info(Messages.getString("TXRXMonitor.failed"), ex); //$NON-NLS-1$
120
// #endif
121
}
122     }
123
124     class TXIOStreamConnectorListener implements IOStreamConnectorListener {
125         public void connectorClosed(IOStreamConnector connector) {
126         }
127
128         public void dataTransfered(byte[] data, int count) {
129             lastTx = System.currentTimeMillis();
130         }
131     }
132
133     class RXIOStreamConnectorListener implements IOStreamConnectorListener {
134         public void connectorClosed(IOStreamConnector connector) {
135         }
136
137         public void dataTransfered(byte[] data, int count) {
138             lastRx = System.currentTimeMillis();
139         }
140     }
141 }
Popular Tags