-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageemitter-config.cpp
138 lines (114 loc) · 3.71 KB
/
messageemitter-config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "messageemitter-config.h"
#include <limits.h>
#include <stdlib.h>
#include <iostream>
#include "argtable3/argtable3.h"
#include "errorcodes.h"
#define DEF_PROTO_PATH "proto"
#define DEF_QUEUE_OUT "ipc:///tmp/message.pkt2"
Config::Config
(
int argc,
char* argv[]
)
{
stop_request = 0;
lastError = parseCmd(argc, argv);
}
int Config::error()
{
return lastError;
}
/**
* @brief Parse command line into struct ClientConfig
* @return 0- success
* 1- show help and exit, or command syntax error
* 2- output file does not exists or can not open to write
**/
int Config::parseCmd
(
int argc,
char* argv[]
)
{
struct arg_str *a_interface = arg_str0("i", "ipaddr", "<IP address>", "Network interface name or address. Default 0.0.0.0");
struct arg_int *a_port = arg_int0("l", "listen", "<port>", "TCP port to listen. Default 50052");
struct arg_str *a_message_out_url = arg_str0("o", "output", "<queue url>", "Default " DEF_QUEUE_OUT);
struct arg_str *a_proto_path = arg_str0("p", "protos", "<path>", "proto file directory. Default " DEF_PROTO_PATH);
struct arg_file *a_input_file = arg_file0("f", "file", "<file name>", "otherwise -i from nanomsg socket or read stdin");
struct arg_int *a_retries = arg_int0("r", "repeat", "<n>", "Repeat each message. Default 1.");
struct arg_int *a_retry_delay = arg_int0("y", "delay", "<seconds>", "Delay on repeats in seconds. Default 0");
struct arg_lit *a_daemonize = arg_lit0("d", "daemonize", "Start as daemon/service");
struct arg_lit *a_verbosity = arg_litn("v", "verbosity", 0, 3, "Verbosity level. 3- debug");
struct arg_lit *a_help = arg_lit0("h", "help", "Show this help");
struct arg_end *a_end = arg_end(20);
void* argtable[] = {
a_interface, a_port, a_proto_path,
a_input_file, a_message_out_url, a_daemonize,
a_retries, a_retry_delay, a_verbosity,
a_help, a_end
};
int nerrors;
// verify the argtable[] entries were allocated successfully
if (arg_nullcheck(argtable) != 0)
{
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return 1;
}
// Parse the command line as defined by argtable[]
nerrors = arg_parse(argc, argv, argtable);
// special case: '--help' takes precedence over error reporting
if ((a_help->count) || nerrors)
{
if (nerrors)
arg_print_errors(stderr, a_end, PROGRAM_NAME);
printf("Usage: %s\n", PROGRAM_NAME);
arg_print_syntax(stdout, argtable, "\n");
printf("%s\n", PROGRAM_DESCRIPTION);
arg_print_glossary(stdout, argtable, " %-25s %s\n");
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return 1;
}
if (a_interface->count)
intface = *a_interface->sval;
else
intface = DEF_ADDRESS;
if (a_port->count)
port = *a_port->ival;
else
port = DEF_PORT;
if (a_proto_path->count)
proto_path = *a_proto_path->sval;
else
proto_path = DEF_PROTO_PATH;
// get real path
char b[PATH_MAX];
char *pp = realpath(proto_path.c_str(), b);
if (pp)
proto_path = std::string(pp);
else {
std::cerr << ERR_INVALID_PROTO_PATH << std::endl;
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return ERRCODE_INVALID_PROTO_PATH;
}
if (a_input_file->count)
file_name = *a_input_file->filename;
else
file_name = ""; // stdin
if (a_message_out_url->count)
message_out_url = *a_message_out_url->sval;
else
message_out_url = DEF_QUEUE_OUT;
verbosity = a_verbosity->count;
daemonize = a_daemonize->count > 0;
if (a_retries->count)
retries = *a_retries->ival;
else
retries = 0;
if (a_retry_delay->count)
retry_delay = *a_retry_delay->ival;
else
retry_delay = 60;
arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
return 0;
}