-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
133 lines (106 loc) · 3.9 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
#include <arpa/inet.h>
#include <errno.h>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdexcept>
#include <signal.h>
#include "ClientDependencies/configReader.h"
#include "ClientDependencies/client.class.h"
#include "Infrastructure/functions.h"
#include "Infrastructure/message.h"
void cleanUp(int placeholder, void* client)
{
(*(Client*)client).shutDown();
}
int main(int argc, char** argv)
{
if(argc != 3)
error_and_die("usage: client <ip4> <port>");
Client client(AF_INET, SOCK_STREAM, 0);
signal(SIGINT, exitProgram);
if( (on_exit(cleanUp, (void*)&client)) != 0 )
error_and_die("error registering exit handler");
client.connectToServer(argv[1], argv[2]);
std::string command;
client.printHelp();
ConfigReader reader(std::cin);
Session session;
session.username = "anonymous";
while(true)
{
std::cout << "enter a command: ";
std::getline(std::cin, command);
try
{
std::string sendRequest;
if( lower(command) == "login" )
{
std::string username, password;
reader.ReadLineParameter("Username", username, 8);
reader.ReadPassword(password, 24);
sendRequest = "LOGIN\n";
sendRequest += username + "\n";
sendRequest += password + "\n";
client.sendMessage(sendRequest);
std::string response = client.readMessage();
std::cout << response << std::endl;
if(response == "Logged in Successfully")
session.username = username;
continue;
}
else if( lower(command) == "send" )
{
std::string recipient, subject, message;
reader.ReadLineParameter("Empfänger", recipient, 8);
reader.ReadLineParameter("Betreff", subject, 80);
reader.ReadTextParameter("Nachricht", message, 10000);
std::cout << session.username << std::endl;
std::cout << session.username.length() << std::endl;
Message msg(session.username, recipient, subject, message);
sendRequest = "SEND\n";
sendRequest += msg.ToNetworkString();
}
else if( lower(command) == "list" )
{
sendRequest = "LIST\n";
}
else if( lower(command) == "read" || lower(command) == "delete" )
{
std::string number;
reader.ReadLineParameter("Message number", number, 8);
sendRequest = lower(command) == "read" ? "READ\n" : "DELETE\n";
sendRequest += number + "\n";
}
else if ( lower(command) == "help" )
{
client.printHelp();
continue;
}
else if( lower(command) == "quit" )
break;
else
sendRequest = command;
std::cout << "Sending Request: [" << sendRequest << "] at " << getCurrentTime() << std::endl;
client.sendMessage(sendRequest);
std::cout << "Start waiting for Response, at " << getCurrentTime() << std::endl;
std::string response = client.readMessage();
std::cout << "Response received, at " << getCurrentTime() << std::endl;
std::cout << "response: \n\n" << response << std::endl;
}
catch(const ConfigReaderException& e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
}
catch(const std::invalid_argument& ex)
{
std::cerr << "ERROR: " << ex.what() << std::endl;
}
catch(...)
{
std::cerr << "unknown error" << std::endl;
}
}
exit(EXIT_SUCCESS);
}