fantom-polycall 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +193 -2
- package/build.fan +33 -0
- package/examples/consumer/consumer.fan +77 -0
- package/fan/Native.fan +270 -0
- package/fan/Peer.fan +193 -0
- package/fan/Polycall.fan +224 -0
- package/fan/Status.fan +114 -0
- package/package.json +43 -4
- package/scripts/consumer-test.mjs +209 -0
- package/test/PolycallTest.fan +214 -0
- package/test/fixtures/bad.polycallrc +1 -0
- package/test/fixtures/good.polycallrc +4 -0
- package/test/fixtures/unknown-key.polycallrc +2 -0
package/fan/Peer.fan
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
//
|
|
2
|
+
// polycall-peer/1 nodes (polycall_peer_*).
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
using [java] java.lang.foreign
|
|
6
|
+
using [java] java.lang.invoke
|
|
7
|
+
|
|
8
|
+
**
|
|
9
|
+
** A polycall-peer/1 node owned by the library. Handles are validated by
|
|
10
|
+
** the library on every call: using or closing a closed node throws
|
|
11
|
+
** `PolycallErr` with `Status.invalidHandle`, never a crash. All methods
|
|
12
|
+
** are thread-safe, and a Peer is const so it can be shared with actors.
|
|
13
|
+
**
|
|
14
|
+
** Release the node explicitly with `close`.
|
|
15
|
+
**
|
|
16
|
+
const class Peer
|
|
17
|
+
{
|
|
18
|
+
**
|
|
19
|
+
** 'polycall_peer_open'. `bind` is "host:port" to listen on (port 0 =
|
|
20
|
+
** ephemeral); null makes a send-only node. `authToken` is the shared
|
|
21
|
+
** secret, required when binding to a non-loopback address.
|
|
22
|
+
**
|
|
23
|
+
static Peer open(Str nodeId, Str? bind := null, Str? authToken := null)
|
|
24
|
+
{
|
|
25
|
+
n := Native.get
|
|
26
|
+
Int handle := n.scoped |arena->Obj?|
|
|
27
|
+
{
|
|
28
|
+
out := n.buffer(arena, 4)
|
|
29
|
+
Polycall.check(Native.invokeInt(n.peerOpen,
|
|
30
|
+
Obj?[n.cstr(arena, nodeId), n.cstr(arena, bind), n.cstr(arena, authToken), out]))
|
|
31
|
+
return n.readInt32(out)
|
|
32
|
+
}
|
|
33
|
+
return make(handle)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private new make(Int handle) { this.handle = handle }
|
|
37
|
+
|
|
38
|
+
** The library's int32 handle (> 0).
|
|
39
|
+
const Int handle
|
|
40
|
+
|
|
41
|
+
**
|
|
42
|
+
** Stop the listener, wake blocked calls (`Status.closed`) and release
|
|
43
|
+
** everything. A second close is `Status.invalidHandle`.
|
|
44
|
+
**
|
|
45
|
+
Void close() { simple(Native.get.peerClose, Obj?[,]) }
|
|
46
|
+
|
|
47
|
+
** The bound "host:port" ("" for a send-only node).
|
|
48
|
+
Str endpoint() { textOut(Native.get.peerEndpoint, Polycall.endpointMax) }
|
|
49
|
+
|
|
50
|
+
** This node's id.
|
|
51
|
+
Str nodeId() { textOut(Native.get.peerNodeId, Polycall.peerIdMax) }
|
|
52
|
+
|
|
53
|
+
** Add or replace `peerId -> endpoint` in THIS node's registry.
|
|
54
|
+
Void register(Str peerId, Str endpoint)
|
|
55
|
+
{
|
|
56
|
+
n := Native.get
|
|
57
|
+
n.scoped |arena->Obj?| { simple(n.peerRegister, Obj?[n.cstr(arena, peerId), n.cstr(arena, endpoint)]); return null }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
** `Status.notFound` when `peerId` is not registered.
|
|
61
|
+
Void unregister(Str peerId)
|
|
62
|
+
{
|
|
63
|
+
n := Native.get
|
|
64
|
+
n.scoped |arena->Obj?| { simple(n.peerUnregister, Obj?[n.cstr(arena, peerId)]); return null }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
** This node's registry as a JSON object '{"id":"host:port",...}'.
|
|
68
|
+
Str list() { jsonOut(Native.get.peerList) }
|
|
69
|
+
|
|
70
|
+
** This node's health as JSON (node id, endpoint, peers, inbox, counters).
|
|
71
|
+
Str health() { jsonOut(Native.get.peerHealth) }
|
|
72
|
+
|
|
73
|
+
** GET /health on `peer` (a registered id or "host:port").
|
|
74
|
+
Void ping(Str peer, Int timeoutMs := 5000)
|
|
75
|
+
{
|
|
76
|
+
n := Native.get
|
|
77
|
+
n.scoped |arena->Obj?| { simple(n.peerPing, Obj?[n.cstr(arena, peer), Native.cint(timeoutMs)]); return null }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
**
|
|
81
|
+
** Deliver `payload` (binary-safe, all `Buf.size` bytes, at most
|
|
82
|
+
** `Polycall.peerMaxPayload`) to `peer`. Exactly one attempt; after
|
|
83
|
+
** `Status.timeout` / `Status.busy` retry with the SAME `messageId` and
|
|
84
|
+
** the receiver drops the duplicate. null generates an id.
|
|
85
|
+
**
|
|
86
|
+
Void send(Str peer, Buf payload, Str? messageId := null, Int timeoutMs := 5000)
|
|
87
|
+
{
|
|
88
|
+
n := Native.get
|
|
89
|
+
n.scoped |arena->Obj?|
|
|
90
|
+
{
|
|
91
|
+
data := n.copyIn(arena, payload)
|
|
92
|
+
simple(n.peerSend, Obj?[n.cstr(arena, peer), data, payload.size, n.cstr(arena, messageId), Native.cint(timeoutMs)])
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
**
|
|
98
|
+
** Take the oldest received message. `timeoutMs` 0 = do not wait
|
|
99
|
+
** (`Status.timeout` when the inbox is empty), `Polycall.waitForever` =
|
|
100
|
+
** until a message, `cancel` (`Status.cancelled`) or `close`
|
|
101
|
+
** (`Status.closed`). When `capacity` is smaller than the payload this is
|
|
102
|
+
** `Status.tooLarge`, `PolycallErr.needed` is the size required and the
|
|
103
|
+
** message stays queued.
|
|
104
|
+
**
|
|
105
|
+
PeerMessage recv(Int timeoutMs := Polycall.waitForever, Int capacity := Polycall.peerMaxPayload)
|
|
106
|
+
{
|
|
107
|
+
n := Native.get
|
|
108
|
+
return n.scoped |arena->Obj?|
|
|
109
|
+
{
|
|
110
|
+
sender := n.buffer(arena, Polycall.peerIdMax)
|
|
111
|
+
id := n.buffer(arena, Polycall.messageIdMax)
|
|
112
|
+
payload := n.buffer(arena, capacity)
|
|
113
|
+
len := n.sizeOut(arena)
|
|
114
|
+
code := Native.invokeInt(n.peerRecv, Obj?[Native.cint(handle), Native.cint(timeoutMs),
|
|
115
|
+
sender, Polycall.peerIdMax, id, Polycall.messageIdMax, payload, capacity, len])
|
|
116
|
+
if (code == Status.tooLarge.code)
|
|
117
|
+
throw PolycallErr(code, n.lastErrorText, n.readSize(len))
|
|
118
|
+
Polycall.check(code)
|
|
119
|
+
return PeerMessage(n.text(sender), n.text(id), n.bytes(payload, n.readSize(len)))
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
** Wake every `recv` currently blocked on this node with `Status.cancelled`.
|
|
124
|
+
Void cancel() { simple(Native.get.peerCancel, Obj?[,]) }
|
|
125
|
+
|
|
126
|
+
override Str toStr() { "Peer($handle)" }
|
|
127
|
+
|
|
128
|
+
//////////////////////////////////////////////////////////////////////////
|
|
129
|
+
// Helpers
|
|
130
|
+
//////////////////////////////////////////////////////////////////////////
|
|
131
|
+
|
|
132
|
+
** Call `mh(handle, args...)` and throw on a negative status.
|
|
133
|
+
private Void simple(MethodHandle mh, Obj?[] args)
|
|
134
|
+
{
|
|
135
|
+
Polycall.check(Native.invokeInt(mh, Obj?[Native.cint(handle)].addAll(args)))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
** Text output with snprintf rules and a fixed-size buffer (status return).
|
|
139
|
+
private Str textOut(MethodHandle mh, Int cap)
|
|
140
|
+
{
|
|
141
|
+
n := Native.get
|
|
142
|
+
return n.scoped |arena->Obj?|
|
|
143
|
+
{
|
|
144
|
+
buf := n.buffer(arena, cap)
|
|
145
|
+
simple(mh, Obj?[buf, cap])
|
|
146
|
+
return n.text(buf)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
** JSON output with an out_len; grows the buffer once when it was too small.
|
|
151
|
+
private Str jsonOut(MethodHandle mh)
|
|
152
|
+
{
|
|
153
|
+
n := Native.get
|
|
154
|
+
return n.scoped |arena->Obj?|
|
|
155
|
+
{
|
|
156
|
+
cap := 4096
|
|
157
|
+
while (true)
|
|
158
|
+
{
|
|
159
|
+
buf := n.buffer(arena, cap)
|
|
160
|
+
len := n.sizeOut(arena)
|
|
161
|
+
code := Native.invokeInt(mh, Obj?[Native.cint(handle), buf, cap, len])
|
|
162
|
+
if (code == Status.tooLarge.code && n.readSize(len) >= cap) { cap = n.readSize(len) + 1; continue }
|
|
163
|
+
Polycall.check(code)
|
|
164
|
+
return n.text(buf)
|
|
165
|
+
}
|
|
166
|
+
throw Err("unreachable")
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
**************************************************************************
|
|
172
|
+
** PeerMessage
|
|
173
|
+
**************************************************************************
|
|
174
|
+
|
|
175
|
+
** A message taken from a node's inbox by `Peer.recv`.
|
|
176
|
+
const class PeerMessage
|
|
177
|
+
{
|
|
178
|
+
new make(Str sender, Str id, Buf payload)
|
|
179
|
+
{
|
|
180
|
+
this.sender = sender
|
|
181
|
+
this.id = id
|
|
182
|
+
this.payload = payload
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
** The sending node's id.
|
|
186
|
+
const Str sender
|
|
187
|
+
** The message id (sender-chosen or generated).
|
|
188
|
+
const Str id
|
|
189
|
+
** The payload bytes, exactly as sent (immutable).
|
|
190
|
+
const Buf payload
|
|
191
|
+
|
|
192
|
+
override Str toStr() { "PeerMessage($sender, $id, $payload.size bytes)" }
|
|
193
|
+
}
|
package/fan/Polycall.fan
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
//
|
|
2
|
+
// fantom-polycall: Fantom binding for the Polycall core (binding ABI v1,
|
|
3
|
+
// core >= 1.1.0).
|
|
4
|
+
//
|
|
5
|
+
// Direct C FFI: every method here calls the real libpolycall
|
|
6
|
+
// (polycall.dll / libpolycall.so.1) through Fantom's Java FFI and the Java
|
|
7
|
+
// Foreign Function & Memory API. Nothing is reimplemented in Fantom and no
|
|
8
|
+
// CLI is spawned.
|
|
9
|
+
//
|
|
10
|
+
|
|
11
|
+
**
|
|
12
|
+
** Library, configuration and RPC entry points of the Polycall core.
|
|
13
|
+
**
|
|
14
|
+
** - library -- `libraryAbiVersion`, `checkAbi`, `version`, `strerror`, `lastError`
|
|
15
|
+
** - config -- `runConfig`, `describe`
|
|
16
|
+
** - RPC -- `call`, `callRaw` (one polycall_rpc v1 round trip)
|
|
17
|
+
** - peers -- see `Peer`
|
|
18
|
+
**
|
|
19
|
+
** Ownership: the library never returns memory to free. Every output is
|
|
20
|
+
** written into a buffer this binding allocates for the duration of one
|
|
21
|
+
** call and copied into a Fantom 'Str' / 'Buf' before it returns.
|
|
22
|
+
**
|
|
23
|
+
** Errors: a negative status is thrown as `PolycallErr`, which carries the
|
|
24
|
+
** `Status`, the raw code, the library's text and this thread's detail.
|
|
25
|
+
** A core that cannot be located is `PolycallLoadErr`.
|
|
26
|
+
**
|
|
27
|
+
const class Polycall
|
|
28
|
+
{
|
|
29
|
+
** The binding ABI generation this package is written against.
|
|
30
|
+
static const Int abiVersion := 1
|
|
31
|
+
|
|
32
|
+
** Buffer size for peer ids incl. NUL; ids are 1..63 of [A-Za-z0-9._-].
|
|
33
|
+
static const Int peerIdMax := 64
|
|
34
|
+
** Buffer size for message ids incl. NUL.
|
|
35
|
+
static const Int messageIdMax := 64
|
|
36
|
+
** Buffer size for "host:port" incl. NUL.
|
|
37
|
+
static const Int endpointMax := 128
|
|
38
|
+
** Largest peer payload (1 MiB).
|
|
39
|
+
static const Int peerMaxPayload := 1.shiftl(20)
|
|
40
|
+
** Largest `call` output (1 MiB).
|
|
41
|
+
static const Int callMaxOutput := 1.shiftl(20)
|
|
42
|
+
** `timeoutMs` for `Peer.recv`: wait until a message, `Peer.cancel` or `Peer.close`.
|
|
43
|
+
static const Int waitForever := 0xffff_ffff
|
|
44
|
+
|
|
45
|
+
private new make() {}
|
|
46
|
+
|
|
47
|
+
//////////////////////////////////////////////////////////////////////////
|
|
48
|
+
// Library
|
|
49
|
+
//////////////////////////////////////////////////////////////////////////
|
|
50
|
+
|
|
51
|
+
** Load the core now (it is otherwise loaded on first use). Throws
|
|
52
|
+
** `PolycallLoadErr` with what to set when it cannot be found.
|
|
53
|
+
static Void load() { Native.get }
|
|
54
|
+
|
|
55
|
+
** 'polycall_ffi_abi_version()' of the loaded library.
|
|
56
|
+
static Int libraryAbiVersion()
|
|
57
|
+
{
|
|
58
|
+
n := Native.get
|
|
59
|
+
return Native.invokeInt(n.abiVersion, Obj?[,])
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
** Throw `PolycallErr` (`Status.unsupported`) unless the loaded library
|
|
63
|
+
** implements binding ABI v1. Call it once before anything else.
|
|
64
|
+
static Void checkAbi()
|
|
65
|
+
{
|
|
66
|
+
got := libraryAbiVersion
|
|
67
|
+
if (got != abiVersion)
|
|
68
|
+
throw PolycallErr(Status.unsupported.code, "binding ABI $got loaded, fantom-polycall needs $abiVersion")
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
** The library version, e.g. "1.1.0". With a `capacity` too small for it
|
|
72
|
+
** (the C buffer size, NUL included) this is `Status.tooLarge`.
|
|
73
|
+
static Str version(Int capacity := 32)
|
|
74
|
+
{
|
|
75
|
+
n := Native.get
|
|
76
|
+
return n.scoped |arena->Obj?|
|
|
77
|
+
{
|
|
78
|
+
buf := n.buffer(arena, capacity)
|
|
79
|
+
len := Native.invokeInt(n.version, Obj?[buf, Native.cint(capacity)])
|
|
80
|
+
if (len < 0) throw PolycallErr.fromStatus(len)
|
|
81
|
+
if (len >= capacity) throw PolycallErr(Status.tooLarge.code, "version needs ${len + 1} bytes, capacity is $capacity")
|
|
82
|
+
return n.text(buf)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
** The library's own static text for a status code
|
|
87
|
+
** ("POLYCALL_E_TIMEOUT: deadline exceeded").
|
|
88
|
+
static Str strerror(Int status) { Native.get.strerrorText(status) }
|
|
89
|
+
|
|
90
|
+
** This thread's detail for its most recent failed library call ("" after
|
|
91
|
+
** a success). `PolycallErr.detail` already holds it for thrown errors.
|
|
92
|
+
static Str lastError() { Native.get.lastErrorText }
|
|
93
|
+
|
|
94
|
+
//////////////////////////////////////////////////////////////////////////
|
|
95
|
+
// Configuration
|
|
96
|
+
//////////////////////////////////////////////////////////////////////////
|
|
97
|
+
|
|
98
|
+
**
|
|
99
|
+
** 'polycall_ffi_run_config': load and validate one configuration file
|
|
100
|
+
** with the grammar 'polycall config validate' uses. Never starts a service.
|
|
101
|
+
** Throws `PolycallErr` with `Status.invalidArgument`, `Status.notFound`
|
|
102
|
+
** (missing file), `Status.config` or `Status.unsupported`.
|
|
103
|
+
**
|
|
104
|
+
static Void runConfig(Str path, ConfigMode mode := ConfigMode.validate)
|
|
105
|
+
{
|
|
106
|
+
n := Native.get
|
|
107
|
+
n.scoped |arena->Obj?|
|
|
108
|
+
{
|
|
109
|
+
check(Native.invokeInt(n.runConfig, Obj?[n.cstr(arena, path), Native.cint(mode.ordinal)]))
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
**
|
|
115
|
+
** 'polycall_ffi_describe': the JSON description of a configuration file.
|
|
116
|
+
** `capacity` is the C buffer size (NUL included); when the JSON does not
|
|
117
|
+
** fit this is `Status.tooLarge` and `PolycallErr.needed` is the size the
|
|
118
|
+
** buffer must have.
|
|
119
|
+
**
|
|
120
|
+
static Str describe(Str path, Int capacity := 65536)
|
|
121
|
+
{
|
|
122
|
+
n := Native.get
|
|
123
|
+
return n.scoped |arena->Obj?|
|
|
124
|
+
{
|
|
125
|
+
buf := n.buffer(arena, capacity)
|
|
126
|
+
len := Native.invokeInt(n.describe, Obj?[n.cstr(arena, path), buf, Native.cint(capacity)])
|
|
127
|
+
if (len < 0) throw PolycallErr.fromStatus(len)
|
|
128
|
+
if (len >= capacity)
|
|
129
|
+
throw PolycallErr(Status.tooLarge.code, "the description needs ${len + 1} bytes, capacity is $capacity", len + 1)
|
|
130
|
+
return n.text(buf)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
//////////////////////////////////////////////////////////////////////////
|
|
135
|
+
// RPC
|
|
136
|
+
//////////////////////////////////////////////////////////////////////////
|
|
137
|
+
|
|
138
|
+
**
|
|
139
|
+
** 'polycall_call': one polycall_rpc v1 round trip to `endpoint`
|
|
140
|
+
** ("host:port"), executed once and never retried. BLOCKS the calling
|
|
141
|
+
** thread for up to `timeoutMs` (1..600000). Never throws `PolycallErr`:
|
|
142
|
+
** inspect `Reply.status`.
|
|
143
|
+
**
|
|
144
|
+
static Reply callRaw(Str endpoint, Str service, Str operation, Str? inputJson,
|
|
145
|
+
Int timeoutMs, Int capacity := callMaxOutput)
|
|
146
|
+
{
|
|
147
|
+
n := Native.get
|
|
148
|
+
return n.scoped |arena->Obj?|
|
|
149
|
+
{
|
|
150
|
+
out := n.buffer(arena, capacity)
|
|
151
|
+
len := n.sizeOut(arena)
|
|
152
|
+
code := Native.invokeInt(n.call, Obj?[n.cstr(arena, endpoint), n.cstr(arena, service),
|
|
153
|
+
n.cstr(arena, operation), n.cstr(arena, inputJson), Native.cint(timeoutMs), out, capacity, len])
|
|
154
|
+
detail := code == 0 ? "" : n.lastErrorText
|
|
155
|
+
if (code == Status.tooLarge.code) return Reply(code, "", n.readSize(len), detail)
|
|
156
|
+
body := n.readSize(len) > 0 ? n.text(out) : ""
|
|
157
|
+
return Reply(code, body, 0, detail)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
**
|
|
162
|
+
** `callRaw`, returning the operation's output JSON or throwing
|
|
163
|
+
** `PolycallErr`. After `Status.remote` the remote error object
|
|
164
|
+
** '{"code":..,"message":..}' is `PolycallErr.body`.
|
|
165
|
+
**
|
|
166
|
+
static Str call(Str endpoint, Str service, Str operation, Str? inputJson,
|
|
167
|
+
Int timeoutMs, Int capacity := callMaxOutput)
|
|
168
|
+
{
|
|
169
|
+
reply := callRaw(endpoint, service, operation, inputJson, timeoutMs, capacity)
|
|
170
|
+
if (reply.status != 0)
|
|
171
|
+
throw PolycallErr(reply.status, reply.detail, reply.needed == 0 ? null : reply.needed + 1, reply.body)
|
|
172
|
+
return reply.body
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
** Throw `PolycallErr` for a negative status (with this thread's detail).
|
|
176
|
+
internal static Void check(Int status)
|
|
177
|
+
{
|
|
178
|
+
if (status != 0) throw PolycallErr.fromStatus(status)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
**************************************************************************
|
|
183
|
+
** ConfigMode
|
|
184
|
+
**************************************************************************
|
|
185
|
+
|
|
186
|
+
** The 'run' argument of 'polycall_ffi_run_config'.
|
|
187
|
+
enum class ConfigMode
|
|
188
|
+
{
|
|
189
|
+
** Validate only: unknown keys are warnings.
|
|
190
|
+
validate,
|
|
191
|
+
** Validate for running with this build: unknown keys are errors and
|
|
192
|
+
** settings the build cannot honour (tls_enabled=true) are `Status.unsupported`.
|
|
193
|
+
run
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
**************************************************************************
|
|
197
|
+
** Reply
|
|
198
|
+
**************************************************************************
|
|
199
|
+
|
|
200
|
+
** The outcome of `Polycall.callRaw`.
|
|
201
|
+
const class Reply
|
|
202
|
+
{
|
|
203
|
+
new make(Int status, Str body, Int needed, Str detail)
|
|
204
|
+
{
|
|
205
|
+
this.status = status
|
|
206
|
+
this.body = body
|
|
207
|
+
this.needed = needed
|
|
208
|
+
this.detail = detail
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
** 0 (POLYCALL_OK) or a negative status.
|
|
212
|
+
const Int status
|
|
213
|
+
** The output JSON on OK; the remote error object on `Status.remote`; "" otherwise.
|
|
214
|
+
const Str body
|
|
215
|
+
** On `Status.tooLarge`: the output size needed (excluding the NUL).
|
|
216
|
+
const Int needed
|
|
217
|
+
** This thread's last-error detail after a failure ("" on OK).
|
|
218
|
+
const Str detail
|
|
219
|
+
|
|
220
|
+
** `status` as a `Status`.
|
|
221
|
+
Status toStatus() { Status.fromCode(status) }
|
|
222
|
+
|
|
223
|
+
override Str toStr() { status == 0 ? body : "${toStatus.cName}: $detail" }
|
|
224
|
+
}
|
package/fan/Status.fan
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Status codes and errors of the Polycall binding ABI v1.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
**
|
|
6
|
+
** The 'POLYCALL_*' status values. `code` is the C value; `cName` is the
|
|
7
|
+
** C macro name ("POLYCALL_E_CONFIG").
|
|
8
|
+
**
|
|
9
|
+
enum class Status
|
|
10
|
+
{
|
|
11
|
+
ok(0),
|
|
12
|
+
invalidArgument(-1),
|
|
13
|
+
noMemory(-2),
|
|
14
|
+
invalidHandle(-3),
|
|
15
|
+
timeout(-4),
|
|
16
|
+
transport(-5),
|
|
17
|
+
protocol(-6),
|
|
18
|
+
notFound(-7),
|
|
19
|
+
auth(-8),
|
|
20
|
+
remote(-9),
|
|
21
|
+
tooLarge(-10),
|
|
22
|
+
busy(-11),
|
|
23
|
+
cancelled(-12),
|
|
24
|
+
config(-13),
|
|
25
|
+
addressInUse(-14),
|
|
26
|
+
unsupported(-15),
|
|
27
|
+
permission(-16),
|
|
28
|
+
closed(-17),
|
|
29
|
+
** POLYCALL_E_INTERNAL ('internal' is a Fantom keyword).
|
|
30
|
+
internalErr(-18, "POLYCALL_E_INTERNAL"),
|
|
31
|
+
** A status this package does not know (a newer library). Its `code` is 1,
|
|
32
|
+
** which the library never returns; `PolycallErr.code` keeps the real value.
|
|
33
|
+
unknown(1, "POLYCALL_E_UNKNOWN")
|
|
34
|
+
|
|
35
|
+
private new make(Int code, Str? cName := null)
|
|
36
|
+
{
|
|
37
|
+
this.code = code
|
|
38
|
+
this.cNameOverride = cName
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private const Str? cNameOverride
|
|
42
|
+
|
|
43
|
+
** The C status value.
|
|
44
|
+
const Int code
|
|
45
|
+
|
|
46
|
+
** The C macro name: "POLYCALL_OK", "POLYCALL_E_TIMEOUT", ...
|
|
47
|
+
Str cName()
|
|
48
|
+
{
|
|
49
|
+
if (this === ok) return "POLYCALL_OK"
|
|
50
|
+
if (cNameOverride != null) return cNameOverride
|
|
51
|
+
return "POLYCALL_E_" + name.toDisplayName.upper.replace(" ", "_")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
** The member for a status code; `unknown` when there is none.
|
|
55
|
+
static Status fromCode(Int code) { vals.find { it.code == code && it !== unknown } ?: unknown }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
**************************************************************************
|
|
59
|
+
** PolycallErr
|
|
60
|
+
**************************************************************************
|
|
61
|
+
|
|
62
|
+
**
|
|
63
|
+
** A negative status returned by the core. `msg` is
|
|
64
|
+
** "<library text>: <detail>", e.g.
|
|
65
|
+
** "POLYCALL_E_CONFIG: invalid configuration: bad.polycallrc:1: malformed key=value entry".
|
|
66
|
+
**
|
|
67
|
+
const class PolycallErr : Err
|
|
68
|
+
{
|
|
69
|
+
** `strerror(code)` plus this thread's last-error detail, read right after the failing call.
|
|
70
|
+
static PolycallErr fromStatus(Int code) { make(code, Native.get.lastErrorText) }
|
|
71
|
+
|
|
72
|
+
new make(Int code, Str detail := "", Int? needed := null, Str body := "", Err? cause := null)
|
|
73
|
+
: super(message(code, detail), cause)
|
|
74
|
+
{
|
|
75
|
+
this.code = code
|
|
76
|
+
this.status = Status.fromCode(code)
|
|
77
|
+
this.detail = detail
|
|
78
|
+
this.needed = needed
|
|
79
|
+
this.body = body
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private static Str message(Int code, Str detail)
|
|
83
|
+
{
|
|
84
|
+
text := Str.defVal
|
|
85
|
+
try text = Native.get.strerrorText(code)
|
|
86
|
+
catch (Err e) text = Status.fromCode(code).cName
|
|
87
|
+
return detail.isEmpty ? text : "$text: $detail"
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
** The raw C status (negative).
|
|
91
|
+
const Int code
|
|
92
|
+
** `code` as a `Status` (`Status.unknown` for a code newer than this package).
|
|
93
|
+
const Status status
|
|
94
|
+
** The calling thread's 'polycall_last_error' detail ("" when the library gave none).
|
|
95
|
+
const Str detail
|
|
96
|
+
** For `Status.tooLarge` on an output buffer: the buffer size (NUL included) that would fit.
|
|
97
|
+
const Int? needed
|
|
98
|
+
** For `Polycall.call` with `Status.remote`: the remote error object JSON.
|
|
99
|
+
const Str body
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
**************************************************************************
|
|
103
|
+
** PolycallLoadErr
|
|
104
|
+
**************************************************************************
|
|
105
|
+
|
|
106
|
+
**
|
|
107
|
+
** The Polycall core could not be loaded. The message says what to set:
|
|
108
|
+
** POLYCALL_LIBRARY to the full path of polycall.dll / libpolycall.so.1
|
|
109
|
+
** (or its directory), or that directory on PATH / LD_LIBRARY_PATH.
|
|
110
|
+
**
|
|
111
|
+
const class PolycallLoadErr : Err
|
|
112
|
+
{
|
|
113
|
+
new make(Str msg, Err? cause := null) : super(msg, cause) {}
|
|
114
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fantom-polycall",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Fantom binding for the Polycall core (binding ABI v1, polycall >= 1.1.0): direct C FFI through Fantom's Java FFI and java.lang.foreign for config validation, RPC calls and peer nodes. Source distribution of a Fantom pod; not a JavaScript library.",
|
|
5
|
+
"files": [
|
|
6
|
+
"build.fan",
|
|
7
|
+
"fan/",
|
|
8
|
+
"test/",
|
|
9
|
+
"examples/",
|
|
10
|
+
"scripts/consumer-test.mjs",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "node scripts/consumer-test.mjs --source",
|
|
16
|
+
"test:consumer": "node scripts/consumer-test.mjs"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/obinexus/fantom-polycall.git"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/obinexus/fantom-polycall#readme",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/obinexus/fantom-polycall/issues"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"polycall",
|
|
28
|
+
"libpolycall",
|
|
29
|
+
"fantom",
|
|
30
|
+
"ffi",
|
|
31
|
+
"bindings",
|
|
32
|
+
"rpc",
|
|
33
|
+
"peer-to-peer"
|
|
34
|
+
],
|
|
35
|
+
"author": "OBINexusComputing",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"polycall": {
|
|
38
|
+
"abi": 1,
|
|
39
|
+
"core": ">=1.1.0",
|
|
40
|
+
"core_revision": "66c744d261abacc0bba475fdbd28abd7ae585385",
|
|
41
|
+
"interop": "direct C FFI (Fantom Java FFI to java.lang.foreign downcall handles resolved from the libpolycall shared library at run time)",
|
|
42
|
+
"fantom": "1.0.84",
|
|
43
|
+
"jdk": ">=22"
|
|
44
|
+
}
|
|
45
|
+
}
|