-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientConnectionManager.go
executable file
·61 lines (46 loc) · 1.58 KB
/
clientConnectionManager.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
package hz
import (
"errors"
)
type ClientConnectionManager struct {}
func (manager *ClientConnectionManager) GetOrConnect(address Address, hzUser string, hzPassword string) *Promise {
connection := NewClientConnection(address)
promise := connection.Connect(address)
promise2 := promise.Then(func(obj interface{}) (interface{}, error) {
connection := obj.(*ClientConnection)
return connection,nil
}, func(err error) error{
return err
})
promise3 := promise2.ThenPromise(func(obj interface{}) *Promise {
connection := obj.(*ClientConnection)
return authenticate(connection, hzUser, hzPassword)
}, func(err error) error{
return err
})
return promise3
}
func authenticate(connection *ClientConnection,hzUser string, hzPassword string) *Promise {
result := new(Promise)
result.SuccessChannel = make(chan interface{}, 1)
result.FailureChannel = make(chan error, 1)
request := EncodeRequest(hzUser, hzPassword, nil, nil, true, "GOLANG", 1)
request.SetCorrelationId(1)
request.SetPartitionId(-1)
request.SetFlags(BEGIN_END_FLAG)
connection.socket.Write(request.Buffer)
rBuffer := make([]byte, 1024)
readBytes, _ := connection.socket.Read(rBuffer)
response := CreateForDecode(rBuffer[:readBytes])
go func() {
authResponse := DecodeResponse(response)
if authResponse.Status == 0 {
connection.Address.Host = authResponse.Address.Host
connection.Address.Port = authResponse.Address.Port
result.SuccessChannel <- connection
} else {
result.FailureChannel <- errors.New("Connection is NOT authenticated" + connection.Address.String())
}
}()
return result
}