Package spread
Class SpreadConnection
- java.lang.Object
-
- spread.SpreadConnection
-
public class SpreadConnection extends java.lang.ObjectA 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 callconnect(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 forgetPrivateGroup(), 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
Nested Classes Modifier and Type Class Description private classSpreadConnection.Listener
-
Field Summary
Fields Modifier and Type Field Description private static intACCEPT_SESSIONprivate java.net.InetAddressaddressprotected java.util.VectoradvancedListenersprivate java.lang.StringauthClassNameprivate java.lang.reflect.MethodauthMethodAuthenticateprivate java.lang.StringauthNameprivate java.lang.ObjectauthObjprotected java.util.VectorbasicListenersprivate static java.lang.ObjectBUFFER_ADD_ADVANCEDprivate static java.lang.ObjectBUFFER_ADD_BASICprivate static java.lang.ObjectBUFFER_DISCONNECTprivate static java.lang.ObjectBUFFER_REMOVE_ADVANCEDprivate static java.lang.ObjectBUFFER_REMOVE_BASICprivate booleancallingListenersprivate booleanconnectedprivate static java.lang.StringDEFAULT_AUTH_NAMEprivate static java.lang.StringDEFAULT_AUTHCLASS_NAMEprivate static intDEFAULT_SPREAD_PORTprivate static intENDIAN_TYPEprivate SpreadGroupgroupprivate booleangroupMembershipprivate SpreadConnection.Listenerlistenerprivate java.util.VectorlistenerBufferprivate java.lang.Booleanlistenersynchroprivate static intMAX_AUTH_METHODSprivate static intMAX_AUTH_NAMEprotected static intMAX_GROUP_NAMEprivate static intMAX_MESSAGE_LENGTHprivate static intMAX_PRIVATE_NAMEprivate intportprivate booleanpriorityprivate java.lang.Booleanrsynchroprivate java.net.Socketsocketprivate java.io.InputStreamsocketInputprivate java.io.OutputStreamsocketOutputprivate static intSP_MAJOR_VERSIONprivate static intSP_MINOR_VERSIONprivate static intSP_PATCH_VERSIONprivate java.lang.Booleanwsynchro
-
Constructor Summary
Constructors Constructor Description SpreadConnection()Initializes a new SpreadConnection object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(AdvancedMessageListener listener)Adds the AdvancedMessageListener to this connection.voidadd(BasicMessageListener listener)Adds the BasicMessageListener to this connection.private voidcheckAccept()private voidcheckVersion()private static intclearEndian(int i)voidconnect(java.net.InetAddress address, int port, java.lang.String privateName, boolean priority, boolean groupMembership)Establishes a connection to a spread daemon.voiddisconnect()Disconnects the connection to the daemon.protected static intflip(int i)private static shortflip(short s)SpreadGroupgetPrivateGroup()Gets the private group for this connection.private voidinstantiateAuthMethod()private SpreadMessageinternal_receive()voidmulticast(SpreadMessage message)Multicasts a message.voidmulticast(SpreadMessage[] messages)Multicasts an array of messages.booleanpoll()Returns true if there are any messages waiting on this connection.private voidreadAuthMethods()private voidreadBytesFromSocket(byte[] buffer, java.lang.String bufferTypeString)private voidreadGroup()SpreadMessagereceive()Receives the next message waiting on this connection.SpreadMessage[]receive(int numMessages)ReceivesnumMessagesmessages on the connection and returns them in an array.voidregisterAuthentication(java.lang.String authName, java.lang.String authClassName)Sets the authentication name and class string for the client side authentication method.voidremove(AdvancedMessageListener listener)Removes the AdvancedMessageListener from this connection.voidremove(BasicMessageListener listener)Removes the BasicMessageListener from this connection.private static booleansameEndian(int i)private voidsendAuthMethod()private voidsendConnect(java.lang.String privateName)private voidsetBufferSizes()private voidstartListener()private voidstopListener()private static voidtoBytes(int i, byte[] buffer, int bufferIndex)private static voidtoBytes(SpreadGroup group, byte[] buffer, int bufferIndex)protected SpreadGrouptoGroup(byte[] buffer, int bufferIndex)protected static inttoInt(byte[] buffer, int bufferIndex)
-
-
-
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, useconnect(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 SpreadExceptionSets 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 wheneverconnect(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 SpreadExceptionEstablishes 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 localhostport- the daemon's port, or 0 for the default port (4803)privateName- the private name to use for this connectionpriority- if true, this is a priority connectiongroupMembership- 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 SpreadExceptionDisconnects 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 messagejava.io.InterruptedIOException
-
internal_receive
private SpreadMessage internal_receive() throws SpreadException, java.io.InterruptedIOException
- Throws:
SpreadExceptionjava.io.InterruptedIOException
-
receive
public SpreadMessage[] receive(int numMessages) throws SpreadException, java.io.InterruptedIOException
ReceivesnumMessagesmessages on the connection and returns them in an array. If there are notnumMessagesmessages 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 messagesjava.io.InterruptedIOException
-
poll
public boolean poll() throws SpreadExceptionReturns 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, andAdvancedMessageListener.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
-
-