-
Notifications
You must be signed in to change notification settings - Fork 1
/
bankClient.cpp
223 lines (201 loc) · 4.91 KB
/
bankClient.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
#include <iostream>
using namespace std;
/*#include "bankServer.h"
#include "bankAccount.h"
#include "transaction.h"*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include "string.h"
#include "stdlib.h"
#include <sstream>
#include<fstream>
#include <sys/socket.h> /* sockets */
#include <netinet/in.h> /* internet sockets */
#include <netdb.h> /* gethostbyaddr */
#include "readWriteFunct.h"
int checkCommand(string& line);
int main(int argc, char *argv[])
{
int sock, i, port = -1;
char buf[256];
char buffer[1024];
string buff;
struct sockaddr_in server;
struct sockaddr *serverptr = (struct sockaddr*)&server;
struct hostent *rem;
char* serverHost = NULL;
ifstream inFile;
if (argc != 7)
{
cout<<"Please give all attributes -h <server host> -p <server port> -i <command file>"<<endl;
exit(1);
}
for(int i=1; i < argc; i++) //diavasma parametrwn grammis entolis me morfi -h <server host> -p <server port> -i <command file>
{
if(i+1 != argc)
{
if(strcmp(argv[i], "-h") == 0)
{
serverHost = argv[i+1];
}
else if(strcmp(argv[i], "-p") == 0)
{
port = atoi(argv[i+1]);
}
else if(strcmp(argv[i], "-i") == 0)
{
inFile.open(argv[i+1]);
}
}
}
if(port == -1 || serverHost == NULL || inFile.is_open() == false)
{
cout<<"Please give all attributes -h <server host> -p <server port> -i <command file>"<<endl;
exit(1);
}
//kwdikas socket me vash tis diafaneies tou mathimatos
/* Create socket */
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
exit(2);
}
/* Find server address */
if ((rem = gethostbyname(serverHost)) == NULL) {
herror("gethostbyname"); exit(1);
}
server.sin_family = AF_INET; /* Internet domain */
memcpy(&server.sin_addr, rem->h_addr, rem->h_length);
server.sin_port = htons(port); /* Server port */
/* Initiate connection */
if (connect(sock, serverptr, sizeof(server)) < 0)
{
perror("connect");
exit(1);
}
printf("Connecting to %s port %d\n", argv[1], port);
//exoume sindethei tora mem ton server
int result;
while(getline(inFile, buff)) //diavasma apo arxeio
{
cout<<buff<<endl;
result = checkCommand(buff);
if(result == 1)
{
continue;
}
else if(result == 0)
{
write_data(sock, buff.c_str());
i = read_data(sock, buffer);
printf("%.*s\n", i, buffer);
}
else if (result == -2)
{
close(sock);
cout<<"terminate client"<<endl;
exit(0);
}
else //result -1
{
cout<<"command doesnt exists"<<endl;
}
}
while(!getline(cin, buff).eof()) //diavasma apo stdin
{
result = checkCommand(buff);
if(result == 1)
{
continue;
}
else if(result == 0)
{
write_data(sock, buff.c_str());
i = read_data(sock, buffer);
printf("%.*s\n", i, buffer);
}
else if (result == -2)
{
close(sock);
cout<<"terminate client"<<endl;
exit(0);
}
else //result -1
{
cout<<"command doesnt exists"<<endl;
}
}
}
int checkCommand(string& line)
{
string command, name;
double amount;
istringstream iss(line);
iss>> command; //to prwto stoixeio einai h entolh
if(iss.fail())
{
return -1;
}
if(command == "add_account")
{
iss>> amount>>name;
if(!iss)
{
return -1;
}
}
else if(command == "add_transfer")
{
string fromAc, ToAc;
iss >>amount >> fromAc >> ToAc;
if(!iss)
{
return -1;
}
}
else if(command == "add_multi_transfer")
{
string dstName1, dstName2;
iss >> amount >> name >> dstName1 >> dstName2; //thelw na exei toulaxiston dyo gia na einai swsth
if(!iss)
{
return -1;
}
}
else if(command == "print_balance")
{
iss>> name;
if(!iss)
{
return -1;
}
}
else if(command == "print_multi_balance")
{
string name1, name2;
iss >> name1 >>name2; //thelw na exei toulaxiston dyo gia na einai swsth
if(!iss)
{
return -1;
}
}
else if(command == "sleep")
{
long time;
iss >> time;
usleep(time);
return 1;
}
else if(command == "exit") //error
{
return -2;
}
else //error
{
return -1;
}
return 0;
}