source: trunk/FACT++/src/chatclient.cc@ 11575

Last change on this file since 11575 was 11574, checked in by tbretz, 13 years ago
Added the possibility to setup DIM_HOST_NODE with the --host program option.
File size: 3.2 KB
Line 
1#include <boost/filesystem.hpp>
2
3#include "Configuration.h"
4#include "ChatClient.h"
5#include "DimSetup.h"
6
7using namespace std;
8
9template <class T>
10void 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// ========================================================================
27void 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>(), "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 */
54void 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
69void PrintHelp()
70{
71 /* Additional help text which is printed after the configuration
72 options goes here */
73}
74
75int 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"), conf.Get<string>("host"));
109
110 if (conf.Get<int>("console")==0)
111 RunShell<ChatShell>(conf);
112 else
113 RunShell<ChatConsole>(conf);
114
115
116 return 0;
117}
Note: See TracBrowser for help on using the repository browser.