-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientApplication.cc
86 lines (69 loc) · 1.92 KB
/
ClientApplication.cc
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
#include "ClientApplication.h"
#include <iostream>
#include <cstdio>
#include <string>
#include <regex>
#include "StringManip.h"
using namespace std;
using namespace TP1;
/**
* Client's application main loop
*
* */
int ClientApplication::runApplication(int argc, char** argv) {
string clientTime = "";
long long convertedTime = 0;
string addr = argv[1];
string port = argv[2];
int connectionFlag = -1;
if (_logging) cout << "Starting Client Application" << endl;
if (_logging) cout << "Number of arguments: " << argc << endl;
connectionFlag = _mediator.connectToServer(addr.c_str(), stoul(port));
if (connectionFlag != 0 ) {
cout << "ERROR! Could not connect to server at " << addr << ":" << port << endl;
exit(1);
}
do {
clientTime = getClientInput();
//This does basic client-side input verification
//If user doesn't send a valid time, it won't be sent
//to server
if (isValidMessage(clientTime)) {
convertedTime = stoll(clientTime);
} else {
cout << "Invalid message!" << endl;
convertedTime = 0;
continue;
}
//sends converted time to server
_mediator.sendRequest(clientTime);
//gets position back from server and display it on screen
displayResponse(_mediator.getResponse());
} while (convertedTime >= 0);
_mediator.closeConnection();
return 0;
}
void ClientApplication::displayResponse(string position) {
if (stoll(position) >= 0) {
cout << position << endl;
} else {
//Server is ending connection
if (_logging) {
cout << "Exiting client" << endl;
}
}
}
string ClientApplication::getClientInput() {
string clientTime;
if (_logging) {
cout << "Please type your time" << endl;
}
getline(cin, clientTime);
return clientTime;
}
/**
* A valid message is a message which starts with a digit
**/
bool ClientApplication::isValidMessage(string message) {
return strtoll(message.c_str(), NULL, 10);
}