oc-lib/models/peer/peer.go

88 lines
2.9 KiB
Go
Raw Normal View History

2024-08-12 12:03:58 +02:00
package peer
import (
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"
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 12:03:58 +02:00
)
2024-10-15 16:37:18 +02:00
// now write a go enum for the state partner with self, blacklist, partner
type PeerState int
const (
NONE PeerState = iota
SELF
PARTNER
BLACKLIST
)
2024-10-30 11:17:52 +01:00
func (m PeerState) String() string {
return [...]string{"NONE", "SELF", "PARTNER", "BLACKLIST"}[m]
}
func (m PeerState) EnumIndex() int {
return int(m)
}
// Peer is a struct that represents a peer
2024-08-12 12:03:58 +02:00
type Peer struct {
utils.AbstractObject
Url string `json:"url" bson:"url" validate:"required"` // Url is the URL of the peer (base64url)
WalletAddress string `json:"wallet_address" bson:"wallet_address" validate:"required"` // WalletAddress is the wallet address of the peer
PublicKey string `json:"public_key" bson:"public_key" validate:"required"` // PublicKey is the public key of the peer
2024-10-15 16:37:18 +02:00
State PeerState `json:"state" bson:"state" default:"0"`
ServicesState map[string]int `json:"services_state,omitempty" bson:"services_state,omitempty"`
2024-10-15 16:37:18 +02:00
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
}
// 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
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)
}
}
// 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
}
// IsMySelf checks if the peer is the local peer
func (p *Peer) IsMySelf() (bool, string) {
d, code, err := NewAccessor(nil).Search(nil, SELF.String(), p.IsDraft)
2024-10-30 11:17:52 +01:00
if code != 200 || err != nil || len(d) == 0 {
return false, ""
}
id := d[0].GetID()
return p.UUID == id, id
2024-08-13 14:33:26 +02:00
}
// LaunchPeerExecution launches an execution on a peer
2024-10-02 10:45:52 +02:00
func (p *Peer) LaunchPeerExecution(peerID string, dataID string, dt tools.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
2024-08-21 08:54:29 +02:00
p.UUID = peerID
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
}
func (d *Peer) GetAccessor(request *tools.APIRequest) utils.Accessor {
data := NewAccessor(request) // Create a new instance of the accessor
2024-08-12 12:03:58 +02:00
return data
}
2024-12-04 12:14:55 +01:00
func (r *Peer) CanDelete() bool {
return false // only draft order can be deleted
2024-12-04 12:14:55 +01:00
}