-
Notifications
You must be signed in to change notification settings - Fork 5
/
connections.go
110 lines (93 loc) · 3.21 KB
/
connections.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
/* For license and copyright information please see LEGAL file in repository */
package chapar
import (
"sync"
er "../error"
"../protocol"
)
// connections store pools of connection to retrieve in many ways!
// TODO::: add analytic methods
type connections struct {
mutex sync.Mutex
poolByPath map[string]protocol.NetworkLinkConnection // It is optimize to convert byte slice to string as key in map: https://github.com/golang/go/commit/f5f5a8b6209f84961687d993b93ea0d397f5d5bf
poolByThingID map[[32]byte]protocol.NetworkLinkConnection
poolByBlackList map[[32]byte]protocol.NetworkLinkConnection
}
func (c *connections) init() {
if c.poolByPath == nil {
c.poolByPath = make(map[string]protocol.NetworkLinkConnection, 16384)
}
if c.poolByThingID == nil {
c.poolByThingID = make(map[[32]byte]protocol.NetworkLinkConnection, 16384)
}
if c.poolByBlackList == nil {
c.poolByBlackList = make(map[[32]byte]protocol.NetworkLinkConnection)
}
}
func (c *connections) newConnection(port *port, frame []byte) (conn protocol.NetworkLinkConnection) {
conn = &Connection{
port: port,
}
conn.init(frame)
// TODO::: get ThingID from peer or func args??
c.registerConnection(conn)
return
}
func (c *connections) establishConnectionByPath(path []byte) (conn *Connection, err protocol.Error) {
return
}
func (c *connections) establishConnectionByThingID(thingID [32]byte) (conn *Connection, err protocol.Error) {
return
}
// GetConnectionByPath get a connection by its path from connections pool!!
func (c *connections) getConnectionByPath(path []byte) (conn protocol.NetworkLinkConnection, err protocol.Error) {
conn = c.poolByPath[string(path)]
if conn == nil {
conn = &Connection{
path: path,
}
err = conn.GetLastByPath()
}
return
}
func (c *connections) getConnectionsByThingID(thingID [32]byte) (conn protocol.NetworkLinkConnection, err protocol.Error) {
conn = c.poolByThingID[thingID]
if conn == nil {
conn = &Connection{
thingID: thingID,
}
err = conn.GetLastByThingID()
}
return
}
func (c *connections) registerConnection(conn protocol.NetworkLinkConnection) {
c.mutex.Lock()
c.poolByPath[conn.Path.GetAsString()] = conn
c.poolByThingID[conn.ThingID] = conn
c.mutex.Unlock()
}
func (c *connections) registerNewPathForConnection(conn protocol.NetworkLinkConnection, alternativePath []byte) {
conn.setAlternativePath(alternativePath)
c.mutex.Lock()
c.poolByPath[string(alternativePath)] = conn
c.mutex.Unlock()
}
func (c *connections) closeConnection(conn protocol.NetworkLinkConnection) {
c.mutex.Lock()
// TODO::: Is it worth to don't delete connection just reset it and send it to pool of unused connection due to GC!
delete(c.poolByPath, conn.Path)
delete(c.poolByThingID, conn.ThingID)
for path := range conn.conn.AlternativePath {
delete(c.poolByPath, path)
}
c.mutex.Unlock()
}
// Shutdown ready the connection pools to shutdown!!
func (c *connections) shutdown() {
protocol.App.LogInfo("Chapar - ShutDown - Connections saving proccess begin...")
protocol.App.LogInfo("Chapar - ShutDown - Number of active connections:", len(c.poolByThingID))
for _, conn := range c.poolByThingID {
conn.saveConn()
}
protocol.App.LogInfo("Chapar - ShutDown - Connections saving proccess end now!")
}