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

Last change on this file since 11793 was 11581, checked in by tbretz, 13 years ago
Removed unintentionally comitted debug output.
File size: 6.3 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 static T shell(conf.GetName().c_str(),
51 conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
52
53 WindowLog &win = shell.GetStreamIn();
54 WindowLog &wout = shell.GetStreamOut();
55
56 // Switching off buffering is not strictly necessary, since
57 // the destructor of shell should flush everything still buffered,
58 // nevertheless it helps to debug problems in the initialization
59 // sequence.
60 const bool backlog = wout.GetBacklog();
61 const bool null = wout.GetNullOutput();
62 if (conf.Has("console") || !conf.Get<bool>("null"))
63 {
64 wout.SetBacklog(false);
65 wout.SetNullOutput(false);
66 wout.Display(true);
67 }
68
69 if (conf.Has("log"))
70 if (!wout.OpenLogFile(conf.Get<string>("log")))
71 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
72
73 S io_service(wout);
74
75 const boost::filesystem::path path(conf.GetName());
76
77 const string pname = path.parent_path().string();
78 const string fname = path.filename();
79
80 const Time now;
81 io_service.Write(now, "/----------------------- Program ------------------------");
82 io_service.Write(now, "| Program: "PACKAGE_STRING" ("+fname+")");
83 io_service.Write(now, "| CallPath: "+pname);
84 io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
85 io_service.Write(now, "| Revision: "REVISION);
86 io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
87 io_service.Write(now, "| URL: "PACKAGE_URL);
88 io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
89 io_service.Write(now, "\\----------------------- Options ------------------------");
90 const multimap<string,string> mmap = conf.GetOptions();
91 for (multimap<string,string>::const_iterator it=mmap.begin(); it!=mmap.end(); it++)
92 io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
93 io_service.Write(now, "\\------------------- Evaluating options -----------------");
94 const int rc = io_service.EvalOptions(conf);
95 if (rc>=0)
96 {
97 ostringstream str;
98 str << "Exit triggered by EvalOptions with rc=" << rc;
99 io_service.Write(now, str.str(), MessageImp::kError);
100 return rc;
101 }
102
103 const map<string,string> &wco = conf.GetWildcardOptions();
104 if (wco.size()>0)
105 {
106 io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
107
108 size_t max = 0;
109 for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
110 if (it->second.length()>max)
111 max = it->second.length();
112
113 for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
114 {
115 ostringstream str;
116 str.setf(ios_base::left);
117 str << setw(max+1) << it->second << " : " << it->first;
118 io_service.Write(now, str.str(), MessageImp::kWarn);
119 }
120 io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
121 return 127;
122 }
123
124 io_service.Message("==================== Starting main loop =================");
125
126 if (conf.Has("console") || !conf.Get<bool>("null"))
127 {
128 wout.SetNullOutput(null);
129 wout.SetBacklog(backlog);
130 }
131
132 shell.SetReceiver(io_service);
133
134 // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
135 thread t(bind(Main::Thread, &io_service, dummy));
136
137 const vector<string> v1 = conf.Vec<string>("cmd");
138 for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
139 shell.ProcessLine(*it);
140
141 const vector<string> v2 = conf.Vec<string>("exec");
142 for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
143 shell.Execute(*it);
144
145 if (conf.Get<bool>("quit"))
146 shell.Stop();
147
148 shell.Run(); // Run the shell
149 io_service.Stop(); // Signal Loop-thread to stop
150 // io_service.Close(); // Obsolete, done by the destructor
151 // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
152
153 // Wait until the StateMachine has finished its thread
154 // before returning and destroying the dim objects which might
155 // still be in use.
156 t.join();
157
158 return 0;
159 }
160}
161
162#endif
Note: See TracBrowser for help on using the repository browser.