-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
178 lines (155 loc) · 3.26 KB
/
serve.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
package main
import (
"bytes"
"context"
"flag"
"log"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/eminom/goscatter/data"
"github.com/eminom/go-coap"
)
func main() {
var fAddr = flag.String("addr", ":16666", "address for listening")
var fMainDir = flag.String("dir", "", "working directory")
flag.Parse()
log.SetFlags(log.Lshortfile | log.Ltime)
if *fMainDir != "" {
err := os.Chdir(*fMainDir)
if err != nil {
log.Fatalf("error: %v", err)
}
}
addr, err := net.ResolveUDPAddr("udp", *fAddr)
if err != nil {
log.Fatalf("error resolving addr: %v", err)
}
ctx, doCancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
startScatter(addr, &wg, ctx.Done())
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Kill, os.Interrupt, syscall.SIGTERM)
select {
case <-sigCh:
case <-ctx.Done():
}
log.Printf("cancelling...")
doCancel()
wg.Wait()
log.Printf("bye")
}
func startScatter(addr *net.UDPAddr, wg *sync.WaitGroup, doneCh <-chan struct{}) {
wg.Add(1)
go runScatter(addr, wg, doneCh)
}
func runScatter(addr *net.UDPAddr, wg *sync.WaitGroup, doneCh <-chan struct{}) {
defer func() {
log.Printf("leaving scat-ter process")
wg.Done()
}()
uSock, err := net.ListenUDP("udp", addr)
if err != nil {
panic(err)
}
var bigBuff [1024 * 1024]byte
workCh := make(chan *data.WorkItem, 1024)
respondForbidden := func(req coap.Message) {
workCh <- data.GenForbidden(req, addr)
}
sMan := data.NewServeMan(workCh)
defer sMan.Close()
// write-procedure. one by one.
wg.Add(1)
go func() {
defer func() {
log.Printf("leaving write-proc")
wg.Done()
}()
W100:
for {
select {
case <-doneCh:
break W100
case wi := <-workCh:
uSock.WriteTo(wi.Buff, wi.Dest)
}
}
W101:
for {
select {
case <-workCh:
// purge this channel.
case <-time.After(2 * time.Second):
break W101
}
}
}()
const ParserCount = 4
bytesDisCh := make(chan *data.WorkItem, 1024)
for i := 0; i < ParserCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-doneCh:
return
case wi := <-bytesDisCh:
msg, err := coap.ParseMessage(wi.Buff)
// TODO: process more messaage type.
if err == nil {
switch msg.Type {
case coap.Confirmable:
switch msg.Code {
case coap.POST:
sMan.ProcessPost(msg, wi.Dest)
case coap.GET:
sMan.ForwardGet(msg, wi.Dest)
case coap.PUT:
sMan.ForwardPut(msg, wi.Dest)
default:
log.Printf("request type not supported: %v", msg.Type)
respondForbidden(msg)
}
default:
log.Printf("ignore msg: %v", msg.Type)
}
} else {
log.Printf("not CoAP")
}
}
}
}()
}
// reader procedure
for {
select {
case <-doneCh:
return
default:
}
uSock.SetReadDeadline(time.Now().Add(1 * time.Second))
n, from, err := uSock.ReadFrom(bigBuff[:])
if err != nil {
if nErr, ok := err.(net.Error); !ok || !nErr.Temporary() {
log.Printf("network error: %T: %v", err, err)
return
}
continue
}
// must make the receiving buffer duplicated.
newWi := &data.WorkItem{
Dest: from,
Buff: bytes.Repeat(bigBuff[:n], 1),
}
select {
case <-doneCh:
return
case bytesDisCh <- newWi:
}
}
}