org.cometd.client
Class BayeuxClient

java.lang.Object
  extended by org.eclipse.jetty.util.component.AbstractLifeCycle
      extended by org.cometd.client.BayeuxClient
All Implemented Interfaces:
Client, org.eclipse.jetty.util.component.LifeCycle

public class BayeuxClient
extends org.eclipse.jetty.util.component.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.
 
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
 
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
org.eclipse.jetty.util.component.LifeCycle.Listener
 
Field Summary
protected  MessagePool _msgPool
           
 
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
_listeners, FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
 
Constructor Summary
BayeuxClient(org.eclipse.jetty.client.HttpClient client, org.eclipse.jetty.client.Address address, String uri)
           
BayeuxClient(org.eclipse.jetty.client.HttpClient client, org.eclipse.jetty.client.Address address, String path, Timer timer)
           
BayeuxClient(org.eclipse.jetty.client.HttpClient client, String url)
           
BayeuxClient(org.eclipse.jetty.client.HttpClient client, String url, Timer timer)
           
 
Method Summary
 void abort()
          Aborts the connection with the server without disconnecting.
 void addExtension(Extension ext)
           
 void addListener(ClientListener listener)
           
protected  void customize(org.eclipse.jetty.client.HttpExchange exchange)
          Customize an Exchange.
 void deliver(Client from, Message message)
          (non-Javadoc)
 void deliver(Client from, String toChannel, Object data, String id)
           
 void disconnect()
          Disconnect this client.
protected  void doStart()
           
protected  void doStop()
           
 void endBatch()
           
protected  void extendIn(Message message)
          Called to extend inbound messages
protected  void extendOut(Message message)
          Called to extend outbound messages
protected  String extendOut(String msg)
          Called to extend outbound string messages.
 int getBackoffIncrement()
           
 int getBackoffInterval()
           
 int getBackoffMaxInterval()
           
 int getBackoffMaxRetries()
          Deprecated.  
 javax.servlet.http.Cookie getCookie(String name)
           
 String getId()
           
 Listener getListener()
          Deprecated.  
 int getMaxQueue()
           
 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(Throwable e, Message[] messages)
          Called with the results of a failed publish
 void publish(String toChannel, Object data, String msgId)
           
 void remove()
          Deprecated. use disconnect()
 void removeExtension(Extension ext)
           
 void removeListener(ClientListener listener)
           
protected  boolean send(BayeuxClient.Exchange exchange, boolean backoff)
          Send the exchange, possibly using a backoff.
protected  void send(org.eclipse.jetty.client.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 value)
          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()
           
 void subscribe(String toChannel)
           
 List<Message> takeMessages()
           
 void unsubscribe(String toChannel)
           
 
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
 
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(org.eclipse.jetty.client.HttpClient client,
                    String url)

BayeuxClient

public BayeuxClient(org.eclipse.jetty.client.HttpClient client,
                    String url,
                    Timer timer)

BayeuxClient

public BayeuxClient(org.eclipse.jetty.client.HttpClient client,
                    org.eclipse.jetty.client.Address address,
                    String path,
                    Timer timer)

BayeuxClient

public BayeuxClient(org.eclipse.jetty.client.HttpClient client,
                    org.eclipse.jetty.client.Address address,
                    String uri)
Method Detail

addExtension

public void addExtension(Extension ext)
Specified by:
addExtension in interface Client

removeExtension

public void removeExtension(Extension ext)
Specified by:
removeExtension in interface Client

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 - the time to wait before retrying in milliseconds

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

Parameters:
retries - the max number of retries

getBackoffMaxRetries

public int getBackoffMaxRetries()
Deprecated. 

Returns:
the max number of retries

setBackoffIncrement

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

Parameters:
interval - the interval to increase the backoff, in milliseconds

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 String getId()
Specified by:
getId in interface Client

doStart

protected void doStart()
                throws Exception
Overrides:
doStart in class org.eclipse.jetty.util.component.AbstractLifeCycle
Throws:
Exception

doStop

protected void doStop()
               throws Exception
Overrides:
doStop in class org.eclipse.jetty.util.component.AbstractLifeCycle
Throws:
Exception

abort

public void abort()
Aborts the connection with the server without disconnecting.


isPolling

public boolean isPolling()

deliver

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

Parameters:
from - The client from which the message arrives or null if no such client can be identified
message - The message to deliver

deliver

public void deliver(Client from,
                    String toChannel,
                    Object data,
                    String id)
Specified by:
deliver in interface Client

getListener

public Listener getListener()
Deprecated. 

Returns:
The listener

hasMessages

public boolean hasMessages()
Specified by:
hasMessages in interface Client

isLocal

public boolean isLocal()
Specified by:
isLocal in interface Client

publish

public void publish(String toChannel,
                    Object data,
                    String msgId)

subscribe

public void subscribe(String toChannel)

unsubscribe

public void unsubscribe(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. 

Parameters:
listener - The listener

takeMessages

public List<Message> takeMessages()
Specified by:
takeMessages in interface Client

endBatch

public void endBatch()
Specified by:
endBatch in interface Client

startBatch

public void startBatch()
Specified by:
startBatch in interface Client

customize

protected void customize(org.eclipse.jetty.client.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

Parameters:
exchange - The exchange to customize

setCookie

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

getCookie

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

addListener

public void addListener(ClientListener listener)
Specified by:
addListener in interface Client

removeListener

public void removeListener(ClientListener listener)
Specified by:
removeListener in interface Client

getMaxQueue

public int getMaxQueue()
Specified by:
getMaxQueue in interface Client

getQueue

public Queue<Message> getQueue()
Specified by:
getQueue in interface Client

setMaxQueue

public void setMaxQueue(int max)
Specified by:
setMaxQueue in interface Client

send

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

Parameters:
exchange - The exchange to send
backoff - if true, use backoff algorithm to send
Returns:
whether the exchange has been sent or not

send

protected void send(org.eclipse.jetty.client.HttpExchange exchange)
             throws IOException
Send the exchange.

Parameters:
exchange - The exchange to send
Throws:
IOException - If the send fails

setInitialized

protected void setInitialized(boolean value)
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:
value - the value for initialized

isInitialized

protected boolean isInitialized()

metaConnect

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

Parameters:
success - Whether the connect response is successful
message - The connect response

metaHandshake

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

Parameters:
success - Whether the handshake response is successful
reestablish - Whether the client was previously connected
message - The handshake response

metaPublishFail

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

Parameters:
e - The exception that caused the failure
messages - The messages that could not be sent

extendOut

protected String extendOut(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 - The message to pass through extensions
Returns:
the extended message

extendOut

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

This method calls the Extensions added by addExtension(Extension)

Parameters:
message - The message to pass through the extensions

extendIn

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

This method calls the Extensions added by addExtension(Extension)

Parameters:
message - The message to pass through the extensions


Copyright © 2008-2010 Dojo Foundation. All Rights Reserved.