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