This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gateway.go
152 lines (126 loc) · 3.12 KB
/
gateway.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
package dummyGateway
import (
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/errors"
"github.com/ronaksoft/rony/internal/gateway"
"sync"
"sync/atomic"
)
/*
Creation Time: 2020 - Oct - 02
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type Config struct {
Exposer func(gw *Gateway)
}
type Gateway struct {
gateway.ConnectHandler
gateway.MessageHandler
gateway.CloseHandler
conns map[uint64]*Conn
connsMtx sync.RWMutex
connsTotal int32
}
func New(config Config) (*Gateway, error) {
g := &Gateway{
conns: make(map[uint64]*Conn, 8192),
}
// Call the exposer make caller have access to this gateway object
if config.Exposer != nil {
config.Exposer(g)
}
return g, nil
}
// OpenConn opens a persistent connection to the gateway. For short lived use RPC or REST methods.
func (g *Gateway) OpenConn(
connID uint64, persistent bool,
onReceiveMessage func(connID uint64, streamID int64, data []byte),
hdr ...*rony.KeyValue,
) {
dConn := g.openConn(connID, persistent)
dConn.onMessage = func(connID uint64, streamID int64, data []byte, _ map[string]string) {
onReceiveMessage(connID, streamID, data)
}
g.ConnectHandler(dConn, hdr...)
}
func (g *Gateway) openConn(connID uint64, persistent bool) *Conn {
dConn := &Conn{
id: connID,
kv: make(map[string]interface{}),
persistent: persistent,
}
g.connsMtx.Lock()
g.conns[connID] = dConn
g.connsMtx.Unlock()
atomic.AddInt32(&g.connsTotal, 1)
return dConn
}
// CloseConn closed the connection
func (g *Gateway) CloseConn(connID uint64) {
g.connsMtx.Lock()
c := g.conns[connID]
delete(g.conns, connID)
g.connsMtx.Unlock()
if c == nil {
return
}
g.CloseHandler(c)
atomic.AddInt32(&g.connsTotal, -1)
}
// RPC emulates sending an RPC command to the connection. It opens a non-persistent connection if connID
// does not exists
func (g *Gateway) RPC(connID uint64, streamID int64, data []byte) error {
g.connsMtx.RLock()
conn := g.conns[connID]
g.connsMtx.RUnlock()
if conn == nil {
return errors.ErrConnectionNotExists
}
go g.MessageHandler(conn, streamID, data)
return nil
}
// REST emulates a Http REST request.
func (g *Gateway) REST(connID uint64, method, path string, body []byte, kvs ...*rony.KeyValue) (respBody []byte, respHdr map[string]string) {
conn := g.openConn(connID, false)
conn.onMessage = func(connID uint64, streamID int64, data []byte, hdr map[string]string) {
respHdr = hdr
respBody = data
}
conn.method = method
conn.path = path
conn.body = body
for _, kv := range kvs {
conn.kv[kv.Key] = kv.Key
}
defer g.CloseConn(connID)
g.MessageHandler(conn, 0, nil)
return
}
func (g *Gateway) Start() {
// Do nothing
}
func (g *Gateway) Run() {
// Do nothing
}
func (g *Gateway) Shutdown() {
// Do nothing
}
func (g *Gateway) GetConn(connID uint64) rony.Conn {
g.connsMtx.RLock()
conn := g.conns[connID]
g.connsMtx.RUnlock()
if conn == nil {
return nil
}
return conn
}
func (g *Gateway) Addr() []string {
return []string{"TEST"}
}
func (g *Gateway) Protocol() rony.GatewayProtocol {
return rony.Dummy
}