-
Notifications
You must be signed in to change notification settings - Fork 2
/
command.h
749 lines (678 loc) · 19 KB
/
command.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#pragma once
#include <map>
#include <vector>
#include <memory>
#include <sstream>
#include <functional>
#include <unistd.h>
#include <math.h>
#include "command_receiver.h"
#include "command_sender.h"
#include "netsnoop.h"
#define MAX_CMD_LENGTH 1024
#define MAX_TOKEN_LENGTH 10
// time in microseconds wait to give a chance for client receive all data
#define STOP_WAIT_TIME 500*1000
// use as an identity of a main command
#define VISIABLE_LATTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
class CommandFactory;
class Command;
class NetStat;
extern std::map<std::string, int> g_cmd_map;
using CommandCallback = std::function<void(const Command *, std::shared_ptr<NetStat>)>;
#pragma region CommandFactory
using CommandArgs = std::map<std::string, std::string>;
using Ctor = CommandFactory *;
using CommandContainer = std::map<std::string, Ctor>;
class CommandFactory
{
public:
static std::shared_ptr<Command> New(const std::string &cmd)
{
ASSERT_RETURN(cmd.length() <= MAX_CMD_LENGTH - MAX_TOKEN_LENGTH, NULL, "cmd too long.");
CommandArgs args;
std::stringstream ss(cmd);
std::string name, key, value;
ss >> name;
if (Container().find(name) == Container().end())
{
LOGWP("illegal command: %s", cmd.c_str());
return NULL;
}
while (ss >> key)
{
if (ss >> value)
{
ASSERT_RETURN(args.find(key) == args.end(),NULL);
args[key] = value;
}
else
{
ASSERT_RETURN(!value.empty(),NULL);
args[key] = "";
}
}
return Container()[name]->NewCommand(cmd, args);
}
protected:
static CommandContainer &Container()
{
static CommandContainer commands;
return commands;
}
virtual std::shared_ptr<Command> NewCommand(const std::string &cmd, CommandArgs args) = 0;
};
template <class DerivedType>
class CommandRegister : public CommandFactory
{
public:
CommandRegister(const std::string &name) : CommandRegister(name, false) {}
CommandRegister(const std::string &name, bool is_private) : name_(name), is_private_(is_private)
{
ASSERT(Container().find(name) == Container().end());
LOGVP("register command: %s", name.c_str());
Container()[name] = this;
}
std::shared_ptr<Command> NewCommand(const std::string &cmd, CommandArgs args) override
{
auto command = std::make_shared<DerivedType>(cmd);
command->is_private = is_private_;
if (command->ResolveArgs(args))
{
LOGDP("create new command: %s (%s)", command->GetCmd().c_str(),command->ToString().c_str());
return command;
}
return NULL;
}
private:
const std::string name_;
bool is_private_;
};
#pragma endregion
#pragma region NetStat
/**
* @brief Network State
*
*/
struct NetStat
{
/**
* @brief Network delay in millseconds
*
*/
int delay;
int max_delay;
int min_delay;
/**
* @brief Jitter in millseconds
*
*/
int jitter;
/**
* @brief The standard deviation of jitter
*
*/
long long jitter_std;
/**
* @brief Packet loss percent
*
*/
double loss;
/**
* @brief Send/Recv packets count
*
*/
long long send_packets;
long long recv_packets;
/**
* @brief Recved illegal packets count
*
*/
long long illegal_packets;
long long reorder_packets;
long long duplicate_packets;
/**
* @brief The packets that stay too long in the network.
*
*/
long long timeout_packets;
/**
* @brief Send/Recv data length
*
*/
long long send_bytes;
long long recv_bytes;
/**
* @brief Command send/recv time in millseconds
*
*/
int send_time;
int recv_time;
/**
* @brief send/recv speed in Byte/s
*
*/
long long send_speed;
long long min_send_speed;
long long max_send_speed;
long long recv_speed;
long long min_recv_speed;
long long max_recv_speed;
/**
* @brief send/recv packets per second
*
*/
long long send_pps;
long long recv_pps;
/*******************************************************************/
/********** The below properties are arithmetic property. **********/
/**
* @brief average recv speed
*
*/
long long recv_avg_speed;
long long send_avg_speed;
int max_send_time;
int min_send_time;
int max_recv_time;
int min_recv_time;
/**
* @brief the peers count when the command start
*
*/
int peers_count;
/**
* @brief the peers count without the faled peers.
*
*/
int peers_failed;
std::string ToString() const
{
bool istty = isatty(fileno(stdout));
std::stringstream ss;
#define W(p) \
if (!istty || p != 0) \
ss << #p " " << p << " "
W(loss);
W(send_speed);
W(recv_speed);
W(send_avg_speed);
W(recv_avg_speed);
W(max_send_speed);
W(max_recv_speed);
W(min_send_speed);
W(min_recv_speed);
W(send_packets);
W(recv_packets);
W(illegal_packets);
W(reorder_packets);
W(duplicate_packets);
W(timeout_packets);
W(send_pps);
W(recv_pps);
W(send_bytes);
W(recv_bytes);
W(send_time);
W(recv_time);
W(max_send_time);
W(max_recv_time);
W(min_send_time);
W(min_recv_time);
W(delay);
W(min_delay);
W(max_delay);
W(jitter);
W(jitter_std);
W(peers_count);
W(peers_failed);
#undef W
return ss.str();
}
void FromCommandArgs(CommandArgs &args)
{
#define RI(p) p = atoi(args[#p].c_str())
#define RLL(p) p = atoll(args[#p].c_str())
#define RF(p) p = atof(args[#p].c_str())
RF(loss);
RLL(send_speed);
RLL(recv_speed);
RLL(send_avg_speed);
RLL(recv_avg_speed);
RLL(max_send_speed);
RLL(max_recv_speed);
RLL(min_send_speed);
RLL(min_recv_speed);
RLL(send_packets);
RLL(recv_packets);
RLL(illegal_packets);
RLL(reorder_packets);
RLL(duplicate_packets);
RLL(timeout_packets);
RLL(send_pps);
RLL(recv_pps);
RLL(send_bytes);
RLL(recv_bytes);
RI(send_time);
RI(recv_time);
RI(max_send_time);
RI(max_recv_time);
RI(min_send_time);
RI(min_recv_time);
RI(delay);
RI(min_delay);
RI(max_delay);
RI(jitter);
RLL(jitter_std);
RI(peers_count);
RI(peers_failed);
#undef RI
#undef RLL
#undef RF
}
// TODO: refactor the code to simplify the logic of 'arithmetic property'.
NetStat &operator+=(const NetStat &stat)
{
#define INT(p) p = p + stat.p
#define DOU(p) INT(p)
#define MAX(p) p = std::max(p, stat.p)
#define MIN(p) p = std::min(p, stat.p)
DOU(loss);
INT(send_speed);
INT(recv_speed);
INT(send_avg_speed);
INT(recv_avg_speed);
MAX(max_send_speed);
MAX(max_recv_speed);
MIN(min_send_speed);
MIN(min_recv_speed);
INT(send_packets);
INT(recv_packets);
INT(illegal_packets);
INT(reorder_packets);
INT(duplicate_packets);
INT(timeout_packets);
INT(send_pps);
INT(recv_pps);
INT(send_bytes);
INT(recv_bytes);
INT(delay);
MIN(min_delay);
MAX(max_delay);
INT(jitter);
INT(jitter_std);
INT(send_time);
INT(recv_time);
MAX(max_send_time);
MAX(max_recv_time);
MIN(min_send_time);
MIN(min_recv_time);
INT(peers_count);
INT(peers_failed);
#undef INT
#undef DOU
#undef MAX
#undef MIN
return *this;
}
NetStat &operator/=(int num)
{
#define INT(p) p /= num
#define DOU(p) INT(p)
#define MAX(p) p = p * 1
#define MIN(p) p = p * 1
DOU(loss);
INT(send_speed);
INT(recv_speed);
INT(send_avg_speed);
INT(recv_avg_speed);
MAX(max_send_speed);
MAX(max_recv_speed);
MIN(min_send_speed);
MIN(min_recv_speed);
INT(send_packets);
INT(recv_packets);
INT(illegal_packets);
INT(reorder_packets);
INT(duplicate_packets);
INT(timeout_packets);
INT(send_pps);
INT(recv_pps);
INT(send_bytes);
INT(recv_bytes);
INT(delay);
MIN(min_delay);
MAX(max_delay);
INT(jitter);
INT(jitter_std);
INT(send_time);
INT(recv_time);
MAX(max_send_time);
MAX(max_recv_time);
MIN(min_send_time);
MIN(min_recv_time);
INT(peers_count);
INT(peers_failed);
#undef INT
#undef DOU
#undef MAX
#undef MIN
return *this;
}
};
#pragma endregion
struct CommandChannel
{
std::shared_ptr<Command> command_;
std::shared_ptr<Context> context_;
std::shared_ptr<Sock> control_sock_;
std::shared_ptr<Sock> data_sock_;
};
/**
* @brief A command stands for a type of network test.
*
*/
class Command
{
public:
// TODO: optimize command structure to simplify sub command.
Command(std::string name, std::string cmd)
: name(name), cmd(cmd), token('$'),
is_private(false), is_multicast(false)
{
}
void RegisterCallback(CommandCallback callback)
{
if (callback)
callbacks_.push_back(callback);
}
void InvokeCallback(std::shared_ptr<NetStat> netstat)
{
for (auto &callback : callbacks_)
{
callback(this, netstat);
}
}
virtual bool ResolveArgs(CommandArgs args) { return true; }
virtual std::shared_ptr<CommandSender> CreateCommandSender(std::shared_ptr<CommandChannel> channel) { return NULL; }
virtual std::shared_ptr<CommandReceiver> CreateCommandReceiver(std::shared_ptr<CommandChannel> channel) { return NULL; }
virtual int GetWait() { return STOP_WAIT_TIME; }
std::string GetCmd() const
{
return (cmd.find(" token") != std::string::npos || token == '$') ? cmd : cmd + " token " + token;
}
virtual std::string ToString() const
{
return GetCmd();
};
std::string name;
bool is_private;
bool is_multicast;
char token;
protected:
void UpdateToken()
{
static unsigned char index = 0;
token = VISIABLE_LATTERS[index++ % (sizeof(VISIABLE_LATTERS) - 1)];
}
private:
std::string cmd;
std::vector<CommandCallback> callbacks_;
DISALLOW_COPY_AND_ASSIGN(Command);
};
struct DataHead
{
// time since epoch in nanoseconds
int64_t timestamp : 64;
// sequence number
uint16_t sequence : 16;
// data length
uint16_t length : 16;
// token used for data validation
char token;
};
#define ECHO_DEFAULT_COUNT 5
#define ECHO_DEFAULT_INTERVAL 200*1000
#define ECHO_DEFAULT_SIZE 32
#define ECHO_DEFAULT_WAIT 500*1000
#define ECHO_DEFAULT_TIMEOUT 100 // milliseconds
#define ECHO_DEFAULT_SPEED 0 // KByte/s
#define ECHO_DEFAULT_TIME 0*1000 // milliseconds
/**
* @brief a main command, server send to client and client should echo
*
*/
class EchoCommand : public Command
{
public:
// format: ping [count <num>] [interval <num>] [size <num>]
// example: ping count 10 interval 100
EchoCommand(std::string cmd)
: count_(ECHO_DEFAULT_COUNT),
interval_(ECHO_DEFAULT_INTERVAL),
size_(ECHO_DEFAULT_SIZE),
wait_(ECHO_DEFAULT_WAIT),
Command("ping", cmd)
{
UpdateToken();
}
bool ResolveArgs(CommandArgs args) override
{
// TODO: optimize these assign.
count_ = args["count"].empty() ? ECHO_DEFAULT_COUNT : std::stoi(args["count"]);
interval_ = args["interval"].empty() ? ECHO_DEFAULT_INTERVAL : std::stod(args["interval"]) * 1000;
size_ = args["size"].empty() ? ECHO_DEFAULT_SIZE : std::stoi(args["size"]);
wait_ = args["wait"].empty() ? ECHO_DEFAULT_WAIT : std::stoi(args["wait"])*1000;
timeout_ = args["timeout"].empty() ? ECHO_DEFAULT_TIMEOUT : std::stoi(args["timeout"]);
if (!args["token"].empty())
token = args["token"].at(0);
auto speed = args["speed"].empty() ? ECHO_DEFAULT_SPEED : std::stoi(args["speed"]);
auto time = args["time"].empty() ? ECHO_DEFAULT_TIME : std::stoi(args["time"]);
if (speed > 0 && time > 0)
{
if(size_ == ECHO_DEFAULT_SIZE) size_ = 1472;
count_ = ceil((speed * 1024) * (time / 1000.0) / size_);
interval_ = 1000000/((1.0*speed*1024)/size_);
}
else if(interval_ > 0 && time > 0)
{
count_ = time * 1000.0 / interval_;
}
// echo can not have zero delay
if (interval_ <= 0)
interval_ = ECHO_DEFAULT_INTERVAL;
return true;
}
std::shared_ptr<CommandSender> CreateCommandSender(std::shared_ptr<CommandChannel> channel) override
{
return std::make_shared<EchoCommandSender>(channel);
}
std::shared_ptr<CommandReceiver> CreateCommandReceiver(std::shared_ptr<CommandChannel> channel) override
{
return std::make_shared<EchoCommandReceiver>(channel);
}
std::string ToString() const override
{
std::stringstream out;
out << name << " count " << count_ << " interval " << interval_/1000.0 << " size " << size_ << " wait " << wait_/1000.0 << " timeout " << timeout_;
return out.str();
}
int GetCount() { return count_; }
int GetInterval() { return interval_; }
int GetSize() { return size_; }
int GetWait() override { return wait_; }
int GetTimeout() { return timeout_; }
private:
int count_;
int interval_;
int size_;
int wait_;
int timeout_;
DISALLOW_COPY_AND_ASSIGN(EchoCommand);
};
#define SEND_DEFAULT_COUNT 100
#define SEND_DEFAULT_INTERVAL 0*1000 // microseconds
#define SEND_DEFAULT_SIZE 1472
#define SEND_DEFAULT_WAIT 500*1000 // microseconds
#define SEND_DEFAULT_TIMEOUT 100 // milliseconds
#define SEND_DEFAULT_SPEED 0 // KByte/s
#define SEND_DEFAULT_TIME 3000 // milliseconds
/**
* @brief a main command, server send data only and client recv only.
*
*/
class SendCommand : public Command
{
public:
SendCommand(std::string cmd)
: count_(SEND_DEFAULT_COUNT),
interval_(SEND_DEFAULT_INTERVAL),
size_(SEND_DEFAULT_SIZE),
wait_(SEND_DEFAULT_WAIT),
timeout_(SEND_DEFAULT_TIMEOUT),
is_finished(false), Command("send", cmd)
{
UpdateToken();
}
bool ResolveArgs(CommandArgs args) override
{
// TODO: optimize these assign.
count_ = args["count"].empty() ? SEND_DEFAULT_COUNT : std::stoi(args["count"]);
interval_ = args["interval"].empty() ? SEND_DEFAULT_INTERVAL : std::stod(args["interval"]) * 1000;
size_ = args["size"].empty() ? SEND_DEFAULT_SIZE : std::stoi(args["size"]);
wait_ = args["wait"].empty() ? SEND_DEFAULT_WAIT : std::stoi(args["wait"]) * 1000;
timeout_ = args["timeout"].empty() ? SEND_DEFAULT_TIMEOUT : std::stoi(args["timeout"]);
if (!args["token"].empty())
token = args["token"].at(0);
is_multicast = !args["multicast"].empty();
if (is_multicast)
LOGDP("enable multicast.");
ASSERT(size_>=sizeof(DataHead));
auto speed = args["speed"].empty() ? SEND_DEFAULT_SPEED : std::stoi(args["speed"]);
auto time = args["time"].empty() ? SEND_DEFAULT_TIME : std::stoi(args["time"]);
if (speed > 0 && time > 0)
{
count_ = ceil((speed * 1024) * (time / 1000.0) / size_);
interval_ = 1000000/((1.0*speed*1024)/size_);
}
else if(interval_ > 0 && time > 0)
{
count_ = time * 1000.0 / interval_;
}
return true;
}
std::shared_ptr<CommandSender> CreateCommandSender(std::shared_ptr<CommandChannel> channel) override
{
return std::make_shared<SendCommandSender>(channel);
}
std::shared_ptr<CommandReceiver> CreateCommandReceiver(std::shared_ptr<CommandChannel> channel) override
{
return std::make_shared<SendCommandReceiver>(channel);
}
std::string ToString() const override
{
std::stringstream out;
out << name << " count " << count_ << " interval " << interval_/1000.0 << " size " << size_ << " wait " << wait_/1000.0 << " timeout " << timeout_;
return out.str();
}
int GetCount() { return count_; }
/**
* @brief Get the Interval object in microseconds
*
* @return int
*/
int GetInterval() { return interval_; }
int GetSize() { return size_; }
/**
* @brief Get the Wait object in microseconds
*
* @return int
*/
int GetWait() override { return wait_; }
/**
* @brief Get the Timeout object in milliseconds
*
* @return int
*/
int GetTimeout() { return timeout_; }
bool is_finished;
private:
int count_;
int interval_;
int size_;
int wait_;
int timeout_;
DISALLOW_COPY_AND_ASSIGN(SendCommand);
};
// #define DEFINE_COMMAND(name,typename) \
// class typename : public Command \
// {\
// public:\
// typename(std::string cmd):Command(#name,cmd){}\
// }
/**
* @brief every client should response a ack command to a main command
*
*/
class AckCommand : public Command
{
public:
AckCommand() : AckCommand("ack") {}
AckCommand(std::string cmd) : Command("ack", cmd) {}
DISALLOW_COPY_AND_ASSIGN(AckCommand);
};
/**
* @brief notify client the command has finished.
*
*/
class StopCommand : public Command
{
public:
StopCommand() : StopCommand("stop") {}
StopCommand(std::string cmd) : Command("stop", cmd) {}
DISALLOW_COPY_AND_ASSIGN(StopCommand);
};
/**
* @brief send the test result to server
*
*/
class ResultCommand : public Command
{
public:
ResultCommand() : ResultCommand("result") {}
ResultCommand(std::string cmd) : Command("result", cmd) {}
bool ResolveArgs(CommandArgs args) override
{
netstat = std::make_shared<NetStat>();
netstat->FromCommandArgs(args);
return true;
}
std::string Serialize(const NetStat &netstat)
{
return name + " " + netstat.ToString();
}
std::shared_ptr<NetStat> netstat;
DISALLOW_COPY_AND_ASSIGN(ResultCommand);
};
class ModeCommand : public Command
{
public:
enum class ModeType
{
None,
UDP,
Multicast //TODO: TCP
};
ModeCommand() : ModeCommand("mode") {}
ModeCommand(std::string cmd) : mode_(ModeType::None), Command("mode", cmd) {}
bool ResolveArgs(CommandArgs args) override
{
mode_ = !args["udp"].empty() ? ModeType::UDP : !args["multicast"].empty() ? ModeType::Multicast : ModeType::None;
return mode_ != ModeType::None;
}
ModeType GetModeType()
{
return mode_;
}
private:
ModeType mode_;
};