2424#include < iostream>
2525#include < iomanip>
2626
27+ #include < event2/util.h>
28+
2729#if defined(SUPPORT_GLOG) && defined(GLOG_TO_STDOUT)
2830void GLogToStdout::send (google::LogSeverity severity, const char * full_filename,
2931 const char * base_filename, int line, const struct ::tm* tm_time,
@@ -89,6 +91,31 @@ void Strings::Append(string & dest, const char * fmt, ...) {
8991 }
9092}
9193
94+ string Strings::ReplaceAll (std::string str, const std::string& from, const std::string& to) {
95+ size_t start_pos = 0 ;
96+ while ((start_pos = str.find (from, start_pos)) != std::string::npos) {
97+ str.replace (start_pos, from.length (), to);
98+ start_pos += to.length (); // Handles case where 'to' is a substring of 'from'
99+ }
100+ return str;
101+ }
102+
103+ string Strings::FormatIP (uint32_t ipv4Int, string format) {
104+ union IPv4Arr {
105+ uint32_t ipv4Int;
106+ uint8_t parts[4 ];
107+ } ipv4Arr;
108+
109+ ipv4Arr.ipv4Int = std::move (ipv4Int);
110+
111+ format = Strings::ReplaceAll (format, string (" {1}" ), std::to_string (ipv4Arr.parts [0 ]));
112+ format = Strings::ReplaceAll (format, string (" {2}" ), std::to_string (ipv4Arr.parts [1 ]));
113+ format = Strings::ReplaceAll (format, string (" {3}" ), std::to_string (ipv4Arr.parts [2 ]));
114+ format = Strings::ReplaceAll (format, string (" {4}" ), std::to_string (ipv4Arr.parts [3 ]));
115+
116+ return format;
117+ }
118+
92119string getJsonStr (const char *c,const jsmntok_t *t) {
93120 if (t == NULL || t->end <= t->start )
94121 return " " ;
@@ -105,12 +132,7 @@ int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
105132 return -1 ;
106133}
107134
108- bool parseConfJson (const string &jsonStr,
109- string &agentType, string &listenIP, string &listenPort,
110- std::vector<PoolConf> &poolConfs,
111- bool &alwaysKeepDownconn, bool &disconnectWhenLostAsicBoost,
112- bool &useIpAsWorkerName, bool &submitResponseFromServer,
113- string &fixedWorkerName) {
135+ bool parseConfJson (const string &jsonStr, AgentConf &conf) {
114136 jsmn_parser p;
115137 jsmn_init (&p);
116138 jsmntok_t t[64 ]; // we expect no more than 64 tokens
@@ -128,15 +150,15 @@ bool parseConfJson(const string &jsonStr,
128150
129151 for (int i = 1 ; i < r; i++) {
130152 if (jsoneq (c, &t[i], " agent_type" ) == 0 ) {
131- agentType = getJsonStr (c, &t[i+1 ]);
153+ conf. agentType_ = getJsonStr (c, &t[i+1 ]);
132154 i++;
133155 }
134156 if (jsoneq (c, &t[i], " agent_listen_ip" ) == 0 ) {
135- listenIP = getJsonStr (c, &t[i+1 ]);
157+ conf. listenIP_ = getJsonStr (c, &t[i+1 ]);
136158 i++;
137159 }
138160 else if (jsoneq (c, &t[i], " agent_listen_port" ) == 0 ) {
139- listenPort = getJsonStr (c, &t[i+1 ]);
161+ conf. listenPort_ = ( uint16_t ) strtoul ( getJsonStr (c, &t[i+1 ]). c_str (), NULL , 10 );
140162 i++;
141163 }
142164 else if (jsoneq (c, &t[i], " pools" ) == 0 ) {
@@ -159,51 +181,79 @@ bool parseConfJson(const string &jsonStr,
159181 return false ;
160182 }
161183
162- PoolConf conf ;
163- conf .host_ = getJsonStr (c, &t[idx + 1 ]);
164- conf .port_ = (uint16_t )strtoul (getJsonStr (c, &t[idx + 2 ]).c_str (), NULL , 10 );
165- conf .upPoolUserName_ = getJsonStr (c, &t[idx + 3 ]);
184+ PoolConf pool ;
185+ pool .host_ = getJsonStr (c, &t[idx + 1 ]);
186+ pool .port_ = (uint16_t )strtoul (getJsonStr (c, &t[idx + 2 ]).c_str (), NULL , 10 );
187+ pool .upPoolUserName_ = getJsonStr (c, &t[idx + 3 ]);
166188
167- poolConfs. push_back (conf );
189+ conf. pools_ . push_back (pool );
168190 }
169191 i += poolCount * 4 ;
170192 }
171193 else if (jsoneq (c, &t[i], " always_keep_downconn" ) == 0 ) {
172194 string opt = getJsonStr (c, &t[i+1 ]);
173195 std::transform (opt.begin (), opt.end (), opt.begin (), ::tolower);
174- alwaysKeepDownconn = (opt == " true" );
196+ conf. alwaysKeepDownconn_ = (opt == " true" );
175197 i++;
176198 }
177199 else if (jsoneq (c, &t[i], " disconnect_when_lost_asicboost" ) == 0 ) {
178200 string opt = getJsonStr (c, &t[i + 1 ]);
179201 std::transform (opt.begin (), opt.end (), opt.begin (), ::tolower);
180- disconnectWhenLostAsicBoost = (opt == " true" );
202+ conf. disconnectWhenLostAsicBoost_ = (opt == " true" );
181203 i++;
182204 }
183205 else if (jsoneq (c, &t[i], " use_ip_as_worker_name" ) == 0 ) {
184206 string opt = getJsonStr (c, &t[i + 1 ]);
185207 std::transform (opt.begin (), opt.end (), opt.begin (), ::tolower);
186- useIpAsWorkerName = (opt == " true" );
208+ conf.useIpAsWorkerName_ = (opt == " true" );
209+ i++;
210+ }
211+ else if (jsoneq (c, &t[i], " ip_worker_name_format" ) == 0 ) {
212+ conf.ipWorkerNameFormat_ = getJsonStr (c, &t[i+1 ]);
187213 i++;
188214 }
189215 else if (jsoneq (c, &t[i], " submit_response_from_server" ) == 0 ) {
190216 string opt = getJsonStr (c, &t[i + 1 ]);
191217 std::transform (opt.begin (), opt.end (), opt.begin (), ::tolower);
192- submitResponseFromServer = (opt == " true" );
218+ conf. submitResponseFromServer_ = (opt == " true" );
193219 i++;
194220 }
195221 else if (jsoneq (c, &t[i], " fixed_worker_name" ) == 0 ) {
196- fixedWorkerName = getJsonStr (c, &t[i+1 ]);
222+ conf. fixedWorkerName_ = getJsonStr (c, &t[i+1 ]);
197223 i++;
198224 }
199225 }
200226
201227 // check parametes
202- if (listenIP.length () && listenPort.length () && poolConfs.size ()) {
203- return true ;
228+ if (conf.listenIP_ .empty ()) {
229+ LOG (ERROR) << " [conf] agent_listen_ip cannot be empty." ;
230+ return false ;
231+ }
232+
233+ if (conf.listenPort_ == 0 ) {
234+ LOG (ERROR) << " [conf] agent_listen_port must be between 1 and 65535." ;
235+ return false ;
236+ }
237+
238+ if (conf.pools_ .empty ()) {
239+ LOG (ERROR) << " [conf] pools cannot be empty." ;
240+ return false ;
241+ }
242+
243+ for (size_t i=0 ; i<conf.pools_ .size (); i++) {
244+ const auto &pool = conf.pools_ [i];
245+ if (pool.host_ .empty ()) {
246+ LOG (ERROR) << " [conf] the host of pool " << i << " cannot be empty." ;
247+ return false ;
248+ }
249+ if (pool.port_ == 0 ) {
250+ LOG (ERROR) << " [conf] the port of pool " << i << " (" << pool.host_
251+ << " ) must be between 1 and 65535." ;
252+ return false ;
253+ }
204254 }
205255
206- return false ;
256+ return true ;
207257}
208258
209259const char *splitNotify (const string &line, int number) {
0 commit comments