-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConfig.cpp
More file actions
209 lines (182 loc) · 5.79 KB
/
Copy pathServerConfig.cpp
File metadata and controls
209 lines (182 loc) · 5.79 KB
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
#include "ServerConfig.hpp"
/*
Server config does not check for the validity of the ip to be ipv4
It expects that this as checked when creating the config
*/
ServerConfig::ServerConfig(void):size(0),_ip(""),_port(0){};
ServerConfig::ServerConfig(std::string ip, int port):size(0), _ip(ip), _port(port){};
ServerConfig::ServerConfig(const ServerConfig &rhs)
{
_ip = rhs.get_Ip();
_port = rhs.get_Port();
size = rhs.size;
if (!this->_configs.empty())
_configs.clear();
if (!rhs._configs.empty())
{
_configs = rhs.get_Configs();
// for (std::vector<Config>::const_iterator i = rhs._configs.begin();
// i != rhs._configs.end(); i++)
// {
// _configs.push_back(*i);
// // size += 1;
// }
}
}
ServerConfig& ServerConfig::operator=(const ServerConfig& other) {
// Defensive self-assignment check
if (this != &other) {
_ip = other.get_Ip();
_port = other.get_Port();
size = other.size;
if (!this->_configs.empty())
_configs.clear();
if (!other._configs.empty())
{
_configs = other.get_Configs();
}
}
return *this;
}
/*Does not check whether the Config IPs are same*/
bool ServerConfig::set_Ip(std::string ip)
{
_ip = ip;
return (true);
}
/*Does not check whether the Config ports are same*/
bool ServerConfig::set_Port(int port)
{
_port = port;
return (true);
}
// ServerConfig::ServerConfig(Config rhs)
// {
// _ip = rhs.get_ip();
// _port = rhs.get_port();
// _configs.push_back(rhs);
// }
// Need to modify to catch exceptions and edge cases
bool ServerConfig::addConfig(Config item)
{
// debug(get_Ip() << " " << item.get_ip() << " " << get_Port() << " " << item.get_port());
// If no configs exist, always add the first one
if (_configs.empty())
{
if ((get_Ip() == std::string("") || get_Ip() == item.get_ip()) &&
(get_Port() == 0 || get_Port() == item.get_port()) )
{
if (get_Ip() == std::string("")) set_Ip(item.get_ip());
if (get_Port() == 0) set_Port(item.get_port());
try
{
std::cout << "Adding first config with server name: " << item.get_server_name() << std::endl;
_configs.push_back(item);
size += 1;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
return false;
}
}
else return false;
return true;
}
// Check if this server name is already present
for (std::vector<Config>::const_iterator it = _configs.begin(); it != _configs.end(); ++it)
{
if (it->get_server_name() == item.get_server_name())
{
std::cerr << "Warning: Server name '" << item.get_server_name()
<< "' already exists in this server block." << std::endl;
return false;
}
}
// If the server name doesn't exist, add it
std::cout << "Adding new config with server name: " << item.get_server_name() << std::endl;
if (get_Ip() == item.get_ip() && get_Port() == item.get_port())
{
try
{
_configs.push_back(item);
size += 1;
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
return false;
}
}
else return false;
return true;
}
std::pair<Config,bool> ServerConfig::getDefaultConfig(void) const
{
if (!_configs.empty())
return (std::pair<Config,bool>(_configs[0], true));
else
return (std::pair<Config,bool>(*(_configs.end()), false));
}
std::pair<Config, bool> ServerConfig::findConfig(std::string server_name)
{
std::cout << "findConfig called with server_name: " << server_name << std::endl;
std::cout << "Total configs in this ServerConfig: " << size << std::endl;
if (size > 0)
{
for (std::vector<Config>::const_iterator it = _configs.begin(); it != _configs.end(); it++)
{
std::cout << "Comparing with config server name: " << it->get_server_name() << std::endl;
if (it->get_server_name() == server_name)
{
std::cout << "Found matching server name" << std::endl;
return std::pair<Config, bool>(*it, true);
}
}
// If not found, return the first config
// mark it as default
std::cout << "No matching server name, returning default config" << std::endl;
return std::pair<Config, bool>(_configs[0], true);
}
// if there is no config
std::cout << "No configs exist in this ServerConfig" << std::endl;
return std::pair<Config, bool>(Config(), false);
}
std::string ServerConfig::get_Ip(void) const
{
return(_ip);
}
int ServerConfig::get_Port(void) const
{
return (_port);
}
// std::vector<Config> const& ServerConfig::get_Configs() const
// {
// return (_configs);
// }
std::vector<Config> ServerConfig::get_Configs() const
{
return (_configs);
}
std::vector<Config> &ServerConfig::UNIT_get_Configs(void)
{
return (_configs);
}
std::ostream& operator<<(std::ostream& streamRef, const ServerConfig& sconf)
{
streamRef << "ip: " << sconf.get_Ip() << "port: " << sconf.get_Port() << std::endl;
int i = 1;
if (sconf.size > 0)
{
std::vector<Config> ret = sconf.get_Configs();
for (std::vector<Config>::iterator it = ret.begin(); it != ret.end(); it++)
{
if (!it->get_server_name().empty())
streamRef << "Config: " << i << " " << it->get_server_name() << " " << *it;
else
streamRef << "Config: " << i << " " << *it;
i++;
}
}
return (streamRef);
}