source: trunk/FACT++/src/chatserv.cc@ 10634

Last change on this file since 10634 was 10630, checked in by tbretz, 14 years ago
Unified help output more.
File size: 3.3 KB
Line 
1#include <iostream>
2
3#include <boost/bind.hpp>
4
5#include "EventImp.h"
6#include "WindowLog.h"
7#include "Configuration.h"
8#include "StateMachineDim.h"
9
10using namespace std;
11
12void 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 host name (Overwites DIM_DNS_NODE environment variable)")
19 ("log,l", var<string>(n), "Write log-file")
20 ;
21
22 conf.AddEnv("dns", "DIM_DNS_NODE");
23
24 conf.AddOptions(config);
25}
26
27/*
28 Extract usage clause(s) [if any] for SYNOPSIS.
29 Translators: "Usage" and "or" here are patterns (regular expressions) which
30 are used to match the usage synopsis in program output. An example from cp
31 (GNU coreutils) which contains both strings:
32 Usage: cp [OPTION]... [-T] SOURCE DEST
33 or: cp [OPTION]... SOURCE... DIRECTORY
34 or: cp [OPTION]... -t DIRECTORY SOURCE...
35 */
36void PrintUsage()
37{
38 cout <<
39 "The chatserv is a Dim-based chat server.\n"
40 "\n"
41 "It is a non-interactive program which acts as a relay of messages "
42 "sent via a Dim command CHAT/MSG and which are redirected to the "
43 "logging service CHAT/MESSAGE.\n"
44 "\n"
45 "Usage: chatserv [OPTIONS]\n"
46 " or: chatserv [OPTIONS]\n"
47 cout << endl;
48}
49
50void PrintHelp()
51{
52 /* Additional help text which is printed after the configuration
53 options goes here */
54}
55
56class ChatServer : public StateMachineDim
57{
58private:
59 int HandleMsg(const EventImp &evt)
60 {
61 Message(evt.GetString());
62 return GetCurrentState();
63 }
64public:
65 ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
66 {
67 AddConfiguration("MSG", "C")
68 (boost::bind(&ChatServer::HandleMsg, this, _1))
69 ("|msg[string]:message to be distributed");
70 }
71};
72
73int main(int argc, const char *argv[])
74{
75 Configuration conf(argv[0]);
76 conf.SetPrintUsage(PrintUsage);
77 SetupConfiguration(conf);
78
79 po::variables_map vm;
80 try
81 {
82 vm = conf.Parse(argc, argv);
83 }
84#if BOOST_VERSION > 104000
85 catch (po::multiple_occurrences &e)
86 {
87 cout << "Error: " << e.what() << " of '" << e.get_option_name() << "' option." << endl;
88 cout << endl;
89 return -1;
90 }
91#endif
92 catch (std::exception &e)
93 {
94 cout << "Error: " << e.what() << endl;
95 cout << endl;
96
97 return -1;
98 }
99
100 if (conf.HasPrint())
101 return -1;
102
103 if (conf.HasVersion())
104 {
105 FACT::PrintVersion(argv[0]);
106 return -1;
107 }
108
109 if (conf.HasHelp())
110 {
111 PrintHelp();
112 return -1;
113 }
114
115 // To allow overwriting of DIM_DNS_NODE set 0 to 1
116 setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
117
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
134The program is stopped by CTRL-C
135
136*/
137// **************************************************************************
Note: See TracBrowser for help on using the repository browser.