Debian Codegraph @

src/java/org/astrogrid/samp/gui/HubMonitor.java

raw
1 package org.astrogrid.samp.gui;
2
3 import java.awt.BorderLayout;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13 import javax.swing.Box;
14 import javax.swing.ImageIcon;
15 import javax.swing.JButton;
16 import javax.swing.JComponent;
17 import javax.swing.JFrame;
18 import javax.swing.JPanel;
19 import javax.swing.SwingUtilities;
20 import org.astrogrid.samp.ErrInfo;
21 import org.astrogrid.samp.Message;
22 import org.astrogrid.samp.Metadata;
23 import org.astrogrid.samp.Response;
24 import org.astrogrid.samp.Subscriptions;
25 import org.astrogrid.samp.client.ClientProfile;
26 import org.astrogrid.samp.client.DefaultClientProfile;
27 import org.astrogrid.samp.client.HubConnection;
28 import org.astrogrid.samp.client.HubConnector;
29 import org.astrogrid.samp.client.MessageHandler;
30 import org.astrogrid.samp.client.SampException;
31 import org.astrogrid.samp.httpd.UtilServer;
32
33 /**
34 * Client application which uses a {@link GuiHubConnector}
35 * to connect to any running hub and display information about all currently
36 * registered clients.
37 *
38 * @author Mark Taylor
39 * @since 16 Jul 2008
40 */
41 public class HubMonitor extends JPanel {
42
43 private final GuiHubConnector connector_;
44 private static Logger logger_ =
45 Logger.getLogger( HubMonitor.class.getName() );
46
47 /**
48 * Constructor.
49 *
50 * @param profile SAMP profile
51 * @param trackMessages if true, the GUI will contain a visual
52 * representation of messages sent and received
53 * @param autoSec number of seconds between automatic hub connection
54 * attempts; <=0 means no automatic connections
55 */
56 public HubMonitor( ClientProfile profile, boolean trackMessages,
57 int autoSec ) {
58 super( new BorderLayout() );
59
60 // Set up a new GuiHubConnector and GUI decorations.
61 connector_ = trackMessages ? new MessageTrackerHubConnector( profile )
62 : new GuiHubConnector( profile );
63
64 // Declare the default subscriptions. This is required so that
65 // the hub knows the client is subscribed to those hub.event
66 // MTypes which inform about client registration, hub shutdown etc.
67 connector_.declareSubscriptions( connector_.computeSubscriptions() );
68
69 // Declare metadata about this application.
70 Metadata meta = new Metadata();
71 meta.setName( "HubMonitor" );
72 meta.setDescriptionText( "GUI hub monitor utility" );
73 try {
74 meta.setIconUrl( UtilServer.getInstance()
75 .exportResource( "/org/astrogrid/samp/images/"
76 + "eye.gif" )
77 .toString() );
78 }
79 catch ( IOException e ) {
80 logger_.warning( "Can't set icon" );
81 }
82 meta.put( "author", "Mark Taylor" );
83 connector_.declareMetadata( meta );
84
85 // Create and place a component which maintains a display of
86 // currently registered clients. A more modest GUI could just use
87 // connector.getClientListModel() as a model for a JList component.
88 add( connector_.createMonitorPanel(), BorderLayout.CENTER );
89
90 // Prepare a container for other widgets at the bottom of the window.
91 JPanel infoBox = new JPanel( new BorderLayout() );
92 add( infoBox, BorderLayout.SOUTH );
93
94 // Create and place components which allow the user to
95 // view and control registration/unregistration explicitly.
96 JComponent connectBox = new JPanel( new BorderLayout() );
97 connectBox.add( new JButton( connector_.createToggleRegisterAction() ),
98 BorderLayout.CENTER );
99 connectBox.add( connector_.createConnectionIndicator(),
100 BorderLayout.EAST );
101 infoBox.add( connectBox, BorderLayout.EAST );
102
103 // Create and place components which provide a compact display
104 // of the connector's status.
105 JComponent statusBox = Box.createHorizontalBox();
106 statusBox.add( connector_.createClientBox( false, 24 ) );
107 if ( connector_ instanceof MessageTrackerHubConnector ) {
108 statusBox.add( ((MessageTrackerHubConnector) connector_)
109 .createMessageBox( 24 ) );
110 }
111 infoBox.add( statusBox, BorderLayout.CENTER );
112
113 // Attempt registration, and arrange that if/when unregistered we look
114 // for a hub to register with on a regular basis.
115 connector_.setActive( true );
116 connector_.setAutoconnect( autoSec );
117 }
118
119 /**
120 * Returns this monitor's HubConnector.
121 *
122 * @return hub connector
123 */
124 public GuiHubConnector getHubConnector() {
125 return connector_;
126 }
127
128 /**
129 * Does the work for the main method.
130 */
131 public static int runMain( String[] args ) {
132 String usage = new StringBuffer()
133 .append( "\n Usage:" )
134 .append( "\n " )
135 .append( HubMonitor.class.getName() )
136 .append( "\n " )
137 .append( " [-help]" )
138 .append( " [+/-verbose]" )
139 .append( "\n " )
140 .append( " [-auto <secs>]" )
141 .append( " [-nomsg]" )
142 .append( " [-nogui]" )
143 .append( "\n " )
144 .append( " [-mtype <pattern>]" )
145 .append( "\n" )
146 .toString();
147 List argList = new ArrayList( Arrays.asList( args ) );
148 int verbAdjust = 0;
149 boolean gui = true;
150 boolean trackMsgs = true;
151 int autoSec = 3;
152 Subscriptions subs = new Subscriptions();
153 for ( Iterator it = argList.iterator(); it.hasNext(); ) {
154 String arg = (String) it.next();
155 if ( arg.startsWith( "-auto" ) && it.hasNext() ) {
156 it.remove();
157 String sauto = (String) it.next();
158 it.remove();
159 autoSec = Integer.parseInt( sauto );
160 }
161 else if ( arg.equals( "-gui" ) ) {
162 it.remove();
163 gui = true;
164 }
165 else if ( arg.equals( "-nogui" ) ) {
166 it.remove();
167 gui = false;
168 }
169 else if ( arg.equals( "-msg" ) ) {
170 it.remove();
171 trackMsgs = true;
172 }
173 else if ( arg.equals( "-nomsg" ) ) {
174 it.remove();
175 trackMsgs = false;
176 }
177 else if ( arg.startsWith( "-mtype" ) && it.hasNext() ) {
178 it.remove();
179 String mpat = (String) it.next();
180 it.remove();
181 subs.addMType( mpat );
182 }
183 else if ( arg.startsWith( "-v" ) ) {
184 it.remove();
185 verbAdjust--;
186 }
187 else if ( arg.startsWith( "+v" ) ) {
188 it.remove();
189 verbAdjust++;
190 }
191 else if ( arg.startsWith( "-h" ) ) {
192 it.remove();
193 System.out.println( usage );
194 return 0;
195 }
196 else {
197 it.remove();
198 System.err.println( usage );
199 return 1;
200 }
201 }
202 assert argList.isEmpty();
203
204 // Adjust logging in accordance with verboseness flags.
205 int logLevel = Level.WARNING.intValue() + 100 * verbAdjust;
206 Logger.getLogger( "org.astrogrid.samp" )
207 .setLevel( Level.parse( Integer.toString( logLevel ) ) );
208
209 // Get profile.
210 final ClientProfile profile =DefaultClientProfile.getProfile();
211
212 // Create the HubMonitor.
213 final HubMonitor monitor =
214 new HubMonitor( profile, trackMsgs, autoSec );
215
216 // Add a handler for extra MTypes if so requested.
217 if ( ! subs.isEmpty() ) {
218 final Subscriptions extraSubs = subs;
219 HubConnector connector = monitor.getHubConnector();
220 final Response dummyResponse = new Response();
221 dummyResponse.setStatus( Response.WARNING_STATUS );
222 dummyResponse.setResult( new HashMap() );
223 dummyResponse.setErrInfo( new ErrInfo( "Message logged, "
224 + "no other action taken" ) );
225 connector.addMessageHandler( new MessageHandler() {
226 public Map getSubscriptions() {
227 return extraSubs;
228 }
229 public void receiveNotification( HubConnection connection,
230 String senderId,
231 Message msg ) {
232 }
233 public void receiveCall( HubConnection connection,
234 String senderId, String msgId,
235 Message msg )
236 throws SampException {
237 connection.reply( msgId, dummyResponse );
238 }
239 } );
240 connector.declareSubscriptions( connector.computeSubscriptions() );
241 }
242
243 // Start the gui in a new window.
244 final boolean isVisible = gui;
245 SwingUtilities.invokeLater( new Runnable() {
246 public void run() {
247 JFrame frame = new JFrame( "SAMP HubMonitor" );
248 frame.getContentPane().add( monitor );
249 frame.setIconImage(
250 new ImageIcon( Metadata.class
251 .getResource( "images/eye.gif" ) )
252 .getImage() );
253 frame.pack();
254 frame.setVisible( isVisible );
255 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
256 }
257 } );
258 return 0;
259 }
260
261 /**
262 * Displays a HubMonitor in a window.
263 * Use -help flag.
264 */
265 public static void main( String[] args ) {
266 int status = runMain( args );
267 if ( status != 0 ) {
268 System.exit( status );
269 }
270 }
271 }