-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.go
161 lines (129 loc) · 3.08 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
package coap
import (
"errors"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/dustin/go-coap"
"github.com/project-flogo/core/support/log"
)
func NewServer(n, addr string, handler coap.Handler) *Server {
srv := &Server{n: n, Addr: addr, Handler: handler}
return srv
}
//Server the server structure
type Server struct {
Handler coap.Handler
Addr string
n string
listener *net.UDPConn
lastError error
serverGroup *sync.WaitGroup
clientsGroup chan bool
}
// Start will start server
// command isn't blocking, will exit after run
func (s *Server) Start() error {
if s.Handler == nil {
return errors.New("No server handler set")
}
if s.listener != nil {
return errors.New("Server already started")
}
addr := s.Addr
if addr == "" {
addr = ":5683"
}
uaddr, err := net.ResolveUDPAddr(s.n, addr)
if err != nil {
return err
}
log.RootLogger().Infof("Starting listener at port %v", addr)
s.listener, err = net.ListenUDP(s.n, uaddr)
if err != nil {
return err
}
s.serverGroup = &sync.WaitGroup{}
s.clientsGroup = make(chan bool, 50000)
s.Handler = &serverHandler{s.Handler, s.clientsGroup}
s.serverGroup.Add(1)
go func() {
err := coap.Serve(s.listener, s.Handler)
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
s.lastError = err
}
}()
return nil
}
// Stop sends stop command to the server
func (s *Server) Stop() error {
if s.listener == nil {
return errors.New("Server not started")
}
if err := s.listener.Close(); err != nil {
return err
}
return s.lastError
}
// IsStarted checks if the server is started
// will return true even if the server is stopped but there are still some requests to finish
func (s *Server) IsStarted() bool {
if s.listener != nil {
return true
}
if len(s.clientsGroup) > 0 {
return true
}
return false
}
// WaitStop waits until server is stopped and all requests are finish
// timeout - is the time to wait for the requests to finish after the server is stopped
// will return error if there are still some requests not finished
func (s *Server) WaitStop(timeout time.Duration) error {
if s.listener == nil {
return errors.New("Server not started")
}
s.serverGroup.Wait()
checkClients := time.Tick(100 * time.Millisecond)
timeoutTime := time.NewTimer(timeout)
for {
select {
case <-checkClients:
if len(s.clientsGroup) == 0 {
return s.lastError
}
case <-timeoutTime.C:
return fmt.Errorf("WaitStop error, timeout after %s waiting for %d client(s) to finish", timeout, len(s.clientsGroup))
}
}
}
type serverHandler struct {
handler coap.Handler
clientsGroup chan bool
}
func (sh *serverHandler) ServeCOAP(l *net.UDPConn, a *net.UDPAddr, m *coap.Message) *coap.Message {
sh.clientsGroup <- true
defer func() {
<-sh.clientsGroup
}()
return sh.handler.ServeCOAP(l, a, m)
}
func toCoapCode(method string) coap.COAPCode {
var code coap.COAPCode
switch method {
case "GET":
code = coap.GET
case "POST":
code = coap.POST
case "PUT":
code = coap.PUT
case "DELETE":
code = coap.DELETE
}
return code
}