MappedOperationHandler.go 866 B

123456789101112131415161718192021222324252627282930313233343536
  1. package zsshrpc_server
  2. import "sync"
  3. type OpHandlerFunc func(request ZSshRpcOperationRequest) ZSshRpcOperationResponse
  4. type MappedOperationHandler struct {
  5. handlerMap map[string]OpHandlerFunc
  6. m sync.RWMutex
  7. }
  8. func NewMappedOperationHandler() *MappedOperationHandler {
  9. o := &MappedOperationHandler{}
  10. o.handlerMap = make(map[string]OpHandlerFunc)
  11. return o
  12. }
  13. func (this *MappedOperationHandler) HandleOperation(request ZSshRpcOperationRequest) ZSshRpcOperationResponse {
  14. this.m.RLock()
  15. vf, ok := this.handlerMap[request.URI]
  16. this.m.RUnlock()
  17. if ok {
  18. return vf(request)
  19. } else {
  20. return ZSshRpcOperationResponse{
  21. StatusCode: ResponseStatusCode_NOT_FOUND,
  22. ResponseJSON: "{}",
  23. }
  24. }
  25. }
  26. func (this *MappedOperationHandler) AddHandler(uri string, handler OpHandlerFunc) {
  27. this.m.Lock()
  28. this.handlerMap[uri] = handler
  29. this.m.Unlock()
  30. }