-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
132 lines (118 loc) · 3.9 KB
/
client.cpp
File metadata and controls
132 lines (118 loc) · 3.9 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
#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"
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);
if(document1.Parse(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: "<<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
if(document1.Parse(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);
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);
receive_message(sock_fd);// ack + connected
send_message(sock_fd,identity_string("client"));
receive_message(sock_fd);// ack + ready_to_serve
cout<<"command format : command_type:key:value"<<endl;
while(true)
{
string command;
cout<<"command > ";
cin>>command;
if(command=="exit")
{
cout<<"Bye~"<<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;
request_for_get_delete(type,key,sock_fd);
}
if(type=="put"||type=="update")
{
char *get_char=strtok(NULL,":");
string key=get_char;
char *get_val=strtok(NULL,":");
string value=get_val;
request_for_update_put(type,key,value,sock_fd);
}
cout<<endl;
}
}