2018-07-10 10:39:09 +01:00
|
|
|
import json, tables, options, macros
|
2018-07-11 11:03:01 +01:00
|
|
|
import asyncdispatch2, router, chronicles
|
2018-06-14 16:52:41 +01:00
|
|
|
import jsonmarshal
|
|
|
|
|
2018-07-11 11:03:01 +01:00
|
|
|
export asyncdispatch2, json, jsonmarshal, router, chronicles
|
2018-06-14 16:52:41 +01:00
|
|
|
|
|
|
|
type
|
2018-07-11 10:19:13 +01:00
|
|
|
RpcServer* = ref object of RootRef
|
2018-07-06 17:47:43 +01:00
|
|
|
router*: RpcRouter
|
2018-06-14 16:52:41 +01:00
|
|
|
|
2018-07-11 10:19:13 +01:00
|
|
|
proc newRpcServer*(): RpcServer =
|
2018-06-21 18:15:21 +01:00
|
|
|
new result
|
2018-07-06 17:47:43 +01:00
|
|
|
result.router = newRpcRouter()
|
2018-06-15 11:12:34 +01:00
|
|
|
|
2018-07-06 17:47:43 +01:00
|
|
|
template rpc*(server: RpcServer, path: string, body: untyped): untyped =
|
|
|
|
server.router.rpc(path, body)
|
|
|
|
|
|
|
|
template hasMethod*(server: RpcServer, methodName: string): bool = server.router.hasMethod(methodName)
|
2018-06-14 16:52:41 +01:00
|
|
|
|
2018-07-10 10:39:09 +01:00
|
|
|
# Wrapper for message processing
|
2018-07-06 17:47:43 +01:00
|
|
|
|
2018-07-11 10:19:13 +01:00
|
|
|
proc route*(server: RpcServer, line: string): Future[string] {.async, gcsafe.} =
|
2018-07-10 10:39:09 +01:00
|
|
|
result = await server.router.route(line)
|
2018-06-14 16:52:41 +01:00
|
|
|
|
2018-07-06 17:47:43 +01:00
|
|
|
# Server registration
|
2018-06-14 16:52:41 +01:00
|
|
|
|
|
|
|
proc register*(server: RpcServer, name: string, rpc: RpcProc) =
|
|
|
|
## Add a name/code pair to the RPC server.
|
2018-07-11 10:19:13 +01:00
|
|
|
server.router.register(name, rpc)
|
2018-06-14 16:52:41 +01:00
|
|
|
|
|
|
|
proc unRegisterAll*(server: RpcServer) =
|
|
|
|
# Remove all remote procedure calls from this server.
|
2018-07-06 17:47:43 +01:00
|
|
|
server.router.clear
|
2018-06-19 18:16:39 +01:00
|
|
|
|
|
|
|
|