forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunicator.go
executable file
·131 lines (117 loc) · 3.78 KB
/
communicator.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
package tars
import (
"crypto/md5"
"encoding/hex"
"fmt"
"sync"
s "github.com/TarsCloud/TarsGo/tars/model"
"github.com/TarsCloud/TarsGo/tars/util/tools"
)
// ProxyPrx interface
type ProxyPrx interface {
SetServant(s.Servant)
}
// NewCommunicator returns a new communicator. A Communicator is used for communicating with
// the server side which should only init once and be global!!!
func NewCommunicator() *Communicator {
c := new(Communicator)
c.init()
return c
}
// Communicator struct
type Communicator struct {
Client *clientConfig
properties sync.Map
}
func (c *Communicator) init() {
if GetClientConfig() != nil {
c.SetProperty("locator", GetClientConfig().Locator)
//TODO
c.Client = GetClientConfig()
} else {
c.Client = &clientConfig{
RefreshEndpointInterval: refreshEndpointInterval,
ReportInterval: reportInterval,
CheckStatusInterval: checkStatusInterval,
AsyncInvokeTimeout: AsyncInvokeTimeout,
ClientQueueLen: ClientQueueLen,
ClientIdleTimeout: tools.ParseTimeOut(ClientIdleTimeout),
ClientReadTimeout: tools.ParseTimeOut(ClientReadTimeout),
ClientWriteTimeout: tools.ParseTimeOut(ClientWriteTimeout),
ClientDialTimeout: tools.ParseTimeOut(ClientDialTimeout),
ReqDefaultTimeout: ReqDefaultTimeout,
ObjQueueMax: ObjQueueMax,
AdapterProxyTicker: tools.ParseTimeOut(AdapterProxyTicker),
AdapterProxyResetCount: AdapterProxyResetCount,
}
}
c.SetProperty("isclient", true)
c.SetProperty("enableset", false)
if GetServerConfig() != nil {
c.SetProperty("notify", GetServerConfig().Notify)
c.SetProperty("node", GetServerConfig().Node)
c.SetProperty("server", GetServerConfig().Server)
c.SetProperty("isclient", false)
if GetServerConfig().Enableset {
c.SetProperty("enableset", true)
c.SetProperty("setdivision", GetServerConfig().Setdivision)
}
}
}
// GetLocator returns locator as string
func (c *Communicator) GetLocator() string {
v, _ := c.GetProperty("locator")
return v
}
// SetLocator sets locator with obj
func (c *Communicator) SetLocator(obj string) {
c.SetProperty("locator", obj)
}
// StringToProxy sets the servant of ProxyPrx p with a string servant
func (c *Communicator) StringToProxy(servant string, p ProxyPrx) {
if servant == "" {
panic("empty servant")
}
sp := newServantProxy(c, servant)
p.SetServant(sp)
}
// SetProperty sets communicator property with a string key and an interface value.
// var comm *tars.Communicator
// comm = tars.NewCommunicator()
// e.g. comm.SetProperty("locator", "tars.tarsregistry.QueryObj@tcp -h ... -p ...")
func (c *Communicator) SetProperty(key string, value interface{}) {
c.properties.Store(key, value)
}
// GetProperty returns communicator property value as string and true for key, or empty string
// and false for not exists key
func (c *Communicator) GetProperty(key string) (string, bool) {
if v, ok := c.properties.Load(key); ok {
return v.(string), ok
}
return "", false
}
// GetPropertyInt returns communicator property value as int and true for key, or 0 and false
// for not exists key
func (c *Communicator) GetPropertyInt(key string) (int, bool) {
if v, ok := c.properties.Load(key); ok {
return v.(int), ok
}
return 0, false
}
// GetPropertyBool returns communicator property value as bool and true for key, or false and false for not exists key
func (c *Communicator) GetPropertyBool(key string) (bool, bool) {
if v, ok := c.properties.Load(key); ok {
return v.(bool), ok
}
return false, false
}
func (c *Communicator) hashKey() string {
hash := md5.New()
hashKeys := []string{"locator", "enableset", "setdivision"}
for _, k := range hashKeys {
if v, ok := c.properties.Load(k); ok {
hash.Write([]byte(fmt.Sprintf("%v:%v", k, v)))
}
}
return hex.EncodeToString(hash.Sum(nil))
}