From fff7e19c94f43ce7efc6b1f25002d00f1ef6c6ea Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 22 Jan 2025 15:20:34 +0200 Subject: [PATCH] working on rpc server --- rpc/lux_log.go | 6 +++++ rpc/lux_rpc_controller.go | 14 ++++++++++++ rpc/{lux_rpc.go => lux_rpc_data.go} | 0 rpc/lux_rpc_server.go | 35 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 rpc/lux_log.go create mode 100644 rpc/lux_rpc_controller.go rename rpc/{lux_rpc.go => lux_rpc_data.go} (100%) create mode 100644 rpc/lux_rpc_server.go diff --git a/rpc/lux_log.go b/rpc/lux_log.go new file mode 100644 index 0000000..af76ee1 --- /dev/null +++ b/rpc/lux_log.go @@ -0,0 +1,6 @@ +package rpc + +import "github.com/op/go-logging" + +//lint:ignore U1000 log will be used later +var log = logging.MustGetLogger("rpc") diff --git a/rpc/lux_rpc_controller.go b/rpc/lux_rpc_controller.go new file mode 100644 index 0000000..a515b0d --- /dev/null +++ b/rpc/lux_rpc_controller.go @@ -0,0 +1,14 @@ +package rpc + +type LuxRpcType int + +const ( + LuxRpcTypeRoot = 0 + LuxRpcTypeQuery = 1 +) + +type LuxRpcController interface { + GetRpcName() string + Register(rpc *LuxRpcServer) + Handle(request LuxRpcRequest, rpcType LuxRpcType) (LuxRpcResponse, LuxRpcError, bool) +} diff --git a/rpc/lux_rpc.go b/rpc/lux_rpc_data.go similarity index 100% rename from rpc/lux_rpc.go rename to rpc/lux_rpc_data.go diff --git a/rpc/lux_rpc_server.go b/rpc/lux_rpc_server.go new file mode 100644 index 0000000..53daeea --- /dev/null +++ b/rpc/lux_rpc_server.go @@ -0,0 +1,35 @@ +package rpc + +import "sync" + +type LuxRpcServer struct { + controllers map[string]LuxRpcController + lock sync.Mutex +} + +func NewLuxRpcServer() LuxRpcServer { + return LuxRpcServer{ + controllers: make(map[string]LuxRpcController, 0), + } +} + +func (rpc *LuxRpcServer) RegisterController(ctrl LuxRpcController) { + rpc.controllers[ctrl.GetRpcName()] = ctrl +} + +func (rpc *LuxRpcServer) HandleRequest(request LuxRpcRequest, rpcType LuxRpcType) (LuxRpcResponse, LuxRpcError, bool) { + // lock rpc + rpc.lock.Lock() + defer rpc.lock.Unlock() + // find controller + ctrl, ok := rpc.controllers[request.Controller] + if !ok { + return LuxRpcResponse{}, LuxRpcError{ + RequestID: request.RequestID, + ErrorCode: 1, + Message: "unknown controller", + }, false + } + + return ctrl.Handle(request, rpcType) +}