From 7234b116439f3bb51246fa5a1f67d9cf16096ab0 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 7 Oct 2023 23:08:18 -0600 Subject: [PATCH] Simplified changes --- gql.go | 2 +- lockable.go | 44 ++++++++++++++++++++++---------------------- node.go | 34 ++++------------------------------ 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/gql.go b/gql.go index 1c3b3fd..bbf42d1 100644 --- a/gql.go +++ b/gql.go @@ -1331,7 +1331,7 @@ func (ext *GQLExt) Process(ctx *Context, node *Node, source NodeID, signal Signa case *StartSignal: ctx.Log.Logf("gql", "starting gql server %s", node.ID) err := ext.StartGQLServer(ctx, node) - changes = changes.AddDetail(GQLExtType, "", "server_started") + changes = changes.Add("server_started") if err == nil { node.QueueSignal(time.Now(), NewStatusSignal(node.ID, "server_started")) } else { diff --git a/lockable.go b/lockable.go index dbe2c4a..946cb9f 100644 --- a/lockable.go +++ b/lockable.go @@ -72,7 +72,7 @@ func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeI var changes Changes = nil switch str { case "not_unlocked": - changes = changes.Add(LockableExtType, "requirements") + changes = changes.Add("requirements") if ext.State == Locking { ext.State = AbortingLock req_info := ext.Requirements[source] @@ -117,7 +117,7 @@ func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID Unlocked, uuid.UUID{}, } - changes = changes.Add(LockableExtType, "requirements") + changes = changes.Add("requirements") messages = messages.Add(ctx, node.ID, node.Key, NewSuccessSignal(signal.ID()), source) } case "remove": @@ -126,7 +126,7 @@ func (ext *LockableExt) HandleLinkSignal(ctx *Context, node *Node, source NodeID messages = messages.Add(ctx, node.ID, node.Key, NewErrorSignal(signal.ID(), "can't link: not_requirement"), source) } else { delete(ext.Requirements, signal.NodeID) - changes = changes.Add(LockableExtType, "requirements") + changes = changes.Add("requirements") messages = messages.Add(ctx, node.ID, node.Key, NewSuccessSignal(signal.ID()), source) } default: @@ -154,7 +154,7 @@ func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source Nod } else if info.MsgID != signal.ReqID { ctx.Log.Logf("lockable", "Got success for wrong signal for %s: %s, expecting %s", source, signal.ReqID, info.MsgID) } else { - changes = changes.Add(LockableExtType, "requirements") + changes = changes.Add("requirements") if info.State == Locking { if ext.State == Locking { info.State = Locked @@ -173,9 +173,9 @@ func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source Nod ctx.Log.Logf("lockable", "WHOLE LOCK: %s - %s - %+v", node.ID, ext.PendingID, ext.PendingOwner) ext.State = Locked ext.Owner = ext.PendingOwner - changes = changes.Add(LockableExtType, "state") - changes = changes.Add(LockableExtType, "owner") - changes = changes.Add(LockableExtType, "pending_owner") + changes = changes.Add("state") + changes = changes.Add("owner") + changes = changes.Add("pending_owner") messages = messages.Add(ctx, node.ID, node.Key, NewSuccessSignal(ext.PendingID), *ext.Owner) } else { ctx.Log.Logf("lockable", "PARTIAL LOCK: %s - %d/%d", node.ID, locked, reqs) @@ -203,17 +203,17 @@ func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source Nod if unlocked == reqs { old_state := ext.State ext.State = Unlocked - changes = changes.Add(LockableExtType, "state") + changes = changes.Add("state") ctx.Log.Logf("lockable", "WHOLE UNLOCK: %s - %s - %+v", node.ID, ext.PendingID, ext.PendingOwner) if old_state == Unlocking { previous_owner := *ext.Owner ext.Owner = ext.PendingOwner ext.ReqID = nil - changes = changes.Add(LockableExtType, "owner") + changes = changes.Add("owner") messages = messages.Add(ctx, node.ID, node.Key, NewSuccessSignal(ext.PendingID), previous_owner) } else if old_state == AbortingLock { messages = messages.Add(ctx ,node.ID, node.Key, NewErrorSignal(*ext.ReqID, "not_unlocked"), *ext.PendingOwner) - changes = changes.Add(LockableExtType, "pending_owner") + changes = changes.Add("pending_owner") ext.PendingOwner = ext.Owner } } else { @@ -240,9 +240,9 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID new_owner := source ext.PendingOwner = &new_owner ext.Owner = &new_owner - changes = changes.Add(LockableExtType, "state") - changes = changes.Add(LockableExtType, "pending_owner") - changes = changes.Add(LockableExtType, "owner") + changes = changes.Add("state") + changes = changes.Add("pending_owner") + changes = changes.Add("owner") messages = messages.Add(ctx, node.ID, node.Key, NewSuccessSignal(signal.ID()), new_owner) } else { ext.State = Locking @@ -251,9 +251,9 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID new_owner := source ext.PendingOwner = &new_owner ext.PendingID = signal.ID() - changes = changes.Add(LockableExtType, "state") - changes = changes.Add(LockableExtType, "pending_owner") - changes = changes.Add(LockableExtType, "requirements") + changes = changes.Add("state") + changes = changes.Add("pending_owner") + changes = changes.Add("requirements") for id, info := range(ext.Requirements) { if info.State != Unlocked { ctx.Log.Logf("lockable", "REQ_NOT_UNLOCKED_WHEN_LOCKING") @@ -275,9 +275,9 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID new_owner := source ext.PendingOwner = nil ext.Owner = nil - changes = changes.Add(LockableExtType, "state") - changes = changes.Add(LockableExtType, "owner") - changes = changes.Add(LockableExtType, "pending_owner") + changes = changes.Add("state") + changes = changes.Add("owner") + changes = changes.Add("pending_owner") messages = messages.Add(ctx, node.ID, node.Key, NewSuccessSignal(signal.ID()), new_owner) } else if source == *ext.Owner { ext.State = Unlocking @@ -285,9 +285,9 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID ext.ReqID = &id ext.PendingOwner = nil ext.PendingID = signal.ID() - changes = changes.Add(LockableExtType, "requirements") - changes = changes.Add(LockableExtType, "pending_owner") - changes = changes.Add(LockableExtType, "state") + changes = changes.Add("requirements") + changes = changes.Add("pending_owner") + changes = changes.Add("state") for id, info := range(ext.Requirements) { if info.State != Locked { ctx.Log.Logf("lockable", "REQ_NOT_LOCKED_WHEN_UNLOCKING") diff --git a/node.go b/node.go index 46a4a29..eb08188 100644 --- a/node.go +++ b/node.go @@ -47,28 +47,10 @@ func RandID() NodeID { return NodeID(uuid.New()) } -type Change struct { - Extension ExtType - Field string - Detail string -} - -type Changes []Change +type Changes []string -func (changes Changes) Add(ext ExtType, field string) Changes { - return append(changes, Change{ - Extension: ext, - Field: field, - Detail: "", - }) -} - -func (changes Changes) AddDetail(ext ExtType, field string, detail string) Changes { - return append(changes, Change{ - Extension: ext, - Field: field, - Detail: detail, - }) +func (changes Changes) Add(detail string) Changes { + return append(changes, detail) } // Extensions are data attached to nodes that process signals @@ -538,15 +520,7 @@ func (node *Node) Stop(ctx *Context) error { } func (node *Node) QueueChanges(ctx *Context, changes Changes) error { - change_map := map[ExtType][]string{} - for _, change := range(changes) { - _, exists := change_map[change.Extension] - if exists == false { - change_map[change.Extension] = []string{} - } - change_map[change.Extension] = append(change_map[change.Extension], change.Field) - } - node.QueueSignal(time.Now(), NewStatusSignal(node.ID, fmt.Sprintf("%+v", change_map))) + node.QueueSignal(time.Now(), NewStatusSignal(node.ID, fmt.Sprintf("%+v", changes))) return nil }