This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdnode.go
More file actions
executable file
·171 lines (152 loc) · 3.59 KB
/
Copy pathdnode.go
File metadata and controls
executable file
·171 lines (152 loc) · 3.59 KB
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
package dnode
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"sync"
)
type DNode struct {
SendChan chan []byte
Closed bool
OnRemote func(remote Remote)
OnReady func()
OnRootMethod func(method string, args *Partial)
closeMutex sync.Mutex
callbacks []reflect.Value
}
type message struct {
Method interface{} `json:"method"`
Arguments *Partial `json:"arguments"`
Links []string `json:"links"`
Callbacks map[string]([]string) `json:"callbacks"`
}
type Remote map[string]interface{}
func New() *DNode {
d := DNode{
make(chan []byte),
false,
nil, nil, nil,
sync.Mutex{},
make([]reflect.Value, 0),
}
return &d
}
func (d *DNode) SendRemote(object interface{}) {
d.Send("methods", object)
}
func (d *DNode) Send(method interface{}, arguments ...interface{}) {
callbacks := make(map[string]([]string))
d.collectCallbacks(arguments, make([]string, 0), callbacks)
rawArgs, err := json.Marshal(arguments)
if err != nil {
panic(err)
}
message := message{
method,
&Partial{Raw: rawArgs},
[]string{},
callbacks,
}
data, err := json.Marshal(message)
if err != nil {
panic(err)
}
d.closeMutex.Lock()
defer d.closeMutex.Unlock()
if !d.Closed {
d.SendChan <- data
}
}
func (d *DNode) Close() {
d.closeMutex.Lock()
defer d.closeMutex.Unlock()
d.Closed = true
close(d.SendChan)
}
func (d *DNode) collectCallbacks(rawObj interface{}, path []string, callbackMap map[string]([]string)) {
switch obj := rawObj.(type) {
case nil:
// skip
case []interface{}:
for i, v := range obj {
d.collectCallbacks(v, append(path, strconv.Itoa(i)), callbackMap)
}
case map[string]interface{}:
for key, value := range obj {
v := reflect.ValueOf(value)
if v.Kind() == reflect.Func {
d.registerCallback(key, v, path, callbackMap)
delete(obj, key)
}
}
default:
v := reflect.ValueOf(obj)
for i := 0; i < v.NumMethod(); i++ {
if v.Type().Method(i).PkgPath == "" { // exported
name := v.Type().Method(i).Name
name = strings.ToLower(name[0:1]) + name[1:]
d.registerCallback(name, v.Method(i), path, callbackMap)
}
}
}
}
func (d *DNode) registerCallback(name string, callback reflect.Value, path []string, callbackMap map[string]([]string)) {
pathCopy := make([]string, len(path)+1)
copy(pathCopy, path)
pathCopy[len(path)] = name
callbackMap[strconv.Itoa(len(d.callbacks))] = pathCopy
d.callbacks = append(d.callbacks, callback)
}
func (d *DNode) ProcessMessage(data []byte) {
var m message
err := json.Unmarshal(data, &m)
if err != nil {
panic(err)
}
for id, path := range m.Callbacks {
methodId, err := strconv.Atoi(id)
if err != nil {
panic(err)
}
callback := Callback(func(args ...interface{}) {
d.Send(methodId, args...)
})
m.Arguments.callbacks = append(m.Arguments.callbacks, CallbackSpec{path, callback})
}
if m.Method == "methods" {
var args [](map[string]interface{})
err = m.Arguments.Unmarshal(&args)
if err != nil {
panic(err)
}
if d.OnRemote != nil {
d.OnRemote(args[0])
}
if d.OnReady != nil {
d.OnReady()
}
return
}
if index, err := strconv.Atoi(fmt.Sprint(m.Method)); err == nil {
args, err := m.Arguments.Array()
if err != nil {
panic(err)
}
if index < 0 || index >= len(d.callbacks) {
return
}
callArgs := make([]reflect.Value, len(args))
for i, v := range args {
callArgs[i] = reflect.ValueOf(v)
}
d.callbacks[index].Call(callArgs)
return
}
if d.OnRootMethod != nil {
d.OnRootMethod(fmt.Sprint(m.Method), m.Arguments)
return
}
panic(fmt.Sprintf("Unknown method: %v.", m.Method))
}