oc-lib/models/peer/peer.go

90 lines
2.2 KiB
Go
Raw Normal View History

2024-08-12 12:03:58 +02:00
package peer
import (
"encoding/json"
2024-08-23 09:53:37 +02:00
"fmt"
2024-08-12 12:03:58 +02:00
"cloud.o-forge.io/core/oc-lib/models/utils"
2024-08-13 14:33:26 +02:00
"cloud.o-forge.io/core/oc-lib/static"
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 12:03:58 +02:00
"github.com/google/uuid"
)
type Peer struct {
utils.AbstractObject
2024-08-23 09:53:37 +02:00
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"`
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"`
FailedExecution []PeerExecution `json:"failed_execution,omitempty" bson:"failed_execution,omitempty"`
}
func (ao *Peer) AddExecution(exec PeerExecution) {
found := false
for _, v := range ao.FailedExecution {
if v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body) {
found = true
break
}
}
if !found {
ao.FailedExecution = append(ao.FailedExecution, exec)
}
}
func (ao *Peer) RemoveExecution(exec PeerExecution) {
new := []PeerExecution{}
for i, v := range ao.FailedExecution {
if !(v.Url == exec.Url && v.Method == exec.Method && fmt.Sprint(v.Body) == fmt.Sprint(exec.Body)) {
new = append(new, ao.FailedExecution[i])
}
}
ao.FailedExecution = new
2024-08-12 12:03:58 +02:00
}
2024-08-13 14:33:26 +02:00
func (ao *Peer) IsMySelf() bool {
id, _ := static.GetMyLocalJsonPeer()
return ao.UUID == id
}
2024-08-23 09:53:37 +02:00
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
2024-08-21 08:54:29 +02:00
p.UUID = peerID
2024-08-23 09:53:37 +02:00
return (&PeerCache{}).LaunchPeerExecution(peerID, dataID, dt, method, body, caller)
2024-08-13 14:33:26 +02:00
}
2024-08-12 12:03:58 +02:00
func (ao *Peer) GetID() string {
return ao.UUID
}
func (r *Peer) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Peer) GetName() string {
return d.Name
}
func (d *Peer) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
2024-08-12 12:03:58 +02:00
data := New()
data.Init(utils.PEER, caller)
2024-08-12 12:03:58 +02:00
return data
}
func (dma *Peer) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Peer) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}