Lumiera  0.pre.03
»edit your freedom«
appstate.cpp
Go to the documentation of this file.
1 /*
2  AppState - application initialisation and behaviour
3 
4  Copyright (C)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
26 #include "lib/error.hpp"
27 #include "common/appstate.hpp"
29 
30 extern "C" {
32 
33 #include "common/interface.h"
35 #include "common/plugin.h"
36 }
37 
38 #include "lib/symbol.hpp"
39 #include "lib/util.hpp"
40 
41 
42 using lib::Literal;
43 using std::unique_ptr;
44 
45 
46 
47 namespace lumiera {
48 
49  namespace { // implementation details
50 
51  inline void
52  log_and_clear_unexpected_errorstate ()
53  {
54  if (lumiera_err errorstate = lumiera_error ())
55  ALERT (common, "*** Unexpected error: %s\n Triggering emergency exit.", errorstate);
56  }
57  }
58 
59 
60 
61 
73  , subsystems_{}
74  , emergency_{false}
75  , core_up_{false}
76  { }
77 
80 
81 
82  string
84  {
85  return setup_.get(key).as<string>();
86  }
87 
88 
89 
90 
91 
92  // ===== Implementation startup and shutdown sequence for main() ========
93 
94 
95 #define _MAYBE_THROW_ \
96  maybeThrow<error::Fatal> ("internal failure while initialising the "\
97  "Lumiera application framework");
98 
99 
100 
101  void
103  {
104  TRACE (common, "initialising application core...");
105 
107  _MAYBE_THROW_
108 
110  _MAYBE_THROW_
111 
112  lumiera_config_interface_init ();
113  _MAYBE_THROW_
114 
115  core_up_= true;
117  _MAYBE_THROW_
118 
119 
120  subsystems_.reset (new SubsystemRunner (options));
121  TRACE (common, "Lumiera core started successfully.");
122  }
123 
124 
125 
126  void
128  {
129  TRACE (common, "maybe startup %s...?", cStr(subsys));
130  REQUIRE (subsystems_);
131  subsystems_->maybeRun (subsys);
132  }
133 
134 
135 
136  typedef AppState::ExitCode ExitCode;
137 
138 
151  ExitCode
153  {
154  if (subsystems_)
155  {
156  emergency_ |= subsystems_->wait();
157  subsystems_.reset(0);
158  }
159 
160  NOTICE (common, "Shutting down Lumiera...");
161 
162  if (emergency_)
163  {
164  ALERT (common, "Triggering emergency exit...");
166  return CLEAN_EMERGENCY_EXIT;
167  }
168  else
169  {
171  return NORMAL_EXIT;
172  }
173  }
174 
175 
176 
177  ExitCode
179  {
180 
181  ERROR (common, "Aborting Lumiera after unhandled error: %s", cStr(problem.what()));
182 
183  log_and_clear_unexpected_errorstate();
184 
185  try
186  {
187  if (subsystems_)
188  {
189  subsystems_->triggerEmergency(true);
190  subsystems_->shutdownAll();
191  }
192  return maybeWait ();
193  }
194  catch (...)
195  {
196  return abort();
197  }
198  }
199 
200 
201 
202  ExitCode
203  AppState::abort () noexcept
204  {
205  log_and_clear_unexpected_errorstate();
206 
207  if (emergency_)
208  {
210  return FAILED_EMERGENCY_EXIT;
211  }
212  else
213  {
215  return CLEAN_EXIT_AFTER_ERROR;
216  }
217  }
218 
219 
220 
221 
228  {
229  if (core_up_)
230  try
231  {
232  TRACE (common, "shutting down basic application layer...");
233  lumiera_config_interface_destroy ();
234  lumiera_interfaceregistry_destroy ();
235  }
236  catch (...)
237  {
238  log_and_clear_unexpected_errorstate();
239  } }
240 
241 
242 
243 
244 } // namespace lumiera
ExitCode abort() noexcept
initiate an fatal emergency shutdown, caused by an unforeseen error condition
Definition: appstate.cpp:203
Dependencies and lifecycle of a partially independent Subsystem of the Application.
Definition: subsys.hpp:61
AppState()
perform initialisation triggered on first access.
Definition: appstate.cpp:71
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:59
int lumiera_plugin_register(LumieraPlugin plugin)
Register a plugin and its interfaces.
Definition: plugin.c:260
LumieraPlugin lumiera_plugin_load(const char *plugin)
Tries to load a plugin Creates a new plugin structure and tries to load and initialise the plugin...
Definition: plugin.c:241
~AppState()
anything which should be closed as late as possible and after the normal shutdown sequence can be pla...
Definition: appstate.cpp:227
const char * ON_GLOBAL_SHUTDOWN
to be triggered at the end of main()
Lumiera plugins define &#39;interfaces&#39; as shown in interface.h, the plugin system handles the loading of...
#define LUMIERA_LOCATION_OF_BOOTSTRAP_INI
"bootstrapIni" : the basic setup configuration to load
Definition: basic-setup.hpp:57
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
virtual CStr what() const noexcept override
std::exception interface : yield a diagnostic message
Frontend for handling the Lumiera application commandline arguments.
Definition: option.hpp:68
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:280
static void trigger(Symbol eventLabel)
trigger lifecycle callbacks registered under the given label
Definition: lifecycle.cpp:72
void maybeStart(lumiera::Subsys &)
building on the state determined by init, decide if the given Subsys needs to be pulled up and...
Definition: appstate.cpp:127
Lumiera interface macros and structures.
Marker types to indicate a literal string and a Symbol.
Implementation helper for managing execution of a collection of subsystems, which may depend on one a...
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
int lumiera_plugin_discover(LumieraPlugin(*callback_load)(const char *plugin), int(*callback_register)(LumieraPlugin))
discover new plugins traverses the configured plugin paths and calls the callback_load function for a...
Definition: plugin.c:187
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:115
const char * ON_GLOBAL_INIT
to be triggered in main()
void init(lumiera::Option &options)
evaluate the result of option parsing and maybe additional configuration such as to be able to determ...
Definition: appstate.cpp:102
void lumiera_interfaceregistry_init(void)
Initialise the interface registry.
Lumiera error handling (C++ interface).
Global registry for interfaces (extension points).
ExitCode maybeWait()
put the main thread of the application into a wait state, as long as some subsystem(s) registered wit...
Definition: appstate.cpp:152
string fetchSetupValue(lib::Literal key)
access basic application setup values (from setup.ini)
Definition: appstate.cpp:83
Registering and managing primary application-global services.
Manage execution of the independent Subsystems of the Lumiera application.
External interface to the lumiera configuration system.
Lumiera public interface.
Definition: advice.cpp:104
static lib::Depend< AppState > instance
get the (single) AppState instance.
Definition: appstate.hpp:74
const char * ON_EMERGENCY
activated on shutdown after premature failure of a subsystem
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62