forked from weilaihui/fdfs_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_client.go
185 lines (163 loc) · 4.87 KB
/
tracker_client.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
185
package fdfs_client
import (
"bytes"
"encoding/binary"
"net"
)
type TrackerClient struct {
pool *ConnectionPool
}
func (this *TrackerClient) trackerQueryStorageStorWithoutGroup() (*StorageServer, error) {
var (
conn net.Conn
recvBuff []byte
err error
)
conn, err = this.pool.Get()
if err != nil {
return nil, err
}
defer conn.Close()
th := &trackerHeader{}
th.cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE
th.sendHeader(conn)
th.recvHeader(conn)
if th.status != 0 {
return nil, Errno{int(th.status)}
}
var (
groupName string
ipAddr string
port int64
storePathIndex uint8
)
recvBuff, _, err = TcpRecvResponse(conn, th.pkgLen)
if err != nil {
logger.Warnf("TcpRecvResponse error :%s", err.Error())
return nil, err
}
buff := bytes.NewBuffer(recvBuff)
// #recv_fmt |-group_name(16)-ipaddr(16-1)-port(8)-store_path_index(1)|
groupName, err = readCstr(buff, FDFS_GROUP_NAME_MAX_LEN)
ipAddr, err = readCstr(buff, IP_ADDRESS_SIZE-1)
binary.Read(buff, binary.BigEndian, &port)
binary.Read(buff, binary.BigEndian, &storePathIndex)
return &StorageServer{ipAddr, int(port), groupName, int(storePathIndex)}, nil
}
func (this *TrackerClient) trackerQueryStorageStorWithGroup(groupName string) (*StorageServer, error) {
var (
conn net.Conn
recvBuff []byte
err error
)
conn, err = this.pool.Get()
defer conn.Close()
if err != nil {
return nil, err
}
th := &trackerHeader{}
th.cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE
th.pkgLen = int64(FDFS_GROUP_NAME_MAX_LEN)
th.sendHeader(conn)
groupBuffer := new(bytes.Buffer)
// 16 bit groupName
groupNameBytes := bytes.NewBufferString(groupName).Bytes()
for i := 0; i < 16; i++ {
if i >= len(groupNameBytes) {
groupBuffer.WriteByte(byte(0))
} else {
groupBuffer.WriteByte(groupNameBytes[i])
}
}
groupBytes := groupBuffer.Bytes()
err = TcpSendData(conn, groupBytes)
if err != nil {
return nil, err
}
th.recvHeader(conn)
if th.status != 0 {
logger.Warnf("recvHeader error [%d]", th.status)
return nil, Errno{int(th.status)}
}
var (
ipAddr string
port int64
storePathIndex uint8
)
recvBuff, _, err = TcpRecvResponse(conn, th.pkgLen)
if err != nil {
logger.Warnf("TcpRecvResponse error :%s", err.Error())
return nil, err
}
buff := bytes.NewBuffer(recvBuff)
// #recv_fmt |-group_name(16)-ipaddr(16-1)-port(8)-store_path_index(1)|
groupName, err = readCstr(buff, FDFS_GROUP_NAME_MAX_LEN)
ipAddr, err = readCstr(buff, IP_ADDRESS_SIZE-1)
binary.Read(buff, binary.BigEndian, &port)
binary.Read(buff, binary.BigEndian, &storePathIndex)
return &StorageServer{ipAddr, int(port), groupName, int(storePathIndex)}, nil
}
func (this *TrackerClient) trackerQueryStorageUpdate(groupName string, remoteFilename string) (*StorageServer, error) {
return this.trackerQueryStorage(groupName, remoteFilename, TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE)
}
func (this *TrackerClient) trackerQueryStorageFetch(groupName string, remoteFilename string) (*StorageServer, error) {
return this.trackerQueryStorage(groupName, remoteFilename, TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE)
}
func (this *TrackerClient) trackerQueryStorage(groupName string, remoteFilename string, cmd int8) (*StorageServer, error) {
var (
conn net.Conn
recvBuff []byte
err error
)
conn, err = this.pool.Get()
defer conn.Close()
if err != nil {
return nil, err
}
th := &trackerHeader{}
th.pkgLen = int64(FDFS_GROUP_NAME_MAX_LEN + len(remoteFilename))
th.cmd = cmd
th.sendHeader(conn)
// #query_fmt: |-group_name(16)-filename(file_name_len)-|
queryBuffer := new(bytes.Buffer)
// 16 bit groupName
groupNameBytes := bytes.NewBufferString(groupName).Bytes()
for i := 0; i < 16; i++ {
if i >= len(groupNameBytes) {
queryBuffer.WriteByte(byte(0))
} else {
queryBuffer.WriteByte(groupNameBytes[i])
}
}
// remoteFilenameLen bit remoteFilename
remoteFilenameBytes := bytes.NewBufferString(remoteFilename).Bytes()
for i := 0; i < len(remoteFilenameBytes); i++ {
queryBuffer.WriteByte(remoteFilenameBytes[i])
}
err = TcpSendData(conn, queryBuffer.Bytes())
if err != nil {
return nil, err
}
th.recvHeader(conn)
if th.status != 0 {
logger.Warnf("recvHeader error [%d]", th.status)
return nil, Errno{int(th.status)}
}
var (
ipAddr string
port int64
storePathIndex uint8
)
recvBuff, _, err = TcpRecvResponse(conn, th.pkgLen)
if err != nil {
logger.Warnf("TcpRecvResponse error :%s", err.Error())
return nil, err
}
buff := bytes.NewBuffer(recvBuff)
// #recv_fmt |-group_name(16)-ipaddr(16-1)-port(8)-store_path_index(1)|
groupName, err = readCstr(buff, FDFS_GROUP_NAME_MAX_LEN)
ipAddr, err = readCstr(buff, IP_ADDRESS_SIZE-1)
binary.Read(buff, binary.BigEndian, &port)
binary.Read(buff, binary.BigEndian, &storePathIndex)
return &StorageServer{ipAddr, int(port), groupName, int(storePathIndex)}, nil
}