forked from samuel/go-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
184 lines (174 loc) · 3.8 KB
/
tracer.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
package zk
import (
"encoding/binary"
"fmt"
"io"
"net"
"sync"
)
var (
requests = make(map[int32]int32) // Map of Xid -> Opcode
requestsLock = &sync.Mutex{}
)
func trace(conn1, conn2 net.Conn, client bool) {
defer conn1.Close()
defer conn2.Close()
buf := make([]byte, 10*1024)
init := true
for {
_, err := io.ReadFull(conn1, buf[:4])
if err != nil {
fmt.Println("1>", client, err)
return
}
blen := int(binary.BigEndian.Uint32(buf[:4]))
_, err = io.ReadFull(conn1, buf[4:4+blen])
if err != nil {
fmt.Println("2>", client, err)
return
}
var cr interface{} = nil
var opcode int32 = -1
if client {
if init {
cr = &connectRequest{}
} else {
xid := int32(binary.BigEndian.Uint32(buf[4:8]))
opcode = int32(binary.BigEndian.Uint32(buf[8:12]))
requestsLock.Lock()
requests[xid] = opcode
requestsLock.Unlock()
switch opcode {
default:
fmt.Printf("Unknown opcode %d\n", opcode)
case opClose:
cr = &closeRequest{}
case opCreate:
cr = &createRequest{}
case opDelete:
cr = &deleteRequest{}
case opExists:
cr = &existsRequest{}
case opGetAcl:
cr = &getAclRequest{}
case opGetChildren:
cr = &getChildrenRequest{}
case opGetChildren2:
cr = &getChildren2Request{}
case opGetData:
cr = &getDataRequest{}
case opPing:
cr = &pingRequest{}
case opSetAcl:
cr = &setAclRequest{}
case opSetData:
cr = &setDataRequest{}
case opSetWatches:
cr = &setWatchesRequest{}
case opSync:
cr = &syncRequest{}
}
}
} else {
if init {
cr = &connectResponse{}
} else {
xid := int32(binary.BigEndian.Uint32(buf[4:8]))
zxid := int64(binary.BigEndian.Uint64(buf[8:16]))
errnum := int32(binary.BigEndian.Uint32(buf[16:20]))
if xid != -1 || zxid != -1 {
requestsLock.Lock()
found := false
opcode, found = requests[xid]
if !found {
println("WEFWEFEW")
opcode = 0
}
delete(requests, xid)
requestsLock.Unlock()
} else {
opcode = opWatcherEvent
}
switch opcode {
default:
fmt.Printf("Unknown opcode %d\n", opcode)
case opClose:
cr = &closeResponse{}
case opCreate:
cr = &createResponse{}
case opDelete:
cr = &deleteResponse{}
case opExists:
cr = &existsResponse{}
case opGetAcl:
cr = &getAclResponse{}
case opGetChildren:
cr = &getChildrenResponse{}
case opGetChildren2:
cr = &getChildren2Response{}
case opGetData:
cr = &getDataResponse{}
case opPing:
cr = &pingResponse{}
case opSetAcl:
cr = &setAclResponse{}
case opSetData:
cr = &setDataResponse{}
case opSetWatches:
cr = &setWatchesResponse{}
case opSync:
cr = &syncResponse{}
case opWatcherEvent:
cr = &watcherEvent{}
}
if errnum != 0 {
cr = &responseHeader{}
}
}
}
opname := "."
if opcode != -1 {
opname = opNames[opcode]
}
if cr == nil {
fmt.Printf("%+v %s %+v\n", client, opname, buf[4:4+blen])
} else {
if _, err := decodePacket(buf[4:4+blen], cr); err != nil {
fmt.Println(err)
}
fmt.Printf("%+v %s %+v\n", client, opname, cr)
}
init = false
written, err := conn2.Write(buf[:4+blen])
if err != nil {
fmt.Println("3>", client, err)
return
} else if written != 4+blen {
fmt.Printf("Written != read: %d != %d\n", written, blen)
return
}
}
}
func handleConnection(conn net.Conn) {
zkConn, err := net.Dial("tcp", "127.0.0.1:2181")
if err != nil {
fmt.Println(err)
return
}
go trace(conn, zkConn, true)
trace(zkConn, conn, false)
}
func startTracer() {
ln, err := net.Listen("tcp", "127.0.0.1:2182")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleConnection(conn)
}
}