-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_handler.go
More file actions
119 lines (101 loc) · 2.64 KB
/
Copy pathjson_handler.go
File metadata and controls
119 lines (101 loc) · 2.64 KB
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
package mongrel2
import (
"encoding/json"
"fmt"
"github.com/seven5/gozmq"
"strconv"
"strings"
)
type JsonRequest struct {
ServerId string
ClientId int
ServicePath string
MongrelInfo map[string]string
Json map[string]interface{}
}
type JsonResponse struct {
ServerId string
ClientId []int
Json map[string]interface{}
}
type JsonHandler interface {
ReadJson() (*JsonRequest, error)
WriteJson(*JsonResponse) error
}
type JsonHandlerDefault struct {
*RawHandlerDefault
}
func (self *JsonHandlerDefault) ReadJson() (*JsonRequest, error) {
payload, err := self.InSocket.Recv(0)
if err != nil {
return nil, err
}
serverId, clientId, path, info, bodyStart, bodySize, error := DecodePayloadStart(payload)
if error != nil {
return nil, error
}
var content map[string]interface{}
if bodySize > 0 {
err = json.Unmarshal(payload[bodyStart:bodyStart+bodySize], &content)
if err != nil {
return nil, err
}
}
result := new(JsonRequest)
result.ServerId = serverId
result.ClientId = clientId
result.MongrelInfo = info
result.ServicePath = path
result.Json = content
return result, nil
}
func (self *JsonHandlerDefault) WriteJson(resp *JsonResponse) error {
c := make([]string, len(resp.ClientId), len(resp.ClientId))
for i, x := range resp.ClientId {
c[i] = strconv.Itoa(x)
}
clientList := strings.Join(c, " ")
b, err := json.Marshal(resp.Json)
if err != nil {
return err
}
body := string(b)
payload := fmt.Sprintf("%s %d:%s, %s", resp.ServerId, len(clientList), clientList, body)
return self.OutSocket.Send([]byte(payload), 0)
}
// ReadLoop is a loop that reads mongrel2 messages until it gets an error. This useful if
// you want to launch a goroutine that reads forever from mongrel2 and makes the read
// messages available on the supplied channel.
func (self *JsonHandlerDefault) ReadLoop(in chan *JsonRequest) {
for {
r, err := self.ReadJson()
if err != nil {
if err == gozmq.ETERM {
fmt.Printf("JSON socket ignoring ETERM on read, assuming shutdown...\n")
return
}
panic(err)
}
in <- r
}
}
// WriteLoop is a loop that sends mongrel two message until it gets an error
// or a message to close. This is useful when you want to launch a goroutine
//that runs forever just taking messages from the out channel supplied and pushing them
//to mongrel2.
func (self *JsonHandlerDefault) WriteLoop(out chan *JsonResponse) {
for {
m := <-out
if m == nil {
return //end of goroutine b/c of shutdown
}
err := self.WriteJson(m)
if err != nil {
if err == gozmq.ETERM {
fmt.Printf("JSON socket ignoring ETERM on write, assuming shutdown...\n")
return
}
panic(err)
}
}
}