ocean.util.app.ext.SignalExt

Application extension which handles signals to the process and calls the onSignal() method of all registered extensions (see ISignalExtExtension). The extension can handle any number of different signals -- depending solely on which signals are specified in the constructor.

Note: not only must the extension be registered with the application but its internal ISelectClient (returned by the selectClient() method) must also be registered with an epoll instance! Until the event is registered with epoll and the event loop started, the signal handlers will not be called in response to signals which have occurred.

Usage example:

1 
2 import ocean.util.app.Application;
3 import ocean.util.app.ext.SignalExt;
4 import ocean.io.select.EpollSelectDispatcher;
5 import core.sys.posix.signal : SIGINT, SIGTERM;
6 
7 // Example application class which does two things:
8 // 1. Owns an instance of SignalExtension and registers some signals
9 //    with it.
10 // 2. Implements ISignalExtExtension to be notified when registered
11 //    signals occur.
12 
13 // It's important to understand that these two things are not connected.
14 // It's perfectly possible for an application class to own a SignalExt
15 // but for another class (indeed other classes) elsewhere to implement
16 // ISignalExtExtension to receive the notification of signals occurring.
17 class MyApp : Application, ISignalExtExtension
18 {
19     private SignalExt signal_ext;
20 
21     this ( )
22     {
23         super("name", "desc");
24 
25         // Construct a signal extension instance and tell it which
26         // signals we're interested in. The list of signals can be
27         // extended after construction via the register() method.
28         auto signals = [SIGINT, SIGTERM];
29         this.signal_ext = new SignalExt(signals);
30 
31         // Register the signal extension with the application class
32         // (this).
33         this.registerExtension(this.signal_ext);
34 
35         // Register this class with the signal extension so that it will
36         // be notified (via its onSignal() method, below) when one of
37         // the registered signals occurs.
38         this.signal_ext.registerExtension(this);
39     }
40 
41     // Signal handler callback required by ISignalExtExtension. Called
42     // when a signal which has been registered with the signal extension
43     // occurs.
44     override void onSignal ( int signum )
45     {
46     }
47 
48     // Application main method required by Application.
49     override int run ( char[][] args )
50     {
51         // Important: onSignal() will not be called until the signal
52         // extension's event has been registered with epoll!
53         auto epoll = new EpollSelectDispatcher;
54         epoll.register(this.signal_ext.selectClient());
55         epoll.eventLoop();
56 
57         return 0;
58     }
59 }

Members

Classes

SignalExt
class SignalExt
Undocumented in source.

Meta

License

Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. Alternatively, this file may be distributed under the terms of the Tango 3-Clause BSD License (see LICENSE_BSD.txt for details).