Moved ExtType and PolicyType definitions to one block

gql_cataclysm v0.2.1
noah metz 2023-07-27 23:15:58 -06:00
parent c763725a34
commit 027c3d4c96
6 changed files with 53 additions and 33 deletions

@ -7,9 +7,38 @@ import (
"runtime" "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 //Function to load an extension from bytes
type ExtensionLoadFunc func(*Context, []byte) (Extension, error) 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 // Information about a registered extension
type ExtensionInfo struct { type ExtensionInfo struct {
Load ExtensionLoadFunc Load ExtensionLoadFunc
@ -101,6 +130,13 @@ func (ctx *Context) GetNode(id NodeID) (*Node, error) {
return target, nil 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 // Route a Signal to dest. Currently only local context routing is supported
func (ctx *Context) Send(source NodeID, dest NodeID, signal Signal) error { func (ctx *Context) Send(source NodeID, dest NodeID, signal Signal) error {
target, err := ctx.GetNode(dest) target, err := ctx.GetNode(dest)

@ -30,7 +30,6 @@ import (
"encoding/pem" "encoding/pem"
) )
const GQLNodeType = NodeType("GQL")
type AuthReqJSON struct { type AuthReqJSON struct {
Time time.Time `json:"time"` Time time.Time `json:"time"`
@ -817,7 +816,6 @@ func (ext *GQLExt) Process(context *Context, princ_id NodeID, node *Node, signal
return return
} }
const GQLExtType = ExtType("gql_thread")
func (ext *GQLExt) Type() ExtType { func (ext *GQLExt) Type() ExtType {
return GQLExtType return GQLExtType
} }

@ -27,7 +27,6 @@ func LoadListenerExt(ctx *Context, data []byte) (Extension, error) {
return NewListenerExt(j), nil return NewListenerExt(j), nil
} }
const ListenerExtType = ExtType("LISTENER")
func (listener *ListenerExt) Type() ExtType { func (listener *ListenerExt) Type() ExtType {
return ListenerExtType return ListenerExtType
} }
@ -58,7 +57,6 @@ func (ext *ListenerExt) Serialize() ([]byte, error) {
return json.MarshalIndent(ext.Buffer, "", " ") return json.MarshalIndent(ext.Buffer, "", " ")
} }
const LockableExtType = ExtType("LOCKABLE")
func (ext *LockableExt) Type() ExtType { func (ext *LockableExt) Type() ExtType {
return LockableExtType return LockableExtType
} }

@ -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) ctx.Log.Logf("node", "RUN_START: %s", node.ID)
err := NodeLoop(ctx, node) err := nodeLoop(ctx, node)
if err != nil { if err != nil {
panic(err) 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 // 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) started := node.Active.CompareAndSwap(false, true)
if started == false { if started == false {
return fmt.Errorf("%s is already started, will not start again", node.ID) 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 ctx.Nodes[id] = node
WriteNode(ctx, node) WriteNode(ctx, node)
go RunNode(ctx, node) go runNode(ctx, node)
return node return node
} }
@ -557,7 +557,7 @@ func LoadNode(ctx * Context, id NodeID) (*Node, error) {
ctx.Log.Logf("db", "DB_NODE_LOADED: %s", id) ctx.Log.Logf("db", "DB_NODE_LOADED: %s", id)
go RunNode(ctx, node) go runNode(ctx, node)
return node, nil return node, nil
} }
@ -603,24 +603,6 @@ func ACLList(list []*Node, resources []string) ACLMap {
return reqs 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 NodeMap map[NodeID]*Node
type ACLInfo struct { type ACLInfo struct {

@ -5,6 +5,18 @@ import (
"fmt" "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 { type Policy interface {
Serializable[PolicyType] Serializable[PolicyType]
Allows(principal_id NodeID, action SignalType, node *Node) error 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) return fmt.Errorf("%s is not a requirement of %s", principal_id, node.ID)
} }
const RequirementOfPolicyType = PolicyType("REQUIREMENT_OF")
type RequirementOfPolicy struct { type RequirementOfPolicy struct {
AllNodesPolicy AllNodesPolicy
} }
@ -113,7 +124,6 @@ type PerNodePolicy struct {
NodeActions NodeActions `json:"node_actions"` NodeActions NodeActions `json:"node_actions"`
} }
const PerNodePolicyType = PolicyType("PER_NODE")
func (policy *PerNodePolicy) Type() PolicyType { func (policy *PerNodePolicy) Type() PolicyType {
return PerNodePolicyType return PerNodePolicyType
} }
@ -136,7 +146,6 @@ type AllNodesPolicy struct {
Actions Actions Actions Actions
} }
const AllNodesPolicyType = PolicyType("ALL_NODES")
func (policy *AllNodesPolicy) Type() PolicyType { func (policy *AllNodesPolicy) Type() PolicyType {
return AllNodesPolicyType return AllNodesPolicyType
} }
@ -261,7 +270,6 @@ func LoadACLExt(ctx *Context, data []byte) (Extension, error) {
return NewACLExt(policies...), nil return NewACLExt(policies...), nil
} }
const ACLExtType = ExtType("ACL")
func (ext *ACLExt) Type() ExtType { func (ext *ACLExt) Type() ExtType {
return ACLExtType return ACLExtType
} }

@ -24,7 +24,6 @@ func (ext *ECDHExt) Process(ctx *Context, princ_id NodeID, node *Node, signal Si
return return
} }
const ECDHExtType = ExtType("ECDH")
func (ext *ECDHExt) Type() ExtType { func (ext *ECDHExt) Type() ExtType {
return ECDHExtType return ECDHExtType
} }
@ -75,7 +74,6 @@ type GroupExt struct {
Members NodeMap Members NodeMap
} }
const GroupExtType = ExtType("GROUP")
func (ext *GroupExt) Type() ExtType { func (ext *GroupExt) Type() ExtType {
return GroupExtType return GroupExtType
} }