org.cometd.client
Class BayeuxClient

java.lang.Object
  extended by AbstractLifeCycle
      extended by org.cometd.client.BayeuxClient
All Implemented Interfaces:
Client
Direct Known Subclasses:
OortComet

public class BayeuxClient
extends AbstractLifeCycle
implements Client

Bayeux protocol Client.

Implements a Bayeux Ajax Push client as part of the cometd project.

The HttpClient attributes are used to share a Timer and MessagePool instance between all Bayeux clients sharing the same HttpClient.

Author:
gregw
See Also:
http://cometd.org

Nested Class Summary
protected  class BayeuxClient.Connect
          The Bayeux Connect exchange.
protected  class BayeuxClient.Exchange
          The base class for all bayeux exchanges.
protected  class BayeuxClient.Handshake
          The Bayeux handshake exchange.
protected  class BayeuxClient.Publish
          Publish message exchange.
 
Field Summary
protected  MessagePool _msgPool
           
 
Constructor Summary
BayeuxClient(HttpClient client, Address address, java.lang.String uri)
           
BayeuxClient(HttpClient client, Address address, java.lang.String path, java.util.Timer timer)
           
BayeuxClient(HttpClient client, java.lang.String url)
           
BayeuxClient(HttpClient client, java.lang.String url, java.util.Timer timer)
           
 
Method Summary
 void addExtension(Extension ext)
          Adds a bayeux client extension.
 void addListener(ClientListener listener)
          Adds a listener.
protected  void customize(HttpExchange exchange)
          Customize an Exchange.
 void deliver(Client from, Message message)
          (non-Javadoc)
 void deliver(Client from, java.lang.String toChannel, java.lang.Object data, java.lang.String id)
          Delivers a message to the remote client represented by this object.
 void disconnect()
          Disconnect this client.
protected  void doStart()
           
protected  void doStop()
           
 void endBatch()
          Ends a batch of messages.
protected  void extendIn(Message message)
          Called to extend inbound messages
protected  void extendOut(Message message)
          Called to extend outbound messages
protected  java.lang.String extendOut(java.lang.String msg)
          Called to extend outbound string messages.
 int getBackoffIncrement()
           
 int getBackoffInterval()
           
 int getBackoffMaxInterval()
           
 int getBackoffMaxRetries()
          Deprecated.  
 javax.servlet.http.Cookie getCookie(java.lang.String name)
           
 java.lang.String getId()
           
 Listener getListener()
          Deprecated.  
 int getMaxQueue()
           
 java.util.Queue<Message> getQueue()
           
 boolean hasMessages()
           
protected  boolean isInitialized()
           
 boolean isLocal()
           
 boolean isPolling()
           
protected  void metaConnect(boolean success, Message message)
          Called with the results of a /meta/connect message
protected  void metaHandshake(boolean success, boolean reestablish, Message message)
          Called with the results of a /meta/handshake message
protected  void metaPublishFail(java.lang.Throwable e, Message[] messages)
          Called with the results of a failed publish
 void publish(java.lang.String toChannel, java.lang.Object data, java.lang.String msgId)
           
 void remove()
          Deprecated. use disconnect()
 void removeExtension(Extension ext)
          Removes a bayeux client extension.
 void removeListener(ClientListener listener)
          Removes a listener
protected  boolean send(BayeuxClient.Exchange exchange, boolean backoff)
          Send the exchange, possibly using a backoff.
protected  void send(HttpExchange exchange)
          Send the exchange.
 void setBackoffIncrement(int interval)
          .
 void setBackOffInterval(int interval)
          If unable to connect/handshake etc, even if following the interval in the advice, wait for this interval initially, and try again.
 void setBackoffMaxInterval(int interval)
           
 void setBackoffMaxRetries(int retries)
          Deprecated. We retry an infinite number of times. use getBackoffIncrement() to set limits
 void setCookie(javax.servlet.http.Cookie cookie)
           
protected  void setInitialized(boolean b)
          False when we have received a success=false message in response to a Connect, or we have had an exception when sending or receiving a Connect.
 void setListener(Listener listener)
          Deprecated.  
 void setMaxQueue(int max)
           
 void startBatch()
          Starts a batch of messages.
 void subscribe(java.lang.String toChannel)
           
 java.util.List<Message> takeMessages()
          Takes any messages queued for a client.
 void unsubscribe(java.lang.String toChannel)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_msgPool

protected MessagePool _msgPool
Constructor Detail

BayeuxClient

public BayeuxClient(HttpClient client,
                    java.lang.String url)

BayeuxClient

public BayeuxClient(HttpClient client,
                    java.lang.String url,
                    java.util.Timer timer)

BayeuxClient

public BayeuxClient(HttpClient client,
                    Address address,
                    java.lang.String path,
                    java.util.Timer timer)

BayeuxClient

public BayeuxClient(HttpClient client,
                    Address address,
                    java.lang.String uri)
Method Detail

addExtension

public void addExtension(Extension ext)
Description copied from interface: Client
Adds a bayeux client extension. A bayeux client extension may examine a message or return a new message. A bayeux client extension should not modify a message as it may be sent to multile clients, instead it should clone the passed message.

Specified by:
addExtension in interface Client
Parameters:
ext - the extension to add
See Also:
Client.removeExtension(Extension)

removeExtension

public void removeExtension(Extension ext)
Description copied from interface: Client
Removes a bayeux client extension.

Specified by:
removeExtension in interface Client
Parameters:
ext - the extension to remove��
See Also:
Client.addExtension(Extension)

setBackOffInterval

public void setBackOffInterval(int interval)
If unable to connect/handshake etc, even if following the interval in the advice, wait for this interval initially, and try again.

Parameters:
interval -

getBackoffInterval

public int getBackoffInterval()
Returns:
the backoff interval to wait before retrying an unsuccessful or failed message

setBackoffMaxRetries

public void setBackoffMaxRetries(int retries)
Deprecated. We retry an infinite number of times. use getBackoffIncrement() to set limits


getBackoffMaxRetries

public int getBackoffMaxRetries()
Deprecated. 


setBackoffIncrement

public void setBackoffIncrement(int interval)
. Each retry will increment by this intervel, until we reach _backoffMaxInterval


getBackoffIncrement

public int getBackoffIncrement()
Returns:
the backoff interval used to increase the backoff time when retrying an unsuccessful or failed message.

setBackoffMaxInterval

public void setBackoffMaxInterval(int interval)

getBackoffMaxInterval

public int getBackoffMaxInterval()

getId

public java.lang.String getId()
Specified by:
getId in interface Client
Returns:
the unique ID representing this client

doStart

protected void doStart()
                throws java.lang.Exception
Throws:
java.lang.Exception

doStop

protected void doStop()
               throws java.lang.Exception
Throws:
java.lang.Exception

isPolling

public boolean isPolling()

deliver

public void deliver(Client from,
                    Message message)
(non-Javadoc)


deliver

public void deliver(Client from,
                    java.lang.String toChannel,
                    java.lang.Object data,
                    java.lang.String id)
Description copied from interface: Client
Delivers a message to the remote client represented by this object.

Specified by:
deliver in interface Client
Parameters:
from - the Client that sends the message
toChannel - the channel onto which the message is sent
data - the data of the message
id - the message ID

getListener

public Listener getListener()
Deprecated. 


hasMessages

public boolean hasMessages()
Specified by:
hasMessages in interface Client
Returns:
whether or not this client has messages to send

isLocal

public boolean isLocal()
Specified by:
isLocal in interface Client
Returns:
true if the client is local, false if this client is either a remote HTTP client or a java client to a remote server.

publish

public void publish(java.lang.String toChannel,
                    java.lang.Object data,
                    java.lang.String msgId)

subscribe

public void subscribe(java.lang.String toChannel)

unsubscribe

public void unsubscribe(java.lang.String toChannel)

remove

public void remove()
Deprecated. use disconnect()

Disconnect this client.


disconnect

public void disconnect()
Disconnect this client.

Specified by:
disconnect in interface Client

setListener

public void setListener(Listener listener)
Deprecated. 


takeMessages

public java.util.List<Message> takeMessages()
Description copied from interface: Client
Takes any messages queued for a client.

Specified by:
takeMessages in interface Client

endBatch

public void endBatch()
Description copied from interface: Client
Ends a batch of messages. Messages will not be delivered that have been queued since the previous Client.startBatch() is called. Batches may be nested and messages are only sent once all batches are ended.

Specified by:
endBatch in interface Client
See Also:
Client.startBatch()

startBatch

public void startBatch()
Description copied from interface: Client
Starts a batch of messages. Messages will not be delivered remotely until the corresponding Client.endBatch() is called. Batches may be nested and messages are only sent once all batches are ended.

Specified by:
startBatch in interface Client
See Also:
Client.endBatch()

customize

protected void customize(HttpExchange exchange)
Customize an Exchange. Called when an exchange is about to be sent to allow Cookies and Credentials to be customized. Default implementation sets any cookies


setCookie

public void setCookie(javax.servlet.http.Cookie cookie)

getCookie

public javax.servlet.http.Cookie getCookie(java.lang.String name)

addListener

public void addListener(ClientListener listener)
Description copied from interface: Client
Adds a listener.

Specified by:
addListener in interface Client
Parameters:
listener - the listener to add
See Also:
Client.removeListener(ClientListener)

removeListener

public void removeListener(ClientListener listener)
Description copied from interface: Client
Removes a listener

Specified by:
removeListener in interface Client
Parameters:
listener - the listener to remove
See Also:
Client.addListener(ClientListener)

getMaxQueue

public int getMaxQueue()
Specified by:
getMaxQueue in interface Client
Returns:
the max queue size
See Also:
Client.setMaxQueue(int)

getQueue

public java.util.Queue<Message> getQueue()
Specified by:
getQueue in interface Client
Returns:
the message queue (its usage must synchronize on this Client instance).

setMaxQueue

public void setMaxQueue(int max)
Specified by:
setMaxQueue in interface Client
Parameters:
max - The size which if a client queue exceeds, forces a call to QueueListener.queueMaxed(Client, Client, Message) to check if the message should be added. If set to -1, there is no queue limit. If set to zero, messages are not queued.
See Also:
Client.getMaxQueue()

send

protected boolean send(BayeuxClient.Exchange exchange,
                       boolean backoff)
Send the exchange, possibly using a backoff.

Parameters:
exchange -
backoff - if true, use backoff algorithm to send
Returns:

send

protected void send(HttpExchange exchange)
             throws java.io.IOException
Send the exchange.

Parameters:
exchange -
Throws:
java.io.IOException

setInitialized

protected void setInitialized(boolean b)
False when we have received a success=false message in response to a Connect, or we have had an exception when sending or receiving a Connect. True when handshake and then connect has happened.

Parameters:
b -

isInitialized

protected boolean isInitialized()

metaConnect

protected void metaConnect(boolean success,
                           Message message)
Called with the results of a /meta/connect message

Parameters:
success - connect was returned with this status

metaHandshake

protected void metaHandshake(boolean success,
                             boolean reestablish,
                             Message message)
Called with the results of a /meta/handshake message

Parameters:
success - connect was returned with this status
reestablish - the client was previously connected.

metaPublishFail

protected void metaPublishFail(java.lang.Throwable e,
                               Message[] messages)
Called with the results of a failed publish


extendOut

protected java.lang.String extendOut(java.lang.String msg)
Called to extend outbound string messages. Some messages are sent as preformatted JSON strings (eg handshake and connect messages). This extendOut method is a variation of the extendOut(Message) method to efficiently cater for these preformatted strings.

This method calls the Extensions added by addExtension(Extension)

Parameters:
msg -
Returns:
the extended message

extendOut

protected void extendOut(Message message)
Called to extend outbound messages

This method calls the Extensions added by addExtension(Extension)


extendIn

protected void extendIn(Message message)
Called to extend inbound messages

This method calls the Extensions added by addExtension(Extension)



Copyright © 2011 Dojo Foundation. All Rights Reserved.