forked from Rodrig79/Project-3-A-Simple-Network-File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.cpp
More file actions
410 lines (352 loc) · 9.38 KB
/
Shell.cpp
File metadata and controls
410 lines (352 loc) · 9.38 KB
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
// CPSC 3500: Shell
// Implements a basic shell (command line interface) for the file system
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cstdlib>
using namespace std;
#include "Shell.h"
#include "Blocks.h"
static const string PROMPT_STRING = "NFS> "; // shell prompt
// Mount the network file system with server name and port number in the format of server:port
void Shell::mountNFS(string fs_loc)
{
//create the socket cs_sock and connect it to the server and port specified in fs_loc
//if all the above operations are completed successfully, set is_mounted to true
//port should be 10160
//Parse fs_loc from Server:Port
unsigned parseIndex = fs_loc.find(":"); //finds index of where the colon appears
string ipAddressStr = fs_loc.substr(0,parseIndex);
string portStr = fs_loc.substr(parseIndex + 1, sizeof(fs_loc));
stringstream stringToInt(portStr);
int port = 0;
stringToInt >> port;
stringstream stringToInt2(ipAddressStr);
int ipAddress = 0;
stringToInt2 >> ipAddress;
//creating socket
int cs_sock = socket(AF_INET, SOCK_STREAM, 0);
//sock_stream = TCP socket
if (cs_sock < 0)
{
printf("Socket Creation Error\n");
}
else{
printf("Socket created successfully\n");
}
//specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = ipAddress;
if (connect(cs_sock, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
cerr << "Connection to remote socket failed." << endl;
}
else
{
printf("Connection to remote socket successful");
is_mounted = true;
}
}
// Unmount the network file system if it was mounted
void Shell::unmountNFS()
{
// close the socket if it was mounted
if (is_mounted)
{
close(cs_sock);
is_mounted = false;
}
}
// Remote procedure call on mkdir
void Shell::mkdir_rpc(string dname)
{
string message = "mkdir " + dname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on cd
void Shell::cd_rpc(string dname)
{
string message = "cd " + dname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on home
void Shell::home_rpc()
{
string message = "home\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on rmdir
void Shell::rmdir_rpc(string dname)
{
string message = "rmdir " + dname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on ls
void Shell::ls_rpc()
{
string message = "ls\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on create
void Shell::create_rpc(string fname)
{
string message = "create " + fname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on append
void Shell::append_rpc(string fname, string data)
{
// to implement
}
// Remote procesure call on cat
void Shell::cat_rpc(string fname)
{
string message = "cat " + fname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on head
void Shell::head_rpc(string fname, int n)
{
// to implement
}
// Remote procedure call on rm
void Shell::rm_rpc(string fname)
{
string message = "rm " + fname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Remote procedure call on stat
void Shell::stat_rpc(string fname)
{
string message = "stat " + fname + "\r\n";
char* server_message = &message[0];
send(cs_sock, server_message, sizeof(server_message), 0);
//TODO: Implement receive
return;
}
// Executes the shell until the user quits.
//Provided by Prof
void Shell::run()
{
// make sure that the file system is mounted
if (!is_mounted)
return;
// continue until the user quits
bool user_quit = false;
while (!user_quit)
{
// print prompt and get command line
string command_str;
cout << PROMPT_STRING;
getline(cin, command_str);
// execute the command
user_quit = execute_command(command_str);
}
// unmount the file system
unmountNFS();
}
// Execute a script.
//Provided by Prof
void Shell::run_script(char *file_name)
{
// make sure that the file system is mounted
if (!is_mounted)
return;
// open script file
ifstream infile;
infile.open(file_name);
if (infile.fail())
{
cerr << "Could not open script file" << endl;
return;
}
// execute each line in the script
bool user_quit = false;
string command_str;
getline(infile, command_str, '\n');
while (!infile.eof() && !user_quit)
{
cout << PROMPT_STRING << command_str << endl;
user_quit = execute_command(command_str);
getline(infile, command_str);
}
// clean up
unmountNFS();
infile.close();
}
// Executes the command. Returns true for quit and false otherwise.
//Provided by prof
bool Shell::execute_command(string command_str)
{
// parse the command line
struct Command command = parse_command(command_str);
// look for the matching command
if (command.name == "")
{
return false;
}
else if (command.name == "mkdir")
{
mkdir_rpc(command.file_name);
}
else if (command.name == "cd")
{
cd_rpc(command.file_name);
}
else if (command.name == "home")
{
home_rpc();
}
else if (command.name == "rmdir")
{
rmdir_rpc(command.file_name);
}
else if (command.name == "ls")
{
ls_rpc();
}
else if (command.name == "create")
{
create_rpc(command.file_name);
}
else if (command.name == "append")
{
append_rpc(command.file_name, command.append_data);
}
else if (command.name == "cat")
{
cat_rpc(command.file_name);
}
else if (command.name == "head")
{
errno = 0;
unsigned long n = strtoul(command.append_data.c_str(), NULL, 0);
if (0 == errno)
{
head_rpc(command.file_name, n);
}
else
{
cerr << "Invalid command line: " << command.append_data;
cerr << " is not a valid number of bytes" << endl;
return false;
}
}
else if (command.name == "rm")
{
rm_rpc(command.file_name);
}
else if (command.name == "stat")
{
stat_rpc(command.file_name);
}
else if (command.name == "quit")
{
return true;
}
return false;
}
// Parses a command line into a command struct. Returned name is blank
// for invalid command lines.
//Provided by Prof
Shell::Command Shell::parse_command(string command_str)
{
// empty command struct returned for errors
struct Command empty = {"", "", ""};
// grab each of the tokens (if they exist)
struct Command command;
istringstream ss(command_str);
int num_tokens = 0;
if (ss >> command.name)
{
num_tokens++;
if (ss >> command.file_name)
{
num_tokens++;
if (ss >> command.append_data)
{
num_tokens++;
string junk;
if (ss >> junk)
{
num_tokens++;
}
}
}
}
// Check for empty command line
if (num_tokens == 0)
{
return empty;
}
// Check for invalid command lines
if (command.name == "ls" ||
command.name == "home" ||
command.name == "quit")
{
if (num_tokens != 1)
{
cerr << "Invalid command line: " << command.name;
cerr << " has improper number of arguments" << endl;
return empty;
}
}
else if (command.name == "mkdir" ||
command.name == "cd" ||
command.name == "rmdir" ||
command.name == "create" ||
command.name == "cat" ||
command.name == "rm" ||
command.name == "stat")
{
if (num_tokens != 2)
{
cerr << "Invalid command line: " << command.name;
cerr << " has improper number of arguments" << endl;
return empty;
}
}
else if (command.name == "append" || command.name == "head")
{
if (num_tokens != 3)
{
cerr << "Invalid command line: " << command.name;
cerr << " has improper number of arguments" << endl;
return empty;
}
}
else
{
cerr << "Invalid command line: " << command.name;
cerr << " is not a command" << endl;
return empty;
}
return command;
}