forked from synerex/gateway_simple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-gateway.go
93 lines (78 loc) · 2.16 KB
/
simple-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
package main
import (
"flag"
api "github.com/synerex/synerex_api"
"github.com/synerex/synerex_nodeapi"
sxutil "github.com/synerex/synerex_sxutil"
"golang.org/x/net/context"
"log"
"strings"
"sync"
)
var (
nodesrv = flag.String("nodesrv", "127.0.0.1:9990", "Node ID Server")
gateway = flag.String("gateway", "0,1", "Speficy Synerex Server IDs(ordered)")
readOnly = flag.Bool("ro", false , "Read Only flag")
idlist []uint64
spMap map[uint64]*sxutil.SupplyOpts
mu sync.Mutex
sxServerAddress string
)
func forwardGatewayMsg(sg api.Synerex_SubscribeGatewayClient , client api.SynerexClient) {
ctx := context.Background() //
for {
msg, err :=sg.Recv()
if err == nil {
client.ForwardToGateway( ctx,msg)
}
}
}
func main() {
flag.Parse()
go sxutil.HandleSigInt()
sxutil.RegisterDeferFunction(sxutil.UnRegisterNode)
sxo := &sxutil.SxServerOpt{
ServerInfo: *gateway,
NodeType: synerex_nodeapi.NodeType_GATEWAY,
ClusterId: 0,
AreaId: "Default",
}
channelTypes := []uint32{0,1,2,3,4,5,6,7,8,9}
// obtain synerex server address from nodeserv
srvs, err := sxutil.RegisterNode(*nodesrv, "SimpleGW", channelTypes, sxo)
if err != nil {
log.Fatal("Can't register node...")
}
log.Printf("Connecting Servers [%s]\n",srvs)
servers := strings.Split(srvs,",")
wg := sync.WaitGroup{} // for syncing other goroutines
client0 := sxutil.GrpcConnectServer(servers[0])
channels := []uint32{0,1,2,3,4,5,6,7,8}
gi := &api.GatewayInfo{
ClientId: sxutil.GenerateIntID(), // new client_ID
GatewayType: api.GatewayType_BIDIRECTIONAL, /// default
Channels:channels,
}
ctx := context.Background() //
sg0, err := client0.SubscribeGateway(ctx,gi )
if err != nil {
log.Printf("Synerex subscribe Error %v\n",err)
}
if len(servers)>1 {
client1 := sxutil.GrpcConnectServer(servers[1])
sg1, err1 := client1.SubscribeGateway(ctx, gi)
if err1 != nil {
log.Printf("Synerex subscribe Error to %s %v\n",servers[1],err1)
}
wg.Add(2)
go forwardGatewayMsg(sg1, client0)
go forwardGatewayMsg(sg0, client1)
}else{
for{
msg, _ := sg0.Recv()
log.Printf("Recv:%v\n",msg)
}
}
wg.Wait()
sxutil.CallDeferFunctions() // cleanup!
}