FileDocCategorySizeDatePackage
BrowserMessage.javaAPI DocAzureus 3.0.3.49892Tue Aug 07 13:36:52 BST 2007com.aelitis.azureus.ui.swt.browser.msg

BrowserMessage

public class BrowserMessage extends Object
Holds a message being dispatched to a {@link MessageListener}.
author
dharkness
created
Jul 18, 2006

Fields Summary
public static final String
MESSAGE_PREFIX
All messages must start with this prefix.
public static final String
MESSAGE_DELIM
Separates prefix and listener ID from rest of message.
public static final int
NO_PARAM
There were no parameters passed with the message.
public static final int
OBJECT_PARAM
Parameters were an encoded JSONObject.
public static final int
ARRAY_PARAM
Parameters were an encoded JSONArray.
public static final int
LIST_PARAM
Parameters were an encoded list.
private int
sequence
private String
listenerId
private String
operationId
private String
params
private int
paramType
private Object
decodedParams
private String
sFullMessage
private ArrayList
completionListeners
private boolean
completed
private boolean
completeDelayed
private String
referer
static int
seqFake
Constructors Summary
public BrowserMessage(String sMsg)


          
        if ( sMsg == null ) {
            throw new IllegalArgumentException("event must be non-null");
        }
        
        this.sFullMessage = sMsg;
        parse();
    
Methods Summary
public voidaddCompletionListener(MessageCompletionListener l)

    	completionListeners.add(l);
    
public voidcomplete(boolean bOnlyNonDelayed, boolean success, java.lang.Object data)
Sets the message complete and fires of the listeners who are waiting for a response.

param
bOnlyNonDelayed Only mark complete if this message does not have a delayed reponse
param
success Success level of the message
param
data Any data the message results wants to send

    	//System.out.println("complete called with " + bOnlyNonDelayed);
    	if (completed || (bOnlyNonDelayed && completeDelayed)) {
    		//System.out.println("exit early" + completed);
    		return;
    	}
    	triggerCompletionListeners(success, data);
    	completed = true;
    
public voiddebug(java.lang.String message)

			debug(message, null);
		
public voiddebug(java.lang.String message, java.lang.Throwable t)

			try {
  			AEDiagnosticsLogger diag_logger = AEDiagnostics.getLogger("v3.CMsgr");
  			String out = "[" + getListenerId() + ":" + getOperationId() + "] "
  					+ message;
  			diag_logger.log(out);
  			if (t != null) {
  				diag_logger.log(t);
  			}
  			if (com.aelitis.azureus.util.Constants.DIAG_TO_STDOUT) {
  				System.out.println(out);
  				if (t != null) {
  					t.printStackTrace();
  				}
  			}
			} catch (Throwable t2) {
				Debug.out(t2);
			}
		
public java.util.ListgetDecodedArray()

        if ( ! isParamArray() ) {
            throw new IllegalStateException("Decoded parameter is not a List");
        }
        return (List) decodedParams;
    
public java.util.ListgetDecodedList()

        if ( ! isParamList() ) {
            throw new IllegalStateException("Decoded parameter is not a List");
        }
        return (List) decodedParams;
    
public java.util.MapgetDecodedMap()

        if ( ! isParamObject() ) {
            throw new IllegalStateException("Decoded parameter is not a Map");
        }
        return (Map) decodedParams;
    
public java.lang.StringgetFullMessage()

        return sFullMessage;
    
public java.lang.StringgetListenerId()

        return listenerId;
    
public java.lang.StringgetOperationId()

        return operationId;
    
public java.lang.StringgetParams()

        return params;
    
public java.lang.StringgetReferer()

			return referer;
		
public intgetSequence()

        return sequence;
    
public booleanisParamArray()

        return paramType == ARRAY_PARAM;
    
public booleanisParamList()

        return paramType == LIST_PARAM;
    
public booleanisParamObject()

        return paramType == OBJECT_PARAM;
    
protected voidparse()
Parses the full message into its component parts.

throws
IllegalArgumentException if the message cannot be parsed

                                        
         
        String text = sFullMessage;
        
        // DJH: StringTokenizer was not used so that listeners
        //      could define their message format
        int delimSeqNum = text.indexOf(MESSAGE_DELIM);
        if ( delimSeqNum == -1 ) {
            throw new IllegalArgumentException("Message has no delimeters: " + text);
        }
        if ( delimSeqNum == text.length() - 1 ) {
            throw new IllegalArgumentException("Message has no sequence number: " + text);
        }
        
        int delimListener = text.indexOf(MESSAGE_DELIM, delimSeqNum + 1);
        if ( delimListener == -1 || delimListener == text.length() - 1 ) {
            throw new IllegalArgumentException("Message has no listener ID: " + text);
        }
        try {
            sequence = Integer.parseInt(text.substring(delimSeqNum + 1, delimListener));
        }
        catch ( NumberFormatException e ) {
        	System.err.println("Plese put the throw back in once souk fixes the seq # bug");
        	sequence = seqFake++;
            //throw new IllegalArgumentException("Message has no sequence number: " + text);
        }
        
        int delimOperation = text.indexOf(MESSAGE_DELIM, delimListener + 1);
        if ( delimOperation == -1 || delimOperation == text.length() - 1 ) {
            // listener ID without operation
            throw new IllegalArgumentException("Message has no operation ID: " + text);
        }
        
        listenerId = text.substring(delimListener + 1, delimOperation);
        
        int delimParams = text.indexOf(MESSAGE_DELIM, delimOperation + 1);
        if ( delimParams == -1 ) {
            // operation without parameters
            operationId = text.substring(delimOperation + 1);
        }
        else if ( delimParams == text.length() - 1 ) {
            // operation without parameters
            operationId = text.substring(delimOperation + 1, delimParams);
            params = null;
            paramType = NO_PARAM;
            decodedParams = null;
        }
        else {
        	// operation with parameters
        	operationId = text.substring(delimOperation + 1, delimParams);
        	params = text.substring(delimParams + 1);
        	char leading = params.charAt(0);
        	try {
        		switch ( leading ) {
        			case '{":
        				paramType = OBJECT_PARAM;
        				decodedParams = JSONUtils.decodeJSON(params);
        				break;

        			case '[":
        				paramType = ARRAY_PARAM;
        				Map decodeJSON = JSONUtils.decodeJSON(params);
        				if (decodeJSON != null) {
        					decodedParams = decodeJSON.get("value");
        				} else {
        					decodedParams = null;
        				}
        				break;

        			default:
        				paramType = LIST_PARAM;
        			decodedParams = JSFunctionParametersParser.parse(params);
        			break;
        		}
        	} catch (Exception e) {
        		decodedParams = null;
        	}
        	if (decodedParams == null) {
        		paramType = NO_PARAM;
        	}
        }
    
public voidremoveCompletionListener(MessageCompletionListener l)

    	completionListeners.remove(l);
    
public voidsetCompleteDelayed(boolean bCompleteDelayed)

    	completeDelayed = bCompleteDelayed;
    
public voidsetReferer(java.lang.String referer)

			this.referer = referer;
		
public java.lang.StringtoString()

        return "[" + sequence + "] " + listenerId + "." 
                + operationId + "(" + params + ")";
    
private voidtriggerCompletionListeners(boolean success, java.lang.Object data)

		for (Iterator iterator = completionListeners.iterator(); iterator.hasNext();) {
			MessageCompletionListener l = (MessageCompletionListener) iterator.next();
			try {
				l.completed(success, data);
			} catch (Exception e) {
				Debug.out(e);
			}
		}