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"
|
2024-08-12 16:11:25 +02:00
|
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
2024-08-12 12:03:58 +02:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// Peer is a struct that represents a peer
|
2024-08-12 12:03:58 +02:00
|
|
|
type Peer struct {
|
|
|
|
utils.AbstractObject
|
2024-08-30 14:50:48 +02:00
|
|
|
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"` // Url is the URL of the peer (base64url)
|
|
|
|
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"` // PublicKey is the public key of the peer
|
|
|
|
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"` // Services is the services of the peer
|
|
|
|
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"` // FailedExecution is the list of failed executions, to be retried
|
2024-08-23 09:53:37 +02:00
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// AddExecution adds an execution to the list of failed executions
|
2024-08-23 09:53:37 +02:00
|
|
|
func (ao *Peer) AddExecution(exec PeerExecution) {
|
|
|
|
found := false
|
2024-08-30 14:50:48 +02:00
|
|
|
for _, v := range ao.FailedExecution { // Check if the execution is already in the list
|
2024-08-23 09:53:37 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// RemoveExecution removes an execution from the list of failed executions
|
2024-08-23 09:53:37 +02:00
|
|
|
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-30 14:50:48 +02:00
|
|
|
// IsMySelf checks if the peer is the local peer
|
2024-08-13 14:33:26 +02:00
|
|
|
func (ao *Peer) IsMySelf() bool {
|
|
|
|
id, _ := static.GetMyLocalJsonPeer()
|
|
|
|
return ao.UUID == id
|
|
|
|
}
|
|
|
|
|
2024-08-30 14:50:48 +02:00
|
|
|
// LaunchPeerExecution launches an execution on a peer
|
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-30 14:50:48 +02:00
|
|
|
return cache.LaunchPeerExecution(peerID, dataID, dt, method, body, caller) // Launch the execution on the peer through the cache
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:11:25 +02:00
|
|
|
func (d *Peer) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
2024-08-30 14:50:48 +02:00
|
|
|
data := New() // Create a new instance of the accessor
|
|
|
|
data.Init(utils.PEER, caller) // Initialize the accessor with the PEER model type
|
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
|
|
|
|
}
|