org.psepr.jClient
Class PsEPRConnection

java.lang.Object
  extended by org.psepr.jClient.PsEPRConnection
All Implemented Interfaces:
org.jivesoftware.smack.ConnectionListener, org.jivesoftware.smack.PacketListener

public class PsEPRConnection
extends java.lang.Object
implements org.jivesoftware.smack.ConnectionListener, org.jivesoftware.smack.PacketListener

Connection to the PsEPR infrastructure.

There are several parameters needed to build an active connection:

 PsEPRServiceIdentity pServiceID = new PsEPRServiceIdentity();
 try {
        if ((fromService == null) || (fromServicePassword == null)) {
                if (verbose > 0 ) {
                        System.out.println("Using configuration default service/password");
                }
                pServiceID.useDefaultIdentity();
        }
        else {
                pServiceID.setSimpleIdentity(fromService, fromServicePassword);
        }
 }
 catch (Exception e) {
        System.out.println("Could not create identity -- either params bad or default service in param file is bad.");
        return;
 }
 try {
        pConn = new PsEPRConnection(pServiceID);
 }
 catch (Exception e) {
        pConn = null;
        System.out.println("PsEPRConnection exception: "+e.toString());
 }
 if (pConn == null) {
        System.out.println("Connection failed");
        return;
 }
 ...
 myPCon.disconnect();
 

If there are more parameters that an application wants to set up, the calling sequence can be:

 PsEPRServiceIdentity SI = new PsEPRServiceIdentity(serviceName, servicePassword);
 try {
     PsEPRConnection myPCon = new PsEPRConnection();
     myPCon.setIdentity(SI);
     myPCon.setInstance(instance);
     ...
     myPCon.connect();
 }
 catch (PsEPRException e) {
     // connection failed
 }
 ...
 myPCon.disconnect();
 

Once the connection is successfully created, leases and other actions are available.

 PayloadParser myPP = new myClassImplementingPayloadParser();
 EventReceiver myPR = new myClassImplementingEventReceiver();
 myPLease = myPCon.getLease(channelName, myPP, myQM);
 ...
 myPConn.releaseLease(myPLease);
 

Author:
Robert.Adams@intel.com

Field Summary
 java.lang.String connectionStateClosed
           
 java.lang.String connectionStateConnected
           
 java.lang.String connectionStateConnecting
           
 java.lang.String connectionStateDisconnected
           
 java.lang.String connectionStateInstantiated
           
 java.lang.String connectionStateReconnecting
           
 int connectionTimeout
           
 boolean isConnected
           
 int reconnectionRetries
           
 int reconnectionRetriesLeft
           
 
Constructor Summary
PsEPRConnection()
          Create a new, unconnected connection.
PsEPRConnection(PsEPRServiceIdentity servID)
          Create a connection by creating a random instance string.
PsEPRConnection(PsEPRServiceIdentity servID, java.lang.String instanceName)
          Create a connection with the basic parameters.
 
Method Summary
 void close()
          Disconnect the connection.
 void connect()
           
 void connectionClosed()
          method for ConnectionListener interface.
 void connectionClosedOnError(java.lang.Exception e)
          method for ConnectionListener interface.
 void forceReconnection()
          Force the connection to disconnect from the current server and try another.
 java.lang.String getConnectionState()
           
 PsEPRLease getLease(java.lang.String chan, PayloadParser xpp, EventReceiver xpr)
          Get a lease on a channel for a certain payload type.
 PsEPRLease getLease(java.lang.String chan, PayloadParser xpp, EventReceiver xpr, LeaseManager lm)
          Get a lease on a channel for a certain payload type.
 PsEPRLease getLease(java.lang.String chan, PayloadParser xpp, EventReceiver xpr, long xdur)
          Get a lease on a channel for a certain payload type.
 LeaseCollection getLeaseCollection()
          Return the collection of all of the outstanding leases.
 int getMessageExpiration()
          Get/set the Jabber message expiration time.
 int getReconnectionRetries()
          Get/set the number of maximum retries for this connection
 int getReconnectionRetriesLeft()
          Get/set the number of retries left on this connection.
 PsEPRRouter getRouter()
          Return the router structure for this connection.
 PsEPRServiceIdentity getServiceIdentity()
           
 java.lang.String getServiceInstance()
           
 void insertEvent(PsEPREvent ev)
          Insert and event into the receiption stream.
 void processPacket(org.jivesoftware.smack.packet.Packet pack)
          Method called when a packet is received.
 void releaseAllLeases()
          Release all leases.
 void releaseLease(PsEPRLease ls)
          Release the specified lease.
 void sendEvent(PsEPREvent ev)
          Send a constructed PsEPREvent to the network.
 void setMessageExpiration(int si)
           
 void setReconnectionRetries(int si)
           
 void setReconnectionRetriesLeft(int si)
           
 void setServiceIdentity(PsEPRServiceIdentity si)
           
 void setServiceInstance(java.lang.String si)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

isConnected

public boolean isConnected

reconnectionRetriesLeft

public int reconnectionRetriesLeft

reconnectionRetries

public int reconnectionRetries

connectionTimeout

public int connectionTimeout

connectionStateInstantiated

public final java.lang.String connectionStateInstantiated
See Also:
Constant Field Values

connectionStateConnecting

public final java.lang.String connectionStateConnecting
See Also:
Constant Field Values

connectionStateConnected

public final java.lang.String connectionStateConnected
See Also:
Constant Field Values

connectionStateReconnecting

public final java.lang.String connectionStateReconnecting
See Also:
Constant Field Values

connectionStateDisconnected

public final java.lang.String connectionStateDisconnected
See Also:
Constant Field Values

connectionStateClosed

public final java.lang.String connectionStateClosed
See Also:
Constant Field Values
Constructor Detail

PsEPRConnection

public PsEPRConnection()
Create a new, unconnected connection.


PsEPRConnection

public PsEPRConnection(PsEPRServiceIdentity servID)
Create a connection by creating a random instance string.

Parameters:
servID -

PsEPRConnection

public PsEPRConnection(PsEPRServiceIdentity servID,
                       java.lang.String instanceName)
Create a connection with the basic parameters.

Parameters:
servID -
instanceName -
Method Detail

getReconnectionRetries

public int getReconnectionRetries()
Get/set the number of maximum retries for this connection


setReconnectionRetries

public void setReconnectionRetries(int si)

getReconnectionRetriesLeft

public int getReconnectionRetriesLeft()
Get/set the number of retries left on this connection. Done when it reaches zero


setReconnectionRetriesLeft

public void setReconnectionRetriesLeft(int si)

getMessageExpiration

public int getMessageExpiration()
Get/set the Jabber message expiration time. Passed with an event when sent.


setMessageExpiration

public void setMessageExpiration(int si)

getServiceIdentity

public PsEPRServiceIdentity getServiceIdentity()

setServiceIdentity

public void setServiceIdentity(PsEPRServiceIdentity si)

getServiceInstance

public java.lang.String getServiceInstance()

setServiceInstance

public void setServiceInstance(java.lang.String si)

getConnectionState

public java.lang.String getConnectionState()

getLeaseCollection

public LeaseCollection getLeaseCollection()
Return the collection of all of the outstanding leases. These leases might not be active and the list is not locked.

Returns:
the collection of all the leases on this connection

getRouter

public PsEPRRouter getRouter()
Return the router structure for this connection.

Returns:
the router instance for this connection. May be null.

close

public void close()
Disconnect the connection.


connect

public void connect()

forceReconnection

public void forceReconnection()
Force the connection to disconnect from the current server and try another. This also causes all of the leases to be renegotiated. A successful reconnection should result with new router connections and leases and the leases should restart.


getLease

public PsEPRLease getLease(java.lang.String chan,
                           PayloadParser xpp,
                           EventReceiver xpr)
Get a lease on a channel for a certain payload type. Use the default LeaseManager with the default lease time.

Parameters:
chan - channel to listen to
xpp - payload parser. Specifies the namespace to listen for
xpr - event receiver for the events with payloads of the namspace
Returns:
lease control object

getLease

public PsEPRLease getLease(java.lang.String chan,
                           PayloadParser xpp,
                           EventReceiver xpr,
                           long xdur)
Get a lease on a channel for a certain payload type. Use the default SimpleLeaseManager with the specified lease intervel.

Parameters:
chan - channel to listen to
xpp - payload parser. Specifies the namespace to listen for
xpr - event receiver for the events with payloads of the namspace
xdur - duration of the lease in seconds
Returns:
lease control object

getLease

public PsEPRLease getLease(java.lang.String chan,
                           PayloadParser xpp,
                           EventReceiver xpr,
                           LeaseManager lm)
Get a lease on a channel for a certain payload type.

Parameters:
chan - channel to listen to
xpp - payload parser. Specifies the namespace to listen for
xpr - event receiver for the events with payloads of the namspace
lm - lease manager for handling the lease on the channel
Returns:
lease control object

releaseLease

public void releaseLease(PsEPRLease ls)
Release the specified lease. Once released, the EventReceiver will not receive any more events from the channel.

Parameters:
ls - lease to release.

releaseAllLeases

public void releaseAllLeases()
Release all leases. Once released, the EventReceivers will not receive any more events.


sendEvent

public void sendEvent(PsEPREvent ev)
Send a constructed PsEPREvent to the network. The Event object needs at least a destination channel and a payload.

Parameters:
ev - The contructed event object to send

insertEvent

public void insertEvent(PsEPREvent ev)
Insert and event into the receiption stream. This is done so some operations can be done with pseudo generated events (like lease timeout events).

Parameters:
ev - The event to insert

connectionClosed

public void connectionClosed()
method for ConnectionListener interface. This is called when the connection is closed. If we think we should be connected, we check the retry values. If we're done retrying, the connection is closed and left closed.

Specified by:
connectionClosed in interface org.jivesoftware.smack.ConnectionListener

connectionClosedOnError

public void connectionClosedOnError(java.lang.Exception e)
method for ConnectionListener interface.

Specified by:
connectionClosedOnError in interface org.jivesoftware.smack.ConnectionListener

processPacket

public void processPacket(org.jivesoftware.smack.packet.Packet pack)
Method called when a packet is received. We verify that it is a message packet, extract our event thing out of it.

Specified by:
processPacket in interface org.jivesoftware.smack.PacketListener