1 | #include <iostream>
|
---|
2 |
|
---|
3 | #include <boost/bind.hpp>
|
---|
4 |
|
---|
5 | #include "Dim.h"
|
---|
6 | #include "FACT.h"
|
---|
7 | #include "EventImp.h"
|
---|
8 | #include "WindowLog.h"
|
---|
9 | #include "Configuration.h"
|
---|
10 | #include "StateMachineDim.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 | cout << "Error: " << e.what() << " of '" << e.get_option_name() << "' option." << endl;
|
---|
90 | cout << endl;
|
---|
91 | return -1;
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 | catch (std::exception &e)
|
---|
95 | {
|
---|
96 | cout << "Error: " << e.what() << endl;
|
---|
97 | cout << endl;
|
---|
98 |
|
---|
99 | return -1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (conf.HasPrint())
|
---|
103 | return -1;
|
---|
104 |
|
---|
105 | if (conf.HasVersion())
|
---|
106 | {
|
---|
107 | FACT::PrintVersion(argv[0]);
|
---|
108 | return -1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (conf.HasHelp())
|
---|
112 | {
|
---|
113 | PrintHelp();
|
---|
114 | return -1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | Dim::Setup(conf.Get<string>("dns"));
|
---|
118 |
|
---|
119 | WindowLog log;
|
---|
120 |
|
---|
121 | if (conf.Has("log"))
|
---|
122 | if (!log.OpenLogFile(conf.Get<string>("log")))
|
---|
123 | cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
124 |
|
---|
125 | ChatServer serv(log);
|
---|
126 | serv.Run(true);
|
---|
127 |
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | // **************************************************************************
|
---|
132 | /** @example chatserv.cc
|
---|
133 |
|
---|
134 | The program is stopped by CTRL-C
|
---|
135 |
|
---|
136 | */
|
---|
137 | // **************************************************************************
|
---|