2023-10-23 19:13:08 +02:00
|
|
|
package crdt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/automerge/automerge-go"
|
2023-10-24 17:00:46 +02:00
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-23 19:13:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AutomergeSync struct {
|
2023-10-24 17:00:46 +02:00
|
|
|
state *automerge.SyncState
|
2023-10-26 17:53:12 +02:00
|
|
|
manager *CrdtMeshManager
|
2023-10-23 19:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AutomergeSync) GenerateMessage() ([]byte, bool) {
|
|
|
|
msg, valid := a.state.GenerateMessage()
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.Bytes(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AutomergeSync) RecvMessage(msg []byte) error {
|
|
|
|
_, err := a.state.ReceiveMessage(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:46 +02:00
|
|
|
func (a *AutomergeSync) Complete() {
|
|
|
|
logging.Log.WriteInfof("Sync Completed")
|
|
|
|
a.manager.SaveChanges()
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func NewAutomergeSync(manager *CrdtMeshManager) *AutomergeSync {
|
2023-10-24 17:00:46 +02:00
|
|
|
return &AutomergeSync{
|
|
|
|
state: automerge.NewSyncState(manager.doc),
|
|
|
|
manager: manager,
|
|
|
|
}
|
2023-10-23 19:13:08 +02:00
|
|
|
}
|