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

Last change on this file since 11580 was 11580, checked in by tbretz, 14 years ago
Added --null option to suppress the output more or less completely.
File size: 6.4 KB
Line 
1#ifndef FACT_MAIN
2#define FACT_MAIN
3
4#include <map>
5#include <thread>
6#include <functional>
7
8#include "LocalControl.h"
9#include "Configuration.h"
10
11namespace Main
12{
13 void SetupConfiguration(Configuration &conf)
14 {
15 const string n = conf.GetName()+".log";
16
17 po::options_description config("Program options");
18 config.add_options()
19 ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
20 ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
21 ("log,l", var<string>(n), "Write log-file")
22 ("null", po_switch(), "Suppresses almost all console output - including errors (only available without --console option)")
23 ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
24 ("cmd", vars<string>(), "Execute one or more commands at startup")
25 ("exec,e", vars<string>(), "Execute one or more scrips at startup")
26 ("quit", po_switch(), "Quit after startup");
27 ;
28
29 conf.AddEnv("dns", "DIM_DNS_NODE");
30 conf.AddEnv("host", "DIM_HOST_NODE");
31
32 conf.AddOptions(config);
33 }
34
35 void Thread(StateMachineImp *io_service, bool dummy)
36 {
37 // This is necessary so that the StateMachien Thread can signal the
38 // Readline to exit
39 io_service->Run(dummy);
40 Readline::Stop();
41 }
42
43 template<class T, class S>
44 int execute(Configuration &conf, bool dummy=false)
45 {
46 Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"));
47
48 // -----------------------------------------------------------------
49
50 cout << conf.Has("console") << endl;
51 cout << conf.Has("null") << endl;
52 static T shell(conf.GetName().c_str(),
53 conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
54
55 WindowLog &win = shell.GetStreamIn();
56 WindowLog &wout = shell.GetStreamOut();
57
58 // Switching off buffering is not strictly necessary, since
59 // the destructor of shell should flush everything still buffered,
60 // nevertheless it helps to debug problems in the initialization
61 // sequence.
62 const bool backlog = wout.GetBacklog();
63 const bool null = wout.GetNullOutput();
64 if (conf.Has("console") || !conf.Get<bool>("null"))
65 {
66 wout.SetBacklog(false);
67 wout.SetNullOutput(false);
68 wout.Display(true);
69 }
70
71 if (conf.Has("log"))
72 if (!wout.OpenLogFile(conf.Get<string>("log")))
73 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
74
75 S io_service(wout);
76
77 const boost::filesystem::path path(conf.GetName());
78
79 const string pname = path.parent_path().string();
80 const string fname = path.filename();
81
82 const Time now;
83 io_service.Write(now, "/----------------------- Program ------------------------");
84 io_service.Write(now, "| Program: "PACKAGE_STRING" ("+fname+")");
85 io_service.Write(now, "| CallPath: "+pname);
86 io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
87 io_service.Write(now, "| Revision: "REVISION);
88 io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
89 io_service.Write(now, "| URL: "PACKAGE_URL);
90 io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
91 io_service.Write(now, "\\----------------------- Options ------------------------");
92 const multimap<string,string> mmap = conf.GetOptions();
93 for (multimap<string,string>::const_iterator it=mmap.begin(); it!=mmap.end(); it++)
94 io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
95 io_service.Write(now, "\\------------------- Evaluating options -----------------");
96 const int rc = io_service.EvalOptions(conf);
97 if (rc>=0)
98 {
99 ostringstream str;
100 str << "Exit triggered by EvalOptions with rc=" << rc;
101 io_service.Write(now, str.str(), MessageImp::kError);
102 return rc;
103 }
104
105 const map<string,string> &wco = conf.GetWildcardOptions();
106 if (wco.size()>0)
107 {
108 io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
109
110 size_t max = 0;
111 for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
112 if (it->second.length()>max)
113 max = it->second.length();
114
115 for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
116 {
117 ostringstream str;
118 str.setf(ios_base::left);
119 str << setw(max+1) << it->second << " : " << it->first;
120 io_service.Write(now, str.str(), MessageImp::kWarn);
121 }
122 io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
123 return 127;
124 }
125
126 io_service.Message("==================== Starting main loop =================");
127
128 if (conf.Has("console") || !conf.Get<bool>("null"))
129 {
130 wout.SetNullOutput(null);
131 wout.SetBacklog(backlog);
132 }
133
134 shell.SetReceiver(io_service);
135
136 // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
137 thread t(bind(Main::Thread, &io_service, dummy));
138
139 const vector<string> v1 = conf.Vec<string>("cmd");
140 for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
141 shell.ProcessLine(*it);
142
143 const vector<string> v2 = conf.Vec<string>("exec");
144 for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
145 shell.Execute(*it);
146
147 if (conf.Get<bool>("quit"))
148 shell.Stop();
149
150 shell.Run(); // Run the shell
151 io_service.Stop(); // Signal Loop-thread to stop
152 // io_service.Close(); // Obsolete, done by the destructor
153 // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
154
155 // Wait until the StateMachine has finished its thread
156 // before returning and destroying the dim objects which might
157 // still be in use.
158 t.join();
159
160 return 0;
161 }
162}
163
164#endif
Note: See TracBrowser for help on using the repository browser.