org.jabber.jabberbeans
Class ConnectionBean

java.lang.Object
  |
  +--org.jabber.jabberbeans.ConnectionBean

public class ConnectionBean
extends java.lang.Object
implements java.io.Serializable, PacketListenerRegistrar

ConnectionBean is the main bean for JabberBeans.

A ConnectionBean models the connection to the server, and all other beans rely on the ConnectionBean for server communication.

Internally, a ConnectionBean is just a "standard" interface to the backend (by default, Jabberbeans-SAX) which handles the server communication - you simply create objects and push them through 'send' to get to the server, and subscribe to the right interfaces to 'receive' new objects as they come in.

All the other beans (MessengerBean, IQBean, and RosterBean so far) are considered optional. Internally, they just use the ConnectionBean to send data, and listen to the events being broadcasted by the ConnectionBean to filter their 'received' data (which they then rebroadcast to their respective listeners.)

Version:
$Revision: 1.1.1.1 $
Author:
David Waite <dwaite@jabber.com>, $Author: lucky $
See Also:
MessengerBean, IQBean, RosterBean, org.jabber.jabberbeans.sax, Serialized Form

Inner Class Summary
 class ConnectionBean.InputStreamInterface
           
 class ConnectionBean.OutputStreamInterface
           
 class ConnectionBean.XMLStreamMonitor
           
 
Field Summary
private  java.util.Vector connectionListeners
          list of implementing interfaces which are listening for connection change events.
static int DEFAULT_PORT
          the default port for jabber servers, 5222
private static java.lang.String DEFAULT_XMLSTREAM_NS
          Default namespace for XMLStream
private  boolean DisableAutoHeader
          used to disable auto-sending XML stream tag
private  InputStreamHandler input
          InputStream handling Thread.
private  OutputStreamHandler output
          OutputStream handling Thread.
private  java.util.Vector packetListeners
          list of implementing interfaces which are listening for packet broadcast events.
private  java.lang.String sessionID
          storage for SessionID
private  java.net.Socket socket
          Socket, if one is used for the connection.
private  ConnectionEvent.EState status
          holds the current connection status.
 
Constructor Summary
ConnectionBean()
           
 
Method Summary
 void addConnectionListener(ConnectionListener l)
          addConnectionListener adds a ConnectionListener to the list of Listeners to be called when the connection state changes.
 void addPacketListener(PacketListener l)
          addPacketListener adds a PacketListener to the list of Listeners to be called when a packet is received or successfully sent out.
 void connect(java.net.InetAddress host)
          connect attempts to establish a connection to the default port on the specified server.
 void connect(java.net.InetAddress host, int port)
          connect attempts to establish a server connection to a specified port.
 void connect(java.net.InetAddress host, java.lang.String name)
          connect attempts to establish a server connection to the default port.
 void connect(java.net.InetAddress host, java.lang.String name, int port)
          connect attempts to establish a server connection to a specified port.
 void connect(java.io.InputStream is, java.io.OutputStream os)
          Begin communication with a Jabber server.
 void connect(java.net.Socket s)
          Begin communication with a Jabber server.
 void delConnectionListener(ConnectionListener l)
          delConnectionListener removes a previously added ConnectionListener from the list of Listeners to be called when a connection state change event happens.
 void delPacketListener(PacketListener l)
          delPacketListener removes a PacketListener to the list of Listeners to be called when a packet is received or successfully sent out.
 void disableStreamHeader()
           
 void disconnect()
          disconnect correctly closes down the connection to the Jabber server in the backend.
private  void disconnected(ConnectionEvent.EReason reason)
           
protected  void fireReceivedPacket(Packet packet)
          Internal method which sends out packet events on received data.
protected  void fireSentPacket(Packet packet)
          Internal method which sends out packet events on sent data.
protected  void fireUnsentPacket(Packet packet)
          Internal method which sends out packet events on failed send data.
 ConnectionEvent.EState getConnectionState()
          returns the current connection state (disconnected, connecting, connected).
 java.lang.String getParser()
           
 java.lang.String getSessionID()
           
 void onInputDeath(java.lang.Exception e)
          handler code for input stream death.
 void onOutputDeath(java.lang.Exception e)
          handler code for output stream death.
 void received(Packet packet)
          received is the method used to indicate that a new packet has arrived.
 void send(Packet packet)
          send is the method used to send all packets to the server.
private  void sendFailed(Packet packet)
          sendFailed is the method used to indicate that a packet failed to send.
private  void sent(Packet packet)
          sent is the method used to indicate that a new packet has been sent.
private  void setConnecting()
           
protected  void setConnectionState(ConnectionEvent.EState state, ConnectionEvent.EReason reason)
          internal method which allows you to set the connection state, then broadcast the change to all listening classes.
 void setParser(java.lang.String s)
           
private  void setSocket(java.net.Socket s)
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

status

private ConnectionEvent.EState status
holds the current connection status. (e.g. connecting)

connectionListeners

private final java.util.Vector connectionListeners
list of implementing interfaces which are listening for connection change events.

packetListeners

private final java.util.Vector packetListeners
list of implementing interfaces which are listening for packet broadcast events.

socket

private java.net.Socket socket
Socket, if one is used for the connection.

input

private InputStreamHandler input
InputStream handling Thread. This class is responsible for parsing of external data (via a SAX-enabled parser), and also broadcasts received packet events.

output

private OutputStreamHandler output
OutputStream handling Thread. This class is responsible for sending packets (providing a more asynchronous sending interface), and also broadcasts sent packet events.

DEFAULT_PORT

public static final int DEFAULT_PORT
the default port for jabber servers, 5222

DEFAULT_XMLSTREAM_NS

private static final java.lang.String DEFAULT_XMLSTREAM_NS
Default namespace for XMLStream

sessionID

private java.lang.String sessionID
storage for SessionID

DisableAutoHeader

private boolean DisableAutoHeader
used to disable auto-sending XML stream tag
Constructor Detail

ConnectionBean

public ConnectionBean()
Method Detail

setParser

public void setParser(java.lang.String s)

getParser

public java.lang.String getParser()

connect

public void connect(java.net.InetAddress host)
             throws java.io.IOException
connect attempts to establish a connection to the default port on the specified server.
Parameters:
host - InetAddress of the server
Throws:
java.io.IOException - socket error in backend

connect

public void connect(java.net.InetAddress host,
                    java.lang.String name)
             throws java.io.IOException
connect attempts to establish a server connection to the default port. This version allows you to specify a string, if the host name the Jabber server expects is different than the one you are connecting with (e.g. for applets).
Parameters:
host - InetAddress of the server
name - String holding the name to send to the Jabber server on connection.
Throws:
java.io.IOException - socket error in backend

connect

public void connect(java.net.InetAddress host,
                    int port)
             throws java.io.IOException
connect attempts to establish a server connection to a specified port. This implementation takes a hostname and port.
Parameters:
host - InetAddress of the server
port - an int specifying the port number
Throws:
java.io.IOException - socket error in backend

connect

public void connect(java.net.InetAddress host,
                    java.lang.String name,
                    int port)
             throws java.io.IOException
connect attempts to establish a server connection to a specified port. This version allows you to specify a string, if the host name the Jabber server expects is different than the one you are connecting with (e.g. for applets).
Parameters:
host - InetAddress of the server
name - String holding the name to send to the Jabber server on connection.
port - an int specifying the port number
Throws:
java.io.IOException - socket error in backend

connect

public void connect(java.net.Socket s)
             throws java.io.IOException
Begin communication with a Jabber server. This method assumes other code responsible for setting up the connection as far as sending the XML Stream header.
Parameters:
s - Socket connecting us with the Jabber server.

connect

public void connect(java.io.InputStream is,
                    java.io.OutputStream os)
             throws java.io.IOException
Begin communication with a Jabber server. This method assumes other code responsible for setting up the connection as far as sending the XML Stream header.
Parameters:
is - InputStream for incoming data from the Jabber server
os - OutputStream for sending data out to the Jabber server.

setConnecting

private void setConnecting()

setSocket

private void setSocket(java.net.Socket s)

disconnect

public void disconnect()
disconnect correctly closes down the connection to the Jabber server in the backend.

disconnected

private void disconnected(ConnectionEvent.EReason reason)

addConnectionListener

public final void addConnectionListener(ConnectionListener l)
addConnectionListener adds a ConnectionListener to the list of Listeners to be called when the connection state changes.
Parameters:
l - a ConnectionListener. Cannot add the same listener twice

delConnectionListener

public final void delConnectionListener(ConnectionListener l)
delConnectionListener removes a previously added ConnectionListener from the list of Listeners to be called when a connection state change event happens.
Parameters:
l - a ConnectionListener value

addPacketListener

public final void addPacketListener(PacketListener l)
addPacketListener adds a PacketListener to the list of Listeners to be called when a packet is received or successfully sent out.
Specified by:
addPacketListener in interface PacketListenerRegistrar
Parameters:
l - a PacketListener value

delPacketListener

public final void delPacketListener(PacketListener l)
delPacketListener removes a PacketListener to the list of Listeners to be called when a packet is received or successfully sent out.
Specified by:
delPacketListener in interface PacketListenerRegistrar
Parameters:
l - a PacketListener value

send

public void send(Packet packet)
send is the method used to send all packets to the server. A Packet is a code element that knows how to explain itself in XML. The Packet is asynchronously sent to the server, with registered ConnectionListeners receiving an event on successful sending.
Parameters:
packet - a Packet being sent to the server

received

public void received(Packet packet)
received is the method used to indicate that a new packet has arrived. This method is ONLY meant to be used by the InputStreamHandler thread. Please avoid the urge to use it. It will probably become non-accessable in the near future, consider this a warning.
Parameters:
packet - a Packet arriving from the server.

sent

private void sent(Packet packet)
sent is the method used to indicate that a new packet has been sent. This method is ONLY meant to be used by the OutputStreamHandler thread.
Parameters:
packet - a Packet arriving from the server.

sendFailed

private void sendFailed(Packet packet)
sendFailed is the method used to indicate that a packet failed to send. This method is ONLY meant to be used by the OutputStreamHandler thread.
Parameters:
packet - a Packet arriving from the server.

fireReceivedPacket

protected final void fireReceivedPacket(Packet packet)
Internal method which sends out packet events on received data. Called on the InputStreamHandler thread.
Parameters:
packet - the packet we are sending out to all subscribed listeners

fireSentPacket

protected final void fireSentPacket(Packet packet)
Internal method which sends out packet events on sent data. Called on the OutputStreamHandler thread.
Parameters:
packet - the packet we are sending out to all subscribed listeners

fireUnsentPacket

protected final void fireUnsentPacket(Packet packet)
Internal method which sends out packet events on failed send data. Called on the OutputStreamHandler thread.
Parameters:
packet - the packet we are sending out to all subscribed listeners

onOutputDeath

public void onOutputDeath(java.lang.Exception e)
handler code for output stream death.

onInputDeath

public void onInputDeath(java.lang.Exception e)
handler code for input stream death.

getConnectionState

public final ConnectionEvent.EState getConnectionState()
returns the current connection state (disconnected, connecting, connected).

setConnectionState

protected final void setConnectionState(ConnectionEvent.EState state,
                                        ConnectionEvent.EReason reason)
internal method which allows you to set the connection state, then broadcast the change to all listening classes.
Parameters:
state - new state to enter, based on possibilities from ConnectionEvent.EState
reason - the reason we are changing states
See Also:
ConnectionEvent.EState

getSessionID

public java.lang.String getSessionID()

disableStreamHeader

public void disableStreamHeader()