Network thread of example plugin. More...
#include <plugins/examples/basics/net_thread.h>
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. |
Network thread of example plugin.
Definition at line 31 of file net_thread.h.
ExampleNetworkThread::ExampleNetworkThread | ( | const char * | name | ) |
ExampleNetworkThread::~ExampleNetworkThread | ( | ) | [virtual] |
Destructor.
Definition at line 47 of file net_thread.cpp.
void ExampleNetworkThread::client_connected | ( | unsigned int | clid | ) | [virtual] |
Client connected.
Ignored.
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.
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.
Exception | thrown if prepare_finalize() has not been called. |
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] |
Handle network message.
The message is put into the inbound queue and processed in processAfterLoop().
msg | message |
Implements fawkes::FawkesNetworkHandler.
Definition at line 100 of file net_thread.cpp.
References fawkes::FawkesNetworkMessage::clid(), fawkes::FawkesNetworkAspect::fnethub, fawkes::Logger::log_error(), fawkes::Logger::log_info(), fawkes::LoggingAspect::logger, fawkes::FawkesNetworkMessage::msgid(), fawkes::FawkesNetworkMessage::payload(), fawkes::FawkesNetworkMessage::payload_size(), and fawkes::FawkesNetworkHub::send().
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.
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.
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.
Reimplemented from fawkes::Thread.
Definition at line 52 of file net_thread.h.