mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Added filtered name lists and the DLL's enumerations, Opened the query connection read-only, Bumped to 0.4.0
- Protocol 8: ListTopics / ListNodes became one ListNames [kind, topics, nodes], each filter a name or a pattern: the topics a node pushed to, the nodes that pushed to a topic, wildcards included. No storage change; the permissions are the lists' (may_list_topic, may_list_nodes_of) - The DLL: the JSON getters are suffixed AsJson (GetNamesListAsJson, GetQueryResultAsJson); ListNamesA/W replaces ListTopics / ListNodes. Beside them, Win32-like enumerations: EnumerateNames / EnumerateQuery, NextName / NextQueryRow (before the first item at first), getters, CloseEnum (enumerations.rs). They don't outlive their client: FreeClient closes them - C#: EnumerateTopics / EnumerateNodes / EnumerateQuery as IEnumerable (QueryRow for a query), fetched at each foreach, closed in finally - Queries: the connection opens the file read-only (SQLITE_OPEN_READ_ONLY), a fifth guard beside query_only, the read-only statement check, one statement only and the PRAGMA-denying authorizer; a unit test checks the handle is read-only - Python, PureBasic, the tool, docs, guides and revamp.md (step 16) updated; versions 0.4.0 everywhere Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+145
-16
@@ -24,7 +24,7 @@ int64_t BakaShrot_CreateClient(void);
|
||||
|
||||
/**
|
||||
* Frees a client and closes its connection (once a call on it in another
|
||||
* thread ends): 1, or 0 if it wasn't one.
|
||||
* thread ends), its enumerations closed too: 1, or 0 if it wasn't one.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_FreeClient(int64_t client);
|
||||
|
||||
@@ -245,34 +245,43 @@ ptrdiff_t BakaShrot_GetMessageNodeA(int64_t client, char *buffer, ptrdiff_t size
|
||||
ptrdiff_t BakaShrot_GetMessageNodeW(int64_t client, uint16_t *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* Lists the topics with messages that this client may push to or read
|
||||
* (every one if its roles have `list_topics` or `query`): how many, -1 on
|
||||
* failure. Then `BakaShrot_GetNamesList()`.
|
||||
* Lists topics (`kind` 1) or nodes (`kind` 2): the topics with messages
|
||||
* that this client may push to or read (every one if its roles have
|
||||
* `list_topics` or `query`), the nodes with messages in the topics it may
|
||||
* read (every one with `list_nodes` or `query`). Only those of the topic
|
||||
* and node pairs that `topics` and `nodes` match: each a name or a
|
||||
* pattern (`sensors.*`), null for any. Every topic: 1, null, null; the
|
||||
* topics a node pushed to: 1, null, the node; the nodes that pushed to a
|
||||
* topic: 2, the topic, null. How many, -1 on failure. Then
|
||||
* `BakaShrot_GetNamesListAsJson()`, or `BakaShrot_EnumerateNames()`
|
||||
* instead.
|
||||
*/
|
||||
int32_t BakaShrot_ListTopics(int64_t client);
|
||||
int32_t BakaShrot_ListNamesA(int64_t client, int32_t kind, const char *topics, const char *nodes);
|
||||
|
||||
/**
|
||||
* Lists the nodes with messages in the topics this client may read (every
|
||||
* one if its roles have `list_nodes` or `query`): how many, -1 on failure. Then
|
||||
* `BakaShrot_GetNamesList()`.
|
||||
* The same as `BakaShrot_ListNamesA`, in UTF-16.
|
||||
*/
|
||||
int32_t BakaShrot_ListNodes(int64_t client);
|
||||
int32_t BakaShrot_ListNamesW(int64_t client,
|
||||
int32_t kind,
|
||||
const uint16_t *topics,
|
||||
const uint16_t *nodes);
|
||||
|
||||
/**
|
||||
* The last list, as JSON:
|
||||
* `{"kind":"topic","truncated":false,"names":["logs",...]}`, `truncated`
|
||||
* if some didn't fit.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetNamesListA(int64_t client, char *buffer, ptrdiff_t size);
|
||||
ptrdiff_t BakaShrot_GetNamesListAsJsonA(int64_t client, char *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_GetNamesListA`, in UTF-16.
|
||||
* The same as `BakaShrot_GetNamesListAsJsonA`, in UTF-16.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetNamesListW(int64_t client, uint16_t *buffer, ptrdiff_t size);
|
||||
ptrdiff_t BakaShrot_GetNamesListAsJsonW(int64_t client, uint16_t *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* Runs read-only SQL on the server (one SELECT or WITH), if this client
|
||||
* may query. Then `BakaShrot_GetQueryResult()`.
|
||||
* may query. Then `BakaShrot_GetQueryResultAsJson()`, or
|
||||
* `BakaShrot_EnumerateQuery()` instead.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_QueryA(int64_t client, const char *sql);
|
||||
|
||||
@@ -285,12 +294,12 @@ ptrdiff_t BakaShrot_QueryW(int64_t client, const uint16_t *sql);
|
||||
* The last query's result, as JSON:
|
||||
* `{"columns":[...],"rows":[[...],...]}`, every value a string or null.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetQueryResultA(int64_t client, char *buffer, ptrdiff_t size);
|
||||
ptrdiff_t BakaShrot_GetQueryResultAsJsonA(int64_t client, char *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_GetQueryResultA`, in UTF-16.
|
||||
* The same as `BakaShrot_GetQueryResultAsJsonA`, in UTF-16.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetQueryResultW(int64_t client, uint16_t *buffer, ptrdiff_t size);
|
||||
ptrdiff_t BakaShrot_GetQueryResultAsJsonW(int64_t client, uint16_t *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* Pushes a message with the file at `FilePath` as its attachment (1 byte
|
||||
@@ -383,6 +392,126 @@ int64_t BakaShrot_GetAttachmentData(int64_t client, void *buffer, int64_t buffer
|
||||
*/
|
||||
ptrdiff_t BakaShrot_FreeAttachmentData(int64_t client);
|
||||
|
||||
/**
|
||||
* Closes an enumeration: 1, or 0 if it wasn't one.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_CloseEnum(int64_t enumeration);
|
||||
|
||||
/**
|
||||
* Lists names as `BakaShrot_ListNames()` does (the same arguments), into
|
||||
* an enumeration: its handle, 0 on failure (`BakaShrot_GetLastError(client)`).
|
||||
* Then `BakaShrot_NextName()` and `BakaShrot_GetEnumName()`, and
|
||||
* `BakaShrot_CloseEnum()`. Closed with its client by
|
||||
* `BakaShrot_FreeClient()`.
|
||||
*/
|
||||
int64_t BakaShrot_EnumerateNamesA(int64_t client,
|
||||
int32_t kind,
|
||||
const char *topics,
|
||||
const char *nodes);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_EnumerateNamesA`, in UTF-16.
|
||||
*/
|
||||
int64_t BakaShrot_EnumerateNamesW(int64_t client,
|
||||
int32_t kind,
|
||||
const uint16_t *topics,
|
||||
const uint16_t *nodes);
|
||||
|
||||
/**
|
||||
* Moves to the next name (the first, the first time): 1, 0 if there are
|
||||
* no more, -1 if it isn't a names enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_NextName(int64_t enumeration);
|
||||
|
||||
/**
|
||||
* The current name: the bytes it needs, 0 if there's none (before the
|
||||
* first, past the last) or it isn't a names enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetEnumNameA(int64_t enumeration, char *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_GetEnumNameA`, in UTF-16.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetEnumNameW(int64_t enumeration, uint16_t *buffer, ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* How many names, -1 if it isn't a names enumeration.
|
||||
*/
|
||||
int32_t BakaShrot_GetNameCount(int64_t enumeration);
|
||||
|
||||
/**
|
||||
* Whether some names didn't fit in the server's answer: 1 or 0, -1 if it
|
||||
* isn't a names enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_IsNamesListTruncated(int64_t enumeration);
|
||||
|
||||
/**
|
||||
* Runs a query as `BakaShrot_Query()` does, into an enumeration: its
|
||||
* handle, 0 on failure (`BakaShrot_GetLastError(client)`). Then
|
||||
* `BakaShrot_GetQueryColumnCount()` and `GetQueryColumnName()`,
|
||||
* `BakaShrot_NextQueryRow()` and `GetQueryValue()` / `IsQueryValueNull()`,
|
||||
* and `BakaShrot_CloseEnum()`. Closed with its client by
|
||||
* `BakaShrot_FreeClient()`.
|
||||
*/
|
||||
int64_t BakaShrot_EnumerateQueryA(int64_t client, const char *sql);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_EnumerateQueryA`, in UTF-16.
|
||||
*/
|
||||
int64_t BakaShrot_EnumerateQueryW(int64_t client, const uint16_t *sql);
|
||||
|
||||
/**
|
||||
* How many columns, -1 if it isn't a query enumeration.
|
||||
*/
|
||||
int32_t BakaShrot_GetQueryColumnCount(int64_t enumeration);
|
||||
|
||||
/**
|
||||
* A column's name (`index` 0 to the count - 1): the bytes it needs, 0 if
|
||||
* there's no such column or it isn't a query enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetQueryColumnNameA(int64_t enumeration,
|
||||
int32_t index,
|
||||
char *buffer,
|
||||
ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_GetQueryColumnNameA`, in UTF-16.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetQueryColumnNameW(int64_t enumeration,
|
||||
int32_t index,
|
||||
uint16_t *buffer,
|
||||
ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* Moves to the next row (the first, the first time): 1, 0 if there are
|
||||
* no more, -1 if it isn't a query enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_NextQueryRow(int64_t enumeration);
|
||||
|
||||
/**
|
||||
* The current row's value in a column (`index` 0 to the count - 1): the
|
||||
* bytes it needs, 0 if it's null (`BakaShrot_IsQueryValueNull()`), there's
|
||||
* no current row or such column, or it isn't a query enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetQueryValueA(int64_t enumeration,
|
||||
int32_t index,
|
||||
char *buffer,
|
||||
ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* The same as `BakaShrot_GetQueryValueA`, in UTF-16.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_GetQueryValueW(int64_t enumeration,
|
||||
int32_t index,
|
||||
uint16_t *buffer,
|
||||
ptrdiff_t size);
|
||||
|
||||
/**
|
||||
* Whether the current row's value in a column is null: 1 or 0, -1 if
|
||||
* there's no current row or such column, or it isn't a query enumeration.
|
||||
*/
|
||||
ptrdiff_t BakaShrot_IsQueryValueNull(int64_t enumeration, int32_t index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif // __cplusplus
|
||||
|
||||
@@ -145,6 +145,54 @@ namespace NibblePoker.BakaShrot.Tests
|
||||
kitchen.Push("listed", "x");
|
||||
Assert.That(kitchen.ListTopics(), Does.StartWith("{\"kind\":\"topic\"").And.Contain("\"listed\""));
|
||||
Assert.That(kitchen.ListNodes(), Does.StartWith("{\"kind\":\"node\"").And.Contain("\"kitchen\""));
|
||||
|
||||
// Filtered: the topics a node pushed to, the nodes of a topic.
|
||||
Assert.That(kitchen.ListTopics(null, "KITCHEN"), Does.Contain("\"listed\""));
|
||||
Assert.That(kitchen.ListNodes("list?d", null), Is.EqualTo("{\"kind\":\"node\",\"names\":[\"kitchen\"],\"truncated\":false}"));
|
||||
Assert.That(kitchen.ListTopics(null, "nobody"), Is.EqualTo("{\"kind\":\"topic\",\"names\":[],\"truncated\":false}"));
|
||||
BakaShrotException invalid = Assert.Catch<BakaShrotException>(() => kitchen.ListNodes("a b", null))!;
|
||||
Assert.That(invalid.Code, Is.EqualTo(ErrorCode.InvalidArgument));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Enumerations()
|
||||
{
|
||||
using Client kitchen = Server.Connect("kitchen");
|
||||
kitchen.Push("enumerated", "x");
|
||||
kitchen.Push("enumerated", "y");
|
||||
Assert.That(kitchen.EnumerateTopics(), Does.Contain("enumerated"));
|
||||
Assert.That(kitchen.EnumerateTopics(null, "KITCHEN"), Does.Contain("enumerated"));
|
||||
Assert.That(kitchen.EnumerateNodes("enumer*", null), Is.EqualTo(new[] { "kitchen" }));
|
||||
Assert.That(kitchen.EnumerateNodes("nothing", null), Is.Empty);
|
||||
|
||||
// Rows: by index or column, NULL as null; asked again each time.
|
||||
IEnumerable<QueryRow> rows = kitchen.EnumerateQuery(
|
||||
"SELECT message_data AS data, NULL AS missing, '' AS empty FROM messages WHERE topic = 'enumerated' ORDER BY message_id");
|
||||
for (int pass = 0; pass < 2; pass++)
|
||||
{
|
||||
List<QueryRow> got = new List<QueryRow>(rows);
|
||||
Assert.That(got.Count, Is.EqualTo(2));
|
||||
Assert.That(got[0].Columns, Is.EqualTo(new[] { "data", "missing", "empty" }));
|
||||
Assert.That(got[0]["data"], Is.EqualTo("x"));
|
||||
Assert.That(got[1][0], Is.EqualTo("y"));
|
||||
Assert.That(got[1]["missing"], Is.Null);
|
||||
Assert.That(got[1]["empty"], Is.EqualTo(""));
|
||||
Assert.Throws<ArgumentException>(() => { string _ = got[0]["nope"]; });
|
||||
}
|
||||
Assert.That(kitchen.EnumerateQuery("SELECT 1 AS a WHERE 0"), Is.Empty);
|
||||
|
||||
// Failures when the foreach starts; closing the client ends one.
|
||||
IEnumerable<string> bad = kitchen.EnumerateNodes("a b", null);
|
||||
Assert.That(Assert.Catch<BakaShrotException>(() => bad.GetEnumerator().MoveNext())!.Code,
|
||||
Is.EqualTo(ErrorCode.InvalidArgument));
|
||||
Assert.That(Assert.Throws<RefusedException>(() => new List<QueryRow>(kitchen.EnumerateQuery("SELECT nope")))!.Code,
|
||||
Is.EqualTo(ErrorCode.QueryFailed));
|
||||
using (IEnumerator<string> topics = kitchen.EnumerateTopics().GetEnumerator())
|
||||
{
|
||||
Assert.That(topics.MoveNext(), Is.True);
|
||||
kitchen.Close();
|
||||
Assert.Throws<ConnectionLostException>(() => topics.MoveNext());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -99,6 +99,38 @@ namespace NibblePoker.BakaShrot
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A row of EnumerateQuery(): its values (null for NULL), by
|
||||
/// index or column name.</summary>
|
||||
public sealed class QueryRow
|
||||
{
|
||||
/// <summary>The query's columns, the same array for every row.</summary>
|
||||
public readonly string[] Columns;
|
||||
public readonly string[] Values;
|
||||
|
||||
internal QueryRow(string[] columns, string[] values)
|
||||
{
|
||||
Columns = columns;
|
||||
Values = values;
|
||||
}
|
||||
|
||||
public string this[int column]
|
||||
{
|
||||
get { return Values[column]; }
|
||||
}
|
||||
|
||||
/// <summary>The first column with that name (as the query wrote it).</summary>
|
||||
public string this[string column]
|
||||
{
|
||||
get
|
||||
{
|
||||
int index = Array.IndexOf(Columns, column);
|
||||
if (index < 0)
|
||||
throw new ArgumentException("No column " + column, "column");
|
||||
return Values[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Client : IDisposable
|
||||
{
|
||||
public readonly string Host, Ca, Certificate, Key, Crl;
|
||||
@@ -293,17 +325,32 @@ namespace NibblePoker.BakaShrot
|
||||
/// <summary>The topics with messages that this client may push to or
|
||||
/// read, as JSON: {"kind":"topic","truncated":false,"names":[...]}.</summary>
|
||||
public string ListTopics()
|
||||
{
|
||||
return ListTopics(null, null);
|
||||
}
|
||||
|
||||
/// <summary>ListTopics(), only those of the topic and node pairs that
|
||||
/// topic and node match, each a name or a pattern ("sensors.*"), null
|
||||
/// for any: ListTopics(null, "kitchen") is the topics kitchen pushed to.</summary>
|
||||
public string ListTopics(string topic, string node)
|
||||
{
|
||||
lock (sync)
|
||||
return Names(Native.BakaShrot_ListTopics(Handle()), "List topics");
|
||||
return Names(Native.BakaShrot_ListNamesW(Handle(), 1, Text(topic), Text(node)), "List topics");
|
||||
}
|
||||
|
||||
/// <summary>The nodes with messages in the topics this client may
|
||||
/// read, as ListTopics() says them.</summary>
|
||||
public string ListNodes()
|
||||
{
|
||||
return ListNodes(null, null);
|
||||
}
|
||||
|
||||
/// <summary>ListNodes(), filtered as ListTopics(topic, node) is:
|
||||
/// ListNodes("logs", null) is the nodes that pushed to logs.</summary>
|
||||
public string ListNodes(string topic, string node)
|
||||
{
|
||||
lock (sync)
|
||||
return Names(Native.BakaShrot_ListNodes(Handle()), "List nodes");
|
||||
return Names(Native.BakaShrot_ListNamesW(Handle(), 2, Text(topic), Text(node)), "List nodes");
|
||||
}
|
||||
|
||||
/// <summary>Runs one read-only SELECT or WITH: the server's JSON,
|
||||
@@ -313,12 +360,98 @@ namespace NibblePoker.BakaShrot
|
||||
lock (sync)
|
||||
{
|
||||
Check(Native.BakaShrot_QueryW(Handle(), Text(sql)), "Query");
|
||||
return GetString(Native.BakaShrot_GetQueryResultW);
|
||||
return GetString(Native.BakaShrot_GetQueryResultAsJsonW);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The topics ListTopics() lists, one at a time, without
|
||||
/// JSON. Asked once a foreach starts (each one asks again), closed
|
||||
/// when it ends.</summary>
|
||||
public IEnumerable<string> EnumerateTopics()
|
||||
{
|
||||
return EnumerateNames(1, null, null);
|
||||
}
|
||||
|
||||
/// <summary>The topics ListTopics(topic, node) lists, as EnumerateTopics() gives them.</summary>
|
||||
public IEnumerable<string> EnumerateTopics(string topic, string node)
|
||||
{
|
||||
return EnumerateNames(1, topic, node);
|
||||
}
|
||||
|
||||
/// <summary>The nodes ListNodes() lists, as EnumerateTopics() gives them.</summary>
|
||||
public IEnumerable<string> EnumerateNodes()
|
||||
{
|
||||
return EnumerateNames(2, null, null);
|
||||
}
|
||||
|
||||
/// <summary>The nodes ListNodes(topic, node) lists, as EnumerateTopics() gives them.</summary>
|
||||
public IEnumerable<string> EnumerateNodes(string topic, string node)
|
||||
{
|
||||
return EnumerateNames(2, topic, node);
|
||||
}
|
||||
|
||||
/// <summary>Query(sql)'s rows, one at a time, without JSON. Asked
|
||||
/// once a foreach starts (each one asks again), closed when it ends.</summary>
|
||||
public IEnumerable<QueryRow> EnumerateQuery(string sql)
|
||||
{
|
||||
long enumeration;
|
||||
lock (sync)
|
||||
enumeration = Opened(Native.BakaShrot_EnumerateQueryW(Handle(), Text(sql)), "Query");
|
||||
try
|
||||
{
|
||||
string[] columns = new string[Native.BakaShrot_GetQueryColumnCount(enumeration)];
|
||||
for (int column = 0; column < columns.Length; column++)
|
||||
columns[column] = GetString(enumeration, ColumnGetter(Native.BakaShrot_GetQueryColumnNameW, column));
|
||||
while (Next(Native.BakaShrot_NextQueryRow(enumeration)))
|
||||
{
|
||||
string[] values = new string[columns.Length];
|
||||
for (int column = 0; column < values.Length; column++)
|
||||
if (Native.BakaShrot_IsQueryValueNull(enumeration, column) == IntPtr.Zero)
|
||||
values[column] = GetString(enumeration, ColumnGetter(Native.BakaShrot_GetQueryValueW, column));
|
||||
yield return new QueryRow(columns, values);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Native.BakaShrot_CloseEnum(enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
// --- Inside ---------------------------------------------------------
|
||||
|
||||
private IEnumerable<string> EnumerateNames(int kind, string topic, string node)
|
||||
{
|
||||
long enumeration;
|
||||
lock (sync)
|
||||
enumeration = Opened(Native.BakaShrot_EnumerateNamesW(Handle(), kind, Text(topic), Text(node)),
|
||||
kind == 1 ? "List topics" : "List nodes");
|
||||
try
|
||||
{
|
||||
while (Next(Native.BakaShrot_NextName(enumeration)))
|
||||
yield return GetString(enumeration, Native.BakaShrot_GetEnumNameW);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Native.BakaShrot_CloseEnum(enumeration);
|
||||
}
|
||||
}
|
||||
|
||||
// An enumeration's handle, 0 on failure.
|
||||
private long Opened(long enumeration, string what)
|
||||
{
|
||||
if (enumeration == 0)
|
||||
throw Failed(what, false);
|
||||
return enumeration;
|
||||
}
|
||||
|
||||
// A Next...(): 1 an item, 0 no more, -1 closed (with its client, by Close()).
|
||||
private static bool Next(IntPtr result)
|
||||
{
|
||||
if (result.ToInt64() < 0)
|
||||
throw new ConnectionLostException("The client was closed while enumerating", ErrorCode.NotConnected);
|
||||
return result != IntPtr.Zero;
|
||||
}
|
||||
|
||||
private long Handle()
|
||||
{
|
||||
if (handle == 0)
|
||||
@@ -343,7 +476,7 @@ namespace NibblePoker.BakaShrot
|
||||
{
|
||||
if (count < 0)
|
||||
throw Failed(what, false);
|
||||
return GetString(Native.BakaShrot_GetNamesListW);
|
||||
return GetString(Native.BakaShrot_GetNamesListAsJsonW);
|
||||
}
|
||||
|
||||
// Strings go as UTF-16: one with a NUL would be cut there.
|
||||
@@ -354,19 +487,32 @@ namespace NibblePoker.BakaShrot
|
||||
return value;
|
||||
}
|
||||
|
||||
private delegate IntPtr Getter(long client, byte[] buffer, IntPtr size);
|
||||
private delegate IntPtr Getter(long handle, byte[] buffer, IntPtr size);
|
||||
private delegate IntPtr IndexGetter(long handle, int index, byte[] buffer, IntPtr size);
|
||||
|
||||
// A string getter: the size first (bytes, the terminator included), then the copy.
|
||||
private string GetString(Getter getter)
|
||||
{
|
||||
int size = getter(handle, null, IntPtr.Zero).ToInt32();
|
||||
return GetString(handle, getter);
|
||||
}
|
||||
|
||||
// A string getter, of a client or an enumeration: the size first
|
||||
// (bytes, the terminator included), then the copy.
|
||||
private static string GetString(long from, Getter getter)
|
||||
{
|
||||
int size = getter(from, null, IntPtr.Zero).ToInt32();
|
||||
if (size <= 2)
|
||||
return "";
|
||||
byte[] buffer = new byte[size];
|
||||
getter(handle, buffer, new IntPtr(size));
|
||||
getter(from, buffer, new IntPtr(size));
|
||||
return Encoding.Unicode.GetString(buffer, 0, size - 2);
|
||||
}
|
||||
|
||||
// A column's getter, as a string getter.
|
||||
private static Getter ColumnGetter(IndexGetter getter, int column)
|
||||
{
|
||||
return delegate(long from, byte[] buffer, IntPtr size) { return getter(from, column, buffer, size); };
|
||||
}
|
||||
|
||||
private void Check(IntPtr result, string what)
|
||||
{
|
||||
if (result == IntPtr.Zero)
|
||||
@@ -430,11 +576,20 @@ namespace NibblePoker.BakaShrot
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetMessageDataW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetMessageTopicW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetMessageNodeW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern int BakaShrot_ListTopics(long client);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern int BakaShrot_ListNodes(long client);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetNamesListW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern int BakaShrot_ListNamesW(long client, int kind, string topics, string nodes);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetNamesListAsJsonW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern IntPtr BakaShrot_QueryW(long client, string sql);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetQueryResultW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetQueryResultAsJsonW(long client, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern long BakaShrot_EnumerateNamesW(long client, int kind, string topics, string nodes);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_NextName(long enumeration);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetEnumNameW(long enumeration, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern long BakaShrot_EnumerateQueryW(long client, string sql);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern int BakaShrot_GetQueryColumnCount(long enumeration);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetQueryColumnNameW(long enumeration, int index, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_NextQueryRow(long enumeration);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_GetQueryValueW(long enumeration, int index, byte[] buffer, IntPtr size);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_IsQueryValueNull(long enumeration, int index);
|
||||
[DllImport(Dll, ExactSpelling = true)] internal static extern IntPtr BakaShrot_CloseEnum(long enumeration);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern long BakaShrot_PushMessageWithFileW(long client, string topic, string data, string filePath);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern long BakaShrot_PushMessageWithDataW(long client, string topic, string data, byte[] attachment, long size);
|
||||
[DllImport(Dll, ExactSpelling = true, CharSet = Utf16)] internal static extern long BakaShrot_SaveAttachmentW(long client, string topic, string node, long messageId, string filePath);
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
holding win-x64/, win-arm64/, linux-x64/, linux-arm64/, each with
|
||||
that release's library and THIRD-PARTY.txt (.github/workflows/ci.yml). -->
|
||||
<PropertyGroup>
|
||||
<Version>0.3.0</Version>
|
||||
<Version>0.4.0</Version>
|
||||
<Authors>Herwin Bozet (NibblePoker)</Authors>
|
||||
<Description>BakaShrot's client: a message store with replication. Over the client library, its Windows and Linux x64 and ARM64 builds included; .NET Framework 2.0 and up, .NET Core, .NET.</Description>
|
||||
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
;{- Code Header
|
||||
; ==- Basic Info -================================
|
||||
; Name: BakaShrot_Client_Prototypes.pbi
|
||||
; Version: 0.0.1
|
||||
; Version: 0.4.0
|
||||
; Author: Herwin Bozet (NibblePoker)
|
||||
;
|
||||
; ==- Compatibility -=============================
|
||||
@@ -40,7 +40,12 @@
|
||||
; about each are in BakaShrot_ErrorCodes.pbi, next to this file (included
|
||||
; below), BakaShrot_ErrorName() names them;
|
||||
; BakaShrot_GetRefusalReasonString(Client) says why in words.
|
||||
; _GetNamesList() is {"kind":"topic","names":[...],"truncated":false}.
|
||||
; _GetNamesListAsJson() is {"kind":"topic","names":[...],"truncated":false}.
|
||||
; _GetQueryResultAsJson() is {"columns":[...],"rows":[[...],...]}.
|
||||
; Without JSON: _EnumerateNames() / _EnumerateQuery() give an enumeration
|
||||
; (a quad, like a client's handle); _NextName() / _NextQueryRow() move to
|
||||
; the next item (the first, the first time: While ... = 1), the getters
|
||||
; read it, _CloseEnum() closes it (_FreeClient() closes its client's).
|
||||
; ============================================================================
|
||||
|
||||
XIncludeFile "BakaShrot_ErrorCodes.pbi"
|
||||
@@ -71,11 +76,23 @@ PrototypeC.i BakaShrot_IsMessageAttachmentReady_Prototype(Client.q)
|
||||
PrototypeC.i BakaShrot_GetMessageData_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.i BakaShrot_GetMessageTopic_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.i BakaShrot_GetMessageNode_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.l BakaShrot_ListTopics_Prototype(Client.q)
|
||||
PrototypeC.l BakaShrot_ListNodes_Prototype(Client.q)
|
||||
PrototypeC.i BakaShrot_GetNamesList_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
; Kind 1 topics, 2 nodes; Topics and Nodes a name or a pattern, "" for any.
|
||||
PrototypeC.l BakaShrot_ListNames_Prototype(Client.q, Kind.l, Topics.p-unicode, Nodes.p-unicode)
|
||||
PrototypeC.i BakaShrot_GetNamesListAsJson_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.i BakaShrot_Query_Prototype(Client.q, Sql.p-unicode)
|
||||
PrototypeC.i BakaShrot_GetQueryResult_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.i BakaShrot_GetQueryResultAsJson_Prototype(Client.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.q BakaShrot_EnumerateNames_Prototype(Client.q, Kind.l, Topics.p-unicode, Nodes.p-unicode)
|
||||
PrototypeC.i BakaShrot_NextName_Prototype(Enum.q)
|
||||
PrototypeC.i BakaShrot_GetEnumName_Prototype(Enum.q, *Buffer, BufferSize.i)
|
||||
PrototypeC.l BakaShrot_GetNameCount_Prototype(Enum.q)
|
||||
PrototypeC.i BakaShrot_IsNamesListTruncated_Prototype(Enum.q)
|
||||
PrototypeC.q BakaShrot_EnumerateQuery_Prototype(Client.q, Sql.p-unicode)
|
||||
PrototypeC.l BakaShrot_GetQueryColumnCount_Prototype(Enum.q)
|
||||
PrototypeC.i BakaShrot_GetQueryColumnName_Prototype(Enum.q, Index.l, *Buffer, BufferSize.i)
|
||||
PrototypeC.i BakaShrot_NextQueryRow_Prototype(Enum.q)
|
||||
PrototypeC.i BakaShrot_GetQueryValue_Prototype(Enum.q, Index.l, *Buffer, BufferSize.i)
|
||||
PrototypeC.i BakaShrot_IsQueryValueNull_Prototype(Enum.q, Index.l)
|
||||
PrototypeC.i BakaShrot_CloseEnum_Prototype(Enum.q)
|
||||
PrototypeC.q BakaShrot_PushMessageWithFile_Prototype(Client.q, Topic.p-unicode, JsonData.p-unicode, FilePath.p-unicode)
|
||||
PrototypeC.q BakaShrot_PushMessageWithData_Prototype(Client.q, Topic.p-unicode, JsonData.p-unicode, *Data, Size.q)
|
||||
PrototypeC.q BakaShrot_SaveAttachment_Prototype(Client.q, Topic.p-unicode, Node.p-unicode, MessageId.q, FilePath.p-unicode)
|
||||
@@ -109,11 +126,22 @@ Global BakaShrot_IsMessageAttachmentReady.BakaShrot_IsMessageAttachmentReady_Pro
|
||||
Global BakaShrot_GetMessageData.BakaShrot_GetMessageData_Prototype
|
||||
Global BakaShrot_GetMessageTopic.BakaShrot_GetMessageTopic_Prototype
|
||||
Global BakaShrot_GetMessageNode.BakaShrot_GetMessageNode_Prototype
|
||||
Global BakaShrot_ListTopics.BakaShrot_ListTopics_Prototype
|
||||
Global BakaShrot_ListNodes.BakaShrot_ListNodes_Prototype
|
||||
Global BakaShrot_GetNamesList.BakaShrot_GetNamesList_Prototype
|
||||
Global BakaShrot_ListNames.BakaShrot_ListNames_Prototype
|
||||
Global BakaShrot_GetNamesListAsJson.BakaShrot_GetNamesListAsJson_Prototype
|
||||
Global BakaShrot_Query.BakaShrot_Query_Prototype
|
||||
Global BakaShrot_GetQueryResult.BakaShrot_GetQueryResult_Prototype
|
||||
Global BakaShrot_GetQueryResultAsJson.BakaShrot_GetQueryResultAsJson_Prototype
|
||||
Global BakaShrot_EnumerateNames.BakaShrot_EnumerateNames_Prototype
|
||||
Global BakaShrot_NextName.BakaShrot_NextName_Prototype
|
||||
Global BakaShrot_GetEnumName.BakaShrot_GetEnumName_Prototype
|
||||
Global BakaShrot_GetNameCount.BakaShrot_GetNameCount_Prototype
|
||||
Global BakaShrot_IsNamesListTruncated.BakaShrot_IsNamesListTruncated_Prototype
|
||||
Global BakaShrot_EnumerateQuery.BakaShrot_EnumerateQuery_Prototype
|
||||
Global BakaShrot_GetQueryColumnCount.BakaShrot_GetQueryColumnCount_Prototype
|
||||
Global BakaShrot_GetQueryColumnName.BakaShrot_GetQueryColumnName_Prototype
|
||||
Global BakaShrot_NextQueryRow.BakaShrot_NextQueryRow_Prototype
|
||||
Global BakaShrot_GetQueryValue.BakaShrot_GetQueryValue_Prototype
|
||||
Global BakaShrot_IsQueryValueNull.BakaShrot_IsQueryValueNull_Prototype
|
||||
Global BakaShrot_CloseEnum.BakaShrot_CloseEnum_Prototype
|
||||
Global BakaShrot_PushMessageWithFile.BakaShrot_PushMessageWithFile_Prototype
|
||||
Global BakaShrot_PushMessageWithData.BakaShrot_PushMessageWithData_Prototype
|
||||
Global BakaShrot_SaveAttachment.BakaShrot_SaveAttachment_Prototype
|
||||
@@ -185,11 +213,22 @@ Procedure.i BakaShrot_LoadClient(DllPath.s)
|
||||
BakaShrot_GetMessageData = BakaShrot_ClientFunction("BakaShrot_GetMessageDataW")
|
||||
BakaShrot_GetMessageTopic = BakaShrot_ClientFunction("BakaShrot_GetMessageTopicW")
|
||||
BakaShrot_GetMessageNode = BakaShrot_ClientFunction("BakaShrot_GetMessageNodeW")
|
||||
BakaShrot_ListTopics = BakaShrot_ClientFunction("BakaShrot_ListTopics")
|
||||
BakaShrot_ListNodes = BakaShrot_ClientFunction("BakaShrot_ListNodes")
|
||||
BakaShrot_GetNamesList = BakaShrot_ClientFunction("BakaShrot_GetNamesListW")
|
||||
BakaShrot_ListNames = BakaShrot_ClientFunction("BakaShrot_ListNamesW")
|
||||
BakaShrot_GetNamesListAsJson = BakaShrot_ClientFunction("BakaShrot_GetNamesListAsJsonW")
|
||||
BakaShrot_Query = BakaShrot_ClientFunction("BakaShrot_QueryW")
|
||||
BakaShrot_GetQueryResult = BakaShrot_ClientFunction("BakaShrot_GetQueryResultW")
|
||||
BakaShrot_GetQueryResultAsJson = BakaShrot_ClientFunction("BakaShrot_GetQueryResultAsJsonW")
|
||||
BakaShrot_EnumerateNames = BakaShrot_ClientFunction("BakaShrot_EnumerateNamesW")
|
||||
BakaShrot_NextName = BakaShrot_ClientFunction("BakaShrot_NextName")
|
||||
BakaShrot_GetEnumName = BakaShrot_ClientFunction("BakaShrot_GetEnumNameW")
|
||||
BakaShrot_GetNameCount = BakaShrot_ClientFunction("BakaShrot_GetNameCount")
|
||||
BakaShrot_IsNamesListTruncated = BakaShrot_ClientFunction("BakaShrot_IsNamesListTruncated")
|
||||
BakaShrot_EnumerateQuery = BakaShrot_ClientFunction("BakaShrot_EnumerateQueryW")
|
||||
BakaShrot_GetQueryColumnCount = BakaShrot_ClientFunction("BakaShrot_GetQueryColumnCount")
|
||||
BakaShrot_GetQueryColumnName = BakaShrot_ClientFunction("BakaShrot_GetQueryColumnNameW")
|
||||
BakaShrot_NextQueryRow = BakaShrot_ClientFunction("BakaShrot_NextQueryRow")
|
||||
BakaShrot_GetQueryValue = BakaShrot_ClientFunction("BakaShrot_GetQueryValueW")
|
||||
BakaShrot_IsQueryValueNull = BakaShrot_ClientFunction("BakaShrot_IsQueryValueNull")
|
||||
BakaShrot_CloseEnum = BakaShrot_ClientFunction("BakaShrot_CloseEnum")
|
||||
BakaShrot_PushMessageWithFile = BakaShrot_ClientFunction("BakaShrot_PushMessageWithFileW")
|
||||
BakaShrot_PushMessageWithData = BakaShrot_ClientFunction("BakaShrot_PushMessageWithDataW")
|
||||
BakaShrot_SaveAttachment = BakaShrot_ClientFunction("BakaShrot_SaveAttachmentW")
|
||||
@@ -226,6 +265,26 @@ Procedure.s BakaShrot_GetString(Client.q, Getter.BakaShrot_StringGetter_Prototyp
|
||||
ProcedureReturn Text
|
||||
EndProcedure
|
||||
|
||||
; The same for a getter of a column (Getter(Enum, Index, *Buffer, BufferSize)).
|
||||
PrototypeC.i BakaShrot_ColumnGetter_Prototype(Enum.q, Index.l, *Buffer, BufferSize.i)
|
||||
Procedure.s BakaShrot_GetColumnString(Enum.q, Index.l, Getter.BakaShrot_ColumnGetter_Prototype)
|
||||
Protected *Buffer, BufferSize.i, Text.s
|
||||
|
||||
BufferSize = Getter(Enum, Index, 0, 0)
|
||||
If BufferSize <= 0
|
||||
ProcedureReturn ""
|
||||
EndIf
|
||||
*Buffer = AllocateMemory(BufferSize)
|
||||
If Not *Buffer
|
||||
ProcedureReturn ""
|
||||
EndIf
|
||||
Getter(Enum, Index, *Buffer, BufferSize)
|
||||
Text = PeekS(*Buffer, -1, #PB_Unicode)
|
||||
FreeMemory(*Buffer)
|
||||
|
||||
ProcedureReturn Text
|
||||
EndProcedure
|
||||
|
||||
; The string getters, as strings.
|
||||
Procedure.s BakaShrot_GetRefusalReasonString(Client.q)
|
||||
ProcedureReturn BakaShrot_GetString(Client, BakaShrot_GetRefusalReason)
|
||||
@@ -247,12 +306,24 @@ Procedure.s BakaShrot_GetMessageNodeString(Client.q)
|
||||
ProcedureReturn BakaShrot_GetString(Client, BakaShrot_GetMessageNode)
|
||||
EndProcedure
|
||||
|
||||
Procedure.s BakaShrot_GetNamesListString(Client.q)
|
||||
ProcedureReturn BakaShrot_GetString(Client, BakaShrot_GetNamesList)
|
||||
Procedure.s BakaShrot_GetNamesListAsJsonString(Client.q)
|
||||
ProcedureReturn BakaShrot_GetString(Client, BakaShrot_GetNamesListAsJson)
|
||||
EndProcedure
|
||||
|
||||
Procedure.s BakaShrot_GetQueryResultString(Client.q)
|
||||
ProcedureReturn BakaShrot_GetString(Client, BakaShrot_GetQueryResult)
|
||||
Procedure.s BakaShrot_GetQueryResultAsJsonString(Client.q)
|
||||
ProcedureReturn BakaShrot_GetString(Client, BakaShrot_GetQueryResultAsJson)
|
||||
EndProcedure
|
||||
|
||||
Procedure.s BakaShrot_GetEnumNameString(Enum.q)
|
||||
ProcedureReturn BakaShrot_GetString(Enum, BakaShrot_GetEnumName)
|
||||
EndProcedure
|
||||
|
||||
Procedure.s BakaShrot_GetQueryColumnNameString(Enum.q, Index.l)
|
||||
ProcedureReturn BakaShrot_GetColumnString(Enum, Index, BakaShrot_GetQueryColumnName)
|
||||
EndProcedure
|
||||
|
||||
Procedure.s BakaShrot_GetQueryValueString(Enum.q, Index.l)
|
||||
ProcedureReturn BakaShrot_GetColumnString(Enum, Index, BakaShrot_GetQueryValue)
|
||||
EndProcedure
|
||||
|
||||
|
||||
@@ -264,7 +335,7 @@ EndProcedure
|
||||
; connected at the same time reads it too.
|
||||
CompilerIf #PB_Compiler_IsMainFile
|
||||
|
||||
Define Client.q, Second.q, Id.q, Size.q, *Attachment, *Copy
|
||||
Define Client.q, Second.q, Enum.q, Id.q, Size.q, *Attachment, *Copy
|
||||
|
||||
; Prints why a client's last call failed, then ends.
|
||||
Procedure Failed(Client.q, What.s)
|
||||
@@ -321,8 +392,16 @@ CompilerIf #PB_Compiler_IsMainFile
|
||||
EndIf
|
||||
FreeMemory(*Attachment) : FreeMemory(*Copy)
|
||||
|
||||
If BakaShrot_ListTopics(Client) < 0 : Failed(Client, "Listing") : EndIf
|
||||
PrintN("Topics: " + BakaShrot_GetNamesListString(Client))
|
||||
If BakaShrot_ListNames(Client, 1, "", "") < 0 : Failed(Client, "Listing") : EndIf
|
||||
PrintN("Topics: " + BakaShrot_GetNamesListAsJsonString(Client))
|
||||
; The topics this client's node pushed to ("" for any topic), one at a time.
|
||||
Enum = BakaShrot_EnumerateNames(Client, 1, "", BakaShrot_GetNodeString(Client))
|
||||
If Not Enum : Failed(Client, "Listing") : EndIf
|
||||
PrintN("Topics it pushed to (" + Str(BakaShrot_GetNameCount(Enum)) + "):")
|
||||
While BakaShrot_NextName(Enum) = 1
|
||||
PrintN(" " + BakaShrot_GetEnumNameString(Enum))
|
||||
Wend
|
||||
BakaShrot_CloseEnum(Enum)
|
||||
|
||||
If BakaShrot_PushMessageTo(Client, "nothing", "{}") < 0
|
||||
PrintN("A push to a topic it may not push to: " + BakaShrot_ErrorName(BakaShrot_GetLastError(Client)) + ", " + BakaShrot_GetRefusalReasonString(Client))
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
;{- Code Header
|
||||
; ==- Basic Info -================================
|
||||
; Name: BakaShrot_ErrorCodes.pbi
|
||||
; Version: 0.0.1
|
||||
; Version: 0.4.0
|
||||
; Author: Herwin Bozet (NibblePoker)
|
||||
;
|
||||
; ==- Compatibility -=============================
|
||||
@@ -16,7 +16,7 @@
|
||||
; BakaShrot error codes
|
||||
;
|
||||
; What BakaShrot_GetLastError() returns after a failed call of the client DLL
|
||||
; (bakashrot.dll), and the code a server sends with each refusal (protocol 7,
|
||||
; (bakashrot.dll), and the code a server sends with each refusal (protocol 8,
|
||||
; crates/bakashrot-proto/src/error.rs).
|
||||
; The Python client has them in bakashrot.ErrorCode. Shipped next to
|
||||
; BakaShrot_Client_Prototypes.pbi, which includes it.
|
||||
|
||||
@@ -58,7 +58,7 @@ from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any, Iterator, Optional, Union
|
||||
|
||||
__version__ = "0.3.0"
|
||||
__version__ = "0.4.0"
|
||||
|
||||
__all__ = [
|
||||
"Client", "Message", "MessagesPage", "NamesList",
|
||||
@@ -264,11 +264,10 @@ class _Dll:
|
||||
"BakaShrot_GetMessageTopicA": (I, [H, S, I]),
|
||||
"BakaShrot_GetMessageNodeA": (I, [H, S, I]),
|
||||
"BakaShrot_GetMessageDataA": (I, [H, S, I]),
|
||||
"BakaShrot_ListTopics": (L, [H]),
|
||||
"BakaShrot_ListNodes": (L, [H]),
|
||||
"BakaShrot_GetNamesListA": (I, [H, S, I]),
|
||||
"BakaShrot_ListNamesA": (L, [H, L, P, P]),
|
||||
"BakaShrot_GetNamesListAsJsonA": (I, [H, S, I]),
|
||||
"BakaShrot_QueryA": (I, [H, P]),
|
||||
"BakaShrot_GetQueryResultA": (I, [H, S, I]),
|
||||
"BakaShrot_GetQueryResultAsJsonA": (I, [H, S, I]),
|
||||
"BakaShrot_PushMessageWithFileA": (Q, [H, P, P, P]),
|
||||
"BakaShrot_PushMessageWithDataA": (Q, [H, P, P, P, Q]),
|
||||
"BakaShrot_SaveAttachmentA": (Q, [H, P, P, Q, P]),
|
||||
@@ -556,20 +555,25 @@ class Client:
|
||||
if not page.more:
|
||||
time.sleep(interval)
|
||||
|
||||
def list_topics(self) -> NamesList:
|
||||
"""The topics with messages that the client may push to or read (every one with list_topics or query)."""
|
||||
return self._list(self._dll.ListTopics, "List topics")
|
||||
def list_topics(self, topic: Optional[str] = None, node: Optional[str] = None) -> NamesList:
|
||||
"""The topics with messages that the client may push to or read (every one with list_topics or query).
|
||||
topic and node, each a name or a pattern ("sensors.*"), keep those of the topic/node pairs they
|
||||
match: list_topics(node="kitchen") is the topics kitchen pushed to."""
|
||||
return self._list(1, topic, node, "List topics")
|
||||
|
||||
def list_nodes(self) -> NamesList:
|
||||
"""The nodes with messages in the topics the client may read (every one with list_nodes or query)."""
|
||||
return self._list(self._dll.ListNodes, "List nodes")
|
||||
def list_nodes(self, topic: Optional[str] = None, node: Optional[str] = None) -> NamesList:
|
||||
"""The nodes with messages in the topics the client may read (every one with list_nodes or query).
|
||||
topic and node as list_topics() takes them: list_nodes(topic="logs") is the nodes that pushed to logs."""
|
||||
return self._list(2, topic, node, "List nodes")
|
||||
|
||||
def _list(self, function, what: str) -> NamesList:
|
||||
def _list(self, kind: int, topic: Optional[str], node: Optional[str], what: str) -> NamesList:
|
||||
topic = None if topic is None else _name(topic)
|
||||
node = None if node is None else _name(node)
|
||||
with self._lock:
|
||||
self._check_connected()
|
||||
if function(self._handle) < 0:
|
||||
if self._dll.ListNamesA(self._handle, kind, topic, node) < 0:
|
||||
raise self._failed(what)
|
||||
parsed = json.loads(self._dll.string(self._dll.GetNamesListA, self._handle))
|
||||
parsed = json.loads(self._dll.string(self._dll.GetNamesListAsJsonA, self._handle))
|
||||
return NamesList(parsed["kind"], parsed["names"], bool(parsed["truncated"]))
|
||||
|
||||
def query_raw(self, sql: str) -> str:
|
||||
@@ -583,7 +587,7 @@ class Client:
|
||||
self._check_connected()
|
||||
if not self._dll.QueryA(self._handle, sql.encode("utf-8")):
|
||||
raise self._failed("Query")
|
||||
return self._dll.string(self._dll.GetQueryResultA, self._handle)
|
||||
return self._dll.string(self._dll.GetQueryResultAsJsonA, self._handle)
|
||||
|
||||
def query(self, sql: str) -> list:
|
||||
"""query_raw(), as a list of dicts, one per row, by column name."""
|
||||
|
||||
@@ -152,6 +152,14 @@ with kitchen:
|
||||
check("list_topics()", topics.kind == "topic" and topics.names == ["logs", "secret"] and not topics.truncated, topics)
|
||||
nodes = kitchen.list_nodes()
|
||||
check("list_nodes()", nodes.kind == "node" and nodes.names == ["kitchen"], nodes)
|
||||
topics = kitchen.list_topics(node="KITCHEN")
|
||||
check("list_topics(node=...): the topics it pushed to", topics.names == ["logs", "secret"], topics)
|
||||
nodes = kitchen.list_nodes(topic="s*")
|
||||
check("list_nodes(topic=pattern): the nodes that pushed to those", nodes.names == ["kitchen"], nodes)
|
||||
check("list_topics(node=...) of a node without messages: none", kitchen.list_topics(node="nobody").names == [])
|
||||
ok, detail = raises(lambda: kitchen.list_nodes(topic="a b"), BakaShrotError, "isn't a topic name or pattern",
|
||||
ErrorCode.INVALID_ARGUMENT)
|
||||
check("list_nodes(topic=...) neither a name nor a pattern: INVALID_ARGUMENT", ok, detail)
|
||||
|
||||
# --- follow() ------------------------------------------------------------
|
||||
followed = []
|
||||
|
||||
Reference in New Issue
Block a user