FileDocCategorySizeDatePackage
NewMessageHandler.javaAPI DocWireless Messaging API4539Mon Nov 10 21:15:10 GMT 2003wmafw

NewMessageHandler

public class NewMessageHandler extends Object implements Runnable, MessageListener
NewMessageHandler is responsible for receiving and processing MessageConnection messages.

Fields Summary
private Thread
th
/ /** Message Handling Thread
private MessageConnection
mc
WMA Message Connection
private boolean
done
Flag to stop thread gracefully
private IncomingMessageListener
incomingMessageListener
The WMA Framework message listener
private Object
mutex
Mutex
Constructors Summary
protected NewMessageHandler(MessageConnection mc)
/ /** Constructor.

param
mc is the message connection to handle.


           
                      
       
        this.mc = mc;
    
Methods Summary
public voidnotifyIncomingMessage(MessageConnection mc)
/ /** Notify the message processor to exit

param
mc is the message connection with incoming messages.

        synchronized(mutex) {
            mutex.notify();
        }
    
public voidrun()
Thread's run method to wait for and process received messages.

        Message msg = null;
        while (!done) {
            //  Try reading (maybe block for) a message
            try {
                synchronized(mutex) {
                    mutex.wait();
                }
                msg = mc.receive();
            } catch (Exception e) {
                // Handle reading errors
                System.out.println("Exception while receiving message " + e);
                continue;
            }
            /********************************/
            /* Process the received message */
            /********************************/
            if (msg instanceof TextMessage) {
                TextMessage tmsg = (TextMessage)msg;
                //  Notify listener about new text message...
                incomingMessageListener.onTextMessage(tmsg.getAddress(), tmsg.getPayloadText());
            } else {
                if (msg instanceof BinaryMessage) {
                    BinaryMessage bmsg = (BinaryMessage)msg;
                    //  Notify listener about new binary message...
                    incomingMessageListener.onBinaryMessage(bmsg.getAddress(), bmsg.getPayloadData());
                } else {
                    //  Ignore
                }
            }
        } // while
    
protected voidsetIncomingMessageListener(IncomingMessageListener listener)
Sets the WMAFramework incoming message nofitication listener.

param
listener is the NotificationListener to set.

        incomingMessageListener = listener;
    
protected voidstart()
Notify the message processor to exit

        try {
            mc.setMessageListener(this);
        } catch (Exception e) {
        }
        th.start();
    
protected voidstop()
Notify the message processor to exit

        done = true;