/*
* NewMessageHandler.java
*
* Author: C. Enrique Ortiz, October 2003
*
* Companion source code to "Advanced Messaging Coding with JSR 120".
*
* Based on original work by C. Enrique Ortiz.
*
* COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2003.
* The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
* The use of the software is subject to the terms of the end-user license
* agreement which accompanies or is included with the software. The software is
* provided "as is" and Sony Ericsson specifically disclaim any warranty or
* condition whatsoever regarding merchantability or fitness for a specific
* purpose, title or non-infringement. No warranty of any kind is made in
* relation to the condition, suitability, availability, accuracy, reliability,
* merchantability and/or non-infringement of the software provided herein.
*/
package wmafw;
import javax.wireless.messaging.*;
/**
* NewMessageHandler is responsible for receiving and processing
* MessageConnection messages.
*/
class NewMessageHandler implements MessageListener, Runnable
{
/**********************/
/* Message Processing */
/**********************/
/** Message Handling Thread */
private Thread th = new Thread(this);
/** WMA Message Connection */
private MessageConnection mc;
/** Flag to stop thread gracefully */
private boolean done; // if true, thread must exit
/** The WMA Framework message listener */
private IncomingMessageListener incomingMessageListener;
/** Mutex */
private Object mutex = new Object();
/****************************************************/
/**************** Private Methods *******************/
/****************************************************/
/**
* Constructor.
* @param mc is the message connection to handle.
*/
protected NewMessageHandler(MessageConnection mc) {
this.mc = mc;
}
/***************************************************/
/**************** Public Methods *******************/
/***************************************************/
/**
* Notify the message processor to exit
* @param mc is the message connection with incoming messages.
*/
public void notifyIncomingMessage(MessageConnection mc) {
synchronized(mutex) {
mutex.notify();
}
}
/**
* Sets the WMAFramework incoming message nofitication listener.
* @param listener is the NotificationListener to set.
*/
protected void setIncomingMessageListener(IncomingMessageListener listener) {
incomingMessageListener = listener;
}
/**
* Notify the message processor to exit
*/
protected void start() {
try {
mc.setMessageListener(this);
} catch (Exception e) {
}
th.start();
}
/**
* Notify the message processor to exit
*/
protected void stop() {
done = true;
}
/**
* Thread's run method to wait for and process received messages.
*/
public void run() {
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
}
}
|