1 | #include <boost/filesystem.hpp>
|
---|
2 |
|
---|
3 | #include "Configuration.h"
|
---|
4 | #include "ChatClient.h"
|
---|
5 | #include "DimSetup.h"
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | template <class T>
|
---|
10 | void RunShell(Configuration &conf)
|
---|
11 | {
|
---|
12 | // A normal kill will call its destructor! (Very nice feature ;) )
|
---|
13 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
14 |
|
---|
15 | WindowLog &win = shell.GetStreamIn();
|
---|
16 | WindowLog &wout = shell.GetStreamOut();
|
---|
17 |
|
---|
18 | if (conf.Has("log"))
|
---|
19 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
20 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
21 |
|
---|
22 | shell.Run();
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | // ========================================================================
|
---|
27 | void SetupConfiguration(Configuration &conf)
|
---|
28 | {
|
---|
29 | const string n = conf.GetName()+".log";
|
---|
30 |
|
---|
31 | po::options_description config("Program options");
|
---|
32 | config.add_options()
|
---|
33 | ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
|
---|
34 | ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
|
---|
35 | ("log,l", var<string>(n), "Write log-file")
|
---|
36 | ("console,c", var<int>(0), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
37 | ;
|
---|
38 |
|
---|
39 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
40 | conf.AddEnv("host", "DIM_HOST_NODE");
|
---|
41 |
|
---|
42 | conf.AddOptions(config);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /*
|
---|
46 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
47 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
48 | are used to match the usage synopsis in program output. An example from cp
|
---|
49 | (GNU coreutils) which contains both strings:
|
---|
50 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
51 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
52 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
53 | */
|
---|
54 | void PrintUsage()
|
---|
55 | {
|
---|
56 | cout << "\n"
|
---|
57 | "The chatclient is a simple Dim based chatclient.\n"
|
---|
58 | "\n"
|
---|
59 | "The chatclient is always started with user intercation. "
|
---|
60 | "Just enter a message. It will be broadcasted through the chatserv, "
|
---|
61 | "which need to be running."
|
---|
62 | "\n"
|
---|
63 | "Usage: chatclient [-c type] [OPTIONS]\n"
|
---|
64 | " or: chatclient [OPTIONS]\n";
|
---|
65 | cout << endl;
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | void PrintHelp()
|
---|
70 | {
|
---|
71 | /* Additional help text which is printed after the configuration
|
---|
72 | options goes here */
|
---|
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 | if (conf.Get<int>("console")==0)
|
---|
87 | RunShell<ChatShell>(conf);
|
---|
88 | else
|
---|
89 | RunShell<ChatConsole>(conf);
|
---|
90 |
|
---|
91 |
|
---|
92 | return 0;
|
---|
93 | }
|
---|