ksu.cis.mom
Class MulticastHandler

java.lang.Object
  extended byjava.lang.Thread
      extended byksu.cis.mom.MulticastHandler
All Implemented Interfaces:
Runnable

public class MulticastHandler
extends Thread

MulticastHandler is responsible for initializing and starting multicast socket, including joining/leaving multicast group. In generally, it handle multicast connection with other agents. It uses the MulticastSocket class to subscribe to multicast group.

When an agent is created, it needs to creates a new multicast handler thread to be able to receive a start of multicast conversation from other agents. When MulticastHandler class is created, it creates the multicast socket and join to specified multicast group. Then, it automatically sends a multicast message to the group indicating that this agent has join the group. When a MulticastHandler receives the message indicating the join, it calls the receiveMulticastJoin method in agent class. Below is how an agent can start the MessageHandler class.

 MulticastHandler mh = new MessageHandler(this, this.port, 0, multicast_address);
 mh.start();
 

To create the MessageHandler, it requires four/five parameters, port number, a reference to parent agent object, time to live of multicast packet, multicast address and/or maximum datagram received packet size.
Note that the multicast address is actually a class D IP addresses that are in the range 224.0.0.0 to 239.255.255.255.
When this class is started, the multicast handler starts a multicast socket on the indicated port and waits for messages from other agents. When multicast handler receives a message, it will check whether the message is a start of new conversation/join/leave/conversation message. If it is a start of conversation, the MulticastHandler simply calls the parent agent's receiveMulticastcastConversation method with the multicast socket, received message and multicast message queue. The agent then verify the received message and start an appropriate conversation. If the received message is for any multicast conversation class, it adds the message to the multicast message queue. Then the multicast conversation class can get the message from this queue later.

Note that the multicast communication has loopback effect(message also send to itself), but the MulticastHandler will ignore this message.

History:
06/12/2003 - creation date

Since:
agentMom 2.0

Field Summary
static int CONTENT
          To indicate that the message is not a request to start conversation.
protected  InetAddress group
          multicast group address
static int JOIN
          Identifies a JOIN multicast PDU
protected  boolean leave
          Flag control receive thread execution
static int LEAVE
          Identifies a LEAVE multicast PDU
 int maxSize
          Default maximum size of received multicast packet.
 MulticastSocket mSocket
          Multicast socket used to send and receive multicast message
 int multicast_port
          Multicast port
 Vector multicast_queue
          Message queue for multicast conversation.
 Agent parent
          Reference to parent agent class.
static int START_CONVERSATION
          To indicate that the message is a request to start conversation.
 int TimeToLive
          Time-to-live for multicast packets sent out on this MulticastSocket in order to control the scope of the multicasts.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
MulticastHandler(Agent p, int port, int ttl, InetAddress group)
          Default constructor for MulticastHandler.
MulticastHandler(Agent p, int port, int ttl, InetAddress group, int packetSize)
          Default constructor for MulticastHandler.
 
Method Summary
 void checkLeave()
          To check the value of leave attribute.
 void queueMessage(Message m)
          Method for adding new message to the queue.
 void run()
          Run thread method for MulticastHandler.It simply waits for incoming multicast message from other agents.
 void sendJoin()
          Sends a multicast JOIN message to notify other agents.
 void sendLeave()
          Sends a multicast LEAVE message to notify other agents.
 void setLeave(boolean leave)
          Set value for leave.
 void write(String s)
          Method for easily printout information on screen.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

maxSize

public int maxSize
Default maximum size of received multicast packet.


START_CONVERSATION

public static final int START_CONVERSATION
To indicate that the message is a request to start conversation.

See Also:
Constant Field Values

CONTENT

public static final int CONTENT
To indicate that the message is not a request to start conversation.

See Also:
Constant Field Values

JOIN

public static final int JOIN
Identifies a JOIN multicast PDU

See Also:
Constant Field Values

LEAVE

public static final int LEAVE
Identifies a LEAVE multicast PDU

See Also:
Constant Field Values

parent

public Agent parent
Reference to parent agent class.


TimeToLive

public int TimeToLive
Time-to-live for multicast packets sent out on this MulticastSocket in order to control the scope of the multicasts.


mSocket

public MulticastSocket mSocket
Multicast socket used to send and receive multicast message


multicast_port

public int multicast_port
Multicast port


multicast_queue

public Vector multicast_queue
Message queue for multicast conversation. It is a Vector type that stores multicast message sent to this agent.


group

protected InetAddress group
multicast group address


leave

protected boolean leave
Flag control receive thread execution

Constructor Detail

MulticastHandler

public MulticastHandler(Agent p,
                        int port,
                        int ttl,
                        InetAddress group)
Default constructor for MulticastHandler. It is responsible for initializing necessary variables and creates multicast socket.

Parameters:
p - - reference to parent agent class.
port - - port number for listening for multicast message.
ttl - - time to live of datagram packet.
group - - multicast address, in the range 224.0.0.0-239.255.255.255

MulticastHandler

public MulticastHandler(Agent p,
                        int port,
                        int ttl,
                        InetAddress group,
                        int packetSize)
Default constructor for MulticastHandler. It is responsible for initializing necessary variables and creates multicast socket. This constructor allows to specify the size of received multicast packet.

Note that the received packet size must be greater than or equal to the sent packet. Otherwise, packet will be partially received.

Parameters:
p - reference to parent agent class.
port - - port number for listening for broadcast message.
ttl - - time to live of datagram packet.
group - - multicast address, in the range 224.0.0.0-239.255.255.255
packetSize - - user defined maximum size in byte of received multicast packet.
Method Detail

sendJoin

public void sendJoin()
              throws IOException

Sends a multicast JOIN message to notify other agents. We need to convert Message object to Byte type to be able to send it using multicast socket.

Throws:
IOException

sendLeave

public void sendLeave()
               throws IOException

Sends a multicast LEAVE message to notify other agents. We need to convert Message object to Byte type to be able to send it using multicast socket.

Throws:
IOException

run

public void run()
Run thread method for MulticastHandler.It simply waits for incoming multicast message from other agents. Then, it checks the message whether it is a start of new multicast conversation, or a message for multicast conversation class.

Note that this is a blocking read message.


write

public void write(String s)
Method for easily printout information on screen.

Parameters:
s - java.lang.String

queueMessage

public void queueMessage(Message m)
Method for adding new message to the queue.

Parameters:
m - - message to be retrived by multicast conversation.

setLeave

public void setLeave(boolean leave)
Set value for leave.

Parameters:
leave - - if value is true, it will make the handler thread to stop the next time this value is checked in the run method.

checkLeave

public void checkLeave()
To check the value of leave attribute.