source: trunk/FACT++/src/Main.h@ 11366

Last change on this file since 11366 was 11349, checked in by tbretz, 14 years ago
Fixed variable names.
File size: 2.7 KB
Line 
1#ifndef FACT_MAIN
2#define FACT_MAIN
3
4#if BOOST_VERSION < 104400
5#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
6#undef BOOST_HAS_RVALUE_REFS
7#endif
8#endif
9#include <boost/thread.hpp>
10
11#include "LocalControl.h"
12
13void MainThread(StateMachineImp *io_service, bool dummy)
14{
15 // This is necessary so that the StateMachien Thread can signal the
16 // Readline to exit
17 io_service->Run(dummy);
18 Readline::Stop();
19}
20
21/*
22template<class S>
23int RunDim(Configuration &conf)
24{
25 WindowLog wout;
26
27 ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
28
29 //log.SetWindow(stdscr);
30 if (conf.Has("log"))
31 if (!wout.OpenLogFile(conf.Get<string>("log")))
32 wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
33
34 // Start io_service.Run to use the StateMachineImp::Run() loop
35 // Start io_service.run to only use the commandHandler command detaching
36 AutoScheduler<S> io_service(wout);
37 if (!io_service.EvalConfiguration(conf))
38 return -1;
39
40 io_service.Run();
41
42 return 0;
43}
44*/
45
46template<class T, class S>
47int Main(const Configuration &conf, bool dummy=false)
48{
49 static T shell(conf.GetName().c_str(),
50 conf.Has("console") ? conf.Get<int>("console")!=1 : 0);
51
52 WindowLog &win = shell.GetStreamIn();
53 WindowLog &wout = shell.GetStreamOut();
54
55 if (conf.Has("log"))
56 if (!wout.OpenLogFile(conf.Get<string>("log")))
57 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
58
59 S io_service(wout);
60 const int rc = io_service.EvalConfiguration(conf);
61 if (rc>=0)
62 return rc;
63
64 shell.SetReceiver(io_service);
65
66// boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
67 boost::thread t(boost::bind(MainThread, &io_service, dummy));
68
69 const vector<string> v1 = conf.Vec<string>("cmd");
70 for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
71 shell.ProcessLine(*it);
72
73 const vector<string> v2 = conf.Vec<string>("exec");
74 for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
75 shell.Execute(*it);
76
77 if (conf.Get<bool>("quit"))
78 shell.Stop();
79
80 shell.Run(); // Run the shell
81 io_service.Stop(); // Signal Loop-thread to stop
82 // io_service.Close(); // Obsolete, done by the destructor
83 // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
84
85 // Wait until the StateMachine has finished its thread
86 // before returning and destroying the dim objects which might
87 // still be in use.
88 t.join();
89
90 return 0;
91}
92
93#endif
Note: See TracBrowser for help on using the repository browser.