123456789101112131415161718192021222324252627282930313233343536 |
- package zsshrpc_server
- import "sync"
- type OpHandlerFunc func(request ZSshRpcOperationRequest) ZSshRpcOperationResponse
- type MappedOperationHandler struct {
- handlerMap map[string]OpHandlerFunc
- m sync.RWMutex
- }
- func NewMappedOperationHandler() *MappedOperationHandler {
- o := &MappedOperationHandler{}
- o.handlerMap = make(map[string]OpHandlerFunc)
- return o
- }
- func (this *MappedOperationHandler) HandleOperation(request ZSshRpcOperationRequest) ZSshRpcOperationResponse {
- this.m.RLock()
- vf, ok := this.handlerMap[request.URI]
- this.m.RUnlock()
- if ok {
- return vf(request)
- } else {
- return ZSshRpcOperationResponse{
- StatusCode: ResponseStatusCode_NOT_FOUND,
- ResponseJSON: "{}",
- }
- }
- }
- func (this *MappedOperationHandler) AddHandler(uri string, handler OpHandlerFunc) {
- this.m.Lock()
- this.handlerMap[uri] = handler
- this.m.Unlock()
- }
|