mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Moved the PureBasic version to OldSource/, Started the Rust rewrite in Source/
The rewrite's plan and decisions are in revamp.md. Steps 0 to 10 are done: the workspace and checks, bakashrot-proto, bakashrot-pki and bakashrot-cert, bakashrot-config, bakashrot-storage (schema v9), the server and the client library (every request but replication's, attachments and retention included), the C DLL with its C, PureBasic and Python bindings, and the bakashrot tool. Step 11 (replication) is next. OldSource/ is the old implementation, kept building and passing as the reference. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RwFeVTGs2SsufaH6zquzcb
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
# Examples
|
||||
|
||||
Ready-to-run setups, each in its own folder with its configs, scripts to
|
||||
start it, and a README. Build first (`Build.ps1` at the root): the scripts
|
||||
take the exes from `Build\`, or from a release folder with `-BinDir`.
|
||||
|
||||
| Example | What it shows |
|
||||
|---|---|
|
||||
| [`SingleServer`](SingleServer/README.md) | One server, one client: pushing, reading, attachments, names, SQL, with the tools. |
|
||||
|
||||
Their keys are in the repository, so public: they're only for trying the
|
||||
examples locally, never reuse them.
|
||||
@@ -0,0 +1,136 @@
|
||||
# One server, one client
|
||||
|
||||
The smallest BakaShrot setup: one server on `127.0.0.1:12360`, one client
|
||||
that pushes and reads messages with the tools.
|
||||
|
||||
> **The keys in `server.ini` and `client.ini` are public, they're in the
|
||||
> repository.** They're only for trying this example on your own machine
|
||||
> (the server only listens on 127.0.0.1). Never reuse them: make your own
|
||||
> with `BakaShrotGenerateKeyPairs.exe`.
|
||||
|
||||
## Running it
|
||||
|
||||
Build first, `Build.ps1` at the root, or use a release folder (`-BinDir
|
||||
Release\<version>` on both scripts). Then, from this folder:
|
||||
|
||||
```powershell
|
||||
powershell -ExecutionPolicy Bypass -File Start-Server.ps1
|
||||
powershell -ExecutionPolicy Bypass -File Run-Client.ps1
|
||||
```
|
||||
|
||||
- `Start-Server.ps1` opens the server in its own window and waits until it
|
||||
listens. Type `quit` in that window to stop it (`help` lists the other
|
||||
commands). `-Here` runs it in the current console instead, `-Reset`
|
||||
deletes its database first, so the ids start from 1 again.
|
||||
- `Run-Client.ps1` goes through what a client does, printing each command
|
||||
before its output. The commands can be pasted as they are from this
|
||||
folder:
|
||||
|
||||
```text
|
||||
== Push a message to the client's default topic (logs, the server's DefaultTopic for it), as node demo
|
||||
> ..\..\Build\BakaShrotPushMessage.exe @messages\hello.json client.ini
|
||||
Pushed as message #1
|
||||
...
|
||||
== Read the latest message of logs:demo (topic:node, by name)
|
||||
> ..\..\Build\BakaShrotGetLatestMessage.exe logs demo client.ini
|
||||
Message logs:demo#2 (ids 1:2), pushed 2026-11-08T21:23:53.590Z
|
||||
Attachment: 159 bytes
|
||||
{"level":"info","text":"Daily report, attached"}
|
||||
```
|
||||
|
||||
The server's database (`example.sqlite`) and what the client saves
|
||||
(`out\`) stay in this folder, git ignores them.
|
||||
|
||||
## What's in here
|
||||
|
||||
| File | What it is |
|
||||
|---|---|
|
||||
| `server.ini` | The server: its key, its names, where it listens, the one client it accepts and what that client may do. Every option is documented in `Server/Config/schema.ini`. |
|
||||
| `client.ini` | The client: its key pair, the node it writes as (`demo`), and the server's address and public key. Every tool takes it as its last argument. |
|
||||
| `Start-Server.ps1` | Starts the server with `server.ini`. |
|
||||
| `Run-Client.ps1` | The client's steps, below. |
|
||||
| `messages\` | What the client pushes (`hello.json`, `reading.json`, `report.json` with `report.txt` attached) and the SQL it runs (`query.sql`). |
|
||||
|
||||
## How the two configs fit together
|
||||
|
||||
- **Keys:** each side has a key pair. The server lists the client's public
|
||||
key in `[BakaShrot.Client.demo] PublicKeys`; the client has the server's
|
||||
in `ServerPublicKey`. Every connection is encrypted, and only reaches the
|
||||
server holding the matching private key.
|
||||
- **Names:** messages are filed under a topic and a node. The topics are
|
||||
listed in `[BakaShrot.Topics]`, the nodes come from the sections that
|
||||
define them (`server`, `demo`). The server gives each name an id of its
|
||||
own; clients use the names.
|
||||
- **Who the client is:** its node, `demo`, in both configs (`Node`).
|
||||
Every message it pushes is stored under it. That's why the server
|
||||
checks it: the key proves who's connecting, the server's `Node` for that
|
||||
key says which node it may write as, so no client can push as another's
|
||||
node.
|
||||
- **Where its pushes go:** the server's `DefaultTopic = logs`, unless a
|
||||
push names another topic in `PushTopics` (`-t sensors`). Only the server
|
||||
has it: `client.ini` has no topic.
|
||||
- **What it may do:** read every topic (`ReadTopics = *`, `AllowRead`),
|
||||
push (`AllowWrite`), run read-only SQL (`AllowQuery`).
|
||||
- **Retention:** `sensors` messages are kept a day, `logs` ones forever.
|
||||
- **Durability:** `Synchronous = FULL` (the default): every message the
|
||||
server acknowledges is flushed to disk first, so it survives a power cut.
|
||||
`NORMAL` pushes faster but can lose the last second or so of messages
|
||||
then; see `Server/Config/schema.ini`.
|
||||
|
||||
## What the client does
|
||||
|
||||
`Run-Client.ps1`, step by step:
|
||||
|
||||
1. **Pushes** `hello.json` to `logs`, its default topic: `BakaShrotPushMessage @messages\hello.json`.
|
||||
JSON goes in a file (`@file`): PowerShell drops the double quotes of an
|
||||
argument.
|
||||
2. **Pushes** `reading.json` to `sensors`: `-t sensors`.
|
||||
3. **Pushes** `report.json` with `report.txt` attached: `-a messages\report.txt`.
|
||||
4. **Reads the latest** message of `logs:demo`: `BakaShrotGetLatestMessage logs demo`.
|
||||
5. **Saves the attachment** back to `out\report-copy.txt` and checks it's
|
||||
the same: `BakaShrotGetAttachment logs demo <id> out\report-copy.txt`.
|
||||
6. **Reads every** message of `logs:demo`, one per line (id, time, data):
|
||||
`BakaShrotGetMessages logs demo --time`.
|
||||
7. **Reads** `sensors:demo`'s latest.
|
||||
8. **Shows the ids** the server has for the names: `BakaShrotResolveIds topic sensors`, `BakaShrotList`.
|
||||
9. **Queries** with SQL: `BakaShrotQuery @messages\query.sql`, on the
|
||||
`messages` view, which has the names.
|
||||
|
||||
To watch messages arrive, leave this running and push from another
|
||||
terminal:
|
||||
|
||||
```powershell
|
||||
..\..\Build\BakaShrotGetMessages.exe logs demo --after latest --follow client.ini
|
||||
```
|
||||
|
||||
## Looking at the database
|
||||
|
||||
`example.sqlite` is a plain SQLite file, the `messages` view has the
|
||||
names. To browse one topic in a tool like SQLiteStudio:
|
||||
|
||||
1. Open the `browse` table: one row. Pick a topic in its `topic` cell (a
|
||||
dropdown of the topics, from its foreign key), and a node in `node` to
|
||||
narrow it, or leave it NULL for every node. Commit.
|
||||
2. Open the `browse_messages` view: that topic's messages, newest first,
|
||||
with their node, id, time (UTC), data and attachment size.
|
||||
|
||||
The server never writes `browse`, so it's safe to change while it runs.
|
||||
Close the tool when done: an open grid keeps the server from finishing
|
||||
its checkpoints, and its WAL file grows meanwhile.
|
||||
|
||||
## From Python
|
||||
|
||||
The same `client.ini` works with the Python client
|
||||
(`Interactors/Python/bakashrot.py`, over the DLL). From the repository's
|
||||
root:
|
||||
|
||||
```python
|
||||
import sys
|
||||
sys.path.insert(0, r"Interactors\Python")
|
||||
from bakashrot import Client
|
||||
|
||||
with Client.from_ini(r"Examples\SingleServer\client.ini", r"Build\BakaShrot_Client.dll") as client:
|
||||
print("pushed", client.push({"from": "python"}))
|
||||
for message in client.get_messages("logs", "demo").messages:
|
||||
print(message.id, message.json())
|
||||
```
|
||||
@@ -0,0 +1,101 @@
|
||||
# ============================================================================
|
||||
# BakaShrot example: what a client does, with the tools and client.ini.
|
||||
#
|
||||
# Start the server first (Start-Server.ps1). Each step prints the command
|
||||
# it runs, then its output: run from this folder, the commands can be
|
||||
# pasted as they are. Every run pushes 3 more messages, the ids go on from
|
||||
# the last run's (Start-Server.ps1 -Reset starts over).
|
||||
#
|
||||
# Usage: powershell -ExecutionPolicy Bypass -File Run-Client.ps1 [-BinDir <dir>]
|
||||
# -BinDir Where the tools are, with BakaShrot_Client.dll next to them:
|
||||
# Build\ by default (Build.ps1), or a Release\<v>\.
|
||||
# ============================================================================
|
||||
|
||||
param(
|
||||
[string]$BinDir = (Join-Path $PSScriptRoot "..\..\Build")
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$Port = 12360
|
||||
|
||||
function Test-Port($Port) {
|
||||
$Client = New-Object System.Net.Sockets.TcpClient
|
||||
try {
|
||||
$Async = $Client.BeginConnect("127.0.0.1", $Port, $null, $null)
|
||||
return ($Async.AsyncWaitHandle.WaitOne(200) -and $Client.Connected)
|
||||
} catch { return $false }
|
||||
finally { $Client.Close() }
|
||||
}
|
||||
|
||||
function Write-Step($Text) {
|
||||
Write-Host ""
|
||||
Write-Host "== $Text" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
# Runs a tool, shows the command and what it printed, stops if it failed.
|
||||
# client.ini is always the last argument.
|
||||
function Invoke-Tool($Name, [string[]]$Arguments) {
|
||||
$Arguments += "client.ini"
|
||||
$Shown = $Arguments | ForEach-Object { if ($_ -match " ") { "`"$_`"" } else { $_ } }
|
||||
Write-Host ("> " + (Join-Path $Bin "$Name.exe") + " " + ($Shown -join " ")) -ForegroundColor DarkGray
|
||||
$Output = & (Join-Path $Bin "$Name.exe") @Arguments 2>&1
|
||||
$Code = $LASTEXITCODE
|
||||
$Output | ForEach-Object { Write-Host " $_" }
|
||||
if ($Code -ne 0) { throw "$Name failed with code $Code" }
|
||||
return ($Output -join "`n")
|
||||
}
|
||||
|
||||
if (-not (Test-Path (Join-Path $BinDir "BakaShrot_Client.dll"))) {
|
||||
throw "No BakaShrot_Client.dll in ${BinDir}: build it first (Build.ps1), or pass -BinDir"
|
||||
}
|
||||
if (-not (Test-Port $Port)) {
|
||||
throw "Nothing listens on port ${Port}: start the server first (Start-Server.ps1)"
|
||||
}
|
||||
|
||||
# From this folder, so every path is short: client.ini, messages\...
|
||||
Push-Location $PSScriptRoot
|
||||
try {
|
||||
$Bin = Resolve-Path -Relative $BinDir
|
||||
New-Item -ItemType Directory -Force "out" | Out-Null
|
||||
|
||||
Write-Step "Push a message to the client's default topic (logs, the server's DefaultTopic for it), as node demo"
|
||||
# JSON goes in a file (@file): PowerShell drops the double quotes of an argument.
|
||||
$null = Invoke-Tool "BakaShrotPushMessage" @("@messages\hello.json")
|
||||
|
||||
Write-Step "Push one to another topic it may push to (sensors)"
|
||||
$null = Invoke-Tool "BakaShrotPushMessage" @("-t", "sensors", "@messages\reading.json")
|
||||
|
||||
Write-Step "Push one with a file attached"
|
||||
$Pushed = Invoke-Tool "BakaShrotPushMessage" @("-a", "messages\report.txt", "@messages\report.json")
|
||||
if ($Pushed -notmatch "message #(\d+)") { throw "Unexpected answer: $Pushed" }
|
||||
$ReportId = $Matches[1]
|
||||
|
||||
Write-Step "Read the latest message of logs:demo (topic:node, by name)"
|
||||
$null = Invoke-Tool "BakaShrotGetLatestMessage" @("logs", "demo")
|
||||
|
||||
Write-Step "Read the attachment back"
|
||||
$null = Invoke-Tool "BakaShrotGetAttachment" @("logs", "demo", $ReportId, "out\report-copy.txt")
|
||||
if ((Get-FileHash "out\report-copy.txt").Hash -ne (Get-FileHash "messages\report.txt").Hash) { throw "The attachment came back different" }
|
||||
Write-Host " Same bytes as messages\report.txt"
|
||||
|
||||
Write-Step "Read every message of logs:demo, with when they were pushed (UTC)"
|
||||
$null = Invoke-Tool "BakaShrotGetMessages" @("logs", "demo", "--time")
|
||||
|
||||
Write-Step "Read the sensors one"
|
||||
$null = Invoke-Tool "BakaShrotGetLatestMessage" @("sensors", "demo")
|
||||
|
||||
Write-Step "What the server calls the names: their ids there"
|
||||
$null = Invoke-Tool "BakaShrotResolveIds" @("topic", "sensors")
|
||||
$null = Invoke-Tool "BakaShrotList" @()
|
||||
|
||||
Write-Step "A raw SQL query (needs AllowQuery = 1), from messages\query.sql"
|
||||
$null = Invoke-Tool "BakaShrotQuery" @("@messages\query.sql")
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Done. To follow new messages as they come, from this folder:" -ForegroundColor Green
|
||||
Write-Host " $(Join-Path $Bin 'BakaShrotGetMessages.exe') logs demo --after latest --follow client.ini"
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
# ============================================================================
|
||||
# BakaShrot example: starts the server with server.ini.
|
||||
#
|
||||
# In a window of its own by default (the server needs a console: it reads
|
||||
# its commands there, and stops at once without one), then waits until it
|
||||
# listens. Type "quit" in that window to stop it, "help" for the others.
|
||||
#
|
||||
# Usage: powershell -ExecutionPolicy Bypass -File Start-Server.ps1 [-BinDir <dir>] [-Here] [-Reset]
|
||||
# -BinDir Where BakaShrot_Server.exe is, with libsodium.dll and
|
||||
# sqlite3.dll: Build\ by default (Build.ps1), or a Release\<v>\.
|
||||
# -Here Runs it in this console instead, until "quit".
|
||||
# -Reset Deletes the example's database first: no messages, ids from 1.
|
||||
#
|
||||
# Its database, example.sqlite, goes next to this script.
|
||||
# ============================================================================
|
||||
|
||||
param(
|
||||
[string]$BinDir = (Join-Path $PSScriptRoot "..\..\Build"),
|
||||
[switch]$Here,
|
||||
[switch]$Reset
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$Server = Join-Path $BinDir "BakaShrot_Server.exe"
|
||||
$Ini = Join-Path $PSScriptRoot "server.ini"
|
||||
$Port = 12360
|
||||
|
||||
function Test-Port($Port) {
|
||||
$Client = New-Object System.Net.Sockets.TcpClient
|
||||
try {
|
||||
$Async = $Client.BeginConnect("127.0.0.1", $Port, $null, $null)
|
||||
return ($Async.AsyncWaitHandle.WaitOne(200) -and $Client.Connected)
|
||||
} catch { return $false }
|
||||
finally { $Client.Close() }
|
||||
}
|
||||
|
||||
if (-not (Test-Path $Server)) {
|
||||
throw "No ${Server}: build it first (Build.ps1), or pass -BinDir"
|
||||
}
|
||||
if (Test-Port $Port) {
|
||||
throw "Something already listens on port $Port, the example server may already run"
|
||||
}
|
||||
|
||||
if ($Reset) {
|
||||
Get-ChildItem $PSScriptRoot -Filter "example.sqlite*" | Remove-Item -Confirm:$false
|
||||
Write-Host "Deleted the example's database"
|
||||
}
|
||||
|
||||
if ($Here) {
|
||||
Push-Location $PSScriptRoot
|
||||
try { & $Server $Ini }
|
||||
finally { Pop-Location }
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
|
||||
$Process = Start-Process -FilePath $Server -ArgumentList "`"$Ini`"" -WorkingDirectory $PSScriptRoot -PassThru
|
||||
$Deadline = (Get-Date).AddSeconds(15)
|
||||
while (-not (Test-Port $Port)) {
|
||||
if ($Process.HasExited) { throw "The server stopped with code $($Process.ExitCode), see its log above" }
|
||||
if ((Get-Date) -gt $Deadline) { throw "The server isn't listening on $Port after 15 s" }
|
||||
Start-Sleep -Milliseconds 100
|
||||
}
|
||||
|
||||
Write-Host "The server runs in its own window (process $($Process.Id)), on 127.0.0.1:$Port."
|
||||
Write-Host "Try it with: powershell -ExecutionPolicy Bypass -File `"$(Join-Path $PSScriptRoot 'Run-Client.ps1')`""
|
||||
Write-Host "Stop it by typing quit in its window."
|
||||
@@ -0,0 +1,26 @@
|
||||
; ============================================================================
|
||||
; BakaShrot example: the client's config, read by every tool (their last
|
||||
; argument), the client DLL and the Python client.
|
||||
;
|
||||
; EXAMPLE KEYS, PUBLIC IN THE REPOSITORY: never use them for anything real.
|
||||
; Make your own with BakaShrotGenerateKeyPairs.exe.
|
||||
; ============================================================================
|
||||
|
||||
[BakaShrot]
|
||||
; This client's key pair. The server lists the public key in
|
||||
; [BakaShrot.Client.demo] PublicKeys.
|
||||
PrivateKey = zDA8o6hp7zkIatlumOKFWqV8j3yYON8QFpIkMAEnpJnKv4FbbX4SmshWDRJp9ds5qpEDVYRHSnE7exw2pkQK1w==
|
||||
PublicKey = yr+BW21+EprIVg0SafXbOaqRA1WER0pxO3scNqZECtc=
|
||||
|
||||
; The node it writes as: the server checks its section for this key says
|
||||
; Node = demo. Every message it pushes is stored as "demo".
|
||||
; No topic here: where its pushes go when they don't name one is the
|
||||
; server's DefaultTopic, which topics it may push to and read its
|
||||
; PushTopics and ReadTopics.
|
||||
Node = demo
|
||||
|
||||
; Where the server is, and its public key: the client only talks to the
|
||||
; server holding the matching private key.
|
||||
ServerHost = 127.0.0.1
|
||||
ServerPort = 12360
|
||||
ServerPublicKey = k32QNiTXL9lA7DhdwBQv9jQXouADujciaOWVZuA3JE4=
|
||||
@@ -0,0 +1 @@
|
||||
{"level":"info","text":"Hello from the example client"}
|
||||
@@ -0,0 +1 @@
|
||||
SELECT topic, node, message_id, pushed_utc, message_data ->> '$.text' FROM messages WHERE topic = 'logs' AND is_json = 1 ORDER BY message_id
|
||||
@@ -0,0 +1 @@
|
||||
{"sensor":"kitchen-temperature","celsius":21.5}
|
||||
@@ -0,0 +1 @@
|
||||
{"level":"info","text":"Daily report, attached"}
|
||||
@@ -0,0 +1,5 @@
|
||||
Daily report
|
||||
============
|
||||
|
||||
Any file can be attached to a message: bytes of any kind, up to the
|
||||
server's MaxAttachmentSize. The message's JSON says what it is.
|
||||
@@ -0,0 +1,67 @@
|
||||
; ============================================================================
|
||||
; BakaShrot example: the server's config (one server, one client).
|
||||
;
|
||||
; EXAMPLE KEYS, PUBLIC IN THE REPOSITORY: never use them for anything real.
|
||||
; Make your own with BakaShrotGenerateKeyPairs.exe.
|
||||
;
|
||||
; Every option is documented in Server/Config/schema.ini.
|
||||
; ============================================================================
|
||||
|
||||
[BakaShrot]
|
||||
; This server's private key. Its public key, which the client needs, is
|
||||
; k32QNiTXL9lA7DhdwBQv9jQXouADujciaOWVZuA3JE4= (the server logs it too).
|
||||
PrivateKey = KymGH/iJFcqE0JtGmRmWIsQnKTEl49Zq+qYEsfLHtWOTfZA2JNcv2UDsOF3AFC/2NBei4AO6NyJo5ZVm4DckTg==
|
||||
|
||||
; The node this server authenticates as when it links to peers. It has
|
||||
; none here.
|
||||
Node = server
|
||||
|
||||
|
||||
; The topics this server has. It gives each an id of its own (they keep it
|
||||
; across restarts); clients and other servers use the names.
|
||||
[BakaShrot.Topics]
|
||||
logs
|
||||
sensors
|
||||
|
||||
; No [BakaShrot.Nodes]: the nodes come from the sections that define them,
|
||||
; "server" above and "demo" below (and the ones peers send, as they come).
|
||||
|
||||
|
||||
; Sensor readings are kept a day, logs forever (no line for them).
|
||||
[BakaShrot.Retention]
|
||||
sensors = 1d
|
||||
|
||||
|
||||
[BakaShrot.Server]
|
||||
; Only reachable from this machine.
|
||||
Host = 127.0.0.1
|
||||
Port = 12360
|
||||
ReplicatingTopics = logs;sensors
|
||||
NetThreadPoolSize = 4
|
||||
; Next to this file: Start-Server.ps1 runs the server from this folder.
|
||||
DatabasePath = ./example.sqlite
|
||||
MaxAttachmentSize = 64M
|
||||
; Every acknowledged push is on disk, even through a power loss (the
|
||||
; default). NORMAL is faster but can lose the last second of messages then.
|
||||
Synchronous = FULL
|
||||
|
||||
|
||||
; The one client: Run-Client.ps1's tools, with client.ini.
|
||||
[BakaShrot.Client.demo]
|
||||
; client.ini's PublicKey.
|
||||
PublicKeys = yr+BW21+EprIVg0SafXbOaqRA1WER0pxO3scNqZECtc=
|
||||
; The node this key writes as, fixed: every message it pushes is stored
|
||||
; under it, so no client can push as another's node. client.ini's Node
|
||||
; must say the same.
|
||||
Node = demo
|
||||
; Where its pushes go when they don't name a topic (BakaShrotPushMessage
|
||||
; without -t). Only a default: it may push to any topic in PushTopics.
|
||||
DefaultTopic = logs
|
||||
; The topics it may push to (always as "demo"), and read from. Without
|
||||
; these two lines: only its DefaultTopic.
|
||||
PushTopics = logs;sensors
|
||||
ReadTopics = *
|
||||
AllowRead = 1
|
||||
AllowWrite = 1
|
||||
; Raw read-only SQL, for BakaShrotQuery.exe.
|
||||
AllowQuery = 1
|
||||
Reference in New Issue
Block a user