-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkbcommands.c
90 lines (82 loc) · 2.54 KB
/
kbcommands.c
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
#include "kbcommands.h"
#include "communicator.h"
#include "datastructure.h"
enum Commands Server_Commands[] = {HELP,CREATOR,DISPLAY,LIST,QUIT};
enum Commands Client_Commands[] = {HELP,CREATOR,DISPLAY,REGISTER,CONNECT,LIST,TERMINATE,QUIT,GET,PUT,SYNC};
char commandPatterns[CMDCOUNT][500];
int canExecute(int isClient, enum Commands cmd)
{
enum Commands *arr;
int i=0, size=0;;
if(isClient)
{
arr = &Client_Commands[0];
size = ARLEN(Client_Commands);
}
else
{
arr = &Server_Commands[0];
size = ARLEN(Server_Commands);
}
while(i<size)
{
if(*(arr+i)==cmd)
{
return EXIT_SUCCESS;
}
i++;
}
return EXIT_FAILURE;
}
void prepareCommandPatterns()
{
strcpy(commandPatterns[HELP], "^HELP\n");
strcpy(commandPatterns[CREATOR], "^CREATOR\n");
strcpy(commandPatterns[DISPLAY], "^DISPLAY\n");
strcpy(commandPatterns[REGISTER], "^REGISTER (.+) ([0-9]+)\n");
strcpy(commandPatterns[CONNECT], "^CONNECT (.+) ([0-9]+)\n");
strcpy(commandPatterns[LIST], "^LIST\n");
strcpy(commandPatterns[TERMINATE], "^TERMINATE ([0-9]+)\n");
strcpy(commandPatterns[QUIT], "^QUIT\n");
strcpy(commandPatterns[GET], "^GET ([0-9]+) (.+)\n");
strcpy(commandPatterns[PUT], "^PUT ([0-9]+) (.+)\n");
strcpy(commandPatterns[SYNC], "^SYNC\n");
}
void doCreator()
{
printf("Name: Kushal Bhandari\nUBIT name: kbhandar\nUB email: kbhandar@buffalo.edu\n");
}
void doList()
{
struct connectionInfo *itr;
if(isClient == 1)
{
itr = peerliststartPtr;
}
else
{
itr = serverliststartPtr;
}
printf("id:\tHostname\t\t\tIP address\tPort No.\n");
int i = 1;
while(itr!=NULL)
{
printf("%d\t%s\t%s\t%s\n",i,itr->fqdn,itr->clientAddress,itr->portNo);
itr = itr->next;
i+=1;
}
}
void doHelp()
{
printf("HELP: Displays list of available commands\n");
printf("CREATOR: Displays author information\n");
printf("DISPLAY: Displays the IP and port of this process\n");
printf("REGISTER <server IP> <port>: Registers this client to the server\n");
printf("CONNECT <destination> <port>: Connects as a peer to the specified address\n");
printf("LIST: Displays a numbered list of all connected connections on this process\n");
printf("TERMINATE <id>: Terminates a connection from the available list of connections\n");
printf("QUIT: Closes all connections and terminates this process\n");
printf("GET <connection_id> <file>: Download the specified file from the host\n");
printf("PUT <connection_id> <file>: Uploads the specified file to the host\n");
printf("SYNC: Synchronizes host specific text files across its peers\n");
}