py-stinger-nng 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_stinger_nng-0.1.0/.agents/skills/release/SKILL.md +92 -0
- py_stinger_nng-0.1.0/.github/workflows/python-tests.yml +34 -0
- py_stinger_nng-0.1.0/.gitignore +228 -0
- py_stinger_nng-0.1.0/.python-version +1 -0
- py_stinger_nng-0.1.0/LICENSE +21 -0
- py_stinger_nng-0.1.0/PKG-INFO +11 -0
- py_stinger_nng-0.1.0/README.md +0 -0
- py_stinger_nng-0.1.0/Taskfile.yml +37 -0
- py_stinger_nng-0.1.0/examples/pubsub.py +122 -0
- py_stinger_nng-0.1.0/pyproject.toml +44 -0
- py_stinger_nng-0.1.0/src/py_stinger_nng/__init__.py +0 -0
- py_stinger_nng-0.1.0/src/py_stinger_nng/client.py +374 -0
- py_stinger_nng-0.1.0/src/py_stinger_nng/server.py +288 -0
- py_stinger_nng-0.1.0/tests/__init__.py +0 -0
- py_stinger_nng-0.1.0/tests/conftest.py +207 -0
- py_stinger_nng-0.1.0/tests/test_client.py +132 -0
- py_stinger_nng-0.1.0/tests/test_end_to_end.py +394 -0
- py_stinger_nng-0.1.0/tests/test_server.py +110 -0
- py_stinger_nng-0.1.0/uv.lock +623 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: release
|
|
3
|
+
description: "Release py-stinger-nng. Use when: releasing a new version, cutting a release, bumping the version, publishing, shipping, preparing a release, running pre-release checks."
|
|
4
|
+
argument-hint: "optional notes or release context"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Release py-stinger-nng
|
|
8
|
+
|
|
9
|
+
Runs pre-release checks, bumps the version in `pyproject.toml`, then offers to
|
|
10
|
+
commit and push.
|
|
11
|
+
|
|
12
|
+
Always verify the working tree is clean before making changes. Run `black`
|
|
13
|
+
first so `mypy` and `ruff` see formatted source. Fail fast: stop at the first
|
|
14
|
+
failing check. Never commit, tag, or publish without explicit confirmation.
|
|
15
|
+
|
|
16
|
+
## Procedure
|
|
17
|
+
|
|
18
|
+
### 1. Format and lint
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
uv run black --target-version py312 src tests examples
|
|
22
|
+
uv run ruff check src tests examples
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Fix anything ruff reports before continuing.
|
|
26
|
+
|
|
27
|
+
### 2. Run mypy
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
uv run mypy --check-untyped-defs src tests examples
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
If mypy reports errors, fix them before continuing. Do not proceed until mypy
|
|
34
|
+
is clean.
|
|
35
|
+
|
|
36
|
+
### 3. Run pytest
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
uv run pytest tests
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The socket tests are timing-sensitive. If anything fails, re-run once to rule
|
|
43
|
+
out flakiness, then fix it. Do not proceed until all tests pass.
|
|
44
|
+
|
|
45
|
+
### 4. Smoke-test the examples
|
|
46
|
+
|
|
47
|
+
In one shell run `uv run examples/server_demo.py`, in another
|
|
48
|
+
`uv run examples/client_demo.py`. Confirm the client prints property values
|
|
49
|
+
immediately on connect, then receives `tick` signals. Stop both.
|
|
50
|
+
|
|
51
|
+
### 5. Bump the version
|
|
52
|
+
|
|
53
|
+
Read the current version from `pyproject.toml`. Then ask the user:
|
|
54
|
+
|
|
55
|
+
> The current version is `X.Y.Z`. How would you like to bump it?
|
|
56
|
+
> - **patch** → `X.Y.Z+1`
|
|
57
|
+
> - **minor** → `X.Y+1.0`
|
|
58
|
+
> - **major** → `X+1.0.0`
|
|
59
|
+
> - **manual** → enter a custom version string
|
|
60
|
+
|
|
61
|
+
Apply the chosen version to the `version` field in `pyproject.toml`.
|
|
62
|
+
|
|
63
|
+
### 6. Confirm commit and push
|
|
64
|
+
|
|
65
|
+
Ask the user:
|
|
66
|
+
|
|
67
|
+
> Ready to commit and push `vX.Y.Z` to `origin/main`?
|
|
68
|
+
> - **Yes** — stage `pyproject.toml` and `uv.lock`, commit with message
|
|
69
|
+
> `chore: bump version to X.Y.Z`, then push to `origin main`
|
|
70
|
+
> - **No** — stop here; leave the version bump in place for the user to handle
|
|
71
|
+
> manually
|
|
72
|
+
|
|
73
|
+
Only run `git add`, `git commit`, and `git push` if the user explicitly
|
|
74
|
+
confirms.
|
|
75
|
+
|
|
76
|
+
### 7. Build
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
uv build
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
If the build fails, report the error and stop. Do not proceed to publish.
|
|
83
|
+
|
|
84
|
+
### 8. Confirm publish
|
|
85
|
+
|
|
86
|
+
Ask the user:
|
|
87
|
+
|
|
88
|
+
> Build succeeded. Publish `vX.Y.Z` to PyPI with `uv publish`?
|
|
89
|
+
> - **Yes** — run `uv publish`
|
|
90
|
+
> - **No** — stop here; the built artifacts are in `dist/`
|
|
91
|
+
|
|
92
|
+
Only run `uv publish` if the user explicitly confirms.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Python tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ['3.12', '3.13', '3.14']
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Install uv
|
|
20
|
+
uses: astral-sh/setup-uv@v7
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: uv sync --locked --dev
|
|
26
|
+
|
|
27
|
+
- name: Run tests
|
|
28
|
+
run: uv run pytest tests
|
|
29
|
+
|
|
30
|
+
- name: Type check
|
|
31
|
+
run: uv run mypy --check-untyped-defs ./src/
|
|
32
|
+
|
|
33
|
+
- name: Lint
|
|
34
|
+
run: uv run ruff check src tests examples
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
*.lcov
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
# Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# UV
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# uv.lock
|
|
103
|
+
|
|
104
|
+
# poetry
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
106
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
107
|
+
# commonly ignored for libraries.
|
|
108
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
109
|
+
# poetry.lock
|
|
110
|
+
# poetry.toml
|
|
111
|
+
|
|
112
|
+
# pdm
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
114
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
115
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
116
|
+
# pdm.lock
|
|
117
|
+
# pdm.toml
|
|
118
|
+
.pdm-python
|
|
119
|
+
.pdm-build/
|
|
120
|
+
|
|
121
|
+
# pixi
|
|
122
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
123
|
+
# pixi.lock
|
|
124
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
125
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
126
|
+
.pixi/*
|
|
127
|
+
!.pixi/config.toml
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule*
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
|
|
201
|
+
# can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer, you
|
|
203
|
+
# could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
# .vscode/
|
|
205
|
+
# Temporary file for partial code execution
|
|
206
|
+
tempCodeRunnerFile.py
|
|
207
|
+
|
|
208
|
+
# Ruff stuff:
|
|
209
|
+
.ruff_cache/
|
|
210
|
+
|
|
211
|
+
# PyPI configuration file
|
|
212
|
+
.pypirc
|
|
213
|
+
|
|
214
|
+
# Marimo
|
|
215
|
+
marimo/_static/
|
|
216
|
+
marimo/_lsp/
|
|
217
|
+
__marimo__/
|
|
218
|
+
|
|
219
|
+
# Streamlit
|
|
220
|
+
.streamlit/secrets.toml
|
|
221
|
+
|
|
222
|
+
# IDEs
|
|
223
|
+
.vscode/
|
|
224
|
+
.idea/
|
|
225
|
+
*.swp
|
|
226
|
+
|
|
227
|
+
# Project specific
|
|
228
|
+
dist/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jacob Brunson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: py-stinger-nng
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Stinger-style IPC (signals, properties, methods) over NNG
|
|
5
|
+
Author-email: Jacob Brunson <github@jacobbrunson.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: py-stinger-conn-iface==0.1.2
|
|
10
|
+
Requires-Dist: pyfory>=1.7.2
|
|
11
|
+
Requires-Dist: pynng>=0.9.0
|
|
File without changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# yaml-language-server: $schema=https://taskfile.dev/schema.json
|
|
2
|
+
|
|
3
|
+
version: '3'
|
|
4
|
+
|
|
5
|
+
tasks:
|
|
6
|
+
default:
|
|
7
|
+
desc: Format, lint, type-check and test
|
|
8
|
+
cmds:
|
|
9
|
+
- task: format
|
|
10
|
+
- task: lint
|
|
11
|
+
- task: type-check
|
|
12
|
+
- task: test
|
|
13
|
+
|
|
14
|
+
type-check:
|
|
15
|
+
desc: Run mypy type checks on src, tests and examples
|
|
16
|
+
cmds:
|
|
17
|
+
- uv run mypy --check-untyped-defs src tests examples
|
|
18
|
+
|
|
19
|
+
format:
|
|
20
|
+
desc: Format code with black
|
|
21
|
+
cmds:
|
|
22
|
+
- uv run black --target-version py312 src tests examples
|
|
23
|
+
|
|
24
|
+
test:
|
|
25
|
+
desc: Run all unit tests
|
|
26
|
+
cmds:
|
|
27
|
+
- uv run pytest tests
|
|
28
|
+
|
|
29
|
+
lint:
|
|
30
|
+
desc: Lint code with ruff
|
|
31
|
+
cmds:
|
|
32
|
+
- uv run ruff check src tests examples
|
|
33
|
+
|
|
34
|
+
sync:
|
|
35
|
+
desc: Sync dependencies with uv
|
|
36
|
+
cmds:
|
|
37
|
+
- uv sync
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""
|
|
2
|
+
A runnable tour of the NNG bus: one server, two clients, and the three things the
|
|
3
|
+
transport gives you that a plain socket does not — topic-filtered subscriptions,
|
|
4
|
+
client-to-client relaying through the server, and retained state that a client which
|
|
5
|
+
joins late can catch up on.
|
|
6
|
+
|
|
7
|
+
Run it with `uv run python examples/pubsub.py`.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import threading
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
from pystingerconniface import ContentType, Message
|
|
14
|
+
|
|
15
|
+
from py_stinger_nng.client import StingerNngClient
|
|
16
|
+
from py_stinger_nng.server import StingerNngServer
|
|
17
|
+
|
|
18
|
+
PUBLISH_PORT = 15555
|
|
19
|
+
RECEIVE_PORT = 15556
|
|
20
|
+
|
|
21
|
+
#: How long to wait on any single step before giving up. Everything here is loopback,
|
|
22
|
+
#: so reaching this means something is genuinely wrong rather than merely slow.
|
|
23
|
+
TIMEOUT_S = 5.0
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def wait_for(event: threading.Event, description: str) -> None:
|
|
27
|
+
if not event.wait(TIMEOUT_S):
|
|
28
|
+
raise TimeoutError(f"timed out waiting for {description}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def wait_until_connected(client: StingerNngClient) -> None:
|
|
32
|
+
"""
|
|
33
|
+
Both of a client's sockets dial in the background, so a freshly constructed client
|
|
34
|
+
is usable but not yet connected. Publishing before it is would fail the future.
|
|
35
|
+
"""
|
|
36
|
+
deadline = time.monotonic() + TIMEOUT_S
|
|
37
|
+
while time.monotonic() < deadline:
|
|
38
|
+
if client.is_connected():
|
|
39
|
+
return
|
|
40
|
+
time.sleep(0.01)
|
|
41
|
+
raise TimeoutError(f"{client.client_id} never connected")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def main() -> None:
|
|
45
|
+
with StingerNngServer(PUBLISH_PORT, RECEIVE_PORT, client_id="hub") as server:
|
|
46
|
+
# The server sees everything sent to its PULL socket, so a general callback is
|
|
47
|
+
# enough to act as a broker: here it relays every reading out to subscribers.
|
|
48
|
+
def relay(message: Message) -> None:
|
|
49
|
+
print(f"hub <- {message.topic}: {message.payload!r}")
|
|
50
|
+
server.publish(
|
|
51
|
+
Message(
|
|
52
|
+
topic=f"broadcast/{message.topic}",
|
|
53
|
+
payload=message.payload,
|
|
54
|
+
qos=message.qos,
|
|
55
|
+
content_type=message.content_type,
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
server.add_message_callback(relay)
|
|
60
|
+
|
|
61
|
+
# Retained state is held by the server, so it is published before either client
|
|
62
|
+
# exists and is still available to whoever asks for it later.
|
|
63
|
+
server.publish(
|
|
64
|
+
Message(topic="config/units", payload=b"celsius", qos=1, retain=True)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
with (
|
|
68
|
+
StingerNngClient(PUBLISH_PORT, RECEIVE_PORT, client_id="sensor") as sensor,
|
|
69
|
+
StingerNngClient(
|
|
70
|
+
PUBLISH_PORT, RECEIVE_PORT, client_id="display"
|
|
71
|
+
) as display,
|
|
72
|
+
):
|
|
73
|
+
wait_until_connected(sensor)
|
|
74
|
+
wait_until_connected(display)
|
|
75
|
+
|
|
76
|
+
relayed = threading.Event()
|
|
77
|
+
|
|
78
|
+
def on_broadcast(message: Message) -> None:
|
|
79
|
+
print(f"display <- {message.topic}: {message.payload!r}")
|
|
80
|
+
relayed.set()
|
|
81
|
+
|
|
82
|
+
# A client's filtering happens at the socket, so it only ever sees topics it
|
|
83
|
+
# subscribed to — unlike the server, which sees the whole bus.
|
|
84
|
+
display.subscribe("broadcast/", on_broadcast)
|
|
85
|
+
|
|
86
|
+
configured = threading.Event()
|
|
87
|
+
cleared = threading.Event()
|
|
88
|
+
|
|
89
|
+
def on_config(message: Message) -> None:
|
|
90
|
+
if message.payload:
|
|
91
|
+
print(f"display <- {message.topic}: {message.payload!r}")
|
|
92
|
+
configured.set()
|
|
93
|
+
else:
|
|
94
|
+
# An empty retained payload is a tombstone, not a value.
|
|
95
|
+
print(f"display <- {message.topic}: cleared")
|
|
96
|
+
cleared.set()
|
|
97
|
+
|
|
98
|
+
display.subscribe("config/", on_config)
|
|
99
|
+
# The client also syncs on its own whenever the SUB socket connects, which
|
|
100
|
+
# here was before the subscription above existed. Asking again replays it
|
|
101
|
+
# now — and note that a replay is a normal publication, so a subscriber has
|
|
102
|
+
# to tolerate seeing the same retained value more than once.
|
|
103
|
+
display.request_sync()
|
|
104
|
+
wait_for(configured, "the retained configuration")
|
|
105
|
+
|
|
106
|
+
reading = Message(
|
|
107
|
+
topic="sensor/temperature",
|
|
108
|
+
payload=b'{"celsius": 21.5}',
|
|
109
|
+
qos=1,
|
|
110
|
+
content_type=ContentType("application/json; charset=utf-8"),
|
|
111
|
+
)
|
|
112
|
+
sensor.publish(reading).result(TIMEOUT_S)
|
|
113
|
+
wait_for(relayed, "the relayed reading")
|
|
114
|
+
|
|
115
|
+
# Clearing retained state publishes a tombstone: live subscribers are told,
|
|
116
|
+
# and a client that connects afterwards is not.
|
|
117
|
+
server.unpublish_retained("config/units")
|
|
118
|
+
wait_for(cleared, "the retained configuration to be cleared")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if __name__ == "__main__":
|
|
122
|
+
main()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "py-stinger-nng"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Stinger-style IPC (signals, properties, methods) over NNG"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = [
|
|
8
|
+
{ name = "Jacob Brunson", email = "github@jacobbrunson.com" }
|
|
9
|
+
]
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"py-stinger-conn-iface==0.1.2",
|
|
13
|
+
"pyfory>=1.7.2",
|
|
14
|
+
"pynng>=0.9.0",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[tool.hatch.build.targets.wheel]
|
|
18
|
+
packages = ["src/py_stinger_nng"]
|
|
19
|
+
|
|
20
|
+
[build-system]
|
|
21
|
+
requires = ["hatchling"]
|
|
22
|
+
build-backend = "hatchling.build"
|
|
23
|
+
|
|
24
|
+
[tool.uv]
|
|
25
|
+
managed = true
|
|
26
|
+
|
|
27
|
+
# Neither dependency ships a py.typed marker, so mypy will not read them by default.
|
|
28
|
+
# pystingerconniface is annotated throughout and defines the types this package is
|
|
29
|
+
# written against, so it is followed anyway rather than flattened to Any.
|
|
30
|
+
[[tool.mypy.overrides]]
|
|
31
|
+
module = ["pystingerconniface.*"]
|
|
32
|
+
follow_untyped_imports = true
|
|
33
|
+
|
|
34
|
+
[[tool.mypy.overrides]]
|
|
35
|
+
module = ["pyfory.*", "pynng.*"]
|
|
36
|
+
ignore_missing_imports = true
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = [
|
|
40
|
+
"black>=23.3.0",
|
|
41
|
+
"mypy>=1.4.1",
|
|
42
|
+
"pytest>=7.4.4",
|
|
43
|
+
"ruff>=0.15.21",
|
|
44
|
+
]
|
|
File without changes
|