Public Member Functions | Protected Member Functions

ExampleNetworkThread Class Reference

Network thread of example plugin. More...

#include <plugins/examples/basics/net_thread.h>

Inheritance diagram for ExampleNetworkThread:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 ExampleNetworkThread (const char *name)
 Constructor.
virtual ~ExampleNetworkThread ()
 Destructor.
virtual void init ()
 Initialize thread.
virtual void finalize ()
 Finalize the thread.
virtual void loop ()
 Thread loop.
virtual void handle_network_message (fawkes::FawkesNetworkMessage *msg)
 Handle network message.
virtual void client_connected (unsigned int clid)
 Client connected.
virtual void client_disconnected (unsigned int clid)
 Client disconnected.

Protected Member Functions

virtual void run ()
 Stub to see name in backtrace for easier debugging.

Detailed Description

Network thread of example plugin.

Author:
Tim Niemueller

Definition at line 31 of file net_thread.h.


Constructor & Destructor Documentation

ExampleNetworkThread::ExampleNetworkThread ( const char *  name  ) 

Constructor.

Parameters:
name thread name

Definition at line 39 of file net_thread.cpp.

ExampleNetworkThread::~ExampleNetworkThread (  )  [virtual]

Destructor.

Definition at line 47 of file net_thread.cpp.


Member Function Documentation

void ExampleNetworkThread::client_connected ( unsigned int  clid  )  [virtual]

Client connected.

Ignored.

Parameters:
clid client ID

Implements fawkes::FawkesNetworkHandler.

Definition at line 121 of file net_thread.cpp.

References fawkes::Logger::log_info(), and fawkes::LoggingAspect::logger.

void ExampleNetworkThread::client_disconnected ( unsigned int  clid  )  [virtual]

Client disconnected.

If the client was a subscriber it is removed.

Parameters:
clid client ID

Implements fawkes::FawkesNetworkHandler.

Definition at line 132 of file net_thread.cpp.

References fawkes::Logger::log_info(), and fawkes::LoggingAspect::logger.

void ExampleNetworkThread::finalize (  )  [virtual]

Finalize the thread.

This method is executed just before the thread is canceled and destroyed. It is always preceeded by a call to prepare_finalize(). If this is not the case this is a failure. The condition can be checked with the boolean variable finalize_prepared.

This method is meant to be used in conjunction with aspects and to cover thread inter-dependencies. This routine MUST bring the thread into a safe state such that it may be canceled and destroyed afterwards. If there is any reason that this cannot happen make your prepare_finalize() reports so.

This method is called by the thread manager just before the thread is being cancelled. Here you can do whatever steps are necessary just before the thread is cancelled. Note that you thread is still running and might be in the middle of a loop, so it is not a good place to give up on all resources used. Mind segmentation faults that could happen. Protect the area with a mutex that you lock at the beginning of your loop and free in the end, and that you lock at the beginning of finalize and then never unlock. Also not that the finalization may be canceled afterwards. The next thing that happens is that either the thread is canceled and destroyed or that the finalization is canceled and the thread has to run again.

Finalize is called on a thread just before it is deleted. It is guaranteed to be called on a fully initialized thread (if no exception is thrown in init()) (this guarantee holds in the Fawkes framework).

The default implementation does nothing besides throwing an exception if prepare_finalize() has not been called.

Exceptions:
Exception thrown if prepare_finalize() has not been called.
See also:
prepare_finalize()
cancel_finalize()

Reimplemented from fawkes::Thread.

Definition at line 68 of file net_thread.cpp.

References fawkes::FawkesNetworkAspect::fnethub, fawkes::Logger::log_info(), fawkes::LoggingAspect::logger, and fawkes::FawkesNetworkHub::remove_handler().

void ExampleNetworkThread::handle_network_message ( fawkes::FawkesNetworkMessage msg  )  [virtual]
void ExampleNetworkThread::init (  )  [virtual]

Initialize thread.

This method is called just after all aspects have been initialized but before the thread is run. Here we add this thread as a handler to the Fawkes network hub. This cannot happen in the constructor as fnethandler has not been initialized at that time.

See also:
Thread::init()
Fawkes Thread Aspects

Reimplemented from fawkes::Thread.

Definition at line 61 of file net_thread.cpp.

References fawkes::FawkesNetworkHub::add_handler(), and fawkes::FawkesNetworkAspect::fnethub.

void ExampleNetworkThread::loop (  )  [virtual]

Thread loop.

Nothing to do here since nobody will every wake us up (we do not have the BlockedTimingAspect nor does any other thread wake us up). This is ok because everything is done in the network handler call.

Note that in general incoming messages should be parsed and appropriate actions enqueued. Then in the next loop iteration you process these incoming messages. This is the best way to avoid strange behavior and low latencies in network message handling.

As an example for this see the FawkesConfigManager.

See also:
FawkesConfigManager

Reimplemented from fawkes::Thread.

Definition at line 90 of file net_thread.cpp.

virtual void ExampleNetworkThread::run (  )  [inline, protected, virtual]

Stub to see name in backtrace for easier debugging.

See also:
Thread::run()

Reimplemented from fawkes::Thread.

Definition at line 52 of file net_thread.h.


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