-
Notifications
You must be signed in to change notification settings - Fork 8
/
RequestSelector.go
40 lines (36 loc) · 1.37 KB
/
RequestSelector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import "errors"
import "net/http"
import "log"
type RequestSelector struct {
selectorRequestMap map[int]RequestDispatcher
}
func (requestSelector *RequestSelector) Dispatch(request Request, responseWriter http.ResponseWriter, httpRequest *http.Request) error {
//I don't protect multiple read in different thread
if selector, contains := requestSelector.selectorRequestMap[request.RequestType()]; !contains {
stErr := "error: Usupported message type"
log.Println(stErr)
return errors.New(stErr)
} else {
return selector.Dispatch(request, responseWriter, httpRequest)
}
}
func (requestSelector *RequestSelector) Init() error {
//create map
requestSelector.selectorRequestMap = make(map[int]RequestDispatcher)
if requestSelector.selectorRequestMap == nil {
stErr := "error: Can't create map"
log.Println(stErr)
return errors.New(stErr)
}
requestSelector.selectorRequestMap[ServiceStatus] = new(ServiceStateDispatcher)
systemMonitorDispatcher := new(SystemMonitorDispatcher)
systemMonitorDispatcher.StartMeasure()
requestSelector.selectorRequestMap[SystemMonitor] = systemMonitorDispatcher
//same dispatcher for two message
topProcessDispatcher := new(TopDispatcher)
topProcessDispatcher.StartMeasure()
requestSelector.selectorRequestMap[TopProcess] = topProcessDispatcher
requestSelector.selectorRequestMap[KillProcess] = topProcessDispatcher
return nil
}