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.33 by tdb, Sat May 18 18:15:56 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines