| 1 |
package org.astrogrid.samp.test; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.Collection; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.List; |
| 11 |
import java.util.Map; |
| 12 |
import java.util.logging.Level; |
| 13 |
import java.util.logging.Logger; |
| 14 |
import org.astrogrid.samp.Client; |
| 15 |
import org.astrogrid.samp.ErrInfo; |
| 16 |
import org.astrogrid.samp.Message; |
| 17 |
import org.astrogrid.samp.Metadata; |
| 18 |
import org.astrogrid.samp.Response; |
| 19 |
import org.astrogrid.samp.SampUtils; |
| 20 |
import org.astrogrid.samp.Subscriptions; |
| 21 |
import org.astrogrid.samp.client.ClientProfile; |
| 22 |
import org.astrogrid.samp.client.DefaultClientProfile; |
| 23 |
import org.astrogrid.samp.client.HubConnection; |
| 24 |
import org.astrogrid.samp.client.HubConnector; |
| 25 |
import org.astrogrid.samp.client.MessageHandler; |
| 26 |
import org.astrogrid.samp.httpd.UtilServer; |
| 27 |
|
| 28 |
/** |
| 29 |
* Subscribes to SAMP messages and logs any received to an output stream. |
| 30 |
* The only responses to messages have samp.status=samp.warning. |
| 31 |
* |
| 32 |
* @author Mark Taylor |
| 33 |
* @since 4 Sep 2008 |
| 34 |
*/ |
| 35 |
public class Snooper { |
| 36 |
|
| 37 |
private final OutputStream out_; |
| 38 |
private final Map clientMap_; |
| 39 |
private static final byte[] newline_; |
| 40 |
static { |
| 41 |
byte[] nl; |
| 42 |
try { |
| 43 |
nl = System.getProperty( "line.separator", "\n" ) |
| 44 |
.getBytes( "UTF-8" ); |
| 45 |
} |
| 46 |
catch ( Exception e ) { |
| 47 |
nl = new byte[] { (byte) '\n' }; |
| 48 |
} |
| 49 |
newline_ = nl; |
| 50 |
} |
| 51 |
|
| 52 |
private static final Logger logger_ = |
| 53 |
Logger.getLogger( Snooper.class.getName() ); |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor using default metadata. |
| 57 |
* |
| 58 |
* @param profile profile |
| 59 |
* @param subs subscriptions defining which messages are received |
| 60 |
* and logged |
| 61 |
* @param out destination stream for logging info |
| 62 |
* @param autoSec number of seconds between auto connection attempts |
| 63 |
*/ |
| 64 |
public Snooper( ClientProfile profile, Subscriptions subs, |
| 65 |
OutputStream out, int autoSec ) { |
| 66 |
this( profile, subs, createDefaultMetadata(), out, autoSec ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Constructor using custom metadata. |
| 71 |
* |
| 72 |
* @param profile profile |
| 73 |
* @param subs subscriptions defining which messages are received |
| 74 |
* and logged |
| 75 |
* @param meta client metadata |
| 76 |
* @param out destination stream for logging info |
| 77 |
* @param autoSec number of seconds between auto connection attempts |
| 78 |
*/ |
| 79 |
public Snooper( ClientProfile profile, final Subscriptions subs, |
| 80 |
Metadata meta, OutputStream out, int autoSec ) { |
| 81 |
HubConnector connector = new HubConnector( profile ); |
| 82 |
connector.declareMetadata( meta ); |
| 83 |
out_ = out; |
| 84 |
|
| 85 |
// Prepare all-purpose response to logged messages. |
| 86 |
final Response response = new Response(); |
| 87 |
response.setStatus( Response.WARNING_STATUS ); |
| 88 |
response.setResult( new HashMap() ); |
| 89 |
response.setErrInfo( new ErrInfo( "Message logged, not acted on" ) ); |
| 90 |
|
| 91 |
// Add a handler which will handle the subscribed messages. |
| 92 |
connector.addMessageHandler( new MessageHandler() { |
| 93 |
public Map getSubscriptions() { |
| 94 |
return subs; |
| 95 |
} |
| 96 |
public void receiveNotification( HubConnection connection, |
| 97 |
String senderId, |
| 98 |
Message msg ) |
| 99 |
throws IOException { |
| 100 |
log( senderId, msg, null ); |
| 101 |
} |
| 102 |
public void receiveCall( HubConnection connection, |
| 103 |
String senderId, |
| 104 |
String msgId, Message msg ) |
| 105 |
throws IOException { |
| 106 |
log( senderId, msg, msgId ); |
| 107 |
connection.reply( msgId, response ); |
| 108 |
} |
| 109 |
} ); |
| 110 |
connector.declareSubscriptions( connector.computeSubscriptions() ); |
| 111 |
clientMap_ = connector.getClientMap(); |
| 112 |
|
| 113 |
// Connect and ready to log. |
| 114 |
connector.setActive( true ); |
| 115 |
connector.setAutoconnect( autoSec ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Logs a received message. |
| 120 |
* |
| 121 |
* @param senderId message sender public ID |
| 122 |
* @param msg message object |
| 123 |
* @param msgId message ID for call/response type messages |
| 124 |
* (null for notify type messages) |
| 125 |
*/ |
| 126 |
private void log( String senderId, Message msg, String msgId ) |
| 127 |
throws IOException { |
| 128 |
StringBuffer sbuf = new StringBuffer(); |
| 129 |
sbuf.append( senderId ); |
| 130 |
Client client = (Client) clientMap_.get( senderId ); |
| 131 |
if ( client != null ) { |
| 132 |
Metadata meta = client.getMetadata(); |
| 133 |
if ( meta != null ) { |
| 134 |
String name = meta.getName(); |
| 135 |
if ( name != null ) { |
| 136 |
sbuf.append( " (" ) |
| 137 |
.append( name ) |
| 138 |
.append( ")" ); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
sbuf.append( " --- " ); |
| 143 |
if ( msgId == null ) { |
| 144 |
sbuf.append( "notify" ); |
| 145 |
} |
| 146 |
else { |
| 147 |
sbuf.append( "call" ) |
| 148 |
.append( " (" ) |
| 149 |
.append( msgId ) |
| 150 |
.append( ")" ); |
| 151 |
} |
| 152 |
out_.write( newline_ ); |
| 153 |
out_.write( sbuf.toString().getBytes( "UTF-8" ) ); |
| 154 |
out_.write( newline_ ); |
| 155 |
out_.write( SampUtils.formatObject( msg, 3 ).getBytes( "UTF-8" ) ); |
| 156 |
out_.write( newline_ ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Returns the default metadata for the Snooper client. |
| 161 |
* |
| 162 |
* @return meta |
| 163 |
*/ |
| 164 |
public static Metadata createDefaultMetadata() { |
| 165 |
Metadata meta = new Metadata(); |
| 166 |
meta.setName( "Snooper" ); |
| 167 |
meta.setDescriptionText( "Listens in to messages" |
| 168 |
+ " for logging purposes" ); |
| 169 |
try { |
| 170 |
meta.setIconUrl( UtilServer.getInstance() |
| 171 |
.exportResource( "/org/astrogrid/samp/images/" |
| 172 |
+ "ears.png" ) |
| 173 |
.toString() ); |
| 174 |
} |
| 175 |
catch ( IOException e ) { |
| 176 |
logger_.warning( "Can't export icon" ); |
| 177 |
} |
| 178 |
meta.put( "Author", "Mark Taylor" ); |
| 179 |
return meta; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Main method. Runs a snooper. |
| 184 |
*/ |
| 185 |
public static void main( String[] args ) throws IOException { |
| 186 |
int status = runMain( args ); |
| 187 |
if ( status != 0 ) { |
| 188 |
System.exit( status ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Does the work for the main method. |
| 194 |
* Use -help flag. |
| 195 |
*/ |
| 196 |
public static int runMain( String[] args ) throws IOException { |
| 197 |
String usage = new StringBuffer() |
| 198 |
.append( "\n Usage:" ) |
| 199 |
.append( "\n " ) |
| 200 |
.append( Snooper.class.getName() ) |
| 201 |
.append( "\n " ) |
| 202 |
.append( " [-help]" ) |
| 203 |
.append( " [-/+verbose]" ) |
| 204 |
.append( "\n " ) |
| 205 |
.append( " [-clientname <appname>]" ) |
| 206 |
.append( " [-clientmeta <metaname> <metavalue>]" ) |
| 207 |
.append( "\n " ) |
| 208 |
.append( " [-mtype <pattern>]" ) |
| 209 |
.append( " [-subs <pattern> <subsinfo>]" ) |
| 210 |
.append( "\n" ) |
| 211 |
.toString(); |
| 212 |
List argList = new ArrayList( Arrays.asList( args ) ); |
| 213 |
int verbAdjust = 0; |
| 214 |
Subscriptions subs = new Subscriptions(); |
| 215 |
Metadata meta = new Metadata(); |
| 216 |
for ( Iterator it = argList.iterator(); it.hasNext(); ) { |
| 217 |
String arg = (String) it.next(); |
| 218 |
if ( arg.startsWith( "-mtype" ) && it.hasNext() ) { |
| 219 |
it.remove(); |
| 220 |
String mpat = (String) it.next(); |
| 221 |
it.remove(); |
| 222 |
subs.addMType( mpat ); |
| 223 |
} |
| 224 |
else if ( arg.equals( "-subs" ) && it.hasNext() ) { |
| 225 |
it.remove(); |
| 226 |
String mpat = (String) it.next(); |
| 227 |
it.remove(); |
| 228 |
String substr; |
| 229 |
if ( it.hasNext() ) { |
| 230 |
substr = (String) it.next(); |
| 231 |
it.remove(); |
| 232 |
} |
| 233 |
else { |
| 234 |
System.err.println( usage ); |
| 235 |
return 1; |
| 236 |
} |
| 237 |
Object subsInfo = SampUtils.fromJson( substr ); |
| 238 |
subs.put( mpat, subsInfo ); |
| 239 |
} |
| 240 |
else if ( arg.equals( "-clientname" ) && it.hasNext() ) { |
| 241 |
it.remove(); |
| 242 |
meta.setName( (String) it.next() ); |
| 243 |
it.remove(); |
| 244 |
} |
| 245 |
else if ( arg.equals( "-clientmeta" ) && it.hasNext() ) { |
| 246 |
it.remove(); |
| 247 |
String mName = (String) it.next(); |
| 248 |
it.remove(); |
| 249 |
String mValue; |
| 250 |
if ( it.hasNext() ) { |
| 251 |
mValue = (String) it.next(); |
| 252 |
it.remove(); |
| 253 |
} |
| 254 |
else { |
| 255 |
System.err.println( usage ); |
| 256 |
return 1; |
| 257 |
} |
| 258 |
Object mVal = SampUtils.parseValue( mValue ); |
| 259 |
if ( mVal == null ) { |
| 260 |
meta.remove( mName ); |
| 261 |
} |
| 262 |
else { |
| 263 |
meta.put( mName, mVal ); |
| 264 |
} |
| 265 |
} |
| 266 |
else if ( arg.startsWith( "-v" ) ) { |
| 267 |
it.remove(); |
| 268 |
verbAdjust--; |
| 269 |
} |
| 270 |
else if ( arg.startsWith( "+v" ) ) { |
| 271 |
it.remove(); |
| 272 |
verbAdjust++; |
| 273 |
} |
| 274 |
else if ( arg.startsWith( "-h" ) ) { |
| 275 |
it.remove(); |
| 276 |
System.out.println( usage ); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
else { |
| 280 |
it.remove(); |
| 281 |
System.err.println( usage ); |
| 282 |
return 1; |
| 283 |
} |
| 284 |
} |
| 285 |
assert argList.isEmpty(); |
| 286 |
|
| 287 |
// Adjust logging in accordance with verboseness flags. |
| 288 |
int logLevel = Level.WARNING.intValue() + 100 * verbAdjust; |
| 289 |
Logger.getLogger( "org.astrogrid.samp" ) |
| 290 |
.setLevel( Level.parse( Integer.toString( logLevel ) ) ); |
| 291 |
|
| 292 |
// Combine custom and default metadata. |
| 293 |
Metadata m2 = createDefaultMetadata(); |
| 294 |
m2.putAll( meta ); |
| 295 |
meta = m2; |
| 296 |
|
| 297 |
// Set default subscriptions (everything) if none has been specified |
| 298 |
// explicitly. |
| 299 |
if ( subs.isEmpty() ) { |
| 300 |
subs.addMType( "*" ); |
| 301 |
} |
| 302 |
|
| 303 |
// Get profile. |
| 304 |
ClientProfile profile = DefaultClientProfile.getProfile(); |
| 305 |
|
| 306 |
// Start and run snooper. |
| 307 |
new Snooper( profile, subs, meta, System.out, 2 ); |
| 308 |
|
| 309 |
// Wait indefinitely. |
| 310 |
Object lock = new String( "Forever" ); |
| 311 |
synchronized( lock ) { |
| 312 |
try { |
| 313 |
lock.wait(); |
| 314 |
} |
| 315 |
catch ( InterruptedException e ) { |
| 316 |
} |
| 317 |
} |
| 318 |
return 0; |
| 319 |
} |
| 320 |
} |