-
Notifications
You must be signed in to change notification settings - Fork 1
/
TransferInfoManager.cpp
286 lines (249 loc) · 7.96 KB
/
TransferInfoManager.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "TransferInfoManager.hpp"
#include <filesystem>
#include <fstream>
#include <sstream>
#include "ErrorsModule.hpp"
#include "LoggerModule.hpp"
namespace TransferInfoManagerConstants
{
constexpr const char *DEFAULT_FILE_NAME = "transfer.info";
constexpr const char *DEFAULT_IP = "127.0.0.1";
constexpr int DEFAULT_PORT = 1234;
constexpr const char *DEFAULT_CLIENT_NAME = "Client";
} // namespace TransferInfoManagerConstants
namespace TransferInfoValidator
{
bool isValidPort(int port)
{
if (port == TransferInfoManagerConstants::DEFAULT_PORT)
{
return true;
}
WARN_LOG("Port number is not the default: {}", port);
return port > 0 && port <= 65535;
}
bool isValidIPAddress(const std::string &ip)
{
if (ip.empty()) // Check for an empty string
{
return false;
}
if (ip == TransferInfoManagerConstants::DEFAULT_IP)
{
return true;
}
WARN_LOG("IP address is not the default: {}", ip);
std::stringstream ss(ip);
int a, b, c, d;
char ch;
return (ss >> a >> ch >> b >> ch >> c >> ch >> d && a <= 255 && b <= 255 &&
c <= 255 && d <= 255);
}
bool isValidName(const std::string &name) { return (name.length() < 100); }
static std::string extractFileName(const std::string &path)
{
size_t pos = path.find_last_of("/\\");
return (pos == std::string::npos) ? path : path.substr(pos + 1);
}
void checkFile(const std::string &input_file_name)
{
std::string file = extractFileName(input_file_name);
DEBUG_LOG("Checking if file: \"{}\" is empty.", file);
if (input_file_name.empty())
{
std::string errorMsg = "The provided file name string is empty.";
throw std::runtime_error(errorMsg);
}
DEBUG_LOG("Checking if file: \"{}\" exists.", file);
if (!std::filesystem::exists(input_file_name))
{
std::string errorMsg =
"The specified file \"" + file + "\" does not exist.";
throw std::runtime_error(errorMsg);
}
if (!std::filesystem::is_regular_file(input_file_name))
{
std::string errorMsg =
"The path \"" + file + "\" does not point to a regular file.";
throw std::runtime_error(errorMsg);
}
if (std::filesystem::is_empty(input_file_name))
{
std::string errorMsg = "The file \"" + file + "\" is empty.";
throw std::runtime_error(errorMsg);
}
}
void validateAndAdjustName(std::string &name)
{
// Check for validity of name
if (!isValidName(name))
{
WARN_LOG("Invalid client name: {}", name);
// Trim name to 100 characters if it's too long
if (name.length() > 100)
{
name = name.substr(0, 100);
WARN_LOG("Trimmed client name to 100 characters: {}", name);
}
// Set to a default name if it's empty
else if (name.empty())
{
name = "DefaultName";
WARN_LOG("Client name is empty. Set to default: {}", name);
}
else
{
throw std::runtime_error("Invalid client name.");
}
}
}
} // namespace TransferInfoValidator
using namespace TransferInfoManagerConstants;
using namespace TransferInfoValidator;
TransferInfoManager::TransferInfoManager()
: TransferInfoManager(DEFAULT_FILE_NAME)
{
// Constructor delegation
}
TransferInfoManager::TransferInfoManager(const std::string &input_file_name)
: client_name_(""),
port_number_(-1),
ip_address_(""),
file_names_({}),
file_name_("")
{
auto [ip, port, name] = readIPAndPortAndNameFromFile(input_file_name);
setIPAndPortAndName(ip, port, name);
}
std::string TransferInfoManager::getName() const { return client_name_; }
std::string TransferInfoManager::getIPAddress() const { return ip_address_; }
int TransferInfoManager::getPort() const { return port_number_; }
std::string TransferInfoManager::getFileName()
{
while (!file_name_.empty() || !file_names_.empty())
{
try
{
checkFile(file_name_);
std::string current_file_name = file_name_;
moveToNextFile(); // Updates file_name_ and file_names_
return current_file_name;
}
catch (const std::exception &e)
{
WARN_LOG(
"File validation failed: {}. Skipping and moving to the next "
"file.",
e.what());
moveToNextFile(); // Updates file_name_ and file_names_
}
}
throw ErrorsModule::NoMoreFilesException();
}
void TransferInfoManager::moveToNextFile()
{
if (!file_names_.empty())
{
file_name_ = file_names_.front();
file_names_.erase(file_names_.begin());
}
else
{
file_name_.clear();
}
}
std::tuple<std::string, int, std::string>
TransferInfoManager::readIPAndPortAndNameFromFile(
const std::string &input_file_name)
{
std::string info_file_name = input_file_name;
if (info_file_name != DEFAULT_FILE_NAME)
{
std::string warnMsg = "Invalid file name: " + info_file_name +
". It should be " + DEFAULT_FILE_NAME +
". Proceeding with "
"the default file name.";
WARN_LOG("transfer.info warning: {}", warnMsg);
info_file_name = DEFAULT_FILE_NAME;
}
try
{
checkFile(info_file_name);
}
catch (const std::exception &e)
{
std::string errorMsg =
"File validation failed: " + std::string(e.what()) +
" Using Defult values: " + DEFAULT_IP + ":" +
std::to_string(DEFAULT_PORT) + ": name: " + DEFAULT_CLIENT_NAME +
".";
WARN_LOG("In setting up client: {}", errorMsg);
return {DEFAULT_IP, DEFAULT_PORT, DEFAULT_CLIENT_NAME};
}
std::ifstream infile(info_file_name);
if (!infile.is_open())
{
std::string errorMsg = "Failed to open file: " + info_file_name +
". It may not exist or may be in use.";
WARN_LOG("Warning setting up client: {}", errorMsg);
throw std::runtime_error(errorMsg);
}
std::string ip_port_str, name;
// Read IP and Port
if (!std::getline(infile, ip_port_str))
{
throw std::runtime_error("Failed to read IP and Port from file.");
}
// Read client name
if (!std::getline(infile, name))
{
throw std::runtime_error("Failed to read Client Name from file.");
}
// Clear existing file names
file_names_.clear();
// Read file names
std::string file_line;
while (std::getline(infile, file_line))
{
file_names_.push_back(file_line);
DEBUG_LOG("File name: {}", file_line);
try
{
checkFile(file_line);
}
catch (const std::exception &e)
{
WARN_LOG("File validation failed: {}", e.what());
}
}
// Update the file_name_ attribute to store the first file name (if any)
if (!file_names_.empty())
{
file_name_ = file_names_.front();
file_names_.erase(
file_names_.begin()); // remove the first filename from the list
}
size_t colon_pos = ip_port_str.find(':');
if (colon_pos != std::string::npos)
{
std::string ip = ip_port_str.substr(0, colon_pos);
int port = std::stoi(ip_port_str.substr(colon_pos + 1));
DEBUG_LOG("IP address: {} | Port number: {} | Client Name: {}", ip,
port, name);
return {ip, port, name};
}
throw std::runtime_error("Failed to parse IP and Port from file.");
}
void TransferInfoManager::setIPAndPortAndName(const std::string &ip, int port,
std::string &name)
{
if (!isValidIPAddress(ip) || !isValidPort(port))
{
ERROR_LOG("Invalid IP address or port number format: {}:{}", ip, port);
throw std::runtime_error("Invalid IP address or port number format.");
}
validateAndAdjustName(name);
ip_address_ = ip;
port_number_ = port;
client_name_ = name;
}