Fawkes API  Fawkes Development Version
fawkes::FawkesNetworkClient Class Reference

Simple Fawkes network client. More...

#include <netcomm/fawkes/client.h>

Public Member Functions

 FawkesNetworkClient ()
 Constructor. More...
 
 FawkesNetworkClient (const char *host, unsigned short int port)
 Constructor. More...
 
 FawkesNetworkClient (unsigned int id, const char *host, unsigned short int port)
 Constructor. More...
 
 ~FawkesNetworkClient ()
 Destructor. More...
 
void connect ()
 Connect to remote. More...
 
void disconnect ()
 Disconnect socket. More...
 
void connect (const char *host, unsigned short int port)
 Connect to new ip and port, and set hostname. More...
 
void connect (const char *hostname, const struct sockaddr *addr, socklen_t addrlen)
 Connect to specific endpoint. More...
 
void connect (const char *hostname, const struct sockaddr_storage &addr)
 Connect to specific endpoint. More...
 
void enqueue (FawkesNetworkMessage *message)
 Enqueue message to send. More...
 
void enqueue_and_wait (FawkesNetworkMessage *message, unsigned int timeout_sec=15)
 Enqueue message to send and wait for answer. More...
 
void wait (unsigned int component_id, unsigned int timeout_sec=15)
 Wait for messages for component ID. More...
 
void wake (unsigned int component_id)
 Wake a waiting thread. More...
 
void interrupt_connect ()
 Interrupt connect(). More...
 
void register_handler (FawkesNetworkClientHandler *handler, unsigned int component_id)
 Register handler. More...
 
void deregister_handler (unsigned int component_id)
 Deregister handler. More...
 
bool connected () const throw ()
 Check if connection is alive. More...
 
bool has_id () const
 Check whether the client has an id. More...
 
unsigned int id () const
 Get the client's ID. More...
 
const char * get_hostname () const
 Get the client's hostname. More...
 

Detailed Description

Simple Fawkes network client.

Allows access to a remote instance via the network. Encapsulates all needed interaction with the network.

Author
Tim Niemueller

Definition at line 51 of file client.h.

Constructor & Destructor Documentation

◆ FawkesNetworkClient() [1/3]

fawkes::FawkesNetworkClient::FawkesNetworkClient ( )

Constructor.

Note, you cannot call the connect() without parameters the first time you establish an connection when using this ctor!

Definition at line 340 of file client.cpp.

◆ FawkesNetworkClient() [2/3]

fawkes::FawkesNetworkClient::FawkesNetworkClient ( const char *  host,
unsigned short int  port 
)

Constructor.

Parameters
hostremote host to connect to.
portport to connect to.

Definition at line 308 of file client.cpp.

◆ FawkesNetworkClient() [3/3]

fawkes::FawkesNetworkClient::FawkesNetworkClient ( unsigned int  id,
const char *  host,
unsigned short int  port 
)

Constructor.

Parameters
idid of the client.
hostremote host to connect to.
portport to connect to.

Definition at line 373 of file client.cpp.

References id().

◆ ~FawkesNetworkClient()

fawkes::FawkesNetworkClient::~FawkesNetworkClient ( )

Destructor.

Definition at line 402 of file client.cpp.

References disconnect().

Member Function Documentation

◆ connect() [1/4]

◆ connect() [2/4]

void fawkes::FawkesNetworkClient::connect ( const char *  host,
unsigned short int  port 
)

Connect to new ip and port, and set hostname.

Parameters
hostremote host name
portnew port to connect to
See also
connect() Look there for more documentation and notes about possible exceptions.

Definition at line 491 of file client.cpp.

References connect().

◆ connect() [3/4]

void fawkes::FawkesNetworkClient::connect ( const char *  hostname,
const struct sockaddr *  addr,
socklen_t  addr_len 
)

Connect to specific endpoint.

Parameters
hostnamehostname, informational only and not used for connecting
addrsockaddr structure of specific endpoint to connect to
addr_lenlength of addr

Definition at line 506 of file client.cpp.

References connect().

◆ connect() [4/4]

void fawkes::FawkesNetworkClient::connect ( const char *  hostname,
const struct sockaddr_storage &  addr 
)

Connect to specific endpoint.

Parameters
hostnamehostname, informational only and not used for connecting
addrsockaddr_storage structure of specific endpoint to connect to

Definition at line 524 of file client.cpp.

References connect().

◆ connected()

◆ deregister_handler()

◆ disconnect()

◆ enqueue()

void fawkes::FawkesNetworkClient::enqueue ( FawkesNetworkMessage message)

Enqueue message to send.

This method takes ownership of the message. If you want to use the message after enqueing you must reference:

message->ref();
fawkes_network_client->enqueue(message);
// message can now still be used

Without extra referencing the message may not be used after enqueuing.

Parameters
messagemessage to send

Definition at line 596 of file client.cpp.

References fawkes::FawkesNetworkClientSendThread::enqueue().

Referenced by fawkes::RemoteBlackBoard::close(), fawkes::RemoteBlackBoard::list(), fawkes::RemoteBlackBoard::list_all(), fawkes::BlackBoardInterfaceProxy::notify_of_data_change(), fawkes::LogView::set_client(), fawkes::NetworkConfiguration::set_mirror_mode(), fawkes::BlackBoardInterfaceProxy::transmit(), fawkes::LogView::~LogView(), and fawkes::PluginTreeView::~PluginTreeView().

◆ enqueue_and_wait()

void fawkes::FawkesNetworkClient::enqueue_and_wait ( FawkesNetworkMessage message,
unsigned int  timeout_sec = 15 
)

Enqueue message to send and wait for answer.

It is guaranteed that an answer cannot be missed. However, if the component sends another message (which is not the answer to the query) this will also trigger the wait condition to be woken up. The component ID to wait for is taken from the message. This message also calls unref() on the message. If you want to use it after enqueuing make sure you ref() before calling this method.

Parameters
messagemessage to send
timeout_sectimeout for the waiting operation in seconds, 0 to wait forever (warning, this may result in a deadlock!)

Definition at line 614 of file client.cpp.

References fawkes::FawkesNetworkMessage::cid(), fawkes::FawkesNetworkClientSendThread::enqueue(), fawkes::Mutex::lock(), fawkes::FawkesNetworkMessage::msgid(), fawkes::WaitCondition::reltimed_wait(), and fawkes::Mutex::unlock().

Referenced by fawkes::NetworkConfiguration::get_value().

◆ get_hostname()

const char * fawkes::FawkesNetworkClient::get_hostname ( ) const

Get the client's hostname.

Returns
hostname or NULL

Definition at line 859 of file client.cpp.

◆ has_id()

bool fawkes::FawkesNetworkClient::has_id ( ) const

Check whether the client has an id.

Returns
true if client has an ID

Definition at line 837 of file client.cpp.

◆ id()

unsigned int fawkes::FawkesNetworkClient::id ( ) const

Get the client's ID.

Returns
the ID

Definition at line 846 of file client.cpp.

Referenced by FawkesNetworkClient().

◆ interrupt_connect()

void fawkes::FawkesNetworkClient::interrupt_connect ( )

Interrupt connect().

This is for example handy to interrupt in connection_died() before a connection_established() event has been received.

Definition at line 576 of file client.cpp.

References fawkes::Mutex::lock(), fawkes::Mutex::unlock(), and fawkes::WaitCondition::wake_all().

◆ register_handler()

void fawkes::FawkesNetworkClient::register_handler ( FawkesNetworkClientHandler handler,
unsigned int  component_id 
)

Register handler.

Handlers are used to handle incoming packets. There may only be one handler per component! Cannot be called while processing a message.

Parameters
handlerhandler to register
component_idcomponent ID to register the handler for.

Definition at line 658 of file client.cpp.

References fawkes::LockMap< KeyType, ValueType, LessKey >::lock(), and fawkes::LockMap< KeyType, ValueType, LessKey >::unlock().

Referenced by fawkes::ConnectionDispatcher::ConnectionDispatcher(), fawkes::NetworkConfiguration::NetworkConfiguration(), fawkes::RemoteBlackBoard::RemoteBlackBoard(), fawkes::ConnectionDispatcher::set_cid(), and fawkes::ConnectionDispatcher::set_client().

◆ wait()

void fawkes::FawkesNetworkClient::wait ( unsigned int  component_id,
unsigned int  timeout_sec = 15 
)

Wait for messages for component ID.

This will wait for messages of the given component ID to arrive. The calling thread is blocked until messages are available.

Parameters
component_idcomponent ID to monitor
timeout_sectimeout for the waiting operation in seconds, 0 to wait forever (warning, this may result in a deadlock!)

Definition at line 785 of file client.cpp.

References fawkes::Mutex::lock(), fawkes::WaitCondition::reltimed_wait(), and fawkes::Mutex::unlock().

◆ wake()

void fawkes::FawkesNetworkClient::wake ( unsigned int  component_id)

Wake a waiting thread.

This will wakeup all threads currently waiting for the specified component ID. This can be helpful to wake a sleeping thread if you received a signal.

Parameters
component_idcomponent ID for threads to wake up

Definition at line 814 of file client.cpp.

References fawkes::Mutex::lock(), fawkes::Mutex::unlock(), and fawkes::WaitCondition::wake_all().


The documentation for this class was generated from the following files: