-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
182 lines (146 loc) · 3.65 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
// Copyright (c) 2010 William R. Conant, WillConant.com
// Use of this source code is governed by the MIT licence:
// http://www.opensource.org/licenses/mit-license.php
package msglite
import (
"net"
"fmt"
"strconv"
"os"
"bufio"
)
const (
readyCommandStr = "<"
messageCommandStr = ">"
queryCommandStr = "?"
timeoutCommandStr = "*"
quitCommandStr = "."
errorCommandStr = "-"
)
type Server struct {
exchange *Exchange
listener net.Listener
quitChan chan bool
}
func NewServer(exchange *Exchange, network string, laddr string) (server *Server) {
server = new(Server)
server.exchange = exchange
server.quitChan = make(chan bool)
var err os.Error
server.listener, err = net.Listen(network, laddr)
if err != nil {
panic(err)
}
if network == "unix" {
os.Chmod(laddr, 0777)
}
return
}
func (server *Server) Run() {
connChan := make(chan net.Conn)
go func() {
for {
conn, err := server.listener.Accept()
if err != nil {
panic(fmt.Sprintf("error accepting connection: %v", err))
}
connChan <- conn
}
}()
for {
select {
case netConn := <- connChan:
go server.handle(&CommandStream{bufio.NewReader(netConn), netConn, false})
case <- server.quitChan:
server.listener.Close()
return
}
}
}
func (server *Server) Quit() {
server.quitChan <- true
}
func (server *Server) handle(stream *CommandStream) {
handleReady := func(params []string) {
if len(params) < 2 {
stream.WriteError(os.NewError("ready format: < timeout onAddr1 [onAddr2..onAddrN]")); return
}
timeout, err := strconv.Atoi64(params[0])
if err != nil {
stream.WriteError(os.NewError("invalid timeout format")); return
}
msg := server.exchange.Ready(timeout, params[1:])
err = stream.WriteMessage(msg)
if err != nil {
stream.WriteError(err); return
}
}
handleMessage := func(params []string) {
if len(params) < 3 || len(params) > 4 {
stream.WriteError(os.NewError("message format: > bodyLen timeout toAddr [replyAddr]")); return
}
bodyLen, err := strconv.Atoi(params[0])
if err != nil {
stream.WriteError(os.NewError("invalid body length format")); return
}
timeout, err := strconv.Atoi64(params[1])
if err != nil {
stream.WriteError(os.NewError("invalid timeout format")); return
}
toAddr := params[2]
var replyAddr string
if len(params) == 4 {
replyAddr = params[3]
}
var body string
if bodyLen > 0 {
body, err = stream.ReadBody(bodyLen)
if err != nil {
stream.WriteError(err); return
}
}
server.exchange.Send(body, timeout, toAddr, replyAddr)
}
handleQuery := func(params []string) {
if len(params) != 3 {
stream.WriteError(os.NewError("query format: ? bodyLen timeout toAddr")); return
}
bodyLen, err := strconv.Atoi(params[0])
if err != nil {
stream.WriteError(os.NewError("invalid body length format")); return
}
timeout, err := strconv.Atoi64(params[1])
if err != nil {
stream.WriteError(os.NewError("invalid timeout format")); return
}
toAddr := params[2]
body, err := stream.ReadBody(bodyLen)
if err != nil {
stream.WriteError(err); return
}
msg := server.exchange.Query(body, timeout, toAddr)
err = stream.WriteMessage(msg)
if err != nil {
stream.WriteError(err); return
}
}
for !stream.closed {
command, err := stream.ReadCommand()
if err != nil {
stream.WriteError(err)
break
}
switch command[0] {
case readyCommandStr:
handleReady(command[1:])
case messageCommandStr:
handleMessage(command[1:])
case queryCommandStr:
handleQuery(command[1:])
case quitCommandStr:
stream.Close()
default:
stream.WriteError(os.NewError("invalid command"))
}
}
}