working on rpc server
This commit is contained in:
parent
d736c76fe4
commit
fff7e19c94
4 changed files with 55 additions and 0 deletions
6
rpc/lux_log.go
Normal file
6
rpc/lux_log.go
Normal 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
14
rpc/lux_rpc_controller.go
Normal 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
35
rpc/lux_rpc_server.go
Normal 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)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue