66 *
77 * TODO:
88 * Threshold interval
9+ * Threadding
910 * Daemonise
1011 */
1112
1213#include < iostream>
1314#include < chrono>
1415#include < thread>
1516#include < deque>
17+ #include < vector>
18+ #include < functional>
1619
1720#include " wrapper/curl.h"
1821#include " wrapper/notify.h"
@@ -24,24 +27,23 @@ int main(int argc, char **argv)
2427{
2528 std::deque<std::string> args = tool::toArgs (argc, argv);
2629 args.pop_front (); // Program execution name not needed
27-
2830 param::config config;
29- unsigned int currentCount = 0 ;
30- bool running = true ;
31- bool notify = false ;
32- std::string messageTitle;
33- std::string messageDetails;
34- wrapper::curl curlJob;
31+ bool running = config.setFromArgs (args);
3532
36- running = config.setFromArgs (args);
33+ // Functional lambda
34+ std::function<void (unsigned int , param::appidName_s, const param::config &)> appidRunningThread = [](unsigned int appid, param::appidName_s game, const param::config &config)
35+ {
36+ unsigned int currentCount = 0 ;
37+ bool notify = false ;
38+ bool running = true ;
39+ std::string messageTitle;
40+ std::string messageDetails;
41+ wrapper::curl curlJob;
42+ unsigned int currentThreadInterval = config.getIntervalMins ();
3743
38- curlJob.setTimeout (config.getConnectionTimeout ());
44+ curlJob.setTimeout (config.getConnectionTimeout ());
3945
40- // Running state
41- while (running)
42- {
43- // Loop through list
44- for (const auto &[appid, game] : config.getAppidMap ())
46+ while (running)
4547 {
4648 curlJob.setUrl (" https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid=" +std::to_string (appid));
4749
@@ -78,10 +80,31 @@ int main(int argc, char **argv)
7880
7981 notify = false ;
8082 }
83+
84+ // Delay
85+ std::this_thread::sleep_for (std::chrono::minutes (currentThreadInterval));
86+ }
87+ };
88+
89+ // If help or version message not used (normal execution)
90+ if (running)
91+ {
92+ // Initialise and run threads for each appid/game
93+ std::vector<std::thread> appidThreadVector;
94+
95+ for (const auto &[appid, game] : config.getAppidMap ())
96+ {
97+ appidThreadVector.emplace_back (std::thread (appidRunningThread, appid, game, config));
8198 }
8299
83- // Delay
84- std::this_thread::sleep_for (std::chrono::minutes (config.getIntervalMins ()));
100+ // Join thread if joinable
101+ for (std::thread &thread : appidThreadVector)
102+ {
103+ if (thread.joinable ())
104+ {
105+ thread.join ();
106+ }
107+ }
85108 }
86109
87110 return 0 ;
0 commit comments