JSON-RPC 2.0 client and server. Client sends method calls to a remote endpoint and returns typed results; Server dispatches incoming requests to registered handlers. Compile with -lib json_rpc.
Server
JSON RPC server
Operations
GetPort #
Gets the port the server is bound to: the port given to the constructor or, for a port of 0, the free one the system chose. A program that needs no fixed number (a test, say) binds port 0 and reads the port back here, so it cannot collide with a port another program, or an outgoing connection of its own, already holds. Whether the bind and listen worked is IsListening().
method : public : GetPort() ~ IntReturn
| Type | Description |
|---|---|
| Int | bound port |
Example
server := Data.JSON.RPC.Server->New(funcs, 0);
port := server->GetPort(); # the port the system choseIsListening #
Tells whether Process() is listening, so connections are accepted. False until Process() has begun listening, and for good if the port could not be bound or listened on. A caller that runs Process() in a thread waits on this, up to a deadline, rather than for a fixed time.
method : public : IsListening() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if listening |
Example
thread->Execute(Nil); # runs server->Process()
waited := 0;
while(<>server->IsListening() & waited < 10000) {
System.Concurrency.Thread->Sleep(10);
waited += 10;
};New # constructor
Constructor
New(funcs:Hash<String,Func2Ref<JsonElement,JsonElement>>)Parameters
| Name | Type | Description |
|---|---|---|
| funcs | Hash<String,Func2Ref<JsonElement,JsonElement>> | collection of named function calls |
Example
funcs := Hash->New()<String, Func2Ref<JsonElement, JsonElement>>;
# register an "add" method
funcs->Insert("add", Func2Ref->New(\
method(params : JsonElement) ~ JsonElement {
a := params->Get("a")->GetInt();
b := params->Get("b")->GetInt();
return JsonElement->New(a + b);
}));
server := Data.JSON.RPC.Server->New(funcs, 9090);
server->Process(); # blocks, accepts connectionsProcess #
Start the server to accept requests
method : public : Process() ~ NilExample
funcs := Hash->New()<String, Func2Ref<JsonElement, JsonElement>>;
server := Data.JSON.RPC.Server->New(funcs, 9090);
server->Process(); # listen on port 9090 for incoming RPC calls