2021-02-15 13:45:51 +01:00
|
|
|
import
|
|
|
|
std/[json, macros],
|
|
|
|
chronos, chronicles,
|
|
|
|
./router,
|
|
|
|
./jsonmarshal
|
2018-06-14 16:52:41 +01:00
|
|
|
|
2019-02-06 18:27:58 +01:00
|
|
|
export chronos, 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
|
|
|
|
2021-02-15 13:45:51 +01:00
|
|
|
proc new(T: type RpcServer): T =
|
|
|
|
T(router: RpcRouter.init())
|
|
|
|
|
|
|
|
proc newRpcServer*(): RpcServer {.deprecated.} = RpcServer.new()
|
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)
|
|
|
|
|
2020-03-17 20:36:23 +02:00
|
|
|
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
|
|
|
|
2021-02-15 13:45:51 +01:00
|
|
|
proc route*(server: RpcServer, line: string): Future[string] {.gcsafe.} =
|
|
|
|
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
|