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) +}