From b218529d2df1fecf6d8dbe64c5f39085ced60577 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Wed, 11 Jul 2018 10:19:13 +0100 Subject: [PATCH 1/4] Remove generics from server and update sockettransport --- json_rpc/server.nim | 10 ++++------ json_rpc/sockettransport.nim | 37 ++++++++++++++++++++---------------- tests/testerrors.nim | 2 +- tests/testethcalls.nim | 2 +- tests/testrpcmacro.nim | 2 +- tests/testserverclient.nim | 2 +- 6 files changed, 29 insertions(+), 26 deletions(-) diff --git a/json_rpc/server.nim b/json_rpc/server.nim index 6ef2720..be54832 100644 --- a/json_rpc/server.nim +++ b/json_rpc/server.nim @@ -5,14 +5,12 @@ import jsonmarshal export asyncdispatch2, json, jsonmarshal, router type - RpcServer*[S] = ref object - servers*: seq[S] + RpcServer* = ref object of RootRef router*: RpcRouter -proc newRpcServer*[S](): RpcServer[S] = +proc newRpcServer*(): RpcServer = new result result.router = newRpcRouter() - result.servers = @[] template rpc*(server: RpcServer, path: string, body: untyped): untyped = server.router.rpc(path, body) @@ -21,14 +19,14 @@ template hasMethod*(server: RpcServer, methodName: string): bool = server.router # Wrapper for message processing -proc route*[T](server: RpcServer[T], line: string): Future[string] {.async, gcsafe.} = +proc route*(server: RpcServer, line: string): Future[string] {.async, gcsafe.} = result = await server.router.route(line) # Server registration proc register*(server: RpcServer, name: string, rpc: RpcProc) = ## Add a name/code pair to the RPC server. - server.router.addRoute(name, rpc) + server.router.register(name, rpc) proc unRegisterAll*(server: RpcServer) = # Remove all remote procedure calls from this server. diff --git a/json_rpc/sockettransport.nim b/json_rpc/sockettransport.nim index 26608fa..e269c2d 100644 --- a/json_rpc/sockettransport.nim +++ b/json_rpc/sockettransport.nim @@ -1,5 +1,9 @@ import server, json, chronicles +type + RpcSocketServer* = ref object of RpcServer + servers: seq[StreamServer] + proc sendError*[T](transport: T, code: int, msg: string, id: JsonNode, data: JsonNode = newJNull()) {.async.} = ## Send error message to client @@ -9,7 +13,7 @@ proc sendError*[T](transport: T, code: int, msg: string, id: JsonNode, proc processClient(server: StreamServer, transport: StreamTransport) {.async, gcsafe.} = ## Process transport data to the RPC server - var rpc = getUserData[RpcServer[StreamTransport]](server) + var rpc = getUserData[RpcSocketServer](server) while true: var maxRequestLength = defaultMaxRequestLength @@ -38,7 +42,7 @@ proc processClient(server: StreamServer, transport: StreamTransport) {.async, gc # Utility functions for setting up servers using stream transport addresses -proc addStreamServer*(server: RpcServer[StreamServer], address: TransportAddress) = +proc addStreamServer*(server: RpcSocketServer, address: TransportAddress) = try: info "Creating server on ", address = $address var transportServer = createStreamServer(address, processClient, {ReuseAddr}, udata = server) @@ -50,11 +54,11 @@ proc addStreamServer*(server: RpcServer[StreamServer], address: TransportAddress # Server was not bound, critical error. raise newException(RpcBindError, "Unable to create server!") -proc addStreamServers*(server: RpcServer[StreamServer], addresses: openarray[TransportAddress]) = +proc addStreamServers*(server: RpcSocketServer, addresses: openarray[TransportAddress]) = for item in addresses: server.addStreamServer(item) -proc addStreamServer*(server: RpcServer[StreamServer], address: string) = +proc addStreamServer*(server: RpcSocketServer, address: string) = ## Create new server and assign it to addresses ``addresses``. var tas4: seq[TransportAddress] @@ -84,11 +88,11 @@ proc addStreamServer*(server: RpcServer[StreamServer], address: string) = # Addresses could not be resolved, critical error. raise newException(RpcAddressUnresolvableError, "Unable to get address!") -proc addStreamServers*(server: RpcServer[StreamServer], addresses: openarray[string]) = +proc addStreamServers*(server: RpcSocketServer, addresses: openarray[string]) = for address in addresses: server.addStreamServer(address) -proc addStreamServer*(server: RpcServer[StreamServer], address: string, port: Port) = +proc addStreamServer*(server: RpcSocketServer, address: string, port: Port) = var tas4: seq[TransportAddress] tas6: seq[TransportAddress] @@ -123,34 +127,35 @@ proc addStreamServer*(server: RpcServer[StreamServer], address: string, port: Po raise newException(RpcBindError, "Could not setup server on " & address & ":" & $int(port)) -type RpcStreamServer* = RpcServer[StreamServer] +proc newRpcSocketServer*: RpcSocketServer = + RpcSocketServer(router: newRpcRouter(), servers: @[]) -proc newRpcStreamServer*(addresses: openarray[TransportAddress]): RpcStreamServer = +proc newRpcSocketServer*(addresses: openarray[TransportAddress]): RpcSocketServer = ## Create new server and assign it to addresses ``addresses``. - result = newRpcServer[StreamServer]() + result = newRpcSocketServer() result.addStreamServers(addresses) -proc newRpcStreamServer*(addresses: openarray[string]): RpcStreamServer = +proc newRpcSocketServer*(addresses: openarray[string]): RpcSocketServer = ## Create new server and assign it to addresses ``addresses``. - result = newRpcServer[StreamServer]() + result = newRpcSocketServer() result.addStreamServers(addresses) -proc newRpcStreamServer*(address = "localhost", port: Port = Port(8545)): RpcStreamServer = +proc newRpcSocketServer*(address: string, port: Port = Port(8545)): RpcSocketServer = # Create server on specified port - result = newRpcServer[StreamServer]() + result = newRpcSocketServer() result.addStreamServer(address, port) -proc start*(server: RpcStreamServer) = +proc start*(server: RpcSocketServer) = ## Start the RPC server. for item in server.servers: item.start() -proc stop*(server: RpcStreamServer) = +proc stop*(server: RpcSocketServer) = ## Stop the RPC server. for item in server.servers: item.stop() -proc close*(server: RpcStreamServer) = +proc close*(server: RpcSocketServer) = ## Cleanup resources of RPC server. for item in server.servers: item.close() \ No newline at end of file diff --git a/tests/testerrors.nim b/tests/testerrors.nim index 1304cf6..afef86c 100644 --- a/tests/testerrors.nim +++ b/tests/testerrors.nim @@ -6,7 +6,7 @@ import unittest, debugclient, ../rpcsockets import strformat, chronicles -var server = newRpcStreamServer("localhost", 8547.Port) +var server = newRpcSocketServer("localhost", 8547.Port) var client = newRpcStreamClient() server.start() diff --git a/tests/testethcalls.nim b/tests/testethcalls.nim index ec23e46..14f59f1 100644 --- a/tests/testethcalls.nim +++ b/tests/testethcalls.nim @@ -7,7 +7,7 @@ from strutils import rsplit template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0] var - server = newRpcStreamServer("localhost", Port(8546)) + server = newRpcSocketServer("localhost", Port(8546)) client = newRpcStreamClient() ## Generate Ethereum server RPCs diff --git a/tests/testrpcmacro.nim b/tests/testrpcmacro.nim index d3cb07c..4b481e9 100644 --- a/tests/testrpcmacro.nim +++ b/tests/testrpcmacro.nim @@ -27,7 +27,7 @@ let }, "c": %1.23} -var s = newRpcStreamServer(["localhost:8545"]) +var s = newRpcSocketServer(["localhost:8545"]) # RPC definitions diff --git a/tests/testserverclient.nim b/tests/testserverclient.nim index bb2a0f7..926b855 100644 --- a/tests/testserverclient.nim +++ b/tests/testserverclient.nim @@ -1,7 +1,7 @@ import unittest, json, chronicles import ../rpcclient, ../rpcsockets -var srv = newRpcStreamServer(["localhost:8545"]) +var srv = newRpcSocketServer(["localhost:8545"]) var client = newRpcStreamClient() # Create RPC on server From b602dd4185f25ce84318d5efabdf4fd0e94fa5e3 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Wed, 11 Jul 2018 10:29:18 +0100 Subject: [PATCH 2/4] Rename sockettransport to sockets under a transport folder --- json_rpc/{sockettransport.nim => transports/sockets.nim} | 2 +- rpcsockets.nim | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename json_rpc/{sockettransport.nim => transports/sockets.nim} (98%) diff --git a/json_rpc/sockettransport.nim b/json_rpc/transports/sockets.nim similarity index 98% rename from json_rpc/sockettransport.nim rename to json_rpc/transports/sockets.nim index e269c2d..a5e5424 100644 --- a/json_rpc/sockettransport.nim +++ b/json_rpc/transports/sockets.nim @@ -1,4 +1,4 @@ -import server, json, chronicles +import ../ server, json, chronicles type RpcSocketServer* = ref object of RpcServer diff --git a/rpcsockets.nim b/rpcsockets.nim index 06ee707..96121c4 100644 --- a/rpcsockets.nim +++ b/rpcsockets.nim @@ -1,2 +1,2 @@ -import json_rpc / [server, sockettransport] -export server, sockettransport +import json_rpc / server, json_rpc / transports / sockets +export server, sockets From 13ae592738f3f155378e7d867ff14c7b175f7781 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Wed, 11 Jul 2018 10:40:20 +0100 Subject: [PATCH 3/4] Rename sockets.nim to socket.nim and rpcsockets.nim to rpcsocket.nim --- json_rpc/transports/{sockets.nim => socket.nim} | 0 rpcsocket.nim | 2 ++ rpcsockets.nim | 2 -- tests/testerrors.nim | 2 +- tests/testethcalls.nim | 2 +- tests/testrpcmacro.nim | 2 +- tests/testserverclient.nim | 2 +- 7 files changed, 6 insertions(+), 6 deletions(-) rename json_rpc/transports/{sockets.nim => socket.nim} (100%) create mode 100644 rpcsocket.nim delete mode 100644 rpcsockets.nim diff --git a/json_rpc/transports/sockets.nim b/json_rpc/transports/socket.nim similarity index 100% rename from json_rpc/transports/sockets.nim rename to json_rpc/transports/socket.nim diff --git a/rpcsocket.nim b/rpcsocket.nim new file mode 100644 index 0000000..7568e4d --- /dev/null +++ b/rpcsocket.nim @@ -0,0 +1,2 @@ +import json_rpc / server, json_rpc / transports / socket +export server, socket diff --git a/rpcsockets.nim b/rpcsockets.nim deleted file mode 100644 index 96121c4..0000000 --- a/rpcsockets.nim +++ /dev/null @@ -1,2 +0,0 @@ -import json_rpc / server, json_rpc / transports / sockets -export server, sockets diff --git a/tests/testerrors.nim b/tests/testerrors.nim index afef86c..63a3709 100644 --- a/tests/testerrors.nim +++ b/tests/testerrors.nim @@ -3,7 +3,7 @@ allow unchecked and unformatted calls. ]# -import unittest, debugclient, ../rpcsockets +import unittest, debugclient, ../rpcsocket import strformat, chronicles var server = newRpcSocketServer("localhost", 8547.Port) diff --git a/tests/testethcalls.nim b/tests/testethcalls.nim index 14f59f1..572c808 100644 --- a/tests/testethcalls.nim +++ b/tests/testethcalls.nim @@ -1,5 +1,5 @@ import unittest, json, tables -import ../rpcclient, ../rpcsockets +import ../rpcclient, ../rpcsocket import stint, ethtypes, ethprocs, stintjson, nimcrypto, ethhexstrings, chronicles from os import getCurrentDir, DirSep diff --git a/tests/testrpcmacro.nim b/tests/testrpcmacro.nim index 4b481e9..43cab64 100644 --- a/tests/testrpcmacro.nim +++ b/tests/testrpcmacro.nim @@ -1,5 +1,5 @@ import unittest, json, tables, chronicles -import ../rpcsockets +import ../rpcsocket type # some nested types to check object parsing diff --git a/tests/testserverclient.nim b/tests/testserverclient.nim index 926b855..a50f03f 100644 --- a/tests/testserverclient.nim +++ b/tests/testserverclient.nim @@ -1,5 +1,5 @@ import unittest, json, chronicles -import ../rpcclient, ../rpcsockets +import ../rpcclient, ../rpcsocket var srv = newRpcSocketServer(["localhost:8545"]) var client = newRpcStreamClient() From b2780cd8c27cdd3027c69e0c978ecf81777d05c4 Mon Sep 17 00:00:00 2001 From: coffeepots Date: Wed, 11 Jul 2018 11:03:01 +0100 Subject: [PATCH 4/4] Export chronicles in server --- json_rpc/server.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json_rpc/server.nim b/json_rpc/server.nim index be54832..b848405 100644 --- a/json_rpc/server.nim +++ b/json_rpc/server.nim @@ -1,8 +1,8 @@ import json, tables, options, macros -import asyncdispatch2, router +import asyncdispatch2, router, chronicles import jsonmarshal -export asyncdispatch2, json, jsonmarshal, router +export asyncdispatch2, json, jsonmarshal, router, chronicles type RpcServer* = ref object of RootRef