ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/Conient.java
(Generate patch)

Comparing projects/cms/source/conient/uk/org/iscream/cms/conient/Conient.java (file contents):
Revision 1.8 by ajm, Sat Jan 20 16:06:53 2001 UTC vs.
Revision 1.34 by tdb, Tue May 21 16:47:10 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * Copyright (C) 2000-2002 i-scream
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
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program is distributed in the hope that it will be useful,
12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21   //---PACKAGE DECLARATION---
22 + package uk.org.iscream.cms.conient;
23  
24   //---IMPORTS---
25   import javax.swing.*;
26   import javax.swing.border.*;
6 import java.awt.Color;
7 import uk.ac.ukc.iscream.util.*;
8
27   import java.awt.*;
28   import java.awt.event.*;
11 import java.net.*;
12 import java.io.*;
13 import java.util.Date;
14 import java.text.DateFormat;
15 import java.util.Locale;
16 import java.util.HashMap;
17 import javax.swing.border.*;
29  
30   /**
31 < * NASTY AND BASIC, PLEASE DON'T COMPLAIN
31 > * This is the main class of the Conient client.
32   *
33 + * This sets up the control panel, the data panel
34 + * and its own display.
35 + *
36   * @author  $Author$
37   * @version $Id$
38   */
39 < public class SwingClient extends JFrame implements Runnable {
39 > public class Conient extends JFrame {
40  
41   //---FINAL ATTRIBUTES---
42  
# Line 30 | Line 44 | public class SwingClient extends JFrame implements Run
44       * The current CVS revision of this class
45       */
46      public final String REVISION = "$Revision$";
47 +
48 +    /**
49 +     * The initial width of the window
50 +     */        
51 +    private final int DEFAULT_WIDTH = 700;
52      
53 <    private final int width = 400;
54 <    private final int height = 700;
53 >    /**
54 >     * The initial height of the window
55 >     */
56 >    private final int DEFAULT_HEIGHT = 600;
57      
58 +    /**
59 +     * The default configuration fle
60 +     * This can be specified on the command line
61 +     */
62 +    public static final String DEFAULT_CONFIG_FILE = "./etc/default.conf";
63 +    
64 +    /**
65 +     * The time in seconds to display the splash
66 +     * screen for
67 +     */
68 +    public static final int DISPLAY_SPLASH_TIME_SECONDS = 3;
69 +    
70   //---STATIC METHODS---
71  
72 +    /**
73 +     * The first method that is called.
74 +     * Sets up the various panels
75 +     *
76 +     * @param args the command line arguments
77 +     */
78      public static void main(String[] args) {
79 <        try {
80 <            String host = null;
81 <            if (args.length != 1) {
82 <                Object response = null;
83 <                while(!(response instanceof String)) {
84 <                    response = JOptionPane.showInputDialog(null, "Please enter the name of a server running the I-Scream client interface:", "I-Scream Server", JOptionPane.INFORMATION_MESSAGE);
46 <                }
47 <                host = (String) response;
48 <            } else {
49 <                host = args[0];
50 <            }
79 >        // get the host from the command line if they gave it
80 >        String host = null;
81 >        String configFile = DEFAULT_CONFIG_FILE;
82 >        if (args.length == 1) {
83 >            configFile = args[0];
84 >        }
85  
86 <            int port = 4510;
87 <            Socket socket = new Socket(host, port);
54 <            BufferedReader inBound = new BufferedReader(new InputStreamReader(socket.getInputStream()));
55 <            PrintWriter outBound = new PrintWriter(socket.getOutputStream());
86 >        // start the configuration system
87 >        Configuration.initialise(configFile);
88  
89 <            Queue dataQueue = new Queue();
90 <            DataReader data = new DataReader(inBound, dataQueue);
89 >        // the data display panel
90 >        DataPanel data = new DataPanel();
91          
92 <            SwingClient client = new SwingClient(dataQueue);
93 <            Thread clientThread = new Thread(client);
94 <            
95 <            clientThread.start();
96 <            data.start();
92 >        // the control panel
93 >        ControlPanel control = new ControlPanel(data);
94 >        
95 >        // the main frame (passed the two panels)
96 >        Conient client = new Conient(data, control);
97 >        conientFrame = (Frame) client;
98 >        Conient.addMessage("Conient {an i-scream Client} (c) 2001 The i-scream Project (http://www.i-scream.org.uk)");
99  
100 <        } catch (Exception e) {
101 <            System.err.println("ERROR START: " + e);
68 <            System.exit(1);
69 <        }  
100 >        Conient.addMessage("Conient ready.");
101 >        
102      }
103      
104 +    /**
105 +     * A static accessor, allowing components of the system
106 +     * to get hold of the root frame of the system.
107 +     *
108 +     * see conientFrame attribute for details.
109 +     *
110 +     * @return the root Conient frame
111 +     */
112 +    public static Frame getFrame() {
113 +        return conientFrame;
114 +    }
115 +    
116   //---CONSTRUCTORS---
117  
118      /**
119 <     * Creates a new Swing Client.
119 >     * Creates a new Swing Client Frame
120       */
121 <    public SwingClient(Queue dataQueue) {
78 <        
79 <        _dataQueue = dataQueue;
80 <        _myQueue = _dataQueue.getQueue();
121 >    private Conient(JSplitPane data, ControlPanel control) {
122          // set up the Frame
82        setTitle("I-Scream Client");
83        //setSize(width, height);
84        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
85        Box box = Box.createVerticalBox();
86
87        // create the i-scream logo at the top
88        JLabel iscream = new JLabel(new ImageIcon("i-scream.gif"));
89        _serverCountLabel = new JLabel("   Monitoring 0 hosts...");
90        _serverCountLabel.setForeground( new Color(0, 0, 102));
91        Box header = Box.createHorizontalBox();
92        header.add(_serverCountLabel);
93        header.add(Box.createHorizontalGlue());
94        header.add(iscream);
95        Box main = Box.createHorizontalBox();
96        JButton
123   utton = new JButton("Connect");
124 +        super("Conient {an i-scream Client}");
125 +        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
126 +        setJMenuBar(control.getMenuBar());
127          
128 <        // set up the text area
129 <        _status.setEditable(false);
130 <        JScrollPane statusPane = new JScrollPane(_status);
131 <        statusPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages "));
103 <        
104 <        Box controlPane = Box.createVerticalBox();
105 <        controlPane.add(button);
106 <        //controlPane.add(Box.createVerticalGlue());
107 <        controlPane.add(statusPane);
108 <        main.add(controlPane);
109 <        main.add(_tabbedPane);
128 >        // set what happens when the X in the corner is clicked        
129 >        addWindowListener(new WindowAdapter() {
130 >            public void windowClosing(WindowEvent e) {System.exit(0);}
131 >        });
132  
133 <        //_tabbedPane.setSelectedIndex(0);
134 <        
135 <        // build the frame
136 <        box.add(header);
115 <        box.add(main);
116 <        
117 <        getContentPane().add(box);
133 >        // add the control pane to the top border
134 >        getContentPane().add(control, "North");
135 >        // add the data pane to the centre        
136 >        getContentPane().add(data);
137  
138 <        // show the window
139 <        show();
140 <        _status.insert("Running...\n",0);
141 <    }
138 >        // setup the status panel        
139 >        JPanel bottom = new JPanel();
140 >        bottom.setLayout(new BorderLayout());
141 >        _messages.setEditable(false);
142 >        _messages.setRows(3);
143 >        JScrollPane messagesPane = new JScrollPane(_messages);
144 >        messagesPane.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages "));
145 >        bottom.add(messagesPane, "Center");
146 >        JPanel statusPanel = new JPanel();
147 >        JPanel linkPanel = new JPanel();
148 >        linkPanel.setLayout(new GridLayout(1,2));
149 >        linkPanel.add(_controlStatus);
150 >        linkPanel.add(_dataStatus);
151 >        statusPanel.setLayout(new GridLayout(2,1));
152 >        statusPanel.add(linkPanel);
153 >        statusPanel.add(_queueStatus);
154 >        bottom.add(statusPanel, "South");
155  
156 < //---PUBLIC METHODS---
156 >        // add the status panel to the bottom border
157 >        getContentPane().add(bottom, "South");
158 >        
159 >        // a nice icon for the window
160 >        setIconImage((new ImageIcon("./resources/server.gif")).getImage());
161  
162 <    public void run() {
163 <        HashMap hostList = new HashMap();
162 >        // and just because we can, a silly splash screen
163 >        // of the dudes that did this funky jibble
164 >        Splash splash = new Splash();
165 >        splash.show();
166 >        // wait
167          try {
168 <            while(true) {
169 <                
170 <                String xml = (String) _dataQueue.get(_myQueue);
132 <                if (xml == null) {
133 <                    // shouldn't really happen...but not sure
134 <                    _status.insert("No XML to update...",0);
135 <                } else {
136 <                    
137 <                    // Get a string without any null characters in it.
138 <                    //  -- maybe String.trim() would be better ?
139 <                    if (xml.indexOf(0) != -1) {
140 <                        xml = xml.substring(0, xml.indexOf(0));
141 <                    }
142 <                    else {
143 <                        xml = xml.substring(0, xml.length());
144 <                    }
145 <                    
146 <                    // Use XMLPacketMaker to make an XMLPacket object.
147 <                    XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml);
148 <                    XMLPacket packet = xmlPacketMaker.createXMLPacket();
149 <                    String hostName = packet.getParam("packet.attributes.machine_name");
150 <                    if(!hostList.containsKey(hostName)) {
151 <                        _serverCount++;
152 <                        _serverCountLabel.setText("   Monitoring " + _serverCount + " hosts...");
153 <                        host = new HostDisplayPanel();      
154 <                        _tabbedPane.addTab(hostName, _serverIcon, host, "Monitor " + hostName);
155 <                        hostList.put(hostName, host);
156 <                        _status.insert("New Host added: " + hostName + "\n", 0);
157 <                    }
158 <                    ((HostDisplayPanel) hostList.get(hostName)).updateHost(packet);
159 <                }
160 <            }
161 <        } catch (Exception e) {
162 <            System.err.println("ERROR RUN: " + e);
168 >            Thread.sleep(DISPLAY_SPLASH_TIME_SECONDS * 1000);
169 >        } catch (InterruptedException e) {
170 >            // don't do anything, we don't care
171          }
172 +        //loose the window
173 +        splash.dispose();
174 +        splash = null;        
175 +        // show the main window
176 +        show();
177      }
178  
179 + //---PUBLIC METHODS---
180 +
181   //---PRIVATE METHODS---
182 +
183 + //---ACCESSOR/MUTATOR METHODS---
184 +
185 +    /**
186 +     * Sets the control link status.
187 +     *
188 +     * @param status the message
189 +     */
190 +    public static void setControlStatus(String status) {
191 +        _controlStatus.setText("Control Link: " + status);
192 +        _controlStatus.repaint();
193 +    }
194      
195 <    protected JPanel makeTextPanel(String text, java.awt.Component item) {
196 <        JPanel panel = new JPanel(false);
197 <        JLabel label = new JLabel(text);
198 <        label.setHorizontalAlignment(JLabel.RIGHT);
199 <        //item.setHorizontalAlignment(JLabel.LEFT);
200 <        panel.setLayout(new GridLayout(1, 2));
201 <        panel.add(label);
202 <        panel.add(item);
176 <        return panel;
195 >    /**
196 >     * Sets the data link status.
197 >     *
198 >     * @param status the message
199 >     */
200 >    public static void setDataStatus(String status) {
201 >        _dataStatus.setText("Data Link: " + status);
202 >        _dataStatus.repaint();
203      }
204  
205 +    /**
206 +     * Updates the queue status.
207 +     *
208 +     * @param currentQueue
209 +     * @param numElements
210 +     */
211 +    public static void setQueueStatus(int currentQueue, int numElements) {
212 +        _queueStatus.setText("Data Queue : " + currentQueue + " Packets Recieved : " + numElements);
213 +        _queueStatus.repaint();
214 +    }
215  
216 < //---ACCESSOR/MUTATOR METHODS---
216 >    /**
217 >     * Adds a system message to the messages list
218 >     *
219 >     * @param message the new message
220 >     */
221 >    public static void addMessage(String message) {
222 >        _messages.insert(message + "\n", 0);
223 >    }
224  
225   //---ATTRIBUTES---
183
184    HostDisplayPanel host;
185    JTabbedPane _tabbedPane = new JTabbedPane();
186    Queue _dataQueue;
187    int _myQueue;
188    JTextArea _status = new JTextArea();
189    ImageIcon _serverIcon = new ImageIcon("server.gif");
190    JLabel _serverCountLabel;
191    int _serverCount = 0;
226      
193    
227   //---STATIC ATTRIBUTES---
228      
229 +    /**
230 +     * Displays information about the data link
231 +     */
232 +    static JLabel _controlStatus = new JLabel("Control Link: Disconnected", JLabel.LEFT);
233 +    {
234 +        _controlStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
235 +    }
236 +    
237 +    /**
238 +     * Displays information about the data link
239 +     */
240 +    static JLabel _dataStatus = new JLabel("Data Link: Disconnected", JLabel.LEFT);
241 +    {
242 +        _dataStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
243 +    }
244 +    
245 +    /**
246 +     * Displays information about the inbound data queue.
247 +     */
248 +    static JLabel _queueStatus = new JLabel(" ", JLabel.LEFT);
249 +    {
250 +        _queueStatus.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
251 +    }
252 +    
253 +    /**
254 +     * The place where system messages are written.
255 +     */
256 +    static JTextArea _messages = new JTextArea();
257 +    {
258 +        _messages.setLineWrap(true);
259 +    }
260 +    
261 +    /**
262 +     * Holds a reference to the root frame for Conient
263 +     * This is only used by dialogs (specifically the configurationn
264 +     * dialog) so that it can be modal, please use the accessor.
265 +     */
266 +    private static Frame conientFrame;
267 +    
268 + //---INNER CLASSES----
269 +
270 +    /**
271 +     * An inner class to display a splash screen
272 +     */
273 +    private class Splash extends JWindow {
274 +        
275 +        /**
276 +         * Constructs a new splash screen
277 +         */
278 +        public Splash() {
279 +            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
280 +            JPanel splash = new JPanel();
281 +            splash.setBackground(Color.black);
282 +            JLabel image = new JLabel((new ImageIcon("./resources/i-scream-splash.gif")));
283 +            splash.add(image);
284 +            setContentPane(splash);
285 +            Dimension screen = getToolkit().getScreenSize();
286 +            pack();
287 +            setLocation((screen.width - getSize().width) / 2,
288 +                        (screen.height - getSize().height) / 2);
289 +            
290 +            
291 +        }
292 +    }
293   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines