Skip to content

Commit cdd8c6f

Browse files
committed
documentation
1 parent a643f30 commit cdd8c6f

File tree

6 files changed

+282
-27
lines changed

6 files changed

+282
-27
lines changed

src/client.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* \file
3+
* \brief Chat client -- interacts with user, displays, sends and encrypts messages
4+
**/
5+
16
#include "client_lib.hpp"
27
#include <boost/asio.hpp>
38
#include <boost/asio/buffer.hpp>
@@ -22,6 +27,11 @@ using json = nlohmann::json;
2227

2328
void process_step(User &user)
2429
{
30+
/**
31+
This function provides UI:
32+
draws screen and displays everything as well as performs navigation
33+
\param user instance of User class
34+
**/
2535
system("clear");
2636
std::string code;
2737

@@ -31,12 +41,13 @@ void process_step(User &user)
3141

3242
if (user.logged_in)
3343
{
34-
fmt::print("Hi {}! \n\nSelect an option below:\n", user.username);
44+
fmt::print("{}, welcome back! \n\nSelect an option below:\n", user.username);
3545
fmt::print(" s | Sync\n");
3646
fmt::print(" c | Chat List\n");
47+
fmt::print("\nSync messages to get your inbox from the server. \nThen proceed to your Chat List.\n");
3748
}
3849
else {
39-
fmt::print("Welcome to Chat client! \n\nSelect an option below:\n");
50+
fmt::print("Welcome to Chat client! \n\nEnter s, if you don't have an account or enter l to log in:\n");
4051
fmt::print(" u | Sign Up\n");
4152
fmt::print(" l | Log In\n");
4253
}
@@ -60,6 +71,7 @@ void process_step(User &user)
6071
else if (user.state == 1) // sign up
6172
{
6273
fmt::print("Sign Up \n\n");
74+
fmt::print("Welcome here! You are about to create an account. \nJust take any free username and you are ready to rock. \nAfter creating an account, go to Chat List section and \nstart new dialog. Sync messages after that.\n\n");
6375

6476
while (!user.logged_in) // ещё не зарегестрировался
6577
{
@@ -80,9 +92,10 @@ void process_step(User &user)
8092
else if (user.state == 2) // log in
8193
{
8294
fmt::print("Log in \n\n");
95+
fmt::print("Now you have to enter your username. If you are not registered yet, please,\ngo back and select Sign up. \n\n");
8396
while (!user.logged_in) // ещё не вошёл
8497
{
85-
fmt::print("Enter username (or b to go back): ");
98+
fmt::print("Enter your username (or b to go back): ");
8699
std::getline(std::cin >> std::ws, code);
87100
if (code == std::string{'b'}) { user.state = 0; break;}
88101

@@ -98,7 +111,7 @@ void process_step(User &user)
98111

99112
else if (user.state == 3) // sync
100113
{
101-
fmt::print("Sync messages... \n\n");
114+
fmt::print("Syncing messages... \n\n");
102115

103116
user.messages = get_messages(user);
104117
user.state = 0;
@@ -115,7 +128,7 @@ void process_step(User &user)
115128
[&](){
116129
system("clear");
117130
fmt::print("Chat List \n\nSelect from the list below: \n\n");
118-
if (user.messages.empty()) fmt::print("Empty. Try to sync messages first. \n\n");
131+
if (user.messages.empty()) fmt::print("Nothing's here. \nEnter n to start your first dialog! \nOr go back (b) and sync messages.\n\n");
119132
else
120133
{
121134
// print out messages
@@ -158,9 +171,10 @@ void process_step(User &user)
158171

159172
else if (user.state == 5) // enter key
160173
{
161-
fmt::print("Decrypt messages\n\nEnter the key from {} or b to go back: ", user.current_chat);
174+
fmt::print("Decrypt messages\n\nIf you've just created chat, enter any word combination. \nYou will use it later to decrypt messages. \n\nEnter the key from {} or b to go back: ", user.current_chat);
162175
std::getline(std::cin >> std::ws, code);
163-
if (code == std::string{'b'}) user.state = 0;
176+
if (code == std::string{'b'})
177+
user.state = 0;
164178
else
165179
{
166180
user.current_key = code;
@@ -190,7 +204,7 @@ void process_step(User &user)
190204
std::string reply = encrypt(code, user.current_key);
191205
if (code == std::string{'b'}) break;
192206
if (send_reply(user.username, user.current_chat, reply))
193-
fmt::print("Sent sucessfully. \n");
207+
fmt::print("Sent. \n");
194208
else {
195209
fmt::print("Cannot send. \n\nenter b to go back>");
196210
std::getline(std::cin >> std::ws, code);
@@ -205,7 +219,7 @@ void process_step(User &user)
205219
fmt::print("Start New Chat\n\n");
206220
while (user.state == 7)
207221
{
208-
fmt::print("Enter username: ");
222+
fmt::print("Enter your friend's username: ");
209223
std::getline(std::cin >> std::ws, code);
210224
if (code == std::string{'b'}) { user.state = 0; break; }
211225
if (login(code)){ // проверяем что existed

src/client_lib.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* \file
3+
* \brief File implements functions from client_lib.hpp
4+
**/
5+
16
#include "client_lib.hpp"
27
#include <nlohmann/json.hpp>
38
#include <boost/asio.hpp>
@@ -11,6 +16,11 @@ using boost::asio::ip::tcp;
1116

1217
bool is_valid_username(std::string username)
1318
{
19+
/**
20+
Checks if given username is correct (consists only from digits, letters and "_")
21+
\param username given username
22+
\return true if username is valid, false otherwise
23+
**/
1424
for (char x : username)
1525
if ((std::isalnum(x) == 0) && (x != '_')) { return false; } // bad character
1626
return true;
@@ -19,6 +29,12 @@ bool is_valid_username(std::string username)
1929

2030
bool login(std::string username)
2131
{
32+
/**
33+
Checks if given username is correct and exists on server
34+
\param username username of the user
35+
\return true if username is valid and exists on server, false otherwise
36+
**/
37+
2238
if (!is_valid_username(username)) return false;
2339

2440
boost::asio::io_service io_service;
@@ -38,6 +54,11 @@ bool login(std::string username)
3854

3955
bool signup(std::string username)
4056
{
57+
/**
58+
Checks if given username is correct and then creates new user on server
59+
\param username username of new user
60+
\return true if username new user creates, false if not or if username is not correct
61+
**/
4162
if (!is_valid_username(username)) return false;
4263

4364
boost::asio::io_service io_service;
@@ -57,6 +78,36 @@ bool signup(std::string username)
5778

5879
json get_messages(User &user)
5980
{
81+
/**
82+
Downloads messages addressed to current user from server
83+
\param user instance of User class
84+
\return json with messages
85+
\code json
86+
get bob
87+
{
88+
"alice": {
89+
"0": {
90+
"receiver": "alice",
91+
"sender": "bob",
92+
"text": "q2ZJuWt="
93+
},
94+
"1": {
95+
"receiver": "alice",
96+
"sender": "bob",
97+
"text": "riY="
98+
}
99+
},
100+
"mike": {
101+
"0": {
102+
"receiver": "bob",
103+
"sender": "mike",
104+
"text": "rh5Ggq3GhzUXe2QxvU=="
105+
}
106+
}
107+
}
108+
\endcode
109+
**/
110+
60111
boost::asio::io_service io_service;
61112
tcp::socket socket(io_service);
62113
socket.connect(tcp::endpoint(boost::asio::ip::address::from_string(ip), port));
@@ -74,6 +125,13 @@ json get_messages(User &user)
74125

75126
bool send_reply(std::string &sender, std::string &receiver, std::string &text)
76127
{
128+
/**
129+
Sends message to server
130+
\param sender username of current user
131+
\param receiver username of recipient
132+
\param text string with encrypted text of the message
133+
\return true if successful
134+
**/
77135
boost::asio::io_service io_service;
78136
tcp::socket socket(io_service);
79137
socket.connect(tcp::endpoint(boost::asio::ip::address::from_string(ip), port));
@@ -89,4 +147,5 @@ bool send_reply(std::string &sender, std::string &receiver, std::string &text)
89147
std::string response{boost::asio::buffer_cast<const char*>(rb.data())};
90148

91149
return (response == "200\n");
92-
}
150+
}
151+

src/client_lib.hpp

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* \file
3+
* \brief File defines functions and a structure for chat client
4+
**/
5+
16
#include <iostream>
27
#include <string>
38
#include <nlohmann/json.hpp>
@@ -7,29 +12,79 @@ using json = nlohmann::json;
712
const std::string ip = "127.0.0.1";
813
const int port = 1234;
914

10-
15+
/**
16+
Checks if given username is correct (consists only from digits, letters and "_")
17+
\param username given username
18+
\return true if username is valid, false otherwise
19+
**/
1120
bool is_valid_username(std::string username);
1221

22+
1323
struct User
1424
{
15-
size_t state = 0; // current position
16-
bool logged_in = false;
17-
std::string username;
25+
/**
26+
* stores data of current user
27+
**/
28+
size_t state = 0; ///< current position of user (current screen)
29+
bool logged_in = false; ///< if user is logged in
30+
std::string username; ///< username of the user
1831

19-
json messages;
32+
json messages; ///< stores messages downloaded from server
2033

21-
std::string current_chat;
22-
std::string current_key;
34+
std::string current_chat; ///< username of current chat
35+
std::string current_key; ///< crypto key of current chat
2336
};
2437

25-
38+
/**
39+
Checks if given username is correct and exists on server
40+
\param username username of the user
41+
\return true if username is valid and exists on server, false otherwise
42+
**/
2643
bool login(std::string username);
2744

28-
45+
/**
46+
Checks if given username is correct and then creates new user on server
47+
\param username username of new user
48+
\return true if username new user creates, false if not or if username is not correct
49+
**/
2950
bool signup(std::string username);
3051

31-
52+
/**
53+
Downloads messages addressed to current user from server
54+
\param user instance of User class
55+
\return json with messages
56+
\code json
57+
get bob
58+
{
59+
"alice": {
60+
"0": {
61+
"receiver": "alice",
62+
"sender": "bob",
63+
"text": "q2ZJuWt="
64+
},
65+
"1": {
66+
"receiver": "alice",
67+
"sender": "bob",
68+
"text": "riY="
69+
}
70+
},
71+
"mike": {
72+
"0": {
73+
"receiver": "bob",
74+
"sender": "mike",
75+
"text": "rh5Ggq3GhzUXe2QxvU=="
76+
}
77+
}
78+
}
79+
\endcode
80+
**/
3281
json get_messages(User &user);
3382

34-
83+
/**
84+
Sends message to server
85+
\param sender username of current user
86+
\param receiver username of recipient
87+
\param text string with encrypted text of the message
88+
\return true if successful
89+
**/
3590
bool send_reply(std::string &sender, std::string &receiver, std::string &text);

src/server.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* \file
3+
* \brief Chat server -- stores users and messages
4+
* supports 4 methods:
5+
* - login <username>
6+
* - signup <username>
7+
* - get <username>
8+
* - send <sender> <receiver> <text of a message>
9+
**/
10+
111
//
212
// echo_server.cpp
313
// ~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)