-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
191 lines (156 loc) · 4.19 KB
/
server.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package ocpp
import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
var server *Server
type ServerTimeoutConfig struct {
// ocpp response timeout in seconds
OcppWait time.Duration
// time allowed to write a message to the peer
WriteWait time.Duration
// time allowed to read the next pong message from the peer
PingWait time.Duration
}
// Server type representes csms server
type Server struct {
// keeps track of all connected ChargePoints
chargepoints map[string]*ChargePoint
// register implemented action handler functions
actionHandlers map[string]func(*ChargePoint, Payload) Payload
// register after-action habdler functions
afterHandlers map[string]func(*ChargePoint, Payload)
// timeout configuration
ocppWait time.Duration
writeWait time.Duration
pingWait time.Duration
mu sync.Mutex
upgrader websocket.Upgrader
preUpgradeHandler func(w http.ResponseWriter, r *http.Request) bool
returnError func(err error)
callQuequeSize int
}
// create new CSMS instance acting as main handler for ChargePoints
func NewServer() *Server {
server = &Server{
chargepoints: make(map[string]*ChargePoint),
actionHandlers: make(map[string]func(*ChargePoint, Payload) Payload),
afterHandlers: make(map[string]func(*ChargePoint, Payload)),
ocppWait: ocppWait,
writeWait: writeWait,
pingWait: pingWait,
upgrader: websocket.Upgrader{
Subprotocols: []string{},
},
}
return server
}
func (s *Server) SetTimeoutConfig(config ServerTimeoutConfig) {
s.ocppWait = config.OcppWait
s.writeWait = config.WriteWait
s.pingWait = config.PingWait
}
// register action handler function
func (s *Server) On(action string, f func(*ChargePoint, Payload) Payload) *Server {
s.actionHandlers[action] = f
return s
}
// register after-action handler function
func (s *Server) After(action string, f func(*ChargePoint, Payload)) *Server {
s.afterHandlers[action] = f
return s
}
func (s *Server) IsConnected(id string) bool {
if cp, ok := s.chargepoints[id]; ok {
return cp.connected
}
return false
}
func (s *Server) getHandler(action string) func(*ChargePoint, Payload) Payload {
return s.actionHandlers[action]
}
func (s *Server) getAfterHandler(action string) func(*ChargePoint, Payload) {
return s.afterHandlers[action]
}
func (s *Server) Delete(id string) {
s.mu.Lock()
if cp, ok := s.chargepoints[id]; ok {
cp.connected = false
}
delete(s.chargepoints, id)
s.mu.Unlock()
}
func (s *Server) Store(cp *ChargePoint) {
s.mu.Lock()
server.chargepoints[cp.Id] = cp
s.mu.Unlock()
}
func (s *Server) Load(id string) (*ChargePoint, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if cp, ok := s.chargepoints[id]; ok {
fmt.Printf("ChargePoint with id: %s exist\n", cp.Id)
return cp, true
}
return nil, false
}
func (s *Server) AddSubProtocol(protocol string) {
for _, p := range server.upgrader.Subprotocols {
if p == protocol {
return
}
}
s.upgrader.Subprotocols = append(s.upgrader.Subprotocols, protocol)
}
func (s *Server) SetCheckOriginHandler(f func(r *http.Request) bool) {
s.upgrader.CheckOrigin = f
}
func (s *Server) SetPreUpgradeHandler(f func(w http.ResponseWriter, r *http.Request) bool) {
s.preUpgradeHandler = f
}
// TODO: add more functionality
func (s *Server) Start(addr string, path string, handler func(http.ResponseWriter, *http.Request)) {
if handler != nil {
http.HandleFunc(path, handler)
} else {
http.HandleFunc(path, defaultWebsocketHandler)
}
http.ListenAndServe(addr, nil)
}
func defaultWebsocketHandler(w http.ResponseWriter, r *http.Request) {
preCheck := server.preUpgradeHandler
if preCheck != nil {
if preCheck(w, r) {
upgrade(w, r)
} else {
server.returnError(errors.New("cannot start server"))
}
} else {
upgrade(w, r)
}
}
func upgrade(w http.ResponseWriter, r *http.Request) {
c, err := server.upgrader.Upgrade(w, r, nil)
if err != nil {
server.returnError(err)
return
}
p := strings.Split(r.URL.Path, "/")
id := p[len(p)-1]
cp := NewChargePoint(c, id, c.Subprotocol(), true)
server.Store(cp)
}
func (s *Server) SetCallQueueSize(size int) {
s.callQuequeSize = size
}
func (s *Server) getCallQueueSize() int {
s.mu.Lock()
size := s.callQuequeSize
s.mu.Unlock()
return size
}