aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/rpc.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet/service/rpc.go')
-rw-r--r--src/gnunet/service/rpc.go29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/gnunet/service/rpc.go b/src/gnunet/service/rpc.go
index de3a2c1..d5740fb 100644
--- a/src/gnunet/service/rpc.go
+++ b/src/gnunet/service/rpc.go
@@ -21,24 +21,35 @@ package service
21import ( 21import (
22 "context" 22 "context"
23 "net/http" 23 "net/http"
24 "net/rpc"
25 "time" 24 "time"
26 25
27 "github.com/bfix/gospel/logger" 26 "github.com/bfix/gospel/logger"
28 "github.com/gorilla/mux" 27 "github.com/gorilla/mux"
28 "github.com/gorilla/rpc/v2"
29 "github.com/gorilla/rpc/v2/json2"
29) 30)
30 31
31//---------------------------------------------------------------------- 32//----------------------------------------------------------------------
33//----------------------------------------------------------------------
34
35// JRPCServer for JSON-RPC handling (wrapper to keep type in our package)
36type JRPCServer struct {
37 *rpc.Server
38}
39
40//----------------------------------------------------------------------
32// JSON-RPC interface for services to be used as the primary client API 41// JSON-RPC interface for services to be used as the primary client API
33// for perform, manage and monitor GNUnet activities. 42// for perform, manage and monitor GNUnet activities.
34//---------------------------------------------------------------------- 43//----------------------------------------------------------------------
35 44
36// StartRPC the JSON-RPC server. It can be terminated by context 45// RunRPCServer runs the JSON-RPC server. It can be terminated by context only.
37func StartRPC(ctx context.Context, endpoint string) (srvRPC *rpc.Server, err error) { 46func RunRPCServer(ctx context.Context, endpoint string) (srvRPC *JRPCServer, err error) {
47 // instantiate RPC service
48 srvRPC = &JRPCServer{rpc.NewServer()}
49 srvRPC.RegisterCodec(json2.NewCodec(), "application/json")
38 50
39 // setup RPC request handler 51 // setup RPC request handler
40 router := mux.NewRouter() 52 router := mux.NewRouter()
41 srvRPC = rpc.NewServer()
42 router.HandleFunc("/", srvRPC.ServeHTTP) 53 router.HandleFunc("/", srvRPC.ServeHTTP)
43 54
44 // instantiate a server and run it 55 // instantiate a server and run it
@@ -51,16 +62,14 @@ func StartRPC(ctx context.Context, endpoint string) (srvRPC *rpc.Server, err err
51 // start listening 62 // start listening
52 go func() { 63 go func() {
53 if err := srv.ListenAndServe(); err != http.ErrServerClosed { 64 if err := srv.ListenAndServe(); err != http.ErrServerClosed {
54 logger.Printf(logger.WARN, "[RPC] Server listen failed: %s", err.Error()) 65 logger.Printf(logger.WARN, "[rpc] server listen failed: %s", err.Error())
55 } 66 }
56 }() 67 }()
57 // wait for shutdown 68 // wait for shutdown
58 go func() { 69 go func() {
59 select { 70 <-ctx.Done()
60 case <-ctx.Done(): 71 if err := srv.Shutdown(context.Background()); err != nil {
61 if err := srv.Shutdown(context.Background()); err != nil { 72 logger.Printf(logger.WARN, "[rpc] server shutdownn failed: %s", err.Error())
62 logger.Printf(logger.WARN, "[RPC] Server shutdownn failed: %s", err.Error())
63 }
64 } 73 }
65 }() 74 }()
66 return 75 return