forked from asynkron/protoactor-go
/
main.go
176 lines (151 loc) · 3.59 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"io"
"log"
"math"
"strconv"
"strings"
"github.com/chzyer/readline"
"encoding/json"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/AsynkronIT/protoactor-go/remote"
proto "github.com/gogo/protobuf/proto"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// Function constructor - constructs new function for listing given directory
var completer = readline.NewPrefixCompleter(
readline.PcItem("tell"),
readline.PcItem("watch"),
readline.PcItem("exit"),
)
func filterInput(r rune) (rune, bool) {
switch r {
// block CtrlZ feature
case readline.CharCtrlZ:
return r, false
}
return r, true
}
var echoPID *actor.PID
func main() {
logo := `
___ _ ___ _ ___
| _ \_ _ ___| |_ ___ / __| | |_ _|
| _/ '_/ _ \ _/ _ \| (__| |__ | |
|_| |_| \___/\__\___(_)___|____|___|
`
fmt.Println(logo)
remote.DefaultSerializerID = 1
remote.Start("127.0.0.1:0")
spawnEcho()
vars := make(map[string]string)
vars["%address%"] = actor.ProcessRegistry.Address
vars["%echo%"] = fmt.Sprintf(`{"Address":"%v", "Id":"echo"}`, actor.ProcessRegistry.Address)
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/readline.tmp",
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
FuncFilterInputRune: filterInput,
})
if err != nil {
panic(err)
}
defer l.Close()
log.SetOutput(l.Stderr())
for {
line, err := l.Readline()
if err == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
line = strings.TrimSpace(line)
for k, v := range vars {
line = strings.Replace(line, k, v, 1000)
}
switch {
case strings.HasPrefix(line, "tell "):
tell(line)
case strings.HasPrefix(line, "watch "):
watch(line)
case line == "exit":
goto exit
case line == "":
default:
log.Println("Unknown command :", strconv.Quote(line))
}
}
exit:
}
func spawnEcho() {
echoPID, _ = actor.SpawnNamed(actor.FromFunc(func(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *actor.Started:
fmt.Println("ECHO: Started")
case *watchRequest:
fmt.Printf("ECHO: Watching %v\n", msg.target.String())
ctx.Watch(msg.target)
case *actor.Terminated:
fmt.Printf("ECHO:Actor %v terminated \n", msg.Who.String())
default:
fmt.Printf("ECHO: %+v\n", msg)
}
}), "echo")
}
func watch(line string) {
parts := strings.SplitN(line, " ", 2)
if len(parts) != 2 {
fmt.Printf("Wrong number of arguments for `watch`. expected: pid\n")
} else {
pidStr := parts[1]
x := strings.SplitN(pidStr, "/", 2)
address := x[0]
id := x[1]
pid := actor.NewPID(address, id)
echoPID.Tell(&watchRequest{
target: pid,
})
}
}
func tell(line string) {
parts := strings.SplitN(line, " ", 4)
if len(parts) != 4 {
fmt.Printf("Wrong number of arguments for `tell`. expected: pid type-name json\n")
} else {
pidStr := parts[1]
typeNameStr := parts[2]
jsonStr := parts[3]
x := strings.SplitN(pidStr, "/", 2)
address := x[0]
id := x[1]
err := parseJson(jsonStr)
if err == nil {
m := &remote.JsonMessage{
Json: jsonStr,
TypeName: typeNameStr,
}
pid := actor.NewPID(address, id)
remote.SendMessage(pid, nil, m, nil, 1)
} else {
fmt.Printf("Invalid JSON payload: %v\n", err)
}
}
}
type watchRequest struct {
target *actor.PID
}
func parseJson(s string) error {
var js map[string]interface{}
return json.Unmarshal([]byte(s), &js)
}