org.jabber.jabberbeans
Class ConnectionBean

java.lang.Object
  |
  +--org.jabber.jabberbeans.ConnectionBean
All Implemented Interfaces:
PacketListenerRegistrar, java.io.Serializable
Direct Known Subclasses:
ConnectionBeanSSL

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.)

See Also:
MessengerBean, IQBean, RosterBean, org.jabber.jabberbeans.sax, Serialized Form

Nested Class Summary
 class ConnectionBean.InputStreamInterface
           
 class ConnectionBean.OutputStreamInterface
           
 class ConnectionBean.XMLStreamMonitor
           
 
Field Summary
static int CONNECTION_TIMEOUT
          the default time is 10000 ms or 10 seconds
static int DEFAULT_PORT
          the default port for jabber servers, 5222
protected static java.lang.String DEFAULT_XMLSTREAM_NS
          Default namespace for XMLStream
protected  boolean DisableAutoHeader
          used to disable auto-sending XML stream tag
 
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 connect(java.net.Socket s, java.net.InetAddress host)
           
 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.
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.
 java.net.InetAddress getAddress()
           
 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.
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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PORT

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

See Also:
Constant Field Values

CONNECTION_TIMEOUT

public static final int CONNECTION_TIMEOUT
the default time is 10000 ms or 10 seconds

See Also:
Constant Field Values

DEFAULT_XMLSTREAM_NS

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

See Also:
Constant Field Values

DisableAutoHeader

protected 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.
java.io.IOException

connect

public void connect(java.net.Socket s,
                    java.net.InetAddress host)
             throws java.io.IOException
java.io.IOException

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.
java.io.IOException

getAddress

public final java.net.InetAddress getAddress()

disconnect

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


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.

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()