-
Notifications
You must be signed in to change notification settings - Fork 0
/
controledObject.go
113 lines (100 loc) · 2.69 KB
/
controledObject.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"GoConsoleBT/controller"
"errors"
)
var CommandChanelNotFoundError = errors.New("command chanel not found")
type ControlledObjectInterface interface {
Execute(command controller.Command) error
}
type ControlledObject struct {
Owner ControlledObjectInterface
dispatcherEnable bool
Control controller.Controller
terminator chan bool
}
func (receiver *ControlledObject) Execute(command controller.Command) error {
if receiver.Owner != nil {
if DEBUG_EXEC {
logger.Printf("exec: %T, %+v \n", command, command)
}
return receiver.Owner.Execute(command)
}
return nil
}
func (receiver *ControlledObject) Deactivate() error {
receiver.Control.Disable()
if receiver.dispatcherEnable {
close(receiver.terminator)
}
receiver.dispatcherEnable = false
return nil
}
func (receiver *ControlledObject) Activate() error {
if !receiver.dispatcherEnable {
if receiver.Control == nil {
logger.Println("command chanel not found")
return CommandChanelNotFoundError
}
receiver.terminator = make(chan bool)
go coCmdDispatcher(receiver, receiver.Control.GetCommandChanel(), receiver.terminator)
}
receiver.dispatcherEnable = true
if bc, ok := receiver.Control.(*BehaviorControl); ok {
if unit, ok := receiver.Owner.(*Unit); ok {
bc.AttachTo(unit) //todo simplify
}
}
receiver.Control.Enable()
return nil
}
func (receiver *ControlledObject) Free() error {
close(receiver.terminator)
return nil
}
func (receiver *ControlledObject) Copy() *ControlledObject {
instance := *receiver
instance.terminator = nil
instance.dispatcherEnable = false
if receiver.Control != nil {
switch receiver.Control.(type) {
case *controller.Control:
instance.Control = receiver.Control.(*controller.Control).Copy()
case *BehaviorControl:
instance.Control = receiver.Control.(*BehaviorControl).Copy()
default:
logger.Println("unknown type of Control")
}
}
if receiver.dispatcherEnable {
logger.Println("dispatcher already enable")
go coCmdDispatcher(receiver, receiver.Control.GetCommandChanel(), receiver.terminator)
}
return &instance
}
func NewControlledObject(cmd controller.Controller, owner ControlledObjectInterface) (*ControlledObject, error) {
instance := new(ControlledObject)
instance.Owner = owner
instance.Control = cmd
instance.dispatcherEnable = false
return instance, nil
}
func coCmdDispatcher(object ControlledObjectInterface, cmdEvents <-chan controller.Command, termEvents chan bool) {
if object == nil {
return
}
for {
select {
case cmd, ok := <-cmdEvents:
if !ok {
return
}
if DEBUG_EVENT {
logger.Printf("receive: %T, %+v \n", cmd, cmd)
}
object.Execute(cmd)
case <-termEvents:
return
}
}
}