forked from 2019202009ankush/Piazza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
372 lines (354 loc) · 10.3 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
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
#include<iostream>
#include<fstream>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sstream>
#include <vector>
#include "/home/manish/Piazza/include/rapidjson/document.h"
#define DOMAIN AF_INET
#define TYPE SOCK_STREAM
#define PROTOCOL 0
#define BUFF_SIZE 512*1024
#define LEVEL SOL_SOCKET
#define SET_OPTIONS SO_REUSEADDR
using namespace std;
using namespace rapidjson;
string create_json_string(vector<pair<string,string>> &data)
{
string json_string="{";
int i=0;
for(i=0;i<data.size()-1;i++)
{
json_string+="\n\t";
json_string+="\""+data[i].first+"\":\""+data[i].second+"\",";
}
json_string+="\n\t";
json_string+="\""+data[i].first+"\":\""+data[i].second+"\"";
json_string+="\n}";
return json_string;
}
int connection_establish(string ip,int port)
{
int option_set=1;
struct sockaddr_in server_addr;
int sock_fd=socket(DOMAIN,TYPE,PROTOCOL);
if(sock_fd<0)
{
cout<<"Cannot get FD for socket"<<endl;
exit(0);
}
//Setting to reuse socket again
//cout<<"Sock FD="<<sock_fd<<endl;
server_addr.sin_family=DOMAIN;
server_addr.sin_port=htons(port);
if(inet_pton(DOMAIN, ip.c_str(), &server_addr.sin_addr)<0)
{
cout<<"Problem in converting IP"<<endl;
}
int connect_stat=connect(sock_fd,(struct sockaddr *)&server_addr, sizeof(server_addr));
//cout<<"Connection Status = "<<connect_stat<<endl;
if(connect_stat<0)
{
return -1;
}
return sock_fd;
}
int recv_response(int sock_fd,string &response)
{
char buff[BUFF_SIZE]={0};
int valread = recv( sock_fd , buff, BUFF_SIZE,0);
if(valread<0)
{
cout<<"Error in Reading SYN ACK"<<endl;
}
response=buff;
return valread;
}
int send_command(int sock_fd,string command) //return 1 on successfully sending command
{
const void * data_send = command.c_str();
int send_stat=send(sock_fd,data_send,command.length(),0);
if(send_stat<0)
{
cout<<"Sending Error"<<endl;
}
return send_stat;
}
int logout(string username,int sock_fd) //On Successful Logout Return 1
{
}
int login(string key,string value,int sock_fd)
{
string send_data;
int stat=0;
vector<pair<string,string>> command;
command.push_back(make_pair("purpose","login"));
command.push_back(make_pair("username",key));
command.push_back(make_pair("password",value));
send_data = create_json_string(command);
while(send_command(sock_fd,send_data)<0);
while(recv_response(sock_fd,send_data)<0);
Document document;
document.Parse(send_data.c_str());
send_data = document["status"].GetString();
if(strcmp(send_data.c_str(),"success")==0)
{
cout<<"Login Sucess!!"<<endl;
stat=1;
}
else if(strcmp(send_data.c_str(),"invalid")==0)
{
cout<<"Enter Proper Password"<<endl;
stat=1;
}
else if(strcmp(send_data.c_str(),"failure")==0)
{
cout<<"Login Failed!!\nNo Such User"<<endl;
stat=1;
}
return stat;
}
int send_syn(int sock_fd) //return 1 on successfully sending syn packet
{
string data="{\n\t\"type\":\"client\"\n}";
const void * data_send = data.c_str();
int send_stat=send(sock_fd,data_send,data.length(),0);
if(send_stat<0)
{
cout<<"Sending Error"<<endl;
}
//cout<<"send_stat"<<send_stat<<endl;
char buff[BUFF_SIZE]={0};
//cout<<"Reading Chunck Size"<<endl;
int valread = recv( sock_fd , buff, BUFF_SIZE,0);
if(valread<0)
{
cout<<"Error in Reading SYN ACK"<<endl;
}
string response_recv=buff;
Document document;
document.Parse(response_recv.c_str());
data = document["status"].GetString();
if(strcmp(data.c_str(),"connected")==0)
{
send_stat=1;
}
else
send_stat=0;
//cout<<"Connection Status Received is:"<<response_recv<<endl;
return send_stat;
}
int get_data(string key,int sock_fd,string &value)
{
vector<pair<string,string>> command;
command.push_back(make_pair("purpose","get"));
command.push_back(make_pair("key",key));
key = create_json_string(command);
while(send_command(sock_fd,key)<0);
while(recv_response(sock_fd,key)<0);
Document document;
document.Parse(key.c_str());
value = document["value"].GetString();
if(strcmp(value.c_str(),"failure")==0)
{
cout<<"Get Failure!!"<<endl;
}
else
{
cout<<"Value is:"<<value<<endl;
}
return 1;
}
int create_user(string key,string value,int sock_fd)
{
vector<pair<string,string>> command;
command.push_back(make_pair("purpose","create_user"));
command.push_back(make_pair("username",key));
command.push_back(make_pair("password",value));
key = create_json_string(command);
while(send_command(sock_fd,key)<0);
while(recv_response(sock_fd,key)<0);
Document document;
document.Parse(key.c_str());
value = document["value"].GetString();
if(strcmp(value.c_str(),"exists")==0)
{
cout<<"User Already Exist!!"<<endl;
}
else if(strcmp(value.c_str(),"success")==0)
{
cout<<"User Created Successfully!!"<<endl;
}
return 1;
}
int delete_data(string key,int sock_fd)
{
vector<pair<string,string>> command;
command.push_back(make_pair("purpose","delete"));
command.push_back(make_pair("key",key));
command.push_back(make_pair("addAs","-1"));
key = create_json_string(command);
while(send_command(sock_fd,key)<0);
while(recv_response(sock_fd,key)<0);
Document document;
document.Parse(key.c_str());
key = document["value"].GetString();
if(strcmp(key.c_str(),"success")==0)
{
cout<<"SuccessFully Deleted!!"<<endl;
}
else if(strcmp(key.c_str(),"failure")==0)
{
cout<<"Delete Failed!!"<<endl;
}
else if(strcmp(key.c_str(),"nexists")==0)
{
cout<<"Key Does Not Exists!!"<<endl;
}
return 1;
}
int put_data(int sock_fd,string key,string value)
{
vector<pair<string,string>> command;
command.push_back(make_pair("purpose","put"));
command.push_back(make_pair("key",key));
command.push_back(make_pair("value",value));
command.push_back(make_pair("addAs","-1"));
key = create_json_string(command);
while(send_command(sock_fd,key)<0);
while(recv_response(sock_fd,key)<0);
Document document;
document.Parse(key.c_str());
key = document["value"].GetString();
if(strcmp(key.c_str(),"success")==0)
{
cout<<"Tuple Added!!"<<endl;
return 1;
}
else if(strcmp(key.c_str(),"exists")==0)
{
cout<<"Tuple with given key already exists!!"<<endl;
return 1;
}
else
{
cout<<"Error occured while inserting"<<endl;
return 1;
}
}
int update_data(int sock_fd,string key,string value)
{
vector<pair<string,string>> command;
command.push_back(make_pair("purpose","update"));
command.push_back(make_pair("key",key));
command.push_back(make_pair("value",value));
command.push_back(make_pair("addAs","-1"));
key = create_json_string(command);
while(send_command(sock_fd,key)<0);
while(recv_response(sock_fd,key)<0);
Document document;
document.Parse(key.c_str());
key = document["value"].GetString();
if(strcmp(key.c_str(),"exists")==0)
{
cout<<"Tuple Updated!!"<<endl;
return 1;
}
else if(strcmp(key.c_str(),"added")==0)
{
cout<<"Tuple Added!!"<<endl;
return 1;
}
else
{
cout<<"Error occured while updating!!"<<endl;
return 1;
}
}
int main()
{
string path,ip,command,username,key,value;
int port,sock_fd;
cout<<"Enter Config File Path:"<<endl;
cin>>path;
fstream details(path);
details>>ip;
port = stoi(ip);
details>>ip;
//cout<<"IP is:"<<ip<<" Port is:"<<port<<endl;
sock_fd=connection_establish(ip,port);
//cout<<"Sock_fd"<<sock_fd<<endl;
if(sock_fd<0)
{
//Connection Establish Failed
cout<<"Problem in Connection Establishment"<<endl;
return 1;
}
/*Send SYN Packet*/
while(send_syn(sock_fd)<0);
/*Send SYN Packet*/
//Connection Properly Established
cout<<"Enter Command"<<endl;
getline(cin,command);
while(strcmp("logout",command.c_str())!=0)
{
/*Command Processing*/
if(command.compare(0,5,"login")==0) //login username password
{
/*Record UserName*/
stringstream get_user(command);
getline(get_user, username, ' ');
getline(get_user, username, ' ');
key=username;
getline(get_user, value, ' ');
//cout<<"UserName is:"<<username<<endl;
/*Record UserName*/
while(login(key,value,sock_fd)!=1);
}
else if(command.compare(0,11,"create_user")==0) //create_user username password
{
stringstream get_user(command);
getline(get_user, key, ' ');
getline(get_user, key, ' ');
getline(get_user, value, ' ');
while(create_user(key,value,sock_fd)!=1);
}
else if(command.compare(0,3,"get")==0) //get key
{
stringstream get_user(command);
getline(get_user, key, ' ');
getline(get_user, key, ' ');
while(get_data(key,sock_fd,value)!=1);
}
else if(command.compare(0,3,"put")==0) //put key value
{
stringstream get_user(command);
getline(get_user, key, ' ');
getline(get_user, key, ' ');
getline(get_user, value, ' ');
while(put_data(sock_fd,key,value)!=1);
}
else if(command.compare(0,6,"update")==0) //update key new_value
{
stringstream get_user(command);
getline(get_user,key,' ');
getline(get_user,key,' ');
getline(get_user,value,' ');
while(update_data(sock_fd,key,value)!=1);
}
else if(command.compare(0,6,"delete")==0) //delete key
{
stringstream get_user(command);
getline(get_user, key, ' ');
getline(get_user, key, ' ');
while(delete_data(key,sock_fd)!=1);
}
/*Command Processing*/
cout<<"Enter Command"<<endl;
getline(cin,command);
}
/*Logout Function*/
while(logout(username,sock_fd)!=1);
/*Logout Function*/
return 0;
}