-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·283 lines (256 loc) · 10.1 KB
/
main.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
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
#include <iostream>
#include <string>
#include <fstream>
#include "datagram.h"
#include "system_utilities.h"
#include "definitions.h"
#include "machines.h"
using namespace std;
ifstream* inp = new ifstream;
node* network[MAX_MACHINES];
int main() {
string* cmd_line = new string;
string* parsed_command = new string[MAX_TOKENS_ON_A_LINE];
int number_of_tokens;
int done;
int cmd;
int ec1, ec2, error_code;
IPAddress ip1, ip2, ip3;
string name = "";
string type = "";
int i, j, k, t1, t2, k1, k2;
datagram* d2 = NULL;
for (int i = 0; i < MAX_MACHINES; i++)
network[i] = NULL;
// Determine and set up source of input.
if(COMMANDS_FROM_FILE) {
(*inp).open("p8input.txt", ios::in);
if(!(*inp)) {
cout << "***Could not open file.";
return 0;
}
}
fillSystemCommandList();
done = 0;
do {
error_code = 0;
//Get command from input and parse into arg0 and argv
if(COMMANDS_FROM_FILE)
getline(*inp, *cmd_line);
else {
cout << ">"; // command line prompt
getline(cin, *cmd_line);//, MAX_CMD_LINE_LENGTH);
}
number_of_tokens = parseCommandLine(*cmd_line, parsed_command);
if(ECHO_COMMAND) print_token_list(number_of_tokens, parsed_command);
cmd = getCommandNumber(parsed_command[0]);
switch(cmd) {
case HALT: {
cout << "** Command HALT recognized" << endl;
done = 1;
break;
}
case SYSTEM_STATUS: {
cout << "** Command SYSTEM_STATUS recognized" << endl;
if (number_of_tokens == 1){
cout << "Nodes in the network:" << endl;
for(i=0; i<MAX_MACHINES; i++) {
if(network[i]!=NULL) {
network[i]->display();
cout << endl;
}
}
}else{
cout << "Selected nodes in the network:" << endl;
for (int j = 1; j < number_of_tokens; j++){
ip1.parse(parsed_command[j]);
for(i = 0; i < MAX_MACHINES; i++){
if (network[i] != NULL && (network[i]->myAddress().sameAddress(ip1))){
network[i]->display();
cout << endl;
}
}
}
}
cout << "End of network." << endl;
break;
}
case CREATE_MACHINE: {
cout << "** Command CREATE_MACHINE recognized" << endl;
type = parsed_command[1];
name = parsed_command[2];
error_code = ip3.parse(parsed_command[3]);
for (i = 0; i < MAX_MACHINES; i++)
{
if (network[i] == NULL)
break;
}
if (i >= MAX_MACHINES) {
error_code = NETWORK_FULL;
break;
}
if (error_code == 0)
{
if (parsed_command[1] == "laptop")
network[i] = new laptop(name, ip3);
else if (parsed_command[1] == "server")
network[i] = new server(name, ip3);
else if (parsed_command[1] == "wan")
network[i] = new WAN(name, ip3);
else
error_code = UNKNOWN_MACHINE_TYPE;
if (error_code == 0)
network[i]->display();
cout << endl;
}
break;
}
case DESTROY_MACHINE: {
cout << "** Command DESTROY_MACHINE recognized" << endl;
error_code = ip1.parse(parsed_command[1]);
if(error_code!=0) break;
for(i=0; i<MAX_MACHINES; i++) {
if(network[i]==NULL) continue;
if(network[i]->amIThisComputer(ip1)) {
/****************new****************/
if (network[i]->myType() == LAPTOP)
delete (laptop*)network[i];
else if (network[i]->myType() == SERVER)
delete (server*)network[i];
else if (network[i]->myType() == WAN_MACHINE)
delete (WAN*)network[i];
//delete network[i];
/****************new****************/
network[i] = NULL;
cout << "Computer removed from the network." << endl;
break;
}
}
if(i>=MAX_MACHINES) error_code = NO_SUCH_MACHINE;
break;
}
case CONNECT: {
cout << "** Command CONNECT recognized" << endl;
ec1 = ip1.parse(parsed_command[1]);
ec2 = ip2.parse(parsed_command[2]);
if ((ec1 != 0) || (ec2 != 0))
{
error_code = BAD_IP_ADDRESS;
break;
}
j = -1;
k = -1;
for(i = 0; i <MAX_MACHINES; i++)
{
if(network[i]==NULL) continue;
if(network[i]->amIThisComputer(ip1)) {
j = i; //j is index of machine1
} else if(network[i]->amIThisComputer(ip2)){
k = i; //k is index of machine2
}
}
if ((j == -1) || (k == -1))
{
error_code = NO_SUCH_MACHINE;
break;
}
t1 = network[j]->myType();
t2 = network[k]->myType();
k1 = network[j]->canAcceptConnection(t2);
k2 = network[k]->canAcceptConnection(t1);
if((k1==0)||(k2==0)) {
error_code = CONNECTION_REFUSED;
break;
}
network[j]->connect(ip2,t2);
network[k]->connect(ip1,t1);
break;
}
case CHECK_CONNECTIONS: {
cout << "** Command CHECK_CONNECTIONS recognized" << endl;
break;
}
case DATAGRAM_CMD: {
cout << "** Command DATAGRAM recognized" << endl;
error_code = ip1.parse(parsed_command[1]);
if(error_code!=0) break;
for(i=0; i<MAX_MACHINES; i++) {
if(network[i]==NULL) continue;
if(network[i]->amIThisComputer(ip1)) {
break;
}
}
if(i>=MAX_MACHINES) {
error_code = NO_SUCH_MACHINE;
break;
}
ec1 = ip1.parse(parsed_command[1]);
ec2 = ip2.parse(parsed_command[2]);
if((ec1!=0) || (ec2!=0)) error_code = BAD_IP_ADDRESS;
else {
d2 = new datagram;
d2->makeDatagram(ip1, ip2, parsed_command[3]);
d2->display();
((laptop*)network[i])->initiateDatagram(d2);
cout << endl;
}
break;
}
case CONSUME_DATAGRAM: {
cout << "** Command CONSUME_DATAGRAM recognized" << endl;
ip1.parse(parsed_command[1]);
for (i = 0; i < MAX_MACHINES; i++)
{
if (network[i] != NULL) {
if (network[i]->amIThisComputer(ip1)) ((laptop*)network[i])->consumeDatagram();
}
}
break;
}
case TIME_CLICK: {
cout << "** Command TIME_CLICK recognized" << endl;
for (i = 0; i < MAX_MACHINES; i++)
{
if (network[i] != NULL) {
if (network[i]->myType() == LAPTOP)
{
((laptop*)network[i])->transferDatagram();
}
if (network[i]->myType() == SERVER)
{
((server*)network[i])->transferDatagram();
}else ((WAN*)network[i])->transferDatagram();
}
}
break;
}
case UNDEFINED_COMMAND: {
cout << "***ERROR: Unrecognized command" << endl;
break;
}
}
if(error_code!=0) printError(error_code);
if(COMMANDS_FROM_FILE) wait();
cout << endl;
}
while(!done);
cout << "That's all, folks.";
freeSystemCommandList(); //deletes the list of command elements
delete[] parsed_command;
delete cmd_line;
delete inp;
//the following deletes any machines that were not "destroyed"
for (int i = 0; i < MAX_MACHINES; i++)
{
if (network[i] != NULL)
{
//delete network[i];
if (network[i]->myType() == LAPTOP)
delete (laptop*)network[i];
else if (network[i]->myType() == SERVER)
delete (server*)network[i];
else if (network[i]->myType() == WAN_MACHINE)
delete (WAN*)network[i];
}
}
}