diff --git a/context.go b/context.go index 2e24869..8260391 100644 --- a/context.go +++ b/context.go @@ -7,9 +7,38 @@ import ( "runtime" ) +type NodeType string +func (node NodeType) Hash() uint64 { + hash := sha512.Sum512([]byte(fmt.Sprintf("NODE: %s", string(node)))) + return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) +} + +type PolicyType string +func (policy PolicyType) Hash() uint64 { + hash := sha512.Sum512([]byte(fmt.Sprintf("POLICY: %s", string(policy)))) + return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) +} + +type ExtType string +func (ext ExtType) Hash() uint64 { + hash := sha512.Sum512([]byte(fmt.Sprintf("EXTENSION: %s", string(ext)))) + return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) +} + //Function to load an extension from bytes type ExtensionLoadFunc func(*Context, []byte) (Extension, error) +const ( + ACLExtType = ExtType("ACL") + ListenerExtType = ExtType("LISTENER") + LockableExtType = ExtType("LOCKABLE") + GQLExtType = ExtType("GQL") + GroupExtType = ExtType("GROUP") + ECDHExtType = ExtType("ECDH") + + GQLNodeType = NodeType("GQL") +) + // Information about a registered extension type ExtensionInfo struct { Load ExtensionLoadFunc @@ -101,6 +130,13 @@ func (ctx *Context) GetNode(id NodeID) (*Node, error) { return target, nil } +// Stop every running loop +func (ctx *Context) Stop() { + for _, node := range(ctx.Nodes) { + node.MsgChan <- Msg{ZeroID, StopSignal} + } +} + // Route a Signal to dest. Currently only local context routing is supported func (ctx *Context) Send(source NodeID, dest NodeID, signal Signal) error { target, err := ctx.GetNode(dest) diff --git a/gql.go b/gql.go index 0692954..d9150d5 100644 --- a/gql.go +++ b/gql.go @@ -30,7 +30,6 @@ import ( "encoding/pem" ) -const GQLNodeType = NodeType("GQL") type AuthReqJSON struct { Time time.Time `json:"time"` @@ -817,7 +816,6 @@ func (ext *GQLExt) Process(context *Context, princ_id NodeID, node *Node, signal return } -const GQLExtType = ExtType("gql_thread") func (ext *GQLExt) Type() ExtType { return GQLExtType } diff --git a/lockable.go b/lockable.go index be80e04..34b48c7 100644 --- a/lockable.go +++ b/lockable.go @@ -27,7 +27,6 @@ func LoadListenerExt(ctx *Context, data []byte) (Extension, error) { return NewListenerExt(j), nil } -const ListenerExtType = ExtType("LISTENER") func (listener *ListenerExt) Type() ExtType { return ListenerExtType } @@ -58,7 +57,6 @@ func (ext *ListenerExt) Serialize() ([]byte, error) { return json.MarshalIndent(ext.Buffer, "", " ") } -const LockableExtType = ExtType("LOCKABLE") func (ext *LockableExt) Type() ExtType { return LockableExtType } diff --git a/node.go b/node.go index 9b82474..3a7d741 100644 --- a/node.go +++ b/node.go @@ -129,9 +129,9 @@ func SoonestSignal(signals []QueuedSignal) (*QueuedSignal, <-chan time.Time) { } } -func RunNode(ctx *Context, node *Node) { +func runNode(ctx *Context, node *Node) { ctx.Log.Logf("node", "RUN_START: %s", node.ID) - err := NodeLoop(ctx, node) + err := nodeLoop(ctx, node) if err != nil { panic(err) } @@ -144,7 +144,7 @@ type Msg struct { } // Main Loop for Threads, starts a write context, so cannot be called from a write or read context -func NodeLoop(ctx *Context, node *Node) error { +func nodeLoop(ctx *Context, node *Node) error { started := node.Active.CompareAndSwap(false, true) if started == false { return fmt.Errorf("%s is already started, will not start again", node.ID) @@ -302,7 +302,7 @@ func NewNode(ctx *Context, id NodeID, node_type NodeType, queued_signals []Queue ctx.Nodes[id] = node WriteNode(ctx, node) - go RunNode(ctx, node) + go runNode(ctx, node) return node } @@ -557,7 +557,7 @@ func LoadNode(ctx * Context, id NodeID) (*Node, error) { ctx.Log.Logf("db", "DB_NODE_LOADED: %s", id) - go RunNode(ctx, node) + go runNode(ctx, node) return node, nil } @@ -603,24 +603,6 @@ func ACLList(list []*Node, resources []string) ACLMap { return reqs } -type NodeType string -func (node NodeType) Hash() uint64 { - hash := sha512.Sum512([]byte(fmt.Sprintf("NODE: %s", string(node)))) - return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) -} - -type PolicyType string -func (policy PolicyType) Hash() uint64 { - hash := sha512.Sum512([]byte(fmt.Sprintf("POLICY: %s", string(policy)))) - return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) -} - -type ExtType string -func (ext ExtType) Hash() uint64 { - hash := sha512.Sum512([]byte(fmt.Sprintf("EXTENSION: %s", string(ext)))) - return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) -} - type NodeMap map[NodeID]*Node type ACLInfo struct { diff --git a/policy.go b/policy.go index b3017f5..b2c5b80 100644 --- a/policy.go +++ b/policy.go @@ -5,6 +5,18 @@ import ( "fmt" ) +type PolicyType string +func (policy PolicyType) Hash() uint64 { + hash := sha512.Sum512([]byte(fmt.Sprintf("POLICY: %s", string(policy)))) + return binary.BigEndian.Uint64(hash[(len(hash)-9):(len(hash)-1)]) +} + +const ( + RequirementOfPolicyType = PolicyType("REQUIREMENT_OF") + PerNodePolicyType = PolicyType("PER_NODE") + AllNodesPolicyType = PolicyType("ALL_NODES") +) + type Policy interface { Serializable[PolicyType] Allows(principal_id NodeID, action SignalType, node *Node) error @@ -44,7 +56,6 @@ func (policy *RequirementOfPolicy) Allows(principal_id NodeID, action SignalType return fmt.Errorf("%s is not a requirement of %s", principal_id, node.ID) } -const RequirementOfPolicyType = PolicyType("REQUIREMENT_OF") type RequirementOfPolicy struct { AllNodesPolicy } @@ -113,7 +124,6 @@ type PerNodePolicy struct { NodeActions NodeActions `json:"node_actions"` } -const PerNodePolicyType = PolicyType("PER_NODE") func (policy *PerNodePolicy) Type() PolicyType { return PerNodePolicyType } @@ -136,7 +146,6 @@ type AllNodesPolicy struct { Actions Actions } -const AllNodesPolicyType = PolicyType("ALL_NODES") func (policy *AllNodesPolicy) Type() PolicyType { return AllNodesPolicyType } @@ -261,7 +270,6 @@ func LoadACLExt(ctx *Context, data []byte) (Extension, error) { return NewACLExt(policies...), nil } -const ACLExtType = ExtType("ACL") func (ext *ACLExt) Type() ExtType { return ACLExtType } diff --git a/user.go b/user.go index 2b66b1a..0543264 100644 --- a/user.go +++ b/user.go @@ -24,7 +24,6 @@ func (ext *ECDHExt) Process(ctx *Context, princ_id NodeID, node *Node, signal Si return } -const ECDHExtType = ExtType("ECDH") func (ext *ECDHExt) Type() ExtType { return ECDHExtType } @@ -75,7 +74,6 @@ type GroupExt struct { Members NodeMap } -const GroupExtType = ExtType("GROUP") func (ext *GroupExt) Type() ExtType { return GroupExtType }