org.miv.mbox.net
Class SenderPool

java.lang.Object
  extended by org.miv.mbox.net.SenderPool

public class SenderPool
extends Object

Create and cache senders automatically.

A sender pool allows to avoid creating a Sender for each remote object we want to send message to, since one can use the same sender for the same host. The sender pool remembers hosts and ports and their associated sender. It can also create senders automatically for a given MBoxLocator.

Since:
20040624
Author:
Antoine Dutot
See Also:
Sender, Receiver

Field Summary
protected  boolean debug
           
protected  HashMap<String,Sender> senders
          Map of senders.
 
Constructor Summary
SenderPool()
          Empty sender pool.
SenderPool(boolean debug)
          Empty sender pool.
 
Method Summary
 Sender connect(MBoxLocator locator)
          Create a new sender for the host part of the given locator and cache it.
 Sender connect(String hostname, int port)
          Create a new sender to "//hostname:port" and cache it.
 Sender getSender(MBoxLocator locator)
          Get the sender for the host given by the given locator.
 Sender getSender(String hostname, int port)
          Get the sender for the host given by the given hostname and port.
 boolean hasSender(MBoxLocator locator)
          Is there a cached connection to the hostname and port of the given locator.
 void send(MBoxLocator to, Packet packet)
          Send a prepacked message to a distant message box, first creating a sender if not already cached.
 void send(String from, MBoxLocator to, Object... messages)
          Send a message to a distant message box, first creating a sender if not already cached.
 void send(String from, String hostname, int port, String to, Object... messages)
          Send a message to a distant message box, first creating a sender if not already cached.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

senders

protected HashMap<String,Sender> senders
Map of senders. Each item is a pair (key,value) where the key is the sender locator host id (MBoxLocator.getHostId()) as a string and the value is the Sender instance.


debug

protected boolean debug
Constructor Detail

SenderPool

public SenderPool()
Empty sender pool.


SenderPool

public SenderPool(boolean debug)
Empty sender pool.

Parameters:
debug - Activate debugging in all senders created.
Method Detail

hasSender

public boolean hasSender(MBoxLocator locator)
Is there a cached connection to the hostname and port of the given locator.

Parameters:
locator - The locator to check.
Returns:
True if a sender is cached for this locator.

getSender

public Sender getSender(MBoxLocator locator)
Get the sender for the host given by the given locator. Only the hostname and port of the locator are considered.

Parameters:
locator - The locator of the host to search.
Returns:
A sender to this host if cached, else null.

getSender

public Sender getSender(String hostname,
                        int port)
Get the sender for the host given by the given hostname and port.

Parameters:
hostname - The hostname of the host to connect to.
port - The port to connect to.
Returns:
A sender to this host if cached, else null.

connect

public Sender connect(String hostname,
                      int port)
               throws IOException,
                      UnknownHostException
Create a new sender to "//hostname:port" and cache it. If a sender already exists it is used.

Parameters:
hostname - The hostname of the host to connect to.
port - The port to connect to.
Throws:
IOException
UnknownHostException

connect

public Sender connect(MBoxLocator locator)
               throws IOException,
                      UnknownHostException
Create a new sender for the host part of the given locator and cache it. If a sender already exists it is used.

Parameters:
locator - The locator used to create the connection.
Throws:
IOException
UnknownHostException

send

public void send(String from,
                 String hostname,
                 int port,
                 String to,
                 Object... messages)
          throws IOException,
                 UnknownHostException
Send a message to a distant message box, first creating a sender if not already cached.

Parameters:
from - The sender object name.
hostname - The host name where the receiver box resides.
port - The port of the host where the receiver box resides.
to - The receiver box name.
messages - The message data.
Throws:
IOException
UnknownHostException

send

public void send(String from,
                 MBoxLocator to,
                 Object... messages)
          throws IOException,
                 UnknownHostException
Send a message to a distant message box, first creating a sender if not already cached.

Parameters:
from - The sender object name.
to - The location and name of the receiver box.
messages - The message data.
Throws:
IOException
UnknownHostException

send

public void send(MBoxLocator to,
                 Packet packet)
          throws IOException,
                 UnknownHostException
Send a prepacked message to a distant message box, first creating a sender if not already cached.

Parameters:
to - The location and name of the receiver box.
packet - The prepacket message.
Throws:
IOException
UnknownHostException


Copyright © 2011. All Rights Reserved.