Skip to content

Commit 81c64d7

Browse files
author
marttcw
committed
multithreaded and threshold time difference
1 parent d4975bd commit 81c64d7

File tree

5 files changed

+103
-39
lines changed

5 files changed

+103
-39
lines changed

README.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
# steamcountsnotifyd
2-
SteamCountsNotifyD is a daemon that notifies you when your selected games gets some player activity
2+
SteamCountsNotifyD is a multithreaded daemon that notifies you when your selected games gets some player activity
33

44
## License
55
SteamCountsNotifyD is released under a free software license (MIT)
66

77
## Dependencies
8-
Minimal dependencies needed, only libcurl to download and libnotify to notify.
9-
* [libcurl](https://curl.haxx.se/libcurl/)
10-
* [libnotify](https://developer.gnome.org/libnotify/)
8+
Minimal dependencies needed:
9+
* [libcurl](https://curl.haxx.se/libcurl/) - For downloading
10+
* [libnotify](https://developer.gnome.org/libnotify/) - For notification
1111

1212
## Requirements to build
13-
* [meson](https://mesonbuild.com/)
14-
* [ninja](https://ninja-build.org/)
13+
* [meson](https://mesonbuild.com/) - To generate ninja file compile/install script
14+
* [ninja](https://ninja-build.org/) - To compile/install the project
1515
* C++17 capable compiler
1616
* [GCC](https://gcc.gnu.org/) - Recommends 9.2 or 8.3
1717
* [Clang](https://clang.llvm.org/)
18+
### C++ Requirements
19+
* C++11 for std::thread
20+
* C++11 for std::function
21+
* C++11 for lambda expressions
22+
* C++17 for std::filesystem
23+
* C++17 for cleaner for loop over std::pair
1824

1925
## Instructions
2026
### Compile
2127
* `cd steamcountsnotifyd`
2228
* `meson build && cd build`
2329
* `ninja`
2430
### Install
25-
* TODO
31+
* `cd steamcountsnotifyd/build/`
32+
* `ninja install` - use `sudo` if necessary
33+
### Uninstall
34+
* `cd steamcountsnotifyd/build/`
35+
* `ninja uninstall` - use `sudo` if necessary
2636

2737
## Usage
28-
* TODO
38+
* Refer to `steamcountsnotifyd -h`
39+
40+
## Configuration
41+
* Uses xdg directory: `$HOME/.config/steamcountsnotifyd/config`
42+
### Example
43+
```
44+
interval 1
45+
thresholdinterval 2
46+
connectiontimeout 10
47+
notifytimeout 10
48+
49+
newappid 244630 "NEOTOKYO" 0
50+
newappid 282440 "Quake Live" 100
51+
```
2952

meson.build

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@ project('steamcountsnotifyd'
44
, version : 'PRE-ALPHA 20190910'
55
)
66

7-
add_project_arguments(['-lstdc++fs'], language : 'cpp')
7+
cc = meson.get_compiler('cpp')
8+
9+
# C++17 filesystem arguments
10+
if cc.get_id() == 'gcc'
11+
message('Compiler: GCC')
12+
add_project_arguments(['-lstdc++fs'], language : 'cpp')
13+
add_project_link_arguments(['-lstdc++fs'], language : 'cpp')
14+
elif cc.get_id() == 'clang'
15+
message('Compiler: LLVM/clang')
16+
add_project_arguments(['-stdlib=libc++'], language : 'cpp')
17+
add_project_link_arguments(['-stdlib=libc++','-lstdc++fs'], language : 'cpp')
18+
endif
819

920
src_inc = include_directories('./src/')
10-
local_inc = include_directories('/usr/local/include/')
1121

12-
libcurl_dep = dependency('libcurl', required : true)
13-
libnotify_dep = dependency('libnotify', required : true)
22+
# Dependencies
23+
thread_dep = dependency('threads') # C++11 threads
24+
libcurl_dep = dependency('libcurl', required : true) # libcurl
25+
libnotify_dep = dependency('libnotify', required : true) # libnotify
1426

1527
src = [
1628
'./src/main.cpp'
@@ -24,8 +36,9 @@ src = [
2436

2537
executable('steamcountsnotifyd'
2638
, sources : src
27-
, dependencies : [libcurl_dep, libnotify_dep]
28-
, include_directories : [src_inc, local_inc]
39+
, dependencies : [thread_dep, libcurl_dep, libnotify_dep]
40+
, include_directories : [src_inc]
41+
, install : true
2942
)
3043

3144

src/core/params.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,27 @@ param::config::~config()
8282
{
8383
}
8484

85-
unsigned int param::config::getIntervalMins()
85+
unsigned int param::config::getIntervalMins() const
8686
{
8787
return this->intervalMins;
8888
}
8989

90-
unsigned int param::config::getConnectionTimeout()
90+
unsigned int param::config::getThresholdIntervalMins() const
91+
{
92+
return this->thresholdIntervalMins;
93+
}
94+
95+
unsigned int param::config::getConnectionTimeout() const
9196
{
9297
return this->connectionTimeout;
9398
}
9499

95-
unsigned int param::config::getNotificationTimeout()
100+
unsigned int param::config::getNotificationTimeout() const
96101
{
97102
return this->notificationTimeout;
98103
}
99104

100-
param::appidNameMap param::config::getAppidMap()
105+
param::appidNameMap param::config::getAppidMap() const
101106
{
102107
return this->appidMap;
103108
}

src/core/params.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ namespace param
3030
public:
3131
config();
3232
~config();
33-
unsigned int getIntervalMins();
34-
unsigned int getThresholdIntervalMins();
35-
unsigned int getConnectionTimeout();
36-
unsigned int getNotificationTimeout();
37-
appidNameMap getAppidMap();
33+
unsigned int getIntervalMins() const;
34+
unsigned int getThresholdIntervalMins() const;
35+
unsigned int getConnectionTimeout() const;
36+
unsigned int getNotificationTimeout() const;
37+
appidNameMap getAppidMap() const;
3838
bool setFromArgs(std::deque<std::string> &args);
3939
};
4040
}

src/main.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
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

Comments
 (0)