Debian Codegraph @

src/Main/subsystemFactory.cxx

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // subsystemFactory.cxx - factory for subsystems
2 //
3 // Copyright (C) 2012 James Turner [email protected]
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "subsystemFactory.hxx"
24
25 #include <simgear/structure/subsystem_mgr.hxx>
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/structure/event_mgr.hxx>
30
31 #include <Main/globals.hxx>
32 #include <Sound/soundmanager.hxx>
33
34 // subsystem includes
35 #include <Aircraft/controls.hxx>
36 #include <Aircraft/FlightHistory.hxx>
37 #include <Main/fg_props.hxx>
38 #include <Main/FGInterpolator.hxx>
39 #include <Main/fg_io.hxx>
40 #include <FDM/fdm_shell.hxx>
41 #include <Environment/environment_mgr.hxx>
42 #include <Environment/ephemeris.hxx>
43 #include <Instrumentation/instrument_mgr.hxx>
44 #ifdef ENABLE_HUD
45 # include <Instrumentation/HUD/HUD.hxx>
46 #endif
47 #include <Systems/system_mgr.hxx>
48 #include <Autopilot/route_mgr.hxx>
49 #include <Autopilot/autopilotgroup.hxx>
50 #include <Traffic/TrafficMgr.hxx>
51 #include <Network/fgcom.hxx>
52 #include <Network/HTTPClient.hxx>
53 #include <Cockpit/cockpitDisplayManager.hxx>
54 #include <GUI/new_gui.hxx>
55 #include <Main/logger.hxx>
56 #include <ATC/atc_mgr.hxx>
57 #include <AIModel/AIManager.hxx>
58 #include <MultiPlayer/multiplaymgr.hxx>
59 #if defined(ENABLE_SWIFT)
60 # include <Network/Swift/swift_connection.hxx>
61 #endif
62 #include <AIModel/submodel.hxx>
63 #include <Aircraft/controls.hxx>
64 #include <Input/input.hxx>
65 #include <Aircraft/replay.hxx>
66 #include <Sound/voice.hxx>
67 #include <Canvas/canvas_mgr.hxx>
68 #include <Canvas/gui_mgr.hxx>
69 #include <Time/light.hxx>
70 #include <Viewer/viewmgr.hxx>
71 #include <Model/acmodel.hxx>
72 #include <Model/modelmgr.hxx>
73
74 using std::vector;
75
76 namespace flightgear
77 {
78
79 SGSubsystem* createSubsystemByName(const std::string& name)
80 {
81 #define MAKE_SUB(cl, n) \
82 if (name == n) return new cl;
83
84 MAKE_SUB(FGSoundManager, "sound");
85 MAKE_SUB(FGInterpolator, "prop-interpolator")
86 MAKE_SUB(FGProperties, "properties");
87 MAKE_SUB(FGHTTPClient, "http");
88 MAKE_SUB(FDMShell, "flight");
89 MAKE_SUB(FGEnvironmentMgr, "environment");
90 MAKE_SUB(Ephemeris, "ephemeris");
91 MAKE_SUB(FGSystemMgr, "systems");
92 MAKE_SUB(FGInstrumentMgr, "instrumentation");
93 #ifdef ENABLE_HUD
94 MAKE_SUB(HUD, "hud");
95 #endif
96 MAKE_SUB(flightgear::CockpitDisplayManager, "cockpit-displays");
97 MAKE_SUB(FGRouteMgr, "route-manager");
98 MAKE_SUB(FGIO, "io");
99 MAKE_SUB(FGLogger, "logger");
100 MAKE_SUB(NewGUI, "gui");
101 MAKE_SUB(CanvasMgr, "Canvas");
102 MAKE_SUB(GUIMgr, "CanvasGUI");
103 MAKE_SUB(FGATCManager, "ATC");
104 MAKE_SUB(FGMultiplayMgr, "mp");
105 MAKE_SUB(FGAIManager, "ai-model");
106 MAKE_SUB(FGSubmodelMgr, "submodel-mgr");
107 MAKE_SUB(FGTrafficManager, "traffic-manager");
108 MAKE_SUB(FGControls, "controls");
109 MAKE_SUB(FGInput, "input");
110 MAKE_SUB(FGReplay, "replay");
111 MAKE_SUB(FGFlightHistory, "history");
112 #ifdef ENABLE_AUDIO_SUPPORT
113 MAKE_SUB(FGVoiceMgr, "voice");
114 #endif
115 #ifdef ENABLE_IAX
116 MAKE_SUB(FGCom, "fgcom");
117 #endif
118 #ifdef ENABLE_SWIFT
119 MAKE_SUB(swift::SwiftConnection, "swift");
120 #endif
121 MAKE_SUB(FGLight, "tides");
122 MAKE_SUB(FGLight, "lighting");
123 MAKE_SUB(FGAircraftModel, "aircraft-model");
124 MAKE_SUB(FGModelMgr, "model-manager");
125 MAKE_SUB(FGViewMgr, "view-manager");
126 #undef MAKE_SUB
127
128 throw sg_range_exception("unknown subsystem:" + name);
129 }
130
131 SGSubsystemMgr::GroupType mapGroupNameToType(const std::string& s)
132 {
133 if (s == "init") return SGSubsystemMgr::INIT;
134 if (s == "general") return SGSubsystemMgr::GENERAL;
135 if (s == "fdm") return SGSubsystemMgr::FDM;
136 if (s == "post-fdm") return SGSubsystemMgr::POST_FDM;
137 if (s == "display") return SGSubsystemMgr::DISPLAY;
138 if (s == "sound") return SGSubsystemMgr::SOUND;
139
140 SG_LOG(SG_GENERAL, SG_ALERT, "unrecognized subsystem group:" << s);
141 return SGSubsystemMgr::GENERAL;
142 }
143
144 static SGSubsystem* getSubsystem(const SGPropertyNode* arg, bool create)
145 {
146 std::string subsystem(arg->getStringValue("subsystem"));
147 std::string name = arg->getStringValue("name");
148
149 if (name.empty()) {
150 // default name is simply the subsytem's name
151 name = subsystem;
152 }
153
154 SGSubsystem* sys = globals->get_subsystem_mgr()->get_subsystem(name);
155 if (!create)
156 return sys;
157
158 if( subsystem.empty() ) {
159 SG_LOG( SG_GENERAL,
160 SG_ALERT,
161 "do_add_subsystem: no subsystem/name supplied" );
162 return 0;
163 }
164
165 if (sys) {
166 SG_LOG( SG_GENERAL,
167 SG_ALERT,
168 "do_add_subsystem: duplicate subsystem name:" << name );
169 return 0;
170 }
171
172 std::string groupname = arg->getStringValue("group");
173 SGSubsystemMgr::GroupType group = SGSubsystemMgr::GENERAL;
174 if (!groupname.empty()) {
175 group = mapGroupNameToType(groupname);
176 }
177
178 try {
179 sys = createSubsystemByName(subsystem);
180 } catch (sg_exception& e) {
181 SG_LOG( SG_GENERAL,
182 SG_ALERT,
183 "subsystem creation failed:" << name
184 << ":" << e.getFormattedMessage() );
185 return 0;
186 }
187
188 bool doInit = arg->getBoolValue("do-bind-init", false);
189 if (doInit) {
190 sys->bind();
191 sys->init();
192 }
193
194 double minTime = arg->getDoubleValue("min-time-sec", 0.0);
195 globals->get_subsystem_mgr()
196 ->add(name.c_str(), sys, group, minTime);
197
198 return sys;
199 }
200
201 static bool
202 do_check_subsystem_running(const SGPropertyNode * arg, SGPropertyNode * root)
203 {
204 return getSubsystem(arg, false) != 0;
205 }
206
207 static bool
208 do_add_subsystem (const SGPropertyNode * arg, SGPropertyNode * root)
209 {
210 return getSubsystem(arg, true) != 0;
211 }
212
213 static bool do_remove_subsystem(const SGPropertyNode * arg, SGPropertyNode * root)
214 {
215 std::string name = arg->getStringValue("subsystem");
216
217 SGSubsystem* instance = globals->get_subsystem_mgr()->get_subsystem(name);
218 if (!instance) {
•219 SG_LOG(SG_GENERAL, SG_ALERT, "do_remove_subsystem: unknown subsystem:" << name);
220 return false;
221 }
222
223 // is it safe to always call these? let's assume so!
224 instance->shutdown();
225 instance->unbind();
226
227 // unplug from the manager (this also deletes the instance!)
228 globals->get_subsystem_mgr()->remove(name.c_str());
229
230 return true;
231 }
232
233 /**
234 * Built-in command: reinitialize one or more subsystems.
235 *
236 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
237 * none is specified, reinitialize all of them.
238 */
239 static bool
240 do_reinit (const SGPropertyNode * arg, SGPropertyNode * root)
241 {
242 bool result = true;
243
244 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
245 if (subsystems.empty()) {
246 globals->get_subsystem_mgr()->reinit();
247 } else {
248 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
249 std::string name = subsystems[i]->getStringValue();
250 SGSubsystem * subsystem = globals->get_subsystem_mgr()->get_subsystem(name.c_str());
251 if (subsystem == 0) {
252 result = false;
253 SG_LOG( SG_GENERAL, SG_ALERT,
254 "Subsystem " << name << " not found" );
255 } else {
256 subsystem->reinit();
257 }
258 }
259 }
260
261 globals->get_event_mgr()->reinit();
262
263 return result;
264 }
265
266 /**
267 * Built-in command: suspend one or more subsystems.
268 *
269 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
270 */
271 static bool
272 do_suspend (const SGPropertyNode * arg, SGPropertyNode * root)
273 {
274 bool result = true;
275
276 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
277 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
278 std::string name = subsystems[i]->getStringValue();
279 SGSubsystem * subsystem = globals->get_subsystem_mgr()->get_subsystem(name.c_str());
280 if (subsystem == 0) {
281 result = false;
282 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
283 } else {
284 subsystem->suspend();
285 }
286 }
287 return result;
288 }
289
290 /**
291 * Built-in command: suspend one or more subsystems.
292 *
293 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
294 */
295 static bool
296 do_resume (const SGPropertyNode * arg, SGPropertyNode * root)
297 {
298 bool result = true;
299
300 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
301 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
302 std::string name = subsystems[i]->getStringValue();
303 SGSubsystem * subsystem = globals->get_subsystem_mgr()->get_subsystem(name.c_str());
304 if (subsystem == 0) {
305 result = false;
306 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
307 } else {
308 subsystem->resume();
309 }
310 }
311 return result;
312 }
313
314 static struct {
315 const char * name;
316 SGCommandMgr::command_t command;
317 } built_ins [] = {
318 { "add-subsystem", do_add_subsystem },
319 { "remove-subsystem", do_remove_subsystem },
320 { "subsystem-running", do_check_subsystem_running },
321 { "reinit", do_reinit },
322 { "suspend", do_suspend },
323 { "resume", do_resume },
324 { 0, 0 } // zero-terminated
325 };
326
327 void registerSubsystemCommands(SGCommandMgr* cmdMgr)
328 {
329 for (int i = 0; built_ins[i].name != 0; i++) {
330 cmdMgr->addCommand(built_ins[i].name, built_ins[i].command);
331 }
332 }
333
334 } // of namepace flightgear