spread
Class SpreadConnection

java.lang.Object
  extended by spread.SpreadConnection

public class SpreadConnection
extends java.lang.Object

A SpreadConnection object is used to establish a connection to a spread daemon. To connect to a spread daemon, first create a new SpreadConnection object, and then call connect(InetAddress, int, String, boolean, boolean):

 SpreadConnection connection = new SpreadConnection();
 connection.connect(null, 0, "name", false, false);
 

The only methods that can be called before connect(InetAddress, int, String, boolean, boolean) are the add (add(BasicMessageListener), add(AdvancedMessageListener)) and remove (remove(BasicMessageListener), remove(AdvancedMessageListener)) methods. If any other methods are called, a SpreadException is thrown, except for getPrivateGroup(), which returns null.

To disconnect from the daemon, call disconnect():

 connection.disconnect();
 

To send a message on this connection, call multicast(SpreadMessage):

 connection.multicast(message);
 

To receive a message sent to this connection, call receive():

 SpreadMessage message = connection.receive();
 


Nested Class Summary
private  class SpreadConnection.Listener
           
 
Field Summary
private static int ACCEPT_SESSION
           
private  java.net.InetAddress address
           
protected  java.util.Vector advancedListeners
           
private  java.lang.String authClassName
           
private  java.lang.reflect.Method authMethodAuthenticate
           
private  java.lang.String authName
           
private  java.lang.Object authObj
           
protected  java.util.Vector basicListeners
           
private static java.lang.Object BUFFER_ADD_ADVANCED
           
private static java.lang.Object BUFFER_ADD_BASIC
           
private static java.lang.Object BUFFER_DISCONNECT
           
private static java.lang.Object BUFFER_REMOVE_ADVANCED
           
private static java.lang.Object BUFFER_REMOVE_BASIC
           
private  boolean callingListeners
           
private  boolean connected
           
private static java.lang.String DEFAULT_AUTH_NAME
           
private static java.lang.String DEFAULT_AUTHCLASS_NAME
           
private static int DEFAULT_SPREAD_PORT
           
private static int ENDIAN_TYPE
           
private  SpreadGroup group
           
private  boolean groupMembership
           
private  SpreadConnection.Listener listener
           
private  java.util.Vector listenerBuffer
           
private  java.lang.Boolean listenersynchro
           
private static int MAX_AUTH_METHODS
           
private static int MAX_AUTH_NAME
           
protected static int MAX_GROUP_NAME
           
private static int MAX_MESSAGE_LENGTH
           
private static int MAX_PRIVATE_NAME
           
private  int port
           
private  boolean priority
           
private  java.lang.Boolean rsynchro
           
private  java.net.Socket socket
           
private  java.io.InputStream socketInput
           
private  java.io.OutputStream socketOutput
           
private static int SP_MAJOR_VERSION
           
private static int SP_MINOR_VERSION
           
private static int SP_PATCH_VERSION
           
private  java.lang.Boolean wsynchro
           
 
Constructor Summary
SpreadConnection()
          Initializes a new SpreadConnection object.
 
Method Summary
 void add(AdvancedMessageListener listener)
          Adds the AdvancedMessageListener to this connection.
 void add(BasicMessageListener listener)
          Adds the BasicMessageListener to this connection.
private  void checkAccept()
           
private  void checkVersion()
           
private static int clearEndian(int i)
           
 void connect(java.net.InetAddress address, int port, java.lang.String privateName, boolean priority, boolean groupMembership)
          Establishes a connection to a spread daemon.
 void disconnect()
          Disconnects the connection to the daemon.
protected static int flip(int i)
           
private static short flip(short s)
           
 SpreadGroup getPrivateGroup()
          Gets the private group for this connection.
private  void instantiateAuthMethod()
           
private  SpreadMessage internal_receive()
           
 void multicast(SpreadMessage message)
          Multicasts a message.
 void multicast(SpreadMessage[] messages)
          Multicasts an array of messages.
 boolean poll()
          Returns true if there are any messages waiting on this connection.
private  void readAuthMethods()
           
private  void readBytesFromSocket(byte[] buffer, java.lang.String bufferTypeString)
           
private  void readGroup()
           
 SpreadMessage receive()
          Receives the next message waiting on this connection.
 SpreadMessage[] receive(int numMessages)
          Receives numMessages messages on the connection and returns them in an array.
 void registerAuthentication(java.lang.String authName, java.lang.String authClassName)
          Sets the authentication name and class string for the client side authentication method.
 void remove(AdvancedMessageListener listener)
          Removes the AdvancedMessageListener from this connection.
 void remove(BasicMessageListener listener)
          Removes the BasicMessageListener from this connection.
private static boolean sameEndian(int i)
           
private  void sendAuthMethod()
           
private  void sendConnect(java.lang.String privateName)
           
private  void setBufferSizes()
           
private  void startListener()
           
private  void stopListener()
           
private static void toBytes(int i, byte[] buffer, int bufferIndex)
           
private static void toBytes(SpreadGroup group, byte[] buffer, int bufferIndex)
           
protected  SpreadGroup toGroup(byte[] buffer, int bufferIndex)
           
protected static int toInt(byte[] buffer, int bufferIndex)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SPREAD_PORT

private static final int DEFAULT_SPREAD_PORT
See Also:
Constant Field Values

MAX_PRIVATE_NAME

private static final int MAX_PRIVATE_NAME
See Also:
Constant Field Values

MAX_MESSAGE_LENGTH

private static final int MAX_MESSAGE_LENGTH
See Also:
Constant Field Values

MAX_GROUP_NAME

protected static final int MAX_GROUP_NAME
See Also:
Constant Field Values

SP_MAJOR_VERSION

private static final int SP_MAJOR_VERSION
See Also:
Constant Field Values

SP_MINOR_VERSION

private static final int SP_MINOR_VERSION
See Also:
Constant Field Values

SP_PATCH_VERSION

private static final int SP_PATCH_VERSION
See Also:
Constant Field Values

DEFAULT_AUTH_NAME

private static final java.lang.String DEFAULT_AUTH_NAME
See Also:
Constant Field Values

DEFAULT_AUTHCLASS_NAME

private static final java.lang.String DEFAULT_AUTHCLASS_NAME
See Also:
Constant Field Values

MAX_AUTH_NAME

private static final int MAX_AUTH_NAME
See Also:
Constant Field Values

MAX_AUTH_METHODS

private static final int MAX_AUTH_METHODS
See Also:
Constant Field Values

ACCEPT_SESSION

private static final int ACCEPT_SESSION
See Also:
Constant Field Values

ENDIAN_TYPE

private static final int ENDIAN_TYPE
See Also:
Constant Field Values

connected

private boolean connected

rsynchro

private java.lang.Boolean rsynchro

wsynchro

private java.lang.Boolean wsynchro

listenersynchro

private java.lang.Boolean listenersynchro

callingListeners

private boolean callingListeners

listener

private SpreadConnection.Listener listener

basicListeners

protected java.util.Vector basicListeners

advancedListeners

protected java.util.Vector advancedListeners

address

private java.net.InetAddress address

port

private int port

priority

private boolean priority

groupMembership

private boolean groupMembership

authName

private java.lang.String authName

authClassName

private java.lang.String authClassName

authObj

private java.lang.Object authObj

authMethodAuthenticate

private java.lang.reflect.Method authMethodAuthenticate

socket

private java.net.Socket socket

socketInput

private java.io.InputStream socketInput

socketOutput

private java.io.OutputStream socketOutput

group

private SpreadGroup group

listenerBuffer

private java.util.Vector listenerBuffer

BUFFER_DISCONNECT

private static final java.lang.Object BUFFER_DISCONNECT

BUFFER_ADD_BASIC

private static final java.lang.Object BUFFER_ADD_BASIC

BUFFER_ADD_ADVANCED

private static final java.lang.Object BUFFER_ADD_ADVANCED

BUFFER_REMOVE_BASIC

private static final java.lang.Object BUFFER_REMOVE_BASIC

BUFFER_REMOVE_ADVANCED

private static final java.lang.Object BUFFER_REMOVE_ADVANCED
Constructor Detail

SpreadConnection

public SpreadConnection()
Initializes a new SpreadConnection object. To connect to a daemon with this object, use connect(InetAddress, int, String, boolean, boolean).

See Also:
connect(InetAddress, int, String, boolean, boolean)
Method Detail

sameEndian

private static boolean sameEndian(int i)

clearEndian

private static int clearEndian(int i)

flip

protected static int flip(int i)

flip

private static short flip(short s)

toBytes

private static void toBytes(SpreadGroup group,
                            byte[] buffer,
                            int bufferIndex)

toBytes

private static void toBytes(int i,
                            byte[] buffer,
                            int bufferIndex)

toInt

protected static int toInt(byte[] buffer,
                           int bufferIndex)

readBytesFromSocket

private void readBytesFromSocket(byte[] buffer,
                                 java.lang.String bufferTypeString)
                          throws SpreadException
Throws:
SpreadException

toGroup

protected SpreadGroup toGroup(byte[] buffer,
                              int bufferIndex)

setBufferSizes

private void setBufferSizes()
                     throws SpreadException
Throws:
SpreadException

sendConnect

private void sendConnect(java.lang.String privateName)
                  throws SpreadException
Throws:
SpreadException

readAuthMethods

private void readAuthMethods()
                      throws SpreadException
Throws:
SpreadException

sendAuthMethod

private void sendAuthMethod()
                     throws SpreadException
Throws:
SpreadException

instantiateAuthMethod

private void instantiateAuthMethod()
                            throws SpreadException
Throws:
SpreadException

checkAccept

private void checkAccept()
                  throws SpreadException
Throws:
SpreadException

checkVersion

private void checkVersion()
                   throws SpreadException
Throws:
SpreadException

readGroup

private void readGroup()
                throws SpreadException
Throws:
SpreadException

registerAuthentication

public void registerAuthentication(java.lang.String authName,
                                   java.lang.String authClassName)
                            throws SpreadException
Sets the authentication name and class string for the client side authentication method. An authentication method can only be registered before connect is called. The authentication method registered will then be used whenever connect(InetAddress, int, String, boolean, boolean) is called.

Parameters:
authName - the short official "name" of the method begin registered.
authClassName - the complete class name for the method (including package)
Throws:
SpreadException - if the connection is already established

connect

public void connect(java.net.InetAddress address,
                    int port,
                    java.lang.String privateName,
                    boolean priority,
                    boolean groupMembership)
             throws SpreadException
Establishes a connection to a spread daemon. Groups can be joined, and messages can be sent or received once the connection has been established.

Parameters:
address - the daemon's address, or null to connect to the localhost
port - the daemon's port, or 0 for the default port (4803)
privateName - the private name to use for this connection
priority - if true, this is a priority connection
groupMembership - if true, membership messages will be received on this connection
Throws:
SpreadException - if the connection cannot be established
See Also:
disconnect()

disconnect

public void disconnect()
                throws SpreadException
Disconnects the connection to the daemon. Nothing else should be done with this connection after disconnecting it.

Throws:
SpreadException - if there is no connection or there is an error disconnecting
See Also:
connect(InetAddress, int, String, boolean, boolean)

getPrivateGroup

public SpreadGroup getPrivateGroup()
Gets the private group for this connection.

Returns:
the SpreadGroup representing this connection's private group, or null if there is no connection

receive

public SpreadMessage receive()
                      throws SpreadException,
                             java.io.InterruptedIOException
Receives the next message waiting on this connection. If there are no messages waiting, the call will block until a message is ready to be received.

Returns:
the message that has just been received
Throws:
SpreadException - if there is no connection or there is any error reading a new message
java.io.InterruptedIOException

internal_receive

private SpreadMessage internal_receive()
                                throws SpreadException,
                                       java.io.InterruptedIOException
Throws:
SpreadException
java.io.InterruptedIOException

receive

public SpreadMessage[] receive(int numMessages)
                        throws SpreadException,
                               java.io.InterruptedIOException
Receives numMessages messages on the connection and returns them in an array. If there are not numMessages messages waiting, the call will block until there are enough messages available.

Parameters:
numMessages - the number of messages to receive
Returns:
an array of messages
Throws:
SpreadException - if there is no connection or if there is any error reading the messages
java.io.InterruptedIOException

poll

public boolean poll()
             throws SpreadException
Returns true if there are any messages waiting on this connection.

Returns:
true if there is at least one message that can be received
Throws:
SpreadException - if there is no connection or if there is an error checking for messages

startListener

private void startListener()

add

public void add(BasicMessageListener listener)
Adds the BasicMessageListener to this connection. If there are no other listeners, this call will start a thread to listen for new messages. From the time this function is called until this listener is removed, BasicMessageListener.messageReceived(SpreadMessage) will be called every time a message is received.

Parameters:
listener - a BasicMessageListener to add to this connection
See Also:
remove(BasicMessageListener)

add

public void add(AdvancedMessageListener listener)
Adds the AdvancedMessageListener to this connection. If there are no other listeners, this call will start a thread to listen for new messages. From the time this function is called until this listener is removed, AdvancedMessageListener.regularMessageReceived(SpreadMessage) will be called every time a regular message is received, and AdvancedMessageListener.membershipMessageReceived(SpreadMessage) will be called every time a membership message is received.

Parameters:
listener - an AdvancedMessageListener to add to this connection
See Also:
remove(AdvancedMessageListener)

stopListener

private void stopListener()

remove

public void remove(BasicMessageListener listener)
Removes the BasicMessageListener from this connection. If this is the only listener on this connection, the listener thread will be stopped.

Parameters:
listener - the listener to remove
See Also:
add(BasicMessageListener)

remove

public void remove(AdvancedMessageListener listener)
Removes the AdvancedMessageListener from this connection. If this is the only listener on this connection, the listener thread will be stopped.

Parameters:
listener - the listener to remove
See Also:
add(AdvancedMessageListener)

multicast

public void multicast(SpreadMessage message)
               throws SpreadException
Multicasts a message. The message will be sent to all the groups specified in the message.

Parameters:
message - the message to multicast
Throws:
SpreadException - if there is no connection or if there is any error sending the message

multicast

public void multicast(SpreadMessage[] messages)
               throws SpreadException
Multicasts an array of messages. Each message will be sent to all the groups specified in the message.

Parameters:
messages - the messages to multicast
Throws:
SpreadException - if there is no connection or if there is any error sending the messages


Copyright © 2004 Spread Concepts LLC. All Rights Reserved.