org.miv.mbox
Class MBoxBase

java.lang.Object
  extended by org.miv.mbox.MBoxBase
All Implemented Interfaces:
MBox, MBoxListener
Direct Known Subclasses:
MBoxStandalone

public abstract class MBoxBase
extends Object
implements MBox, MBoxListener

MBox default implementation usable as base class.

The goal of this implementation is to provide a base to construct other classes able to handle messages comming from different sources and threads. The presupposed in this class is that the inheriting class will check messages at regular time using the method processMessages().

Since:
20041102
Author:
Antoine Dutot
See Also:
MBox

Nested Class Summary
static class MBoxBase.Message
          Simple message container.
 
Field Summary
protected  ArrayList<MBoxBase.Message> messages
          List of unread messages.
protected  ArrayList<MBoxBase.Message> mtmp
          Temporary array to store messages when sent to the listener to avoid blocking the Receiver that can post messages during the execution of messages.
 
Constructor Summary
MBoxBase()
          New empty Message Box.
 
Method Summary
 int getMessageCount()
           
 boolean isMBoxEmpty()
           
 ArrayList<MBoxBase.Message> popPendingMessages()
          Like processMessages() but instead of calling the processMessage(String, Object[]) method for each message, returns the list of pending messages.
 void post(String from, Object... data)
          Post a message in the message box.
abstract  void processMessage(String from, Object[] data)
          Method to override to process each incomming message.
 void processMessages()
          Process all messages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mtmp

protected ArrayList<MBoxBase.Message> mtmp
Temporary array to store messages when sent to the listener to avoid blocking the Receiver that can post messages during the execution of messages.


messages

protected ArrayList<MBoxBase.Message> messages
List of unread messages.

Constructor Detail

MBoxBase

public MBoxBase()
New empty Message Box.

Method Detail

isMBoxEmpty

public boolean isMBoxEmpty()

getMessageCount

public int getMessageCount()

post

public void post(String from,
                 Object... data)
Description copied from interface: MBox
Post a message in the message box. This method can be called from another thread.

Specified by:
post in interface MBox
Parameters:
from - Identifier of the sending object.
data - Array of posted objects.

processMessages

public void processMessages()
Process all messages. Call this method at regular intervals to empty the message box. This method will call processMessage(String, Object[]) for each message in the box.


popPendingMessages

public ArrayList<MBoxBase.Message> popPendingMessages()
Like processMessages() but instead of calling the processMessage(String, Object[]) method for each message, returns the list of pending messages. The messages are removed from the list of pending messages and therefore will not be processed by the next calls to processMessages().

Returns:
A list of all messages not yet processed, removed from the list of pending messages.

processMessage

public abstract void processMessage(String from,
                                    Object[] data)
Method to override to process each incomming message.

Specified by:
processMessage in interface MBoxListener
Parameters:
from - The address of the sender.
data - The data sent by the sender.


Copyright © 2011. All Rights Reserved.