1 | #include <iostream>
|
---|
2 |
|
---|
3 | #include "Dim.h"
|
---|
4 | #include "EventImp.h"
|
---|
5 | #include "WindowLog.h"
|
---|
6 | #include "Configuration.h"
|
---|
7 | #include "StateMachineDim.h"
|
---|
8 | #include "ReadlineColor.h"
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | void SetupConfiguration(Configuration &conf)
|
---|
13 | {
|
---|
14 | const string n = conf.GetName()+".log";
|
---|
15 |
|
---|
16 | po::options_description config("Program options");
|
---|
17 | config.add_options()
|
---|
18 | ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
|
---|
19 | ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
|
---|
20 | ("log,l", var<string>(n), "Write log-file")
|
---|
21 | ;
|
---|
22 |
|
---|
23 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
24 | conf.AddEnv("host", "DIM_HOST_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 | Comment(evt.GetString());
|
---|
64 | return GetCurrentState();
|
---|
65 | }
|
---|
66 | public:
|
---|
67 | ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
|
---|
68 | {
|
---|
69 | AddEvent("MSG", "C")
|
---|
70 | (bind(&ChatServer::HandleMsg, this, placeholders::_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 | if (!conf.DoParse(argc, argv, PrintHelp))
|
---|
82 | return 127;
|
---|
83 |
|
---|
84 | Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"));
|
---|
85 |
|
---|
86 | WindowLog log;
|
---|
87 |
|
---|
88 | ReadlineColor::PrintBootMsg(log, conf.GetName(), false);
|
---|
89 |
|
---|
90 | if (conf.Has("log"))
|
---|
91 | if (!log.OpenLogFile(conf.Get<string>("log")))
|
---|
92 | cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
93 |
|
---|
94 | return ChatServer(log).Run();
|
---|
95 | }
|
---|
96 |
|
---|
97 | // **************************************************************************
|
---|
98 | /** @example chatserv.cc
|
---|
99 |
|
---|
100 | The program is stopped by CTRL-C
|
---|
101 |
|
---|
102 | */
|
---|
103 | // **************************************************************************
|
---|