1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00
Files
BakaShrot/Tools/Common/ToolClient.pbi
T
aziascreations 9caf816dca Add pushed_at timestamps and per-topic retention
- Every message is stamped with the time its push arrived (UTC ms since
  1970, the server's clock), carried as is by replication. Exposed in
  LatestMessage and Messages, BakaShrot_GetMessagePushedAt() in the DLL,
  Message.pushed_at / .pushed in Python, GetMessages --time,
  GetLatestMessage, and the messages view (pushed_at, pushed_utc).
- [BakaShrot.Retention] in each server's config: a max age per topic, by
  name, id, pattern or *, ages like 30d or 1d12h. A background pass
  (Server/Storage/Retention.pbi, every RetentionInterval seconds) deletes
  what's older, in batches through the writer. node_heads is left as is,
  so ids never go back and peers don't resend what was deleted.
- Replicated messages already past the receiver's max age are skipped on
  arrival, their head still raised so they aren't sent again.
- Schema v6: data.pushed_at (older databases stamped at upgrade time),
  a (topic_id, pushed_at) index, the unused archives table dropped. The
  writer takes the next id from node_heads.
- Folded into protocol 3, not released yet.
- Tests/Retention-Test.ps1 (52 checks), Python checks for pushed_at.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RwFeVPXGLs3KK1MAQn3ouP
2026-11-08 17:01:48 +01:00

141 lines
4.6 KiB
Plaintext

; ============================================================================
; BakaShrot Tools - Client connection
;
; Shared by the tools that talk to a server through BakaShrot_Client.dll.
; The DLL must be next to the tool, the identity and server come from an ini
; file laid out like the client.ini Build.ps1 writes:
; [BakaShrot]
; PrivateKey, PublicKey, Topic, Node, ServerHost, ServerPort,
; ServerPublicKey (the server's, it logs it when it starts)
; Topic and Node can be names or ids (Commons/Networking/Names.pbi). They
; were TopicId and NodeId before, still read when Topic and Node are missing.
; ============================================================================
XIncludeFile "../../Client/BakaShrot_Client_Prototypes.pbi"
; Loads the DLL, reads IniPath and connects to its server with its identity.
; Prints what failed and returns #False on failure, the DLL is then unloaded.
Procedure.i ToolClient_Connect(IniPath.s)
Protected PrivateKey.s, PublicKey.s, ServerPublicKey.s, Topic.s, Node.s, ServerHost.s, ServerPort.l
If Not OpenPreferences(IniPath)
PrintN("Failed to open '" + IniPath + "'")
ProcedureReturn #False
EndIf
PreferenceGroup("BakaShrot")
PrivateKey = ReadPreferenceString("PrivateKey", "")
PublicKey = ReadPreferenceString("PublicKey", "")
ServerPublicKey = ReadPreferenceString("ServerPublicKey", "")
Topic = Trim(ReadPreferenceString("Topic", ReadPreferenceString("TopicId", "")))
Node = Trim(ReadPreferenceString("Node", ReadPreferenceString("NodeId", "")))
ServerHost = ReadPreferenceString("ServerHost", "")
ServerPort = ReadPreferenceLong("ServerPort", 0)
ClosePreferences()
If Not BakaShrot_LoadClient(GetPathPart(ProgramFilename()) + "BakaShrot_Client.dll")
PrintN("Failed to load BakaShrot_Client.dll")
ProcedureReturn #False
EndIf
If Not BakaShrot_SetPrivateKey(PrivateKey)
PrintN("Invalid PrivateKey in '" + IniPath + "'")
BakaShrot_UnloadClient()
ProcedureReturn #False
EndIf
If Not BakaShrot_SetPublicKey(PublicKey)
PrintN("Invalid PublicKey in '" + IniPath + "'")
BakaShrot_UnloadClient()
ProcedureReturn #False
EndIf
If Not BakaShrot_SetServerPublicKey(ServerPublicKey)
PrintN("Missing or invalid ServerPublicKey in '" + IniPath + "', the server logs it when it starts")
BakaShrot_UnloadClient()
ProcedureReturn #False
EndIf
If Not BakaShrot_SetTopic(Topic)
PrintN("Invalid Topic '" + Topic + "' in '" + IniPath + "', expected a name or an id")
BakaShrot_UnloadClient()
ProcedureReturn #False
EndIf
If Not BakaShrot_SetNode(Node)
PrintN("Invalid Node '" + Node + "' in '" + IniPath + "', expected a name or an id")
BakaShrot_UnloadClient()
ProcedureReturn #False
EndIf
If Not BakaShrot_Connect(ServerHost, ServerPort)
PrintN("Failed to connect and authenticate to " + ServerHost + ":" + Str(ServerPort))
BakaShrot_UnloadClient()
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
; Ms (milliseconds since 1970 UTC, as BakaShrot_GetMessagePushedAt() gives
; it) as "2026-11-08T14:03:59.123Z".
Procedure.s ToolClient_FormatUtc(Ms.q)
ProcedureReturn FormatDate("%yyyy-%mm-%ddT%hh:%ii:%ss", Ms / 1000) + "." + RSet(Str(Ms % 1000), 3, "0") + "Z"
EndProcedure
; Disconnects and unloads the DLL.
Procedure ToolClient_Close()
BakaShrot_Disconnect()
BakaShrot_UnloadClient()
EndProcedure
; Prints why the last request failed.
Procedure ToolClient_PrintFailure()
If BakaShrot_IsConnected()
PrintN("Refused by the server: " + BakaShrot_GetRefusalReasonString())
Else
PrintN("The connection to the server failed")
EndIf
EndProcedure
; Reads a whole UTF-8 text file (a byte order mark is skipped) into *Text.
; In one go: ReadString() with #PB_File_IgnoreEOL takes seconds on a 16 MB
; line. Prints why and returns #False if it can't be read.
Procedure.i ToolClient_ReadTextFile(Path.s, *Text.String)
Protected FileId.i, Size.q, *Data, Offset.i
FileId = ReadFile(#PB_Any, Path)
If Not FileId
PrintN("Failed to open '" + Path + "'")
ProcedureReturn #False
EndIf
Size = Lof(FileId)
*Data = AllocateMemory(Size + 1)
If Not *Data
CloseFile(FileId)
PrintN("'" + Path + "' is too big to read")
ProcedureReturn #False
EndIf
If ReadData(FileId, *Data, Size) <> Size
FreeMemory(*Data)
CloseFile(FileId)
PrintN("Failed to read '" + Path + "'")
ProcedureReturn #False
EndIf
CloseFile(FileId)
If Size >= 3 And PeekA(*Data) = $EF And PeekA(*Data + 1) = $BB And PeekA(*Data + 2) = $BF
Offset = 3
EndIf
*Text\s = ""
If Size > Offset
*Text\s = PeekS(*Data + Offset, Size - Offset, #PB_UTF8 | #PB_ByteLength)
EndIf
FreeMemory(*Data)
ProcedureReturn #True
EndProcedure