Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Friends | List of all members
ola::io::SelectServer Class Reference

Detailed Description

A single threaded I/O event management system.

SelectServer is the core of the event driven system. It's responsible for invoking Callbacks when certain events occur.

Example
The following snippet shows how a SelectServer can be used with a listening UDP socket. The ReceiveMessage function will be called whenever there is new data on the UDP socket.
#include <ola/Logging.h>
#include <ola/base/Array.h>
#include <ola/io/SelectServer.h>
#include <ola/network/Socket.h>
// The port to listen on.
static const unsigned int PORT = 12345;
// Called when a UDP datagram arrives.
void ReceiveMessage(UDPSocket *socket) {
uint8_t data[1500];
ssize_t data_size = arraysize(data);
if (socket->RecvFrom(data, &data_size, &client)) {
OLA_INFO << "Received " << data_size << " bytes from " << client;
} else {
OLA_WARN << "Recv failure";
}
}
int main() {
IPV4SocketAddress listen_address(IPV4Address::WildCard(), PORT);
UDPSocket udp_socket;
udp_socket.Init();
udp_socket.Bind(listen_address);
udp_socket.SetOnData(ola::NewCallback(&ReceiveMessage, &udp_socket));
ss.AddReadDescriptor(&udp_socket);
ss.Run();
}

The SelectServer has a number of different implementations depending on the platform. On systems with epoll, the flag –use-epoll will use epoll() rather than select(). The PollerInterface defines the contract between the SelectServer and the lower level, platform dependant Poller classes.

All methods except Execute() and Terminate() must be called from the thread that Run() was called in.

Inheritance diagram for ola::io::SelectServer:
ola::io::SelectServerInterface ola::thread::SchedulingExecutorInterface ola::thread::ExecutorInterface ola::thread::SchedulerInterface

Public Member Functions

 SelectServer (ola::ExportMap *export_map=NULL, Clock *clock=NULL)
 Create a new SelectServer.
 ~SelectServer ()
 Clean up.
bool IsRunning () const
 Checks if the SelectServer is running.
const TimeStampWakeUpTime () const
 The time when this SelectServer was woken up.
void Terminate ()
 Exit from the Run() loop.
void SetDefaultInterval (const TimeInterval &block_interval)
 Set the duration to block for.
void Run ()
 Enter the event loop.
void RunOnce ()
 Do a single pass through the event loop. Does not block.
void RunOnce (const TimeInterval &block_interval)
 Do a single pass through the event loop.
bool AddReadDescriptor (ReadFileDescriptor *descriptor)
 Register a ReadFileDescriptor for read-events.
bool AddReadDescriptor (ConnectedDescriptor *descriptor, bool delete_on_close=false)
 Register a ConnectedDescriptor for read-events.
void RemoveReadDescriptor (ReadFileDescriptor *descriptor)
 Remove a RemoveReadDescriptor for read-events.
void RemoveReadDescriptor (ConnectedDescriptor *descriptor)
 Remove a ConnectedDescriptor for read-events.
bool AddWriteDescriptor (WriteFileDescriptor *descriptor)
 Register a WriteFileDescriptor for write-events.
void RemoveWriteDescriptor (WriteFileDescriptor *descriptor)
 Remove a WriteFileDescriptor for write-events.
ola::thread::timeout_id RegisterRepeatingTimeout (unsigned int ms, ola::Callback0< bool > *callback)
 Execute a callback periodically.
ola::thread::timeout_id RegisterRepeatingTimeout (const ola::TimeInterval &interval, ola::Callback0< bool > *callback)
 Execute a callback periodically.
ola::thread::timeout_id RegisterSingleTimeout (unsigned int ms, ola::SingleUseCallback0< void > *callback)
 Execute a callback after a certain time interval.
ola::thread::timeout_id RegisterSingleTimeout (const ola::TimeInterval &interval, ola::SingleUseCallback0< void > *callback)
 Execute a callback after a certain time interval.
void RemoveTimeout (ola::thread::timeout_id id)
 Cancel an existing timeout.
void RunInLoop (ola::Callback0< void > *callback)
 Execute a callback on every event loop.
void Execute (ola::BaseCallback0< void > *callback)
 Execute the supplied callback at some point in the future.

Friends

class ::SelectServerTest

Constructor & Destructor Documentation

ola::io::SelectServer::SelectServer ( ola::ExportMap export_map = NULL,
Clock clock = NULL 
)

Create a new SelectServer.

Parameters
export_mapthe ExportMap to use for stats
clockthe Clock to use to keep time
ola::io::SelectServer::~SelectServer ( )

Clean up.

The SelectServer should be terminated before it's deleted.

Member Function Documentation

bool ola::io::SelectServer::AddReadDescriptor ( ReadFileDescriptor descriptor)
virtual

Register a ReadFileDescriptor for read-events.

Parameters
descriptorthe ReadFileDescriptor to add.
Returns
true if the descriptor was added, false if the descriptor was previously added.

When the descriptor is ready for reading, PerformRead() will be called.

Implements ola::io::SelectServerInterface.

bool ola::io::SelectServer::AddReadDescriptor ( ConnectedDescriptor descriptor,
bool  delete_on_close = false 
)
virtual

Register a ConnectedDescriptor for read-events.

Parameters
descriptorthe ConnectedDescriptor to add.
delete_on_closeif true, ownership of the ConnectedDescriptor is transferred to the SelectServer.
Returns
true if the descriptor was added, false if the descriptor was previously added.

When the descriptor is ready for reading, PerformRead() will be called. Prior to PerformRead(), IsClosed() is called. If this returns true, and delete_on_close was set, the descriptor will be deleted.

Implements ola::io::SelectServerInterface.

bool ola::io::SelectServer::AddWriteDescriptor ( WriteFileDescriptor descriptor)
virtual

Register a WriteFileDescriptor for write-events.

Parameters
descriptorthe WriteFileDescriptor to add.

When the descriptor is writeable, PerformWrite() is called.

Implements ola::io::SelectServerInterface.

void ola::io::SelectServer::Execute ( ola::BaseCallback0< void > *  callback)
virtual

Execute the supplied callback at some point in the future.

Parameters
callbackthe callback to run.

This method guarantees the callback will not be run immediately.

Implements ola::thread::ExecutorInterface.

bool ola::io::SelectServer::IsRunning ( ) const
inline

Checks if the SelectServer is running.

Returns
true if the SelectServer is in the Run() method.
timeout_id ola::io::SelectServer::RegisterRepeatingTimeout ( unsigned int  period,
ola::Callback0< bool > *  callback 
)
virtual

Execute a callback periodically.

Parameters
periodthe number of milliseconds between each execution of the callback.
callbackthe callback to run. Ownership is transferred.
Returns
a timeout_id which can be used later to cancel the timeout.
Deprecated:
Use the version that takes a TimeInterval instead.

Returning false from the callback will cause it to be cancelled.

Implements ola::io::SelectServerInterface.

timeout_id ola::io::SelectServer::RegisterRepeatingTimeout ( const ola::TimeInterval period,
ola::Callback0< bool > *  callback 
)
virtual

Execute a callback periodically.

Parameters
periodthe time interval between each execution of the callback.
callbackthe callback to run. Ownership is transferred.
Returns
a timeout_id which can be used later to cancel the timeout.

Returning false from the callback will cause it to be cancelled.

Implements ola::io::SelectServerInterface.

timeout_id ola::io::SelectServer::RegisterSingleTimeout ( unsigned int  delay,
ola::SingleUseCallback0< void > *  callback 
)
virtual

Execute a callback after a certain time interval.

Parameters
delaythe number of milliseconds before the callback is executed.
callbackthe callback to run. Ownership is transferred.
Returns
a timeout_id which can be used later to cancel the timeout.
Deprecated:
Use the version that takes a TimeInterval instead.

Implements ola::io::SelectServerInterface.

timeout_id ola::io::SelectServer::RegisterSingleTimeout ( const ola::TimeInterval delay,
ola::SingleUseCallback0< void > *  callback 
)
virtual

Execute a callback after a certain time interval.

Parameters
delaythe time interval to wait before the callback is executed.
callbackthe callback to run. Ownership is transferred.
Returns
a timeout_id which can be used later to cancel the timeout.

Implements ola::io::SelectServerInterface.

void ola::io::SelectServer::RemoveReadDescriptor ( ReadFileDescriptor descriptor)
virtual

Remove a RemoveReadDescriptor for read-events.

Parameters
descriptorthe descriptor to remove.
Warning
Descriptors must be removed from the SelectServer before they are closed. Not doing so will result in hard to debug failures.

Implements ola::io::SelectServerInterface.

void ola::io::SelectServer::RemoveReadDescriptor ( ConnectedDescriptor descriptor)
virtual

Remove a ConnectedDescriptor for read-events.

Parameters
descriptorthe descriptor to remove.
Warning
Descriptors must be removed from the SelectServer before they are closed. Not doing so will result in hard to debug failures.

Implements ola::io::SelectServerInterface.

void ola::io::SelectServer::RemoveTimeout ( ola::thread::timeout_id  id)
virtual

Cancel an existing timeout.

Parameters
idthe timeout_id returned by a call to RegisterRepeatingTimeout or RegisterSingleTimeout.

Implements ola::io::SelectServerInterface.

void ola::io::SelectServer::RemoveWriteDescriptor ( WriteFileDescriptor descriptor)
virtual

Remove a WriteFileDescriptor for write-events.

Parameters
descriptorthe descriptor to remove.
Warning
Descriptors must be removed from the SelectServer before they are closed. Not doing so will result in hard to debug failures.

Implements ola::io::SelectServerInterface.

void ola::io::SelectServer::Run ( )

Enter the event loop.

Run() will return once Terminate() has been called.

void ola::io::SelectServer::RunInLoop ( ola::Callback0< void > *  callback)

Execute a callback on every event loop.

Parameters
callbackthe Callback to execute. Ownership is transferrred to the SelectServer.

Be very cautious about using this, it's almost certainly not what you want.

There is no way to remove a Callback added with this method.

void ola::io::SelectServer::RunOnce ( const TimeInterval block_interval)

Do a single pass through the event loop.

Parameters
block_intervalThe maximum time to block if there is no I/O or timer events.
void ola::io::SelectServer::SetDefaultInterval ( const TimeInterval block_interval)

Set the duration to block for.

Parameters
block_intervalthe interval to block for.

This controls the upper bound on the duration between callbacks added with RunInLoop().

void ola::io::SelectServer::Terminate ( )

Exit from the Run() loop.

Terminate() may be called from any thread.

const TimeStamp * ola::io::SelectServer::WakeUpTime ( ) const
virtual

The time when this SelectServer was woken up.

Returns
The TimeStamp of when the SelectServer was woken up.

If running within the same thread as the SelectServer, this is a efficient way to get the current time.

Implements ola::io::SelectServerInterface.


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