1 | #include <iostream>
|
---|
2 |
|
---|
3 | #include <boost/bind.hpp>
|
---|
4 |
|
---|
5 | #include "Dim.h"
|
---|
6 | #include "EventImp.h"
|
---|
7 | #include "WindowLog.h"
|
---|
8 | #include "Configuration.h"
|
---|
9 | #include "StateMachineDim.h"
|
---|
10 | #include "ReadlineColor.h"
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | void SetupConfiguration(Configuration &conf)
|
---|
15 | {
|
---|
16 | const string n = conf.GetName()+".log";
|
---|
17 |
|
---|
18 | po::options_description config("Program options");
|
---|
19 | config.add_options()
|
---|
20 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
---|
21 | ("log,l", var<string>(n), "Write log-file")
|
---|
22 | ;
|
---|
23 |
|
---|
24 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
25 |
|
---|
26 | conf.AddOptions(config);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /*
|
---|
30 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
31 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
32 | are used to match the usage synopsis in program output. An example from cp
|
---|
33 | (GNU coreutils) which contains both strings:
|
---|
34 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
35 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
36 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
37 | */
|
---|
38 | void PrintUsage()
|
---|
39 | {
|
---|
40 | cout <<
|
---|
41 | "The chatserv is a Dim-based chat server.\n"
|
---|
42 | "\n"
|
---|
43 | "It is a non-interactive program which acts as a relay of messages "
|
---|
44 | "sent via a Dim command CHAT/MSG and which are redirected to the "
|
---|
45 | "logging service CHAT/MESSAGE.\n"
|
---|
46 | "\n"
|
---|
47 | "Usage: chatserv [OPTIONS]\n"
|
---|
48 | " or: chatserv [OPTIONS]\n";
|
---|
49 | cout << endl;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void PrintHelp()
|
---|
53 | {
|
---|
54 | /* Additional help text which is printed after the configuration
|
---|
55 | options goes here */
|
---|
56 | }
|
---|
57 |
|
---|
58 | class ChatServer : public StateMachineDim
|
---|
59 | {
|
---|
60 | private:
|
---|
61 | int HandleMsg(const EventImp &evt)
|
---|
62 | {
|
---|
63 | Message(evt.GetString());
|
---|
64 | return GetCurrentState();
|
---|
65 | }
|
---|
66 | public:
|
---|
67 | ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
|
---|
68 | {
|
---|
69 | AddEvent("MSG", "C")
|
---|
70 | (boost::bind(&ChatServer::HandleMsg, this, _1))
|
---|
71 | ("|msg[string]:message to be distributed");
|
---|
72 | }
|
---|
73 | };
|
---|
74 |
|
---|
75 | int main(int argc, const char *argv[])
|
---|
76 | {
|
---|
77 | Configuration conf(argv[0]);
|
---|
78 | conf.SetPrintUsage(PrintUsage);
|
---|
79 | SetupConfiguration(conf);
|
---|
80 |
|
---|
81 | po::variables_map vm;
|
---|
82 | try
|
---|
83 | {
|
---|
84 | vm = conf.Parse(argc, argv);
|
---|
85 | }
|
---|
86 | #if BOOST_VERSION > 104000
|
---|
87 | catch (po::multiple_occurrences &e)
|
---|
88 | {
|
---|
89 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
---|
90 | return -1;
|
---|
91 | }
|
---|
92 | #endif
|
---|
93 | catch (exception& e)
|
---|
94 | {
|
---|
95 | cerr << "Program options invalid due to: " << e.what() << endl;
|
---|
96 | return -1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (conf.HasVersion() || conf.HasPrint())
|
---|
100 | return -1;
|
---|
101 |
|
---|
102 | if (conf.HasHelp())
|
---|
103 | {
|
---|
104 | PrintHelp();
|
---|
105 | return -1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | Dim::Setup(conf.Get<string>("dns"));
|
---|
109 |
|
---|
110 | WindowLog log;
|
---|
111 |
|
---|
112 | ReadlineColor::PrintBootMsg(log, conf.GetName(), false);
|
---|
113 |
|
---|
114 | if (conf.Has("log"))
|
---|
115 | if (!log.OpenLogFile(conf.Get<string>("log")))
|
---|
116 | cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
117 |
|
---|
118 | ChatServer serv(log);
|
---|
119 | serv.Run(true);
|
---|
120 |
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | // **************************************************************************
|
---|
125 | /** @example chatserv.cc
|
---|
126 |
|
---|
127 | The program is stopped by CTRL-C
|
---|
128 |
|
---|
129 | */
|
---|
130 | // **************************************************************************
|
---|