-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
executable file
·120 lines (104 loc) · 3.42 KB
/
client.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
#include "frontend.h"
#include "replicas.h"
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
using namespace mp2;
using namespace std;
using boost::shared_ptr;
int main(int argc, char **argv) {
shared_ptr<Replicas> replicas(new Replicas(argc, argv));
FrontEnd frontEnd(replicas);
/*try {
shared_ptr<StateMachine> machine = frontEnd.create("testmachine", "initstate");
cout << machine->getState() << endl;
cout << machine->apply("testop") << endl;
// (*replicas)[0].exit();
cout << machine->getState() << endl;
frontEnd.remove("testmachine");
cout << "Prebuilt tests done!" << endl;
} catch (ReplicaError e) {
cerr << e.message << endl;
}*/
// get user input
// Summary of possible inputs:
// create new machine: create [name] [initial_state]
// get current state: get [name]
// apply operation: apply [name] [operation]
// remove machine: remove [name]
string input;
map<string, shared_ptr<StateMachine> > machines;
while(1) {
try {
// receive input from terminal
getline(cin, input);
// parse inputs
vector<string> args;
istringstream iss(input);
do {
string sub;
iss >> sub;
if(sub != "")
args.push_back(sub);
} while (iss);
// do operations and loop
if(args.size() < 1)
continue;
// create new machine: create [name] [initial_state]
if(args[0] == "create") {
if(args.size() != 3) {
cout << "Expected 3 arguments" << endl;
}else{
cout << "Creating machine " << args[1] << endl;
shared_ptr<StateMachine> temp = frontEnd.create(args[1], args[2]);
machines.insert(pair<string, shared_ptr<StateMachine> >(args[1], temp));
}
// get current state: get [name]
}else if(args[0] == "get") {
if(args.size() != 2) {
cout << "Expected 2 arguments" << endl;
}else{
cout << "Getting state from machine " << args[1] << endl;
if(machines.find(args[1]) == machines.end()) {
shared_ptr<StateMachine> temp = frontEnd.get(args[1]);
machines.insert(pair<string, shared_ptr<StateMachine> >(args[1], temp));
}
cout << machines[args[1]]->getState() << endl;
}
// apply operation: apply [name] [operation]
}else if(args[0] == "apply") {
if(args.size() != 3) {
cout << "Expected 3 arguments" << endl;
}else{
cout << "Applying operation " << args[2] << " to machine " << args[1] << endl;
if(machines.find(args[1]) == machines.end()) {
shared_ptr<StateMachine> temp = frontEnd.get(args[1]);
machines.insert(pair<string, shared_ptr<StateMachine> >(args[1], temp));
}
cout << machines[args[1]]->apply(args[2]) << endl;
}
// remove machine: remove [name]
}else if(args[0] == "remove") {
if(args.size() != 2) {
cout << "Expected 2 arguments" << endl;
break;
}
cout << "Removing machine " << args[1] << endl;
if(machines.find(args[1]) == machines.end()) {
shared_ptr<StateMachine> temp = frontEnd.get(args[1]);
machines.insert(pair<string, shared_ptr<StateMachine> >(args[1], temp));
}
frontEnd.remove(args[1]);
// invalid inputs
}else if(args[0] == "exit") {
return 0;
}else{
cout << "Invalid arguments" << endl;
}
} catch (ReplicaError e) {
cerr << e.message << endl;
}
}
return 0;
}