Skip to content

Commit e11facb

Browse files
Merge pull request #10 from NikolasK-source/main
v1.0.0
2 parents bc58b82 + c466a2e commit e11facb

12 files changed

+55
-110
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
[submodule "libs/cxxopts"]
22
path = libs/cxxopts
33
url = https://github.com/NikolasK-source/cxxopts.git
4+
[submodule "libs/cxxshm"]
5+
path = libs/cxxshm
6+
url = https://github.com/NikolasK-source/cxxshm.git
7+
[submodule "libs/cxxsemaphore"]
8+
path = libs/cxxsemaphore
9+
url = https://github.com/NikolasK-source/cxxsemaphore.git

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.13.4 FATAL_ERROR)
44
# ======================================================================================================================
55

66
# project
7-
project(write-shm LANGUAGES CXX VERSION 0.1.3)
7+
project(write-shm LANGUAGES CXX VERSION 1.0.0)
88

99
# settings
1010
set(Target "write-shm") # Executable name (without file extension!)

docs/index.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,19 @@ write-shm -n mem < file
4545
write-shm -n mem -r < file
4646
```
4747

48-
## Using the Flatpak package
48+
## Install
49+
50+
### Using the Arch User Repository (recommended for Arch based Linux distributions)
51+
The application is available as [write-shm](https://aur.archlinux.org/packages/write-shm) in the [Arch User Repository](https://aur.archlinux.org/).
52+
See the [Arch Wiki](https://wiki.archlinux.org/title/Arch_User_Repository) for information about how to install AUR packages.
53+
54+
55+
### Using the Modbus Collection Flatpak Package: Shared Memory Modbus
56+
[SHM-Modbus](https://nikolask-source.github.io/SHM_Modbus/) is a collection of the shared memory modbus tools.
57+
It is available as flatpak and published on flathub as ```network.koesling.shm-modbs```.
58+
59+
60+
### Using the Standalone Flatpak package
4961
The flatpak package can be installed via the .flatpak file.
5062
This can be downloaded from the GitHub [projects release page](https://github.com/NikolasK-source/write_shm/releases):
5163

@@ -62,7 +74,7 @@ To enable calling with ```write-shm``` [this script](https://gist.github.com/Nik
6274
In order to be executable everywhere, the path in which the script is placed must be in the ```PATH``` environment variable.
6375

6476

65-
## Build from Source
77+
### Build from Source
6678

6779
The following packages are required for building the application:
6880
- cmake

libs/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
# ---------------------------------------- subdirectories --------------------------------------------------------------
33
# ======================================================================================================================
44

5-
5+
add_subdirectory(cxxopts EXCLUDE_FROM_ALL)
6+
add_subdirectory(cxxsemaphore)
7+
add_subdirectory(cxxshm)
68

79
# ---------------------------------------- link libraries --------------------------------------------------------------
810
# ======================================================================================================================
9-
target_link_libraries(${Target} PRIVATE rt)
1011

11-
add_subdirectory(cxxopts EXCLUDE_FROM_ALL)
12+
target_link_libraries(${Target} PRIVATE rt)
1213
target_link_libraries(${Target} PRIVATE cxxopts)
14+
target_link_libraries(${Target} PRIVATE cxxsemaphore)
15+
target_link_libraries(${Target} PRIVATE cxxshm)

libs/cxxsemaphore

Submodule cxxsemaphore added at c5aec9e

libs/cxxshm

Submodule cxxshm added at fc145c4

network.koesling.test-write-shm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
id: network.koesling.test-write-shm
22
runtime: org.freedesktop.Platform
3-
runtime-version: '21.08'
3+
runtime-version: '23.08'
44
sdk: org.freedesktop.Sdk
55
command: write-shm
66
finish-args:

network.koesling.write-shm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
id: network.koesling.write-shm
22
runtime: org.freedesktop.Platform
3-
runtime-version: '21.08'
3+
runtime-version: '23.08'
44
sdk: org.freedesktop.Sdk
55
command: write-shm
66
finish-args:

src/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
# ======================================================================================================================
33

44
target_sources(${Target} PRIVATE main.cpp)
5-
target_sources(${Target} PRIVATE SHM.cpp)
65
target_sources(${Target} PRIVATE license.cpp)
76

87
# ---------------------------------------- header files (*.jpp, *.h, ...) ----------------------------------------------
98
# ======================================================================================================================
109

11-
target_sources(${Target} PRIVATE SHM.hpp)
1210
target_sources(${Target} PRIVATE license.hpp)
1311

1412
# ---------------------------------------- subdirectories --------------------------------------------------------------

src/SHM.cpp

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/SHM.hpp

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/main.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* This program is free software. You can redistribute it and/or modify it under the terms of the MIT License.
44
*/
55

6-
#include "SHM.hpp"
76
#include "license.hpp"
87

98
#include <cxxopts.hpp>
9+
#include <cxxsemaphore.hpp>
10+
#include <cxxshm.hpp>
1011
#include <filesystem>
1112
#include <iostream>
1213
#include <memory>
@@ -29,6 +30,9 @@ int main(int argc, char **argv) {
2930
options.add_options()("p,passthrough",
3031
"output everything that is written to the shared memory to stdout",
3132
cxxopts::value<bool>()->default_value("false"));
33+
options.add_options()("s,semaphore",
34+
"protect the shared memory with an existing named semaphore against simultaneous access",
35+
cxxopts::value<std::string>());
3236
options.add_options()("h,help", "print usage");
3337
options.add_options()("version", "print version information");
3438
options.add_options()("license", "show licenses");
@@ -67,15 +71,28 @@ int main(int argc, char **argv) {
6771
}
6872

6973
// open shm
70-
std::unique_ptr<SHM> shm;
74+
std::unique_ptr<cxxshm::SharedMemory> shm;
7175
try {
72-
shm = std::make_unique<SHM>(args["name"].as<std::string>());
76+
shm = std::make_unique<cxxshm::SharedMemory>(args["name"].as<std::string>());
7377
} catch (const std::system_error &e) {
7478
std::cerr << e.what() << std::endl;
7579
exit(EX_OSERR);
7680
} catch (const cxxopts::option_has_no_value_exception &) {
7781
std::cerr << "Specifying an shared memory name is mandatory." << std::endl;
7882
exit_usage();
83+
} catch (const std::exception &e) {
84+
std::cerr << e.what() << std::endl;
85+
exit(EX_SOFTWARE);
86+
}
87+
88+
std::unique_ptr<cxxsemaphore::Semaphore> semaphore;
89+
if (args.count("semaphore")) {
90+
try {
91+
semaphore = std::make_unique<cxxsemaphore::Semaphore>(args["semaphore"].as<std::string>());
92+
} catch (std::exception &e) {
93+
std::cerr << e.what() << std::endl;
94+
return EX_SOFTWARE;
95+
}
7996
}
8097

8198
const auto SHM_SIZE = shm->get_size();
@@ -88,6 +105,8 @@ int main(int argc, char **argv) {
88105

89106
const int INVERT_MASK = args["invert"].as<bool>() ? ~0 : 0;
90107

108+
if (semaphore) semaphore->wait();
109+
91110
// copy file to shm (and buffer)
92111
std::size_t remaining = SHM_SIZE;
93112
std::size_t pos = 0;
@@ -124,4 +143,6 @@ int main(int argc, char **argv) {
124143
remaining -= COPY_SIZE;
125144
}
126145
}
146+
147+
if (semaphore) semaphore->post();
127148
}

0 commit comments

Comments
 (0)