Skip to content

Commit 73e7afa

Browse files
author
marttcw
committed
fixed thread issue, no more lambda, separated thread to other file
1 parent 81c64d7 commit 73e7afa

File tree

8 files changed

+145
-110
lines changed

8 files changed

+145
-110
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Minimal dependencies needed:
1818
### C++ Requirements
1919
* C++11 for std::thread
2020
* C++11 for std::function
21-
* C++11 for lambda expressions
2221
* C++17 for std::filesystem
2322
* C++17 for cleaner for loop over std::pair
2423

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project('steamcountsnotifyd'
22
, 'cpp'
33
, default_options : ['warning_level=3', 'cpp_std=c++17', 'optimization=3', 'werror=false']
4-
, version : 'PRE-ALPHA 20190910'
4+
, version : 'PRE-ALPHA 20190911'
55
)
66

77
cc = meson.get_compiler('cpp')
@@ -27,6 +27,7 @@ libnotify_dep = dependency('libnotify', required : true) # libnotify
2727
src = [
2828
'./src/main.cpp'
2929
, './src/core/params.cpp'
30+
, './src/core/counterThread.cpp'
3031
, './src/tool/args.cpp'
3132
, './src/tool/getPlayerCount.cpp'
3233
, './src/tool/homeDirectory.cpp'

src/core/counterThread.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "core/counterThread.h"
2+
3+
#include <thread>
4+
#include <chrono>
5+
6+
#include "tool/getPlayerCount.h"
7+
#include "wrapper/curl.h"
8+
#include "wrapper/notify.h"
9+
10+
void cthread::appidRunning(unsigned int appid, param::appidName_s game, const param::config &config)
11+
{
12+
unsigned int currentCount = 0;
13+
bool notify = false;
14+
bool running = true;
15+
std::string messageTitle;
16+
std::string messageDetails;
17+
wrapper::curl curlJob;
18+
unsigned int currentThreadInterval = config.getIntervalMins();
19+
20+
curlJob.setTimeout(config.getConnectionTimeout());
21+
22+
while (running)
23+
{
24+
curlJob.setUrl("https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid="+std::to_string(appid));
25+
26+
// Perform the job
27+
curlJob.perform();
28+
29+
// Determine message
30+
if (curlJob.getHttpResponseCode() == 200)
31+
{
32+
currentCount = tool::getPlayerCount(curlJob.getHttpData());
33+
// If threshold met, then start notifying the user
34+
if (currentCount >= game.threshold)
35+
{
36+
notify = true;
37+
messageTitle = game.name;
38+
messageDetails = "Player counts: "+std::to_string(currentCount);
39+
currentThreadInterval = config.getThresholdIntervalMins();
40+
}
41+
// Otherwise reset to the desired under threshold interval
42+
else
43+
{
44+
notify = false;
45+
currentThreadInterval = config.getIntervalMins();
46+
}
47+
}
48+
else
49+
{
50+
messageTitle = game.name+"(ERROR)";
51+
messageDetails = "Cannot fetch player numbers - no internet connection or steam API is down.";
52+
}
53+
54+
// Clear data
55+
curlJob.clearHttpData();
56+
57+
// Notify
58+
if (notify)
59+
{
60+
wrapper::notify notifyJob(messageTitle, messageDetails);
61+
notifyJob.setTimeout(config.getNotificationTimeout());
62+
notifyJob.show();
63+
notifyJob.unref();
64+
65+
notify = false;
66+
}
67+
68+
// Delay
69+
std::this_thread::sleep_for(std::chrono::minutes(currentThreadInterval));
70+
}
71+
}
72+

src/core/counterThread.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef CORE__COUNTERTHREAD_H
2+
#define CORE__COUNTERTHREAD_H
3+
4+
#include <functional>
5+
#include "core/params.h"
6+
7+
namespace cthread
8+
{
9+
void appidRunning(unsigned int appid, param::appidName_s game, const param::config &config);
10+
}
11+
12+
#endif

src/core/params.cpp

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,46 @@ void param::config::readConfigurationFile(const std::string &confFilePath)
1515
{
1616
std::unordered_map<std::string, unsigned int> strMapToVal;
1717
// Set default parameters
18-
strMapToVal["interval"] = 1;
19-
strMapToVal["thresholdinterval"] = 2;
20-
strMapToVal["connectiontimeout"] = 10;
21-
strMapToVal["notificationtimeout"] = 10;
22-
this->appidMap[244630] = {"NEOTOKYO", 0};
23-
this->appidMap[282440] = {"Quake Live", 100};
18+
strMapToVal["interval"] = 1; // Minutes
19+
strMapToVal["thresholdinterval"] = 2; // Minutes
20+
strMapToVal["connectiontimeout"] = 10; // Seconds
21+
strMapToVal["notificationtimeout"] = 10; // Seconds
2422

25-
// Configuration file stream
26-
std::ifstream confFile(confFilePath);
27-
28-
for (std::string line; std::getline(confFile, line); )
23+
// Go over the configuration file
24+
if (std::filesystem::exists(confFilePath))
2925
{
30-
std::istringstream iss(line);
31-
std::string parameter;
32-
appidName_s valAN;
33-
int val;
26+
// Configuration file stream
27+
std::ifstream confFile(confFilePath);
3428

35-
iss >> parameter;
36-
if (parameter == "newappid")
37-
{
38-
iss >> val >> std::quoted(valAN.name) >> valAN.threshold;
39-
this->appidMap[val] = valAN;
40-
}
41-
else
29+
for (std::string line; std::getline(confFile, line); )
4230
{
43-
iss >> val;
44-
if (val >= 0)
31+
std::istringstream iss(line);
32+
std::string parameter;
33+
appidName_s valAN;
34+
int val;
35+
36+
iss >> parameter;
37+
if (parameter == "newappid")
4538
{
46-
strMapToVal[parameter] = static_cast<unsigned int>(val);
39+
iss >> val >> std::quoted(valAN.name) >> valAN.threshold;
40+
this->appidMap[val] = valAN;
4741
}
4842
else
4943
{
50-
std::cerr << "ERROR: Value for '" << parameter << "' cannot be less than 0.\n";
44+
iss >> val;
45+
if (val >= 0)
46+
{
47+
strMapToVal[parameter] = static_cast<unsigned int>(val);
48+
}
49+
else
50+
{
51+
std::cerr << "ERROR: Value for '" << parameter << "' cannot be less than 0.\n";
52+
}
5153
}
5254
}
53-
}
5455

55-
confFile.close();
56+
confFile.close();
57+
}
5658

5759
this->intervalMins = strMapToVal["interval"];
5860
this->thresholdIntervalMins = strMapToVal["thresholdinterval"];
@@ -63,19 +65,7 @@ void param::config::readConfigurationFile(const std::string &confFilePath)
6365
param::config::config()
6466
{
6567
// Read configuration file
66-
const std::string &confFilePath = tool::getHomeDirectory()+"/.config/steamcountsnotifyd/config";
67-
68-
if (std::filesystem::exists(confFilePath))
69-
{
70-
this->readConfigurationFile(confFilePath);
71-
}
72-
73-
/* VERBOSE
74-
for (auto &an : this->appidMap)
75-
{
76-
std::cout << an.first << ' ' << an.second.name << ' ' << an.second.threshold << '\n';
77-
}
78-
*/
68+
this->readConfigurationFile(tool::getHomeDirectory()+"/.config/steamcountsnotifyd/config");
7969
}
8070

8171
param::config::~config()

src/main.cpp

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,45 @@
55
* version Pre-Alpha 2019-09-11
66
*
77
* TODO:
8-
* Threshold interval
9-
* Threadding
108
* Daemonise
119
*/
1210

1311
#include <iostream>
14-
#include <chrono>
1512
#include <thread>
1613
#include <deque>
1714
#include <vector>
1815
#include <functional>
1916

17+
#include "core/counterThread.h"
18+
#include "core/params.h"
19+
#include "tool/args.h"
20+
#include "tool/getPlayerCount.h"
2021
#include "wrapper/curl.h"
22+
#include "wrapper/daemon.h"
2123
#include "wrapper/notify.h"
22-
#include "tool/getPlayerCount.h"
23-
#include "tool/args.h"
24-
#include "core/params.h"
2524

25+
/*
26+
* Main function of steamcountsnotifyd
27+
*/
2628
int main(int argc, char **argv)
2729
{
2830
std::deque<std::string> args = tool::toArgs(argc, argv);
2931
args.pop_front(); // Program execution name not needed
3032
param::config config;
3133
bool running = config.setFromArgs(args);
3234

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();
43-
44-
curlJob.setTimeout(config.getConnectionTimeout());
45-
46-
while (running)
47-
{
48-
curlJob.setUrl("https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?appid="+std::to_string(appid));
49-
50-
// Perform the job
51-
curlJob.perform();
52-
53-
// Determine message
54-
if (curlJob.getHttpResponseCode() == 200)
55-
{
56-
currentCount = tool::getPlayerCount(curlJob.getHttpData());
57-
if (currentCount >= game.threshold)
58-
{
59-
notify = true;
60-
messageTitle = game.name;
61-
messageDetails = "Player counts: "+std::to_string(currentCount);
62-
}
63-
}
64-
else
65-
{
66-
messageTitle = "ERROR";
67-
messageDetails = "Cannot fetch player numbers - no internet connection or steam API is down.";
68-
}
69-
70-
// Clear data
71-
curlJob.clearHttpData();
72-
73-
// Notify
74-
if (notify)
75-
{
76-
wrapper::notify::init();
77-
wrapper::notify notifyJob(messageTitle, messageDetails);
78-
notifyJob.setTimeout(config.getNotificationTimeout());
79-
notifyJob.show();
80-
81-
notify = false;
82-
}
83-
84-
// Delay
85-
std::this_thread::sleep_for(std::chrono::minutes(currentThreadInterval));
86-
}
87-
};
88-
8935
// If help or version message not used (normal execution)
9036
if (running)
9137
{
38+
//std::function<void(unsigned int, param::appidName_s, const param::config &)> thread::appidRunning;
39+
wrapper::notify::init("steamcountsnotifyd");
40+
9241
// Initialise and run threads for each appid/game
9342
std::vector<std::thread> appidThreadVector;
9443

9544
for (const auto &[appid, game] : config.getAppidMap())
9645
{
97-
appidThreadVector.emplace_back(std::thread(appidRunningThread, appid, game, config));
46+
appidThreadVector.emplace_back(std::thread(std::function(cthread::appidRunning), appid, game, config));
9847
}
9948

10049
// Join thread if joinable
@@ -105,6 +54,8 @@ int main(int argc, char **argv)
10554
thread.join();
10655
}
10756
}
57+
58+
wrapper::notify::uninit();
10859
}
10960

11061
return 0;

src/wrapper/notify.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ wrapper::notify::notify(std::string_view summary, std::string_view body, std::st
77

88
wrapper::notify::~notify()
99
{
10-
g_object_unref(G_OBJECT(this->notification));
11-
notify_uninit();
1210
}
1311

14-
void wrapper::notify::init()
12+
void wrapper::notify::init(std::string_view threadName)
13+
{
14+
notify_init(threadName.data());
15+
}
16+
17+
void wrapper::notify::uninit()
1518
{
16-
notify_init("neonotifytokyo");
19+
notify_uninit();
1720
}
1821

1922
void wrapper::notify::show()
@@ -25,3 +28,8 @@ void wrapper::notify::setTimeout(unsigned int timeout)
2528
{
2629
notify_notification_set_timeout(this->notification, timeout);
2730
}
31+
32+
void wrapper::notify::unref()
33+
{
34+
g_object_unref(G_OBJECT(this->notification));
35+
}

src/wrapper/notify.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ namespace wrapper
1313
public:
1414
notify(std::string_view summary, std::string_view body, std::string_view icon = "dialog-information");
1515
~notify();
16-
void static init();
16+
void static init(std::string_view threadName);
17+
void static uninit();
1718
void show();
1819
void setTimeout(unsigned int timeout);
20+
void unref();
1921
};
2022
}
2123

0 commit comments

Comments
 (0)