dht::client Class Reference

Interface for DHT (Distributed Hash Table) implementations. More...

#include <dht/client.h>

Inheritance diagram for dht::client:

dht::kadc::client List of all members.

Public Types

enum  { disconnected = 1, connecting, connected, disconnecting }

Public Member Functions

virtual ~client ()
 Destructor.
virtual int in_state () const
 Returns the state of client (disconnected, connecting etc.).
virtual const char * in_state_str () const
 Returns the current state of client as a string.
virtual const char * state_str (int state) const
 Returns the client state as a string.
virtual void init (const dht::name_value_map &opts)=0
 Optional initialization for the implementation.
virtual void observer_attach (dht::event_observer *h, int event_mask)
 Adds observer.
virtual void observer_remove (dht::event_observer *h)
 Removes observer.
virtual void observer_remove (dht::event_observer *h, int event_mask)
 Removes observer.
virtual void connect (dht::notify_handler *handler=NULL)=0
 Connects client to DHT.
virtual void disconnect (dht::notify_handler *handler=NULL)=0
 Disconnects client from DHT.
virtual void find (const dht::key &fkey, dht::search_handler *handler)=0
 Searches the DHT for value matching the given key.
virtual void store (const dht::key &skey, const dht::value &svalue, dht::notify_handler *handler=NULL)=0
 Stores a value to DHT.
virtual const addr_inet_type & external_addr ()=0
 Gets client's external address.
virtual int process (time_value_type &max_wait)=0
 Does DHT specific processing.
virtual int process (time_value_type *max_wait=NULL)=0
 Does DHT specific processing.
virtual reactor_type * reactor ()=0
 Returns the reactor which is used for event demultiplexing.
virtual void reactor (reactor_type *reactor)=0
 Sets the reactor which is used for event demultiplexing.
virtual int handler_cancel (dht::notify_handler *handler)=0
 cancels a handler

Protected Member Functions

event_observer_notifier * observer_notifier ()

Detailed Description

Interface for DHT (Distributed Hash Table) implementations.

The interface does not need to be thread safe. If the functions in the class are called from several threads the application must manage the synchronization so that several functions are not called at once. Likewise, if the implementation of the interface internally uses threads it must make sure all handlers are called only from the thread that called process() function.

The most convenient way to obtain results is by adding observers for different events, such as change of state (from connecting to connected), search results etc. Several of the functions can also receive pointer to handler that will be called when the operation is finished. The handler will only be called while being in process() and from the same thread that called process(). This rule must be obeyed by implementations even in the case when the call's success/failure status could be directly determined when calling the function.

Generally all heavy weight processing must happen in process() functions. The other functions, such as connect(), find(), store() and disconnect() should delay processing that requires waiting, for example network operations to finish, to when user calls one of the process() functions. The calling application should for the most time be calling process() functions and react when observers or handler objects are notified of new events.

The functions are allowed to throw exceptions in case of errors. Exceptions should be reserved for serious usage/operational errors, such as calling store() without being connected, or running out of memory. If the function that received a handler throws exception, the handler's success/failure function will not be called. In other error events, such as timeout of connecting, the specified handler if any should be called with failure status.

In general the typical usage sequence of the interface is the following:


Constructor & Destructor Documentation

virtual dht::client::~client (  )  [virtual]

Destructor.

When destructor called the implementation should clean up as quickly as possible. If any handlers are pending they do not need to be called. For clean shutdown disconnect() should be called first (if connect() has been called)

Reimplemented in dht::kadc::client.


Member Function Documentation

virtual int dht::client::in_state (  )  const [virtual]

Returns the state of client (disconnected, connecting etc.).

virtual const char* dht::client::in_state_str (  )  const [virtual]

Returns the current state of client as a string.

virtual const char* dht::client::state_str ( int  state  )  const [virtual]

Returns the client state as a string.

Parameters:
state the state
See also:
in_state()

virtual void dht::client::init ( const dht::name_value_map opts  )  [pure virtual]

Optional initialization for the implementation.

Parameters:
opts name/value pairs of options, implementation specific
Should be called once before any other functions if separate initialization needed by the implementation

Implemented in dht::kadc::client.

virtual void dht::client::observer_attach ( dht::event_observer h,
int  event_mask 
) [virtual]

Adds observer.

Parameters:
h observer
event_mask events that will cause observer to be notified
Can be used to add observers for specified events. See event_observer class for allowed event masks.

virtual void dht::client::observer_remove ( dht::event_observer h  )  [virtual]

Removes observer.

Parameters:
h the observer to remove
Removes the specified observer

virtual void dht::client::observer_remove ( dht::event_observer h,
int  event_mask 
) [virtual]

Removes observer.

Parameters:
h the observer to remove
event_mask mask of events from which to remove
Removes the specified observer with specified event mask

virtual void dht::client::connect ( dht::notify_handler handler = NULL  )  [pure virtual]

Connects client to DHT.

Parameters:
handler Optional handler to call when connect finished/failed.
Function returns immediately, success or failure is notified separately once in process().

Instead of using the handler to get notified of connection, it is often better to add an observer for state changes to get notified when in connected state

Implemented in dht::kadc::client.

virtual void dht::client::disconnect ( dht::notify_handler handler = NULL  )  [pure virtual]

Disconnects client from DHT.

Parameters:
handler The handler to call when disconnect finished/failed.
Function returns immediately, success or failure is notified separately once in process().

Instead of using the handler to get notified of disconnect, it is often better to add an observer for state changes to get notified when in disconnected state

Implemented in dht::kadc::client.

virtual void dht::client::find ( const dht::key fkey,
dht::search_handler handler 
) [pure virtual]

Searches the DHT for value matching the given key.

Parameters:
fkey The key to search
handler The handler which is notified when a value is found and also finally when search is finished. If the same key/value pair is found many times from the DHT, the handler is allowed to be called more than once for the same key/value pair.
See also:
search_handler

Implemented in dht::kadc::client.

virtual void dht::client::store ( const dht::key skey,
const dht::value svalue,
dht::notify_handler handler = NULL 
) [pure virtual]

Stores a value to DHT.

Parameters:
skey The key that can be used to find the value
svalue The value to store into DHT
handler The handler which is notified when storing the value is done. A successfull store does not have to mean the value can be immediately retrieved from the DHT using find().
See also:
search_handler

Implemented in dht::kadc::client.

virtual const addr_inet_type& dht::client::external_addr (  )  [pure virtual]

Gets client's external address.

Returns the IP address of the client as seen by other participants of the network. If the address can not be detected, should point to INADDR_NONE address. Calling the function must be safe when connected, is allowed to throw an exception in other states.

Implemented in dht::kadc::client.

virtual int dht::client::process ( time_value_type &  max_wait  )  [pure virtual]

Does DHT specific processing.

Parameters:
max_wait Maximum time to wait
The function does DHT specific processing only for maximum of time specified with max_wait. It must return if an event that triggers calling a handler or observer happens. It is allowed to call several handlers at one go if several events happen at the same time. It is also allowed to return earlier even if no events that cause calling of a handler happens. Consider max_wait as a hint how long the application is willing to wait.

The max_wait value is decremented to reflect how much time this call took. For instance, if a time value of 3 seconds is passed to process and an event occurs after 2 seconds, max_wait will equal 1 second. This can be used if an application wishes to handle events for some fixed amount of time.

See also:
reactor()

Implemented in dht::kadc::client.

virtual int dht::client::process ( time_value_type *  max_wait = NULL  )  [pure virtual]

Does DHT specific processing.

Parameters:
max_wait Maximum time to wait or NULL if indefinite.
The function does DHT specific processing only for maximum of time specified with max_wait. It must return if an event that triggers calling a handler or observer happens. It is allowed to call several handlers at one go if several events happen at the same time. It is also allowed to return earlier even if no events that cause calling of a handler happens. Consider max_wait as a hint how long the application is willing to wait.

The max_wait value is decremented to reflect how much time this call took. For instance, if a time value of 3 seconds is passed to process and an event occurs after 2 seconds, max_wait will equal 1 second. This can be used if an application wishes to handle events for some fixed amount of time.

See also:
reactor()

Implemented in dht::kadc::client.

virtual reactor_type* dht::client::reactor (  )  [pure virtual]

Returns the reactor which is used for event demultiplexing.

Besides calling process(), where implementations usually should use internally (ACE's) reactor for its processing needs, the application can directly call reactor. The DHT implementation registers the events it looks for to the reactor and thus will be notified accordingly if the application is running in an reactor loop. Calling directly the reactor is an alternative to using process() and usually the recommended way if the application needs to look for other I/O events besides the DHT. See ACE documentation for information on reactor usage.

By default implementations should use ACE_Reactor::instance() as their reactor unless otherwise set by the application.

Implemented in dht::kadc::client.

virtual void dht::client::reactor ( reactor_type *  reactor  )  [pure virtual]

Sets the reactor which is used for event demultiplexing.

Parameters:
reactor The reactor DHT implementation should use.
Besides calling process(), which usually should use internally (ACE's) reactor for its processing needs, the application can directly call reactor. The DHT implementation registers the events it looks for to the reactor and thus will be notified accordingly if the application is running in an reactor loop. Calling directly the reactor is an alternative to using process() and usually the recommended way if the application needs to look for other I/O events besides the DHT. See ACE documentation for information on reactor usage.

By default implementations should use ACE_Reactor::instance() as their reactor unless otherwise set by the application.

Implemented in dht::kadc::client.

virtual int dht::client::handler_cancel ( dht::notify_handler handler  )  [pure virtual]

cancels a handler

Parameters:
handler the handler to cancel
If a handler has been registered for calling, for example in connect(), disconnect() or find(), then it can be cancelled by calling this function. If called the specified handler won't be called.

Implemented in dht::kadc::client.


The documentation for this class was generated from the following file:
Generated on Thu Mar 1 16:18:47 2007 for libdht by  doxygen 1.5.1