Don't have time to explain :/

gql_cataclysm
noah metz 2023-11-11 13:53:41 -07:00
parent 520219f7b0
commit c54101f4a3
5 changed files with 83 additions and 9 deletions

@ -614,6 +614,11 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
return nil, err
}
err = ctx.RegisterPolicy(reflect.TypeOf(OwnerOfPolicy{}), OwnerOfPolicyType)
if err != nil {
return nil, err
}
err = ctx.RegisterPolicy(reflect.TypeOf(ParentOfPolicy{}), ParentOfPolicyType)
if err != nil {
return nil, err

@ -81,7 +81,7 @@ func NewEventStateSignal(source NodeID, state string, t time.Time) *EventStateSi
type EventControlSignal struct {
SignalHeader
Command string
Command string `gv:"command"`
}
func (signal EventControlSignal) String() string {

@ -1292,12 +1292,12 @@ func NewGQLExtContext() *GQLExtContext {
return name, nil
})
err = context.RegisterField(graphql.String, "State", EventExtType, "state", func(p graphql.ResolveParams, ctx *ResolveContext, val reflect.Value)(interface{}, error) {
err = context.RegisterField(graphql.String, "EventState", EventExtType, "state", func(p graphql.ResolveParams, ctx *ResolveContext, val reflect.Value)(interface{}, error) {
state := val.String()
return state, nil
})
err = context.RegisterInterface("Event", "EventNode", []string{"Node"}, []string{"EventName", "State"}, map[string]SelfField{}, map[string]ListField{})
err = context.RegisterInterface("Event", "EventNode", []string{"Node"}, []string{"EventName", "EventState"}, map[string]SelfField{}, map[string]ListField{})
if err != nil {
panic(err)
}
@ -1371,7 +1371,20 @@ func NewGQLExtContext() *GQLExtContext {
panic(err)
}
err = context.RegisterInterface("Lockable", "DefaultLockable", []string{"Node"}, []string{}, map[string]SelfField{
err = context.RegisterField(graphql.String, "LockableState", LockableExtType, "state",
func(p graphql.ResolveParams, ctx *ResolveContext, value reflect.Value)(interface{}, error) {
state, ok := value.Interface().(ReqState)
if ok == false {
return nil, fmt.Errorf("value is %+v, not ReqState", value.Type())
}
return ReqStateStrings[state], nil
})
if err != nil {
panic(err)
}
err = context.RegisterInterface("Lockable", "DefaultLockable", []string{"Node"}, []string{"LockableState"}, map[string]SelfField{
"Owner": {
"owner",
LockableExtType,
@ -1414,7 +1427,7 @@ func NewGQLExtContext() *GQLExtContext {
panic(err)
}
err = context.RegisterNodeType(GQLNodeType, "GQLServer", []string{"Node", "Lockable", "Group"}, []string{"Listen", "Owner", "Requirements", "SubGroups"})
err = context.RegisterNodeType(GQLNodeType, "GQLServer", []string{"Node", "Lockable", "Group"}, []string{"LockableState", "Listen", "Owner", "Requirements", "SubGroups"})
if err != nil {
panic(err)
}
@ -1434,6 +1447,11 @@ func NewGQLExtContext() *GQLExtContext {
panic(err)
}
err = context.AddSignalMutation("eventControl", "event_id", reflect.TypeOf(EventControlSignal{}))
if err != nil {
panic(err)
}
context.Subscription.AddFieldConfig("Self", &graphql.Field{
Type: context.Interfaces["Node"].Interface,
Subscribe: func(p graphql.ResolveParams) (interface{}, error) {

@ -75,9 +75,24 @@ func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeI
if info_found {
state, found := ext.Requirements[info.Destination]
if found == true {
ctx.Log.Logf("lockable", "got mapped response %+v for %+v in state %s", signal, info, ReqStateStrings[state])
switch state {
changes.Add(LockableExtType, "wait_infos")
ctx.Log.Logf("lockable", "got mapped response %+v for %+v in state %s while in %s", signal, info, ReqStateStrings[state], ReqStateStrings[ext.State])
switch ext.State {
case AbortingLock:
ext.Requirements[info.Destination] = Unlocked
all_unlocked := true
for _, state := range(ext.Requirements) {
if state != Unlocked {
all_unlocked = false
break
}
}
if all_unlocked == true {
changes.Add(LockableExtType, "state")
ext.State = Unlocked
}
case Locking:
changes.Add(LockableExtType, "state")
ext.State = AbortingLock
ext.Requirements[info.Destination] = Unlocked
for id, state := range(ext.Requirements) {
@ -247,7 +262,7 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID
}
lock_signal := NewLockSignal("lock")
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("lock", id, lock_signal, 5000*time.Millisecond)
ext.WaitInfos[lock_signal.Id] = node.QueueTimeout("lock", id, lock_signal, 500*time.Millisecond)
ext.Requirements[id] = Locking
messages = messages.Add(ctx, id, node, nil, lock_signal)
@ -255,7 +270,7 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID
}
default:
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(signal.ID(), "not_unlocked"))
ctx.Log.Logf("lockable", "Tried to lock %s while locked", node.ID)
ctx.Log.Logf("lockable", "Tried to lock %s while %s", node.ID, ext.State)
}
case "unlock":
if ext.State == Locked {
@ -300,6 +315,7 @@ func (ext *LockableExt) HandleTimeoutSignal(ctx *Context, node *Node, source Nod
wait_info, found := node.ProcessResponse(ext.WaitInfos, signal)
if found == true {
changes.Add(LockableExtType, "wait_infos")
state, found := ext.Requirements[wait_info.Destination]
if found == true {
ctx.Log.Logf("lockable", "%s timed out %s", wait_info.Destination, ReqStateStrings[state])
@ -364,6 +380,40 @@ func (ext *LockableExt) Process(ctx *Context, node *Node, source NodeID, signal
return messages, changes
}
type OwnerOfPolicy struct {
PolicyHeader
Rules Tree `gv:"rules"`
}
func NewOwnerOfPolicy(rules Tree) OwnerOfPolicy {
return OwnerOfPolicy{
PolicyHeader: NewPolicyHeader(),
Rules: rules,
}
}
func (policy OwnerOfPolicy) ContinueAllows(ctx *Context, current PendingACL, signal Signal) RuleResult {
return Deny
}
func (policy OwnerOfPolicy) Allows(ctx *Context, principal_id NodeID, action Tree, node *Node)(Messages, RuleResult) {
l_ext, err := GetExt[*LockableExt](node, LockableExtType)
if err != nil {
ctx.Log.Logf("lockable", "OwnerOfPolicy.Allows called on node without LockableExt")
return nil, Deny
}
if l_ext.Owner == nil {
return nil, Deny
}
if principal_id == *l_ext.Owner {
return nil, Allow
}
return nil, Deny
}
type RequirementOfPolicy struct {
PerNodePolicy
}

@ -238,6 +238,7 @@ var (
EventStateSignalType = NewSignalType("VEX_MATCH_STATUS")
MemberOfPolicyType = NewPolicyType("MEMBER_OF")
OwnerOfPolicyType = NewPolicyType("OWNER_OF")
ParentOfPolicyType = NewPolicyType("PARENT_OF")
RequirementOfPolicyType = NewPolicyType("REQUIEMENT_OF")
PerNodePolicyType = NewPolicyType("PER_NODE")