working on rpc server

This commit is contained in:
mykola2312 2025-01-22 15:20:34 +02:00
parent d736c76fe4
commit fff7e19c94
4 changed files with 55 additions and 0 deletions

6
rpc/lux_log.go Normal file
View file

@ -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")

14
rpc/lux_rpc_controller.go Normal file
View file

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

35
rpc/lux_rpc_server.go Normal file
View file

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