generated from multiversx/mx-chain-ws-connector-template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsConnectorFactory.go
89 lines (75 loc) · 2.5 KB
/
wsConnectorFactory.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
package factory
import (
"os"
"github.com/multiversx/mx-chain-communication-go/websocket/data"
factoryHost "github.com/multiversx/mx-chain-communication-go/websocket/factory"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-core-go/marshal/factory"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-ws-connector-template-go/config"
"github.com/multiversx/mx-chain-ws-connector-template-go/process"
)
var log = logger.GetOrCreate("ws-connector")
// CreateWSConnector will create a ws connector able to receive and process incoming data
// from a multiversx node
func CreateWSConnector(cfg config.WebSocketConfig) (process.WSConnector, error) {
marshaller, err := factory.NewMarshalizer(cfg.MarshallerType)
if err != nil {
return nil, err
}
blockContainer, err := createBlockContainer()
if err != nil {
return nil, err
}
dataProcessor, err := process.NewFirehoseDataProcessor(
os.Stdout, // DO NOT CHANGE
blockContainer,
&marshal.GogoProtoMarshalizer{}, // DO NOT CHANGE
)
if err != nil {
return nil, err
}
wsHost, err := createWsHost(marshaller, cfg)
if err != nil {
return nil, err
}
err = wsHost.SetPayloadHandler(dataProcessor)
if err != nil {
return nil, err
}
return wsHost, nil
}
func createBlockContainer() (process.BlockContainerHandler, error) {
container := block.NewEmptyBlockCreatorsContainer()
err := container.Add(core.ShardHeaderV1, block.NewEmptyHeaderCreator())
if err != nil {
return nil, err
}
err = container.Add(core.ShardHeaderV2, block.NewEmptyHeaderV2Creator())
if err != nil {
return nil, err
}
err = container.Add(core.MetaHeader, block.NewEmptyMetaBlockCreator())
if err != nil {
return nil, err
}
return container, nil
}
func createWsHost(wsMarshaller marshal.Marshalizer, cfg config.WebSocketConfig) (factoryHost.FullDuplexHost, error) {
return factoryHost.CreateWebSocketHost(factoryHost.ArgsWebSocketHost{
WebSocketConfig: data.WebSocketConfig{
URL: cfg.Url,
WithAcknowledge: cfg.WithAcknowledge,
Mode: cfg.Mode,
RetryDurationInSec: int(cfg.RetryDuration),
BlockingAckOnError: cfg.BlockingAckOnError,
DropMessagesIfNoConnection: false,
AcknowledgeTimeoutInSec: cfg.AcknowledgeTimeoutInSec,
Version: cfg.Version,
},
Marshaller: wsMarshaller,
Log: log,
})
}