2022-10-12 11:55:36 +08:00
|
|
|
package hpds_node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.hpds.cc/Component/network"
|
|
|
|
"git.hpds.cc/Component/network/frame"
|
2023-04-05 17:25:22 +08:00
|
|
|
"git.hpds.cc/Component/network/log"
|
2022-10-12 11:55:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
apLogPrefix = "\033[32m[hpds:access_point]\033[0m "
|
|
|
|
)
|
|
|
|
|
|
|
|
// AccessPoint is responsible for sending data to hpds.
|
|
|
|
type AccessPoint interface {
|
|
|
|
// Close will close the connection to HPDS-Mq.
|
|
|
|
Close() error
|
|
|
|
// Connect to HPDS-Mq.
|
|
|
|
Connect() error
|
|
|
|
// SetDataTag will set the tag of data when invoking Write().
|
2023-04-05 17:25:22 +08:00
|
|
|
SetDataTag(tag frame.Tag)
|
2022-10-12 11:55:36 +08:00
|
|
|
// Write the data to downstream.
|
|
|
|
Write(p []byte) (n int, err error)
|
|
|
|
// WriteWithTag will write data with specified tag, default transactionId is epoch time.
|
2023-04-05 17:25:22 +08:00
|
|
|
WriteWithTag(tag frame.Tag, data []byte) error
|
2022-10-12 11:55:36 +08:00
|
|
|
// SetErrorHandler set the error handler function when server error occurs
|
|
|
|
SetErrorHandler(fn func(err error))
|
|
|
|
// SetReceiveHandler [Experimental] set to observe handler function
|
2023-04-05 17:25:22 +08:00
|
|
|
SetReceiveHandler(fn func(tag frame.Tag, data []byte))
|
|
|
|
// Broadcast the data to all downstream
|
|
|
|
Broadcast(data []byte) error
|
2022-10-12 11:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// hpds-AccessPoint
|
|
|
|
type accessPoint struct {
|
|
|
|
name string
|
|
|
|
mqEndpoint string
|
|
|
|
client *network.Client
|
2023-04-05 17:25:22 +08:00
|
|
|
tag frame.Tag
|
|
|
|
fn func(frame.Tag, []byte)
|
2022-10-12 11:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ AccessPoint = &accessPoint{}
|
|
|
|
|
|
|
|
// NewAccessPoint create a hpds-AccessPoint
|
|
|
|
func NewAccessPoint(name string, opts ...Option) AccessPoint {
|
|
|
|
options := NewOptions(opts...)
|
2023-04-05 17:25:22 +08:00
|
|
|
client := network.NewClient(name, network.ClientTypeSource, options.ClientOptions...)
|
2022-10-12 11:55:36 +08:00
|
|
|
|
|
|
|
return &accessPoint{
|
|
|
|
name: name,
|
|
|
|
mqEndpoint: options.MqAddr,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the data to downstream.
|
|
|
|
func (s *accessPoint) Write(data []byte) (int, error) {
|
2023-04-05 17:25:22 +08:00
|
|
|
err := s.WriteWithTag(s.tag, data)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(data), nil
|
2022-10-12 11:55:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDataTag will set the tag of data when invoking Write().
|
2023-04-05 17:25:22 +08:00
|
|
|
func (s *accessPoint) SetDataTag(tag frame.Tag) {
|
2022-10-12 11:55:36 +08:00
|
|
|
s.tag = tag
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:26:19 +08:00
|
|
|
// Close will close the connection to MessageQueue.
|
2022-10-12 11:55:36 +08:00
|
|
|
func (s *accessPoint) Close() error {
|
|
|
|
if err := s.client.Close(); err != nil {
|
2023-04-05 20:59:07 +08:00
|
|
|
log.Errorf("%s Close(): %v", apLogPrefix, err)
|
2022-10-12 11:55:36 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-04-05 17:25:22 +08:00
|
|
|
log.Debugf("%s is closed", apLogPrefix)
|
2022-10-12 11:55:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to YoMo-MessageQueue.
|
|
|
|
func (s *accessPoint) Connect() error {
|
|
|
|
// set backFlowFrame handler
|
|
|
|
s.client.SetBackFlowFrameObserver(func(frm *frame.BackFlowFrame) {
|
|
|
|
if s.fn != nil {
|
|
|
|
s.fn(frm.GetDataTag(), frm.GetCarriage())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := s.client.Connect(context.Background(), s.mqEndpoint)
|
|
|
|
if err != nil {
|
2023-04-05 20:59:07 +08:00
|
|
|
log.Errorf("%s Connect() error: %s", apLogPrefix, err)
|
2022-10-12 11:55:36 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteWithTag will write data with specified tag, default transactionID is epoch time.
|
2023-04-05 17:25:22 +08:00
|
|
|
func (s *accessPoint) WriteWithTag(tag frame.Tag, data []byte) error {
|
2022-10-12 11:55:36 +08:00
|
|
|
f := frame.NewDataFrame()
|
2023-03-24 08:49:01 +08:00
|
|
|
f.SetCarriage(tag, data)
|
2022-10-12 11:55:36 +08:00
|
|
|
f.SetSourceId(s.client.ClientId())
|
2023-04-05 21:17:46 +08:00
|
|
|
log.Debugf("%s WriteWithTag: tid=%s, source_id=%s, data[%d]",
|
|
|
|
apLogPrefix, f.TransactionId(), f.SourceId(), len(data))
|
2022-10-12 11:55:36 +08:00
|
|
|
return s.client.WriteFrame(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetErrorHandler set the error handler function when server error occurs
|
|
|
|
func (s *accessPoint) SetErrorHandler(fn func(err error)) {
|
|
|
|
s.client.SetErrorHandler(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReceiveHandler [Experimental] set to observe handler function
|
2023-04-05 17:25:22 +08:00
|
|
|
func (s *accessPoint) SetReceiveHandler(fn func(frame.Tag, []byte)) {
|
2022-10-12 11:55:36 +08:00
|
|
|
s.fn = fn
|
2023-04-05 20:59:07 +08:00
|
|
|
log.Debugf("%s SetReceiveHandler(%v)", apLogPrefix, s.fn)
|
2023-04-05 17:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Broadcast Write the data to all downstream
|
|
|
|
func (s *accessPoint) Broadcast(data []byte) error {
|
|
|
|
f := frame.NewDataFrame()
|
|
|
|
f.SetCarriage(s.tag, data)
|
|
|
|
f.SetSourceId(s.client.ClientId())
|
|
|
|
f.SetBroadcast(true)
|
|
|
|
log.Debugf("Broadcast", "data_frame", f.String())
|
|
|
|
return s.client.WriteFrame(f)
|
2022-10-12 11:55:36 +08:00
|
|
|
}
|