-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher_challenge.cpp
executable file
·141 lines (108 loc) · 2.88 KB
/
dispatcher_challenge.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
138
139
140
141
#include <iostream>
#include <map>
#include <string>
#include <iostream>
//
// supporting tools and software
//
// Validate and test your json commands
// https://jsonlint.com/
// RapidJSON : lots and lots of examples to help you use it properly
// https://github.com/Tencent/rapidjson
//
// std::function
// std::bind
// std::placeholders
// std::map
// std::make_pair
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace rapidjson;
using namespace std;
bool g_done = false;
//
// TEST COMMANDS
//
auto help_command = R"(
{
"command":"help",
"payload": {
"usage":"Enter json command in 'command':'<command>','payload': { // json payload of arguments }",
}
}
)";
auto exit_command = R"(
{
"command":"exit",
"payload": {
"reason":"Exiting program on user request."
}
}
)";
class Controller {
public:
bool help(rapidjson::Value &payload)
{
cout << "Controller::help: command: ";
// implement
return true;
}
bool exit(rapidjson::Value &payload)
{
cout << "Controller::exit: command: \n";
// implement
return true;
}
// implement 3-4 more commands
};
// Bonus Question: why did I type cast this?
typedef std::function<bool(rapidjson::Value &)> CommandHandler;
class CommandDispatcher {
public:
// ctor - need impl
CommandDispatcher()
{
}
// dtor - need impl
virtual ~CommandDispatcher()
{
// question why is it virtual? Is it needed in this case?
}
bool addCommandHandler(std::string command, CommandHandler handler)
{
cout << "CommandDispatcher: addCommandHandler: " << command << std::endl;
// implement
return true;
}
bool dispatchCommand(std::string command_json)
{
cout << "COMMAND: " << command_json << endl;
// implement
return true;
}
private:
std::map<std::string, CommandHandler> command_handlers_;
// Question: why delete these?
// delete unused constructors
CommandDispatcher (const CommandDispatcher&) = delete;
CommandDispatcher& operator= (const CommandDispatcher&) = delete;
};
int main()
{
std::cout << "COMMAND DISPATCHER: STARTED" << std::endl;
CommandDispatcher command_dispatcher;
Controller controller; // controller class of functions to "dispatch" from Command Dispatcher
// Implement
// add command handlers in Controller class to CommandDispatcher using addCommandHandler
// command line interface for testing
string command;
while( ! g_done ) {
cout << "COMMANDS: {\"command\":\"exit\", \"payload\":{\"reason\":\"User requested exit.\"}}\n";
cout << "\tenter command : ";
getline(cin, command);
command_dispatcher.dispatchCommand(command);
}
std::cout << "COMMAND DISPATCHER: ENDED" << std::endl;
return 0;
}