Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
appstate.cpp
Go to the documentation of this file.
1 /*
2  AppState - application initialisation and behaviour
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
35 #include "lib/error.hpp"
36 #include "common/appstate.hpp"
38 
39 extern "C" {
41 
42 #include "common/interface.h"
44 #include "common/plugin.h"
45 }
46 
47 #include "lib/symbol.hpp"
48 #include "lib/util.hpp"
49 
50 
51 using util::cStr;
52 using lib::Literal;
53 using std::unique_ptr;
54 
55 
56 
57 namespace lumiera {
58 
59  namespace { // implementation details
60 
61  inline void
62  log_and_clear_unexpected_errorstate ()
63  {
64  if (lumiera_err errorstate = lumiera_error ())
65  ALERT (common, "*** Unexpected error: %s\n Triggering emergency exit.", errorstate);
66  }
67  }
68 
69 
70 
71 
83  , subsystems_{}
84  , emergency_{false}
85  , core_up_{false}
86  { }
87 
90 
91 
92  string
94  {
95  return setup_.get(key).as<string>();
96  }
97 
98 
99 
100 
101 
102  // ===== Implementation startup and shutdown sequence for main() ========
103 
104 
105 #define _MAYBE_THROW_ \
106  maybeThrow<error::Fatal> ("internal failure while initialising the "\
107  "Lumiera application framework");
108 
109 
110 
111  void
113  {
114  TRACE (common, "initialising application core...");
115 
118 
121 
124 
125  core_up_= true;
128 
129 
130  subsystems_.reset (new SubsystemRunner (options));
131  TRACE (common, "Lumiera core started successfully.");
132  }
133 
134 
135 
136  void
138  {
139  TRACE (common, "maybe startup %s...?", cStr(subsys));
140  REQUIRE (subsystems_);
141  subsystems_->maybeRun (subsys);
142  }
143 
144 
145 
147 
148 
161  ExitCode
163  {
164  if (subsystems_)
165  {
166  emergency_ |= subsystems_->wait();
167  subsystems_.reset(0);
168  }
169 
170  NOTICE (common, "Shutting down Lumiera...");
171 
172  if (emergency_)
173  {
174  ALERT (common, "Triggering emergency exit...");
176  return CLEAN_EMERGENCY_EXIT;
177  }
178  else
179  {
181  return NORMAL_EXIT;
182  }
183  }
184 
185 
186 
187  ExitCode
189  {
190 
191  ERROR (common, "Aborting Lumiera after unhandled error: %s", cStr(problem.what()));
192 
193  log_and_clear_unexpected_errorstate();
194 
195  try
196  {
197  if (subsystems_)
198  {
199  subsystems_->triggerEmergency(true);
200  subsystems_->shutdownAll();
201  }
202  return maybeWait ();
203  }
204  catch (...)
205  {
206  return abort();
207  }
208  }
209 
210 
211 
212  ExitCode
213  AppState::abort () throw()
214  {
215  log_and_clear_unexpected_errorstate();
216 
217  if (emergency_)
218  {
220  return FAILED_EMERGENCY_EXIT;
221  }
222  else
223  {
225  return CLEAN_EXIT_AFTER_ERROR;
226  }
227  }
228 
229 
230 
231 
238  {
239  if (core_up_)
240  try
241  {
242  TRACE (common, "shutting down basic application layer...");
245  }
246  catch (...)
247  {
248  log_and_clear_unexpected_errorstate();
249  } }
250 
251 
252 
253 
254 } // namespace lumiera
Dependencies and lifecycle of a partially independent Subsystem of the Application.
Definition: subsys.hpp:70
AppState()
perform initialisation triggered on first access.
Definition: appstate.cpp:81
int lumiera_plugin_register(LumieraPlugin plugin)
Register a plugin and its interfaces.
Definition: plugin.c:269
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:250
~AppState()
anything which should be closed as late as possible and after the normal shutdown sequence can be pla...
Definition: appstate.cpp:237
const char * ON_GLOBAL_SHUTDOWN
to be triggered at the end of main()
Lumiera plugins define 'interfaces' as shown in interface.h, the plugin system handles the loading of...
#define _MAYBE_THROW_
Definition: appstate.cpp:105
#define LUMIERA_LOCATION_OF_BOOTSTRAP_INI
"bootstrapIni" : the basic setup configuration to load
Definition: basic-setup.hpp:66
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:75
Frontend for handling the Lumiera application commandline arguments.
Definition: option.hpp:76
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:275
static void trigger(Symbol eventLabel)
trigger lifecycle callbacks registered under the given label
Definition: lifecycle.cpp:82
ExitCode abort()
initiate an fatal emergency shutdown, caused by an unforeseen error condition
Definition: appstate.cpp:213
const char * cStr(string const &org)
convenience shortcut: conversion to c-String via string.
Definition: util.hpp:380
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:137
opt::variable_value const & get(lib::Literal key) const
Lumiera interface macros and structures.
TRACE(test,"inserted %d", data)
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...
void lumiera_config_interface_destroy(void)
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:196
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
const char * ON_GLOBAL_INIT
to be triggered in main()
const char * lumiera_err
Definition: error.h:57
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:112
void lumiera_config_interface_init(void)
void lumiera_interfaceregistry_destroy(void)
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:162
string fetchSetupValue(lib::Literal key)
access basic application setup values (from setup.ini)
Definition: appstate.cpp:93
BasicSetup setup_
Definition: appstate.hpp:135
Registering and managing primary application-global services.
Manage execution of the independend Subsystems of the Lumiera application.
External interface to the lumiera configuration system.
virtual CStr what() const noexceptoverride
std::exception interface : yield a diagnostic message
Lumiera public interface.
AppState::ExitCode ExitCode
Definition: appstate.cpp:146
static lib::Depend< AppState > instance
get the (single) AppState instance.
Definition: appstate.hpp:83
const char * ON_EMERGENCY
activated on shutdown after premature failure of a subsystem
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:69