mirror of
https://github.com/logos-messaging/sds-go-bindings.git
synced 2026-01-02 14:13:08 +00:00
feat: sds lint stub
This commit is contained in:
parent
4399ce6ad9
commit
3bcf73b531
166
sds/sds.go
166
sds/sds.go
@ -1,3 +1,5 @@
|
||||
//go:build !lint
|
||||
|
||||
package sds
|
||||
|
||||
/*
|
||||
@ -137,39 +139,23 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const requestTimeout = 30 * time.Second
|
||||
const EventChanBufferSize = 1024
|
||||
|
||||
//export SdsGoCallback
|
||||
func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
|
||||
if resp != nil {
|
||||
m := (*C.SdsResp)(resp)
|
||||
m.ret = ret
|
||||
m.msg = msg
|
||||
if resp != nil {
|
||||
m := (*C.SdsResp)(resp)
|
||||
m.ret = ret
|
||||
m.msg = msg
|
||||
m.len = len
|
||||
wg := (*sync.WaitGroup)(m.ffiWg)
|
||||
wg.Done()
|
||||
}
|
||||
}
|
||||
|
||||
type EventCallbacks struct {
|
||||
OnMessageReady func(messageId MessageID, channelId string)
|
||||
OnMessageSent func(messageId MessageID, channelId string)
|
||||
OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string)
|
||||
OnPeriodicSync func()
|
||||
}
|
||||
|
||||
// ReliabilityManager represents an instance of a nim-sds ReliabilityManager
|
||||
type ReliabilityManager struct {
|
||||
rmCtx unsafe.Pointer
|
||||
callbacks EventCallbacks
|
||||
}
|
||||
|
||||
func NewReliabilityManager() (*ReliabilityManager, error) {
|
||||
Debug("Creating new Reliability Manager")
|
||||
rm := &ReliabilityManager{}
|
||||
Debug("Creating new Reliability Manager")
|
||||
rm := &ReliabilityManager{}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
var resp = C.allocResp(unsafe.Pointer(&wg))
|
||||
defer C.freeResp(resp)
|
||||
@ -191,128 +177,22 @@ func NewReliabilityManager() (*ReliabilityManager, error) {
|
||||
return rm, nil
|
||||
}
|
||||
|
||||
// The event callback sends back the rm ctx to know to which
|
||||
// rm is the event being emited for. Since we only have a global
|
||||
// callback in the go side, We register all the rm's that we create
|
||||
// so we can later obtain which instance of `ReliabilityManager` it should
|
||||
// be invoked depending on the ctx received
|
||||
|
||||
var rmRegistry map[unsafe.Pointer]*ReliabilityManager
|
||||
|
||||
func init() {
|
||||
rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager)
|
||||
}
|
||||
|
||||
func registerReliabilityManager(rm *ReliabilityManager) {
|
||||
_, ok := rmRegistry[rm.rmCtx]
|
||||
if !ok {
|
||||
rmRegistry[rm.rmCtx] = rm
|
||||
}
|
||||
}
|
||||
|
||||
func unregisterReliabilityManager(rm *ReliabilityManager) {
|
||||
delete(rmRegistry, rm.rmCtx)
|
||||
}
|
||||
|
||||
//export sdsGlobalEventCallback
|
||||
func sdsGlobalEventCallback(callerRet C.int, msg *C.char, len C.size_t, userData unsafe.Pointer) {
|
||||
if callerRet == C.RET_OK {
|
||||
eventStr := C.GoStringN(msg, C.int(len))
|
||||
rm, ok := rmRegistry[userData] // userData contains rm's ctx
|
||||
if ok {
|
||||
rm.OnEvent(eventStr)
|
||||
}
|
||||
} else {
|
||||
if len != 0 {
|
||||
errMsg := C.GoStringN(msg, C.int(len))
|
||||
Error("sdsGlobalEventCallback retCode not ok, retCode: %v: %v", callerRet, errMsg)
|
||||
} else {
|
||||
Error("sdsGlobalEventCallback retCode not ok, retCode: %v", callerRet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type jsonEvent struct {
|
||||
EventType string `json:"eventType"`
|
||||
}
|
||||
|
||||
type msgEvent struct {
|
||||
MessageId MessageID `json:"messageId"`
|
||||
ChannelId string `json:"channelId"`
|
||||
}
|
||||
|
||||
type missingDepsEvent struct {
|
||||
MessageId MessageID `json:"messageId"`
|
||||
MissingDeps []MessageID `json:"missingDeps"`
|
||||
ChannelId string `json:"channelId"`
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) {
|
||||
rm.callbacks = callbacks
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) OnEvent(eventStr string) {
|
||||
|
||||
jsonEvent := jsonEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &jsonEvent)
|
||||
if err != nil {
|
||||
Error("could not unmarshal sds event string: %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
switch jsonEvent.EventType {
|
||||
case "message_ready":
|
||||
rm.parseMessageReadyEvent(eventStr)
|
||||
case "message_sent":
|
||||
rm.parseMessageSentEvent(eventStr)
|
||||
case "missing_dependencies":
|
||||
rm.parseMissingDepsEvent(eventStr)
|
||||
case "periodic_sync":
|
||||
if rm.callbacks.OnPeriodicSync != nil {
|
||||
rm.callbacks.OnPeriodicSync()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) {
|
||||
|
||||
msgEvent := msgEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
||||
if err != nil {
|
||||
Error("could not parse message ready event %v", err)
|
||||
}
|
||||
|
||||
if rm.callbacks.OnMessageReady != nil {
|
||||
rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId)
|
||||
}
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) {
|
||||
|
||||
msgEvent := msgEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
||||
if err != nil {
|
||||
Error("could not parse message sent event %v", err)
|
||||
}
|
||||
|
||||
if rm.callbacks.OnMessageSent != nil {
|
||||
rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId)
|
||||
}
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) {
|
||||
|
||||
missingDepsEvent := missingDepsEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &missingDepsEvent)
|
||||
if err != nil {
|
||||
Error("could not parse missing dependencies event %v", err)
|
||||
}
|
||||
|
||||
if rm.callbacks.OnMissingDependencies != nil {
|
||||
rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId)
|
||||
}
|
||||
if callerRet == C.RET_OK {
|
||||
eventStr := C.GoStringN(msg, C.int(len))
|
||||
rm, ok := rmRegistry[userData] // userData contains rm's ctx
|
||||
if ok {
|
||||
rm.OnEvent(eventStr)
|
||||
}
|
||||
} else {
|
||||
if len != 0 {
|
||||
errMsg := C.GoStringN(msg, C.int(len))
|
||||
Error("sdsGlobalEventCallback retCode not ok, retCode: %v: %v", callerRet, errMsg)
|
||||
} else {
|
||||
Error("sdsGlobalEventCallback retCode not ok, retCode: %v", callerRet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) Cleanup() error {
|
||||
|
||||
123
sds/sds_common.go
Normal file
123
sds/sds_common.go
Normal file
@ -0,0 +1,123 @@
|
||||
package sds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"unsafe"
|
||||
"time"
|
||||
)
|
||||
|
||||
const requestTimeout = 30 * time.Second
|
||||
const EventChanBufferSize = 1024
|
||||
|
||||
type EventCallbacks struct {
|
||||
OnMessageReady func(messageId MessageID, channelId string)
|
||||
OnMessageSent func(messageId MessageID, channelId string)
|
||||
OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string)
|
||||
OnPeriodicSync func()
|
||||
}
|
||||
|
||||
// ReliabilityManager represents an instance of a nim-sds ReliabilityManager
|
||||
type ReliabilityManager struct {
|
||||
rmCtx unsafe.Pointer
|
||||
callbacks EventCallbacks
|
||||
}
|
||||
|
||||
// The event callback sends back the rm ctx to know to which
|
||||
// rm is the event being emited for. Since we only have a global
|
||||
// callback in the go side, We register all the rm's that we create
|
||||
// so we can later obtain which instance of `ReliabilityManager` it should
|
||||
// be invoked depending on the ctx received
|
||||
var rmRegistry map[unsafe.Pointer]*ReliabilityManager
|
||||
|
||||
func init() {
|
||||
rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager)
|
||||
}
|
||||
|
||||
func registerReliabilityManager(rm *ReliabilityManager) {
|
||||
_, ok := rmRegistry[rm.rmCtx]
|
||||
if !ok {
|
||||
rmRegistry[rm.rmCtx] = rm
|
||||
}
|
||||
}
|
||||
|
||||
func unregisterReliabilityManager(rm *ReliabilityManager) {
|
||||
delete(rmRegistry, rm.rmCtx)
|
||||
}
|
||||
|
||||
type jsonEvent struct {
|
||||
EventType string `json:"eventType"`
|
||||
}
|
||||
|
||||
type msgEvent struct {
|
||||
MessageId MessageID `json:"messageId"`
|
||||
ChannelId string `json:"channelId"`
|
||||
}
|
||||
|
||||
type missingDepsEvent struct {
|
||||
MessageId MessageID `json:"messageId"`
|
||||
MissingDeps []MessageID `json:"missingDeps"`
|
||||
ChannelId string `json:"channelId"`
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) {
|
||||
rm.callbacks = callbacks
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) OnEvent(eventStr string) {
|
||||
jsonEvent := jsonEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &jsonEvent)
|
||||
if err != nil {
|
||||
Error("could not unmarshal sds event string: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch jsonEvent.EventType {
|
||||
case "message_ready":
|
||||
rm.parseMessageReadyEvent(eventStr)
|
||||
case "message_sent":
|
||||
rm.parseMessageSentEvent(eventStr)
|
||||
case "missing_dependencies":
|
||||
rm.parseMissingDepsEvent(eventStr)
|
||||
case "periodic_sync":
|
||||
if rm.callbacks.OnPeriodicSync != nil {
|
||||
rm.callbacks.OnPeriodicSync()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) {
|
||||
msgEvent := msgEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
||||
if err != nil {
|
||||
Error("could not parse message ready event %v", err)
|
||||
}
|
||||
|
||||
if rm.callbacks.OnMessageReady != nil {
|
||||
rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId)
|
||||
}
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) {
|
||||
msgEvent := msgEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
||||
if err != nil {
|
||||
Error("could not parse message sent event %v", err)
|
||||
}
|
||||
|
||||
if rm.callbacks.OnMessageSent != nil {
|
||||
rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId)
|
||||
}
|
||||
}
|
||||
|
||||
func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) {
|
||||
missingDepsEvent := missingDepsEvent{}
|
||||
err := json.Unmarshal([]byte(eventStr), &missingDepsEvent)
|
||||
if err != nil {
|
||||
Error("could not parse missing dependencies event %v", err)
|
||||
}
|
||||
|
||||
if rm.callbacks.OnMissingDependencies != nil {
|
||||
rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId)
|
||||
}
|
||||
}
|
||||
|
||||
38
sds/sds_lint.go
Normal file
38
sds/sds_lint.go
Normal file
@ -0,0 +1,38 @@
|
||||
//go:build lint
|
||||
|
||||
package sds
|
||||
|
||||
import "errors"
|
||||
|
||||
// This file provides lint-only stubs that avoid requiring libsds.h/cgo
|
||||
// so linters can analyze this package without native dependencies.
|
||||
|
||||
// ErrLintBuild indicates a stubbed, lint-only build without native libsds.
|
||||
var ErrLintBuild = errors.New("sds: lint-only build stub: native libsds not linked")
|
||||
|
||||
// NewReliabilityManager returns an error in lint builds.
|
||||
func NewReliabilityManager() (*ReliabilityManager, error) {
|
||||
return nil, ErrLintBuild
|
||||
}
|
||||
|
||||
// Cleanup returns an error in lint builds.
|
||||
func (rm *ReliabilityManager) Cleanup() error { return ErrLintBuild }
|
||||
|
||||
// Reset returns an error in lint builds.
|
||||
func (rm *ReliabilityManager) Reset() error { return ErrLintBuild }
|
||||
|
||||
// WrapOutgoingMessage returns an error in lint builds.
|
||||
func (rm *ReliabilityManager) WrapOutgoingMessage(message []byte, messageId MessageID, channelId string) ([]byte, error) {
|
||||
return nil, ErrLintBuild
|
||||
}
|
||||
|
||||
// UnwrapReceivedMessage returns an error in lint builds.
|
||||
func (rm *ReliabilityManager) UnwrapReceivedMessage(message []byte) (*UnwrappedMessage, error) {
|
||||
return nil, ErrLintBuild
|
||||
}
|
||||
|
||||
// MarkDependenciesMet returns an error in lint builds.
|
||||
func (rm *ReliabilityManager) MarkDependenciesMet(messageIDs []MessageID, channelId string) error { return ErrLintBuild }
|
||||
|
||||
// StartPeriodicTasks returns an error in lint builds.
|
||||
func (rm *ReliabilityManager) StartPeriodicTasks() error { return ErrLintBuild }
|
||||
Loading…
x
Reference in New Issue
Block a user