-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
197 lines (141 loc) · 4.77 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
#include<bits/stdc++.h>
#include "COMMON_FUNCN.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "jsonstring.h"
using namespace rapidjson;
using namespace std;
#define FILENAME "cs_config.txt"
//**************************************do get and delete together*****************************************//
void request_for_get_delete(string type,string key,int sock_fd)
{
Document document1;
send_message(sock_fd,get_delete_CS(type,key));
string value_json=receive_message(sock_fd);
cout<<"recvd msg: "<<value_json<<endl;
if(document1.ParseInsitu((char*)value_json.c_str()).HasParseError())
{
cout<<"error in request for client parsing string"<<endl;
}
else if(strcmp(document1["req_type"].GetString(),"data")==0)
{
assert(document1.IsObject());
assert(document1.HasMember("req_type"));
assert(document1.HasMember("message"));
string value=document1["message"].GetString();
cout<<"value for the "<<key<<" is "<<value<<endl;
}
else if(strcmp(document1["req_type"].GetString(),"ack")==0)
{
assert(document1.IsObject());
assert(document1.HasMember("req_type"));
assert(document1.HasMember("message"));
string value=document1["message"].GetString();
cout<<value<<endl;
}
}
//*************************************************do put and update together**************************************//
void request_for_update_put(string type,string key,string value, int sock_fd)
{
Document document1;
send_message(sock_fd,put_update_CS(type,key,value));
string value_json=receive_message(sock_fd);// 1) ack+no_slave_server_active
// 2) ack+put_success
// 3) ack+commit_failed
cout<<"recvd msg:: "<<value_json<<endl;
if(document1.ParseInsitu((char*)value_json.c_str()).HasParseError())
{
cout<<"error in request for client parsing string"<<endl;
}
else if(strcmp(document1["req_type"].GetString(),"data")==0)
{
assert(document1.IsObject());
assert(document1.HasMember("req_type"));
assert(document1.HasMember("message"));
string value1=document1["message"].GetString();
cout<<value1<<endl;
}
else if(strcmp(document1["req_type"].GetString(),"ack")==0)
{
assert(document1.IsObject());
assert(document1.HasMember("req_type"));
assert(document1.HasMember("message"));
string value1=document1["message"].GetString();
cout<<value1<<endl;
}
}
//*********************************************************************************************//
int main(int argc,char **argv)
{
if(argc<=2)
{
cout<<"enter ip port "<<endl;
return 0;
}
string ip_address=argv[1];
string port_number=argv[2];
int sock_fd=initialize_socket(ip_address,port_number);
cout<<"socket id is"<<sock_fd<<endl;
ifstream file(FILENAME);
string cs_ip,cs_port;
if (file.is_open())
{
getline(file, cs_ip);
getline(file, cs_port);
file.close();
}
cout<<" cs ip "<<cs_ip<<" port"<<cs_port<<endl;
connect_f(sock_fd,cs_ip,cs_port);
cout<<"after connect_f "<<endl;
cout<<receive_message(sock_fd)<<endl;
cout<<"connected in client"<<endl;
send_message(sock_fd,identity_string("client"));
cout<<receive_message(sock_fd)<<endl;
cout<<"in client ready to serve"<<endl;
cout<<"command format : command_type:key:value"<<endl;
while(true)
{
string command;
cout<<"command >> ";
cin>>command;
if(command=="exit")
{
cout<<"TATA!!!! "<<endl;
return 0;
}
char *command_char=(char*)command.c_str();
string type=strtok(command_char,":");
if(type=="get"||type=="delete")
{
char *get_char=strtok(NULL,":");
string key=get_char;
// if(key==NULL)
// {
// cout<<"wrong command enter again\n";
// continue;
// }
request_for_get_delete(type,key,sock_fd);
}
if(type=="put"||type=="update")
{
char *get_char=strtok(NULL,":");
string key=get_char;
// if(key==NULL)
// {
// cout<<"wrong command enter again\n";
// continue;
// }
char *get_val=strtok(NULL,":");
string value=get_val;
// if(value==NULL)
// {
// cout<<"wrong command enter again\n";
// continue;
// }
request_for_update_put(type,key,value,sock_fd);
}
cout<<endl;
}
}
//**********************************************************************************************************//