KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > client > monitoring > SessionMonitoringApplet


1 /*
2  * LogViewerApplet.java
3  *
4  * Created on May 31, 2003, 2:11 PM
5  */

6
7 package com.quikj.application.web.talk.client.monitoring;
8
9 import java.net.*;
10 import com.quikj.client.framework.*;
11 import com.quikj.application.web.oamp.messaging.*;
12 import com.quikj.application.web.talk.messaging.oamp.*;
13 /**
14  *
15  * @author amit
16  */

17 public class SessionMonitoringApplet extends java.applet.Applet JavaDoc implements HTTPConnectionClosedInterface
18 {
19     
20     private ServerCommunications com = null;
21
22     private String JavaDoc authCode = "";
23     private int port = 8087;
24     private int plugin = 3;
25     private String JavaDoc featureName = "Talk Application";
26     private MonitorDisplayFormatter statisticsDisplayer = null;
27     
28     /** Initializes the applet LogViewerApplet */
29     public void init()
30     {
31         OAMPFrameworkMessageParser.setParserType(OAMPFrameworkMessageParser.NANOXML_PARSER);
32         initParam();
33         
34         initComponents();
35     }
36     
37     public void start()
38     {
39         doLogin();
40     }
41     
42     public void stop()
43     {
44         disconnect();
45     }
46     
47     private void initParam()
48     {
49         String JavaDoc parm = getParameter("auth-code");
50         if (parm != null)
51         {
52             authCode = parm;
53         }
54         
55         parm = getParameter("port");
56         if (parm != null)
57         {
58             try
59             {
60                 port = Integer.parseInt(parm);
61             }
62             catch (NumberFormatException JavaDoc ex)
63             {
64                 return;
65             }
66         }
67         
68         parm = getParameter("plugin");
69         if (parm != null)
70         {
71             try
72             {
73                 plugin = Integer.parseInt(parm);
74             }
75             catch (NumberFormatException JavaDoc ex)
76             {
77                 return;
78             }
79         }
80         
81         parm = getParameter("feature-name");
82         if (parm != null)
83         {
84             featureName = parm;
85         }
86     }
87     
88     /** This method is called from within the init() method to
89      * initialize the form.
90      * WARNING: Do NOT modify this code. The content of this method is
91      * always regenerated by the Form Editor.
92      */

93     private void initComponents()//GEN-BEGIN:initComponents
94
{
95         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
96
97         buttonPanel = new java.awt.Panel JavaDoc();
98         statisticsArea = new java.awt.TextArea JavaDoc();
99         statusBar = new java.awt.TextField JavaDoc();
100
101         setLayout(new java.awt.GridBagLayout JavaDoc());
102
103         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
104         gridBagConstraints.gridx = 0;
105         gridBagConstraints.gridy = 0;
106         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
107         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
108         gridBagConstraints.weightx = 100.0;
109         add(buttonPanel, gridBagConstraints);
110
111         statisticsArea.setEditable(false);
112         statisticsArea.setFont(new java.awt.Font JavaDoc("Monospaced", 0, 12));
113         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
114         gridBagConstraints.gridx = 0;
115         gridBagConstraints.gridy = 1;
116         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
117         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
118         gridBagConstraints.weightx = 100.0;
119         gridBagConstraints.weighty = 100.0;
120         add(statisticsArea, gridBagConstraints);
121
122         statusBar.setEditable(false);
123         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
124         gridBagConstraints.gridx = 0;
125         gridBagConstraints.gridy = 2;
126         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
128         gridBagConstraints.weightx = 100.0;
129         add(statusBar, gridBagConstraints);
130
131     }//GEN-END:initComponents
132

133     
134     private void doLogin()
135     {
136         // connect and send the registration message
137

138         if (connect() == false)
139         {
140             statusBar.setText("Connection to " + port + " failed");
141             return;
142         }
143         
144         // send the registration message to the server
145
if (login() == false)
146         {
147             statusBar.setText("Could not login to the server");
148         }
149     }
150     
151     private boolean connect()
152     {
153         if (com != null)
154         {
155             disconnect(); // disconnect, if connected
156
}
157         
158         try
159         {
160             com = new ServerCommunications(getCodeBase().getHost(),
161             port,
162             plugin);
163         }
164         catch (UnknownHostException ex)
165         {
166             return false;
167         }
168         
169         // establish a connection to the server
170
statusBar.setText("Connecting to "
171         + getCodeBase().getHost()
172         + ':'
173         + port
174         + " .....");
175         
176         if (com.connect() == false)
177         {
178             statusBar.setText("Connection to server "
179             + getCodeBase().getHost()
180             + ':'
181             + port
182             + " failed");
183             return false;
184         }
185         
186         // start the message listener thread
187
com.start();
188         
189         statusBar.setText("Connected to "
190         + getCodeBase().getHost()
191         + ':'
192         + port);
193         
194         com.setClosedListener(this);
195         
196         return true;
197     }
198     
199     private void disconnect()
200     {
201         if (com != null)
202         {
203             if (com.isConnected() == true)
204             {
205                 com.disconnect();
206             }
207         }
208         
209         statusBar.setText("Disconnected");
210         
211     }
212     
213     private boolean login()
214     {
215         RegistrationRequestMessage message = new RegistrationRequestMessage();
216         message.setAuthCode(authCode);
217         
218         int req_id = com.sendRequestMessage("text/xml",
219         OAMPFrameworkMessageParser.format(OAMPSystemMessageParser.OAMP_SYSTEM_FEATURE_NAME,
220         message),
221         new LoginResponseListener(),
222         10000);
223         if (req_id == -1)
224         {
225             return false;
226         }
227         
228         return true;
229     }
230     
231     public void connectionClosed(String JavaDoc host, int port, int appl_id)
232     {
233         disconnect();
234         statusBar.setText("Lost connection with the server");
235     }
236     
237     class LoginResponseListener implements HTTPMessageListenerInterface
238     {
239         public void messageReceived(int req_id,
240         int status,
241         String JavaDoc content_type,
242         int http_status,
243         String JavaDoc reason,
244         String JavaDoc message)
245         {
246             if (status == HTTPMessageListenerInterface.TIMEOUT)
247             {
248                 // close the connection
249
disconnect();
250                 
251                 statusBar.setText("Time-out receiving response from server");
252             }
253             else if (status == HTTPMessageListenerInterface.RECEIVED)
254             {
255                 if (http_status == HTTPRspMessage.OK)
256                 {
257                     OAMPFrameworkMessageParser.setParserType(OAMPFrameworkMessageParser.NANOXML_PARSER);
258                     OAMPFrameworkMessageParser parser = null;
259                     try
260                     {
261                         parser = new OAMPFrameworkMessageParser();
262                     }
263                     catch (Exception JavaDoc ex)
264                     {
265                         disconnect();
266                         statusBar.setText("Error obtaining XML parser");
267                         return;
268                     }
269                     
270                     if (parser.parse(message, false) == false)
271                     {
272                         disconnect();
273                         statusBar.setText("Error decoding server response");
274                         return;
275                     }
276                     
277                     if (parser.isSystemMessage() == false)
278                     {
279                         disconnect();
280                         statusBar.setText("Bad server response");
281                         return;
282                     }
283                     
284                     OAMPSystemMessageParser sysparser = new OAMPSystemMessageParser();
285                     if (sysparser.parse(parser.getFirstChild(), false) == false)
286                     {
287                         disconnect();
288                         statusBar.setText("Error decoding server system message response");
289                         return;
290                     }
291                     
292                     OAMPMessageInterface parsed_message = sysparser.getMessage();
293                     if ((parsed_message instanceof
294                     com.quikj.application.web.oamp.messaging.RegistrationResponseMessage) == true)
295                     {
296                         RegistrationResponseMessage response_msg = (RegistrationResponseMessage)parsed_message;
297                         
298                         statusBar.setText("Logged in");
299                         statisticsDisplayer = new MonitorDisplayFormatter(statisticsArea);
300                         com.setRequestListener(new RequestListener());
301                         
302                         // send the monitor start message to request monitoring, for all active endpoints
303
MonitorEndPointsStartMessage msg = new MonitorEndPointsStartMessage();
304                         req_id = com.sendRequestMessage("text/xml",
305                         OAMPFrameworkMessageParser.format(featureName, msg),
306                         null,
307                         10000);
308                         
309                         if (req_id < 0)
310                         {
311                             statusBar.setText("Could not send the request for session monitoring");
312                             return;
313                         }
314                     }
315                     else
316                     {
317                         disconnect();
318                         statusBar.setText("Bad server response");
319                         return;
320                     }
321                 }
322                 else
323                 {
324                     disconnect();
325                     
326                     if (reason != null)
327                     {
328                         statusBar.setText(reason + " (" + http_status + ")");
329                     }
330                     else
331                     {
332                         statusBar.setText("Server returned error: " + http_status);
333                     }
334                 }
335             }
336             else // unexpected status
337
{
338                 disconnect();
339                 
340                 statusBar.setText("Unknown status: " + status + " received from the server");
341             }
342         }
343     }
344     
345     class RequestListener implements HTTPMessageListenerInterface
346     {
347         public void messageReceived(int req_id,
348         int status,
349         String JavaDoc content_type,
350         int http_status,
351         String JavaDoc reason,
352         String JavaDoc message)
353         {
354             // parse the message
355
OAMPFrameworkMessageParser parser = null;
356             
357             try
358             {
359                 parser = new OAMPFrameworkMessageParser();
360             }
361             catch (Exception JavaDoc ex)
362             {
363                 statusBar.setText("Error obtaining XML parser: "
364                 + ex.getClass().getName());
365                 return;
366             }
367             
368             if (parser.parse(message, true) == false)
369             {
370                 statusBar.setText("Received request message could not be parsed: "
371                 + parser.getErrorMessage());
372                 return;
373             }
374             
375             if (parser.isSystemMessage() == true)
376             {
377                 // ignore the message
378
return;
379             }
380             
381             applicationMessageReceived(req_id, parser.getFirstChild());
382             
383         }
384     }
385     
386     public void applicationMessageReceived(int request_id, Object JavaDoc message_node)
387     {
388         OAMPTalkMessageParser parser = new OAMPTalkMessageParser();
389         if (parser.parse(message_node, true) == false)
390         {
391             // print error message
392
statusBar.setText("Received feature request message could not be parsed: "
393             + parser.getErrorMessage()
394             + ", feature = " + featureName);
395             return;
396         }
397         
398         OAMPMessageInterface parsed_msg = parser.getMessage();
399         if ((parsed_msg instanceof MonitorEndPointsStatisticsMessage) == true)
400         {
401             MonitorEndPointsStatisticsMessage message = (MonitorEndPointsStatisticsMessage) parsed_msg;
402             
403             statisticsDisplayer.clear();
404             
405             int num_tables = message.numElements();
406             for (int i = 0; i < num_tables; i++)
407             {
408                 StatisticsElement tbl = message.elementAt(i);
409                 statisticsDisplayer.appendText(tbl.getName() + "\n\n");
410                 
411                 // pad column header fields to accomodate the data
412
String JavaDoc[] received_columns = tbl.getMetaData().getRow();
413                 String JavaDoc[] columns = new String JavaDoc[received_columns.length];
414                 String JavaDoc[][] rows = tbl.getRows();
415                 int num_rows = tbl.numRows();
416                 
417                 for (int col = 0; col < columns.length; col++)
418                 {
419                     int min_size = received_columns[col].length();
420                     for (int row = 0; row < num_rows; row++)
421                     {
422                         if (rows[row][col].length() > min_size)
423                         {
424                             min_size = rows[row][col].length();
425                         }
426                     }
427                     
428                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(received_columns[col]);
429                     int padding_amount = min_size - received_columns[col].length();
430                     for (int x = 0; x < padding_amount; x++)
431                     {
432                         buffer.append(' ');
433                     }
434                     buffer.append(" ");
435                     
436                     columns[col] = buffer.toString();
437                 }
438                 
439                 
440                 // finally, display the table
441
statisticsDisplayer.appendTable(columns, rows);
442                 
443                 statisticsDisplayer.appendText("\n\n\n");
444             }
445             
446         }
447         // else ignore
448
}
449     
450     // Variables declaration - do not modify//GEN-BEGIN:variables
451
private java.awt.TextArea JavaDoc statisticsArea;
452     private java.awt.TextField JavaDoc statusBar;
453     private java.awt.Panel JavaDoc buttonPanel;
454     // End of variables declaration//GEN-END:variables
455

456 }
457
Popular Tags