git.stg.codes
/
stg.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Move strategy into job.
[stg.git]
/
projects
/
stargazer
/
plugins
/
other
/
rscript
/
rscript.cpp
diff --git
a/projects/stargazer/plugins/other/rscript/rscript.cpp
b/projects/stargazer/plugins/other/rscript/rscript.cpp
index 8b0e2054a984154961eeb1e31dffbd54ff6fe96e..e675d4886c56460ca234a3b78468f164f3c12dd4 100644
(file)
--- a/
projects/stargazer/plugins/other/rscript/rscript.cpp
+++ b/
projects/stargazer/plugins/other/rscript/rscript.cpp
@@
-78,18
+78,17
@@
int RS::SETTINGS::ParseSettings(const STG::ModuleSettings & s)
{
int p;
STG::ParamValue pv;
{
int p;
STG::ParamValue pv;
-std::vector<STG::ParamValue>::const_iterator pvi;
netRouters.clear();
///////////////////////////
pv.param = "Port";
netRouters.clear();
///////////////////////////
pv.param = "Port";
-pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+
auto
pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
if (pvi == s.moduleParams.end() || pvi->value.empty())
{
errorStr = "Parameter \'Port\' not found.";
printfd(__FILE__, "Parameter 'Port' not found\n");
return -1;
}
if (pvi == s.moduleParams.end() || pvi->value.empty())
{
errorStr = "Parameter \'Port\' not found.";
printfd(__FILE__, "Parameter 'Port' not found\n");
return -1;
}
-if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
+if (ParseIntInRange(pvi->value[0], 2, 65535, &p)
!= 0
)
{
errorStr = "Cannot parse parameter \'Port\': " + errorStr;
printfd(__FILE__, "Cannot parse parameter 'Port'\n");
{
errorStr = "Cannot parse parameter \'Port\': " + errorStr;
printfd(__FILE__, "Cannot parse parameter 'Port'\n");
@@
-106,7
+105,7
@@
if (pvi == s.moduleParams.end() || pvi->value.empty())
return -1;
}
return -1;
}
-if (ParseIntInRange(pvi->value[0], 5, 600, &sendPeriod))
+if (ParseIntInRange(pvi->value[0], 5, 600, &sendPeriod)
!= 0
)
{
errorStr = "Cannot parse parameter \'SendPeriod\': " + errorStr;
printfd(__FILE__, "Cannot parse parameter 'SendPeriod'\n");
{
errorStr = "Cannot parse parameter \'SendPeriod\': " + errorStr;
printfd(__FILE__, "Cannot parse parameter 'SendPeriod'\n");
@@
-146,13
+145,9
@@
subnetFile = pvi->value[0];
NRMapParser nrMapParser;
if (!nrMapParser.ReadFile(subnetFile))
NRMapParser nrMapParser;
if (!nrMapParser.ReadFile(subnetFile))
- {
netRouters = nrMapParser.GetMap();
netRouters = nrMapParser.GetMap();
- }
else
else
- {
- STG::PluginLogger::get("rscript")("mod_rscript: error opening subnets file '%s'", subnetFile.c_str());
- }
+ STG::PluginLogger::get("rscript")("mod_rscript: error opening subnets file '%s'", subnetFile.c_str());
return 0;
}
return 0;
}
@@
-163,7
+158,7
@@
REMOTE_SCRIPT::REMOTE_SCRIPT()
: sendPeriod(15),
halfPeriod(8),
isRunning(false),
: sendPeriod(15),
halfPeriod(8),
isRunning(false),
- users(
NULL
),
+ users(
nullptr
),
sock(0),
onAddUserNotifier(*this),
onDelUserNotifier(*this),
sock(0),
onAddUserNotifier(*this),
onDelUserNotifier(*this),
@@
-171,15
+166,11
@@
REMOTE_SCRIPT::REMOTE_SCRIPT()
{
}
//-----------------------------------------------------------------------------
{
}
//-----------------------------------------------------------------------------
-REMOTE_SCRIPT::~REMOTE_SCRIPT()
-{
-}
-//-----------------------------------------------------------------------------
void REMOTE_SCRIPT::Run(std::stop_token token)
{
sigset_t signalSet;
sigfillset(&signalSet);
void REMOTE_SCRIPT::Run(std::stop_token token)
{
sigset_t signalSet;
sigfillset(&signalSet);
-pthread_sigmask(SIG_BLOCK, &signalSet,
NULL
);
+pthread_sigmask(SIG_BLOCK, &signalSet,
nullptr
);
isRunning = true;
isRunning = true;
@@
-194,8
+185,8
@@
isRunning = false;
//-----------------------------------------------------------------------------
int REMOTE_SCRIPT::ParseSettings()
{
//-----------------------------------------------------------------------------
int REMOTE_SCRIPT::ParseSettings()
{
-
int
ret = rsSettings.ParseSettings(settings);
-if (ret)
+
auto
ret = rsSettings.ParseSettings(settings);
+if (ret
!= 0
)
errorStr = rsSettings.GetStrError();
sendPeriod = rsSettings.GetSendPeriod();
errorStr = rsSettings.GetStrError();
sendPeriod = rsSettings.GetSendPeriod();
@@
-214,19
+205,13
@@
users->AddNotifierUserAdd(&onAddUserNotifier);
users->AddNotifierUserDel(&onDelUserNotifier);
if (GetUsers())
users->AddNotifierUserDel(&onDelUserNotifier);
if (GetUsers())
- {
return -1;
return -1;
- }
if (PrepareNet())
if (PrepareNet())
- {
return -1;
return -1;
- }
if (!isRunning)
if (!isRunning)
- {
- m_thread = std::jthread([this](auto token){ Run(token); });
- }
+ m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
errorStr = "";
return 0;
errorStr = "";
return 0;
@@
-253,7
+238,7
@@
if (isRunning)
for (int i = 0; i < 25 && isRunning; i++)
{
struct timespec ts = {0, 200000000};
for (int i = 0; i < 25 && isRunning; i++)
{
struct timespec ts = {0, 200000000};
- nanosleep(&ts,
NULL
);
+ nanosleep(&ts,
nullptr
);
}
}
}
}
@@
-265,6
+250,8
@@
if (isRunning)
logger("Cannot stop thread.");
m_thread.detach();
}
logger("Cannot stop thread.");
m_thread.detach();
}
+else
+ m_thread.join();
return 0;
}
return 0;
}
@@
-323,7
+310,7
@@
void REMOTE_SCRIPT::PeriodicSend()
{
std::lock_guard lock(m_mutex);
{
std::lock_guard lock(m_mutex);
-
std::map<uint32_t, RS::USER>::iterator it(authorizedUsers.begin()
);
+
auto it = authorizedUsers.begin(
);
while (it != authorizedUsers.end())
{
if (difftime(stgTime, it->second.lastSentTime) - (rand() % halfPeriod) > sendPeriod)
while (it != authorizedUsers.end())
{
if (difftime(stgTime, it->second.lastSentTime) - (rand() % halfPeriod) > sendPeriod)
@@
-391,19
+378,16
@@
RS::PACKET_TAIL packetTail;
memset(packetTail.padding, 0, sizeof(packetTail.padding));
memcpy(packetTail.magic, RS_ID, sizeof(RS_ID));
memset(packetTail.padding, 0, sizeof(packetTail.padding));
memcpy(packetTail.magic, RS_ID, sizeof(RS_ID));
-std::vector<std::string>::const_iterator it;
std::string params;
std::string params;
-for(it = rsSettings.GetUserParams().begin();
- it != rsSettings.GetUserParams().end();
- ++it)
+for (const auto& param : rsSettings.GetUserParams())
{
{
-
std::string parameter(rsu.user->GetParamValue(it->c_str())
);
- if (params.length() +
parameter
.length() > RS_PARAMS_LEN - 1)
+
auto value = rsu.user->GetParamValue(param
);
+ if (params.length() +
value
.length() > RS_PARAMS_LEN - 1)
{
{
- logger("Script params string length %d exceeds the limit of %d symbols.", params.length() +
parameter
.length(), RS_PARAMS_LEN);
+ logger("Script params string length %d exceeds the limit of %d symbols.", params.length() +
value
.length(), RS_PARAMS_LEN);
break;
}
break;
}
- params +=
parameter
+ " ";
+ params +=
value
+ " ";
}
strncpy(reinterpret_cast<char*>(packetTail.params), params.c_str(), RS_PARAMS_LEN);
packetTail.params[RS_PARAMS_LEN - 1] = 0;
}
strncpy(reinterpret_cast<char*>(packetTail.params), params.c_str(), RS_PARAMS_LEN);
packetTail.params[RS_PARAMS_LEN - 1] = 0;
@@
-435,7
+419,7
@@
for (const auto& ip : rsu.routers)
sendAddr.sin_port = htons(rsSettings.GetPort());
sendAddr.sin_addr.s_addr = ip;
sendAddr.sin_port = htons(rsSettings.GetPort());
sendAddr.sin_addr.s_addr = ip;
- return sendto(sock, buffer, sizeof(buffer), 0, reinterpret_cast<struct sockaddr*>(&sendAddr), sizeof(sendAddr));
+ return sendto(sock, buffer, sizeof(buffer), 0, reinterpret_cast<struct sockaddr*>(&sendAddr), sizeof(sendAddr))
> 0
;
}
return false;
}
return false;
@@
-472,10
+456,8
@@
UserPtr u;
int h = users->OpenSearch();
assert(h && "USERS::OpenSearch is always correct");
int h = users->OpenSearch();
assert(h && "USERS::OpenSearch is always correct");
-while (!users->SearchNext(h, &u))
- {
+while (users->SearchNext(h, &u) != 0)
SetUserNotifiers(u);
SetUserNotifiers(u);
- }
users->CloseSearch(h);
return false;
users->CloseSearch(h);
return false;
@@
-484,14
+466,10
@@
return false;
std::vector<uint32_t> REMOTE_SCRIPT::IP2Routers(uint32_t ip)
{
std::lock_guard lock(m_mutex);
std::vector<uint32_t> REMOTE_SCRIPT::IP2Routers(uint32_t ip)
{
std::lock_guard lock(m_mutex);
-for (size_t i = 0; i < netRouters.size(); ++i)
- {
- if ((ip & netRouters[i].subnetMask) == (netRouters[i].subnetIP & netRouters[i].subnetMask))
- {
- return netRouters[i].routers;
- }
- }
-return std::vector<uint32_t>();
+for (auto& nr : netRouters)
+ if ((ip & nr.subnetMask) == (nr.subnetIP & nr.subnetMask))
+ return nr.routers;
+return {};
}
//-----------------------------------------------------------------------------
void REMOTE_SCRIPT::SetUserNotifiers(UserPtr u)
}
//-----------------------------------------------------------------------------
void REMOTE_SCRIPT::SetUserNotifiers(UserPtr u)
@@
-525,7
+503,7
@@
authorizedUsers.insert(std::make_pair(user->GetCurrIP(), rsu));
void REMOTE_SCRIPT::DelRSU(UserPtr user)
{
std::lock_guard lock(m_mutex);
void REMOTE_SCRIPT::DelRSU(UserPtr user)
{
std::lock_guard lock(m_mutex);
-
std::map<uint32_t, RS::USER>::iterator it(authorizedUsers.begin()
);
+
auto it = authorizedUsers.begin(
);
while (it != authorizedUsers.end())
{
if (it->second.user == user)
while (it != authorizedUsers.end())
{
if (it->second.user == user)
@@
-536,9
+514,7
@@
while (it != authorizedUsers.end())
}
++it;
}
}
++it;
}
-/*const std::map<uint32_t, RS::USER>::iterator it(
- authorizedUsers.find(user->GetCurrIP())
- );
+/*const auto it = authorizedUsers.find(user->GetCurrIP());
if (it != authorizedUsers.end())
{
Send(it->second, true);
if (it != authorizedUsers.end())
{
Send(it->second, true);
@@
-548,7
+524,7
@@
if (it != authorizedUsers.end())
//-----------------------------------------------------------------------------
void RS::IP_NOTIFIER::Notify(const uint32_t & /*oldValue*/, const uint32_t & newValue)
{
//-----------------------------------------------------------------------------
void RS::IP_NOTIFIER::Notify(const uint32_t & /*oldValue*/, const uint32_t & newValue)
{
-if (newValue)
+if (newValue
!= 0
)
rs.AddRSU(user);
else
rs.DelRSU(user);
rs.AddRSU(user);
else
rs.DelRSU(user);