-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic.go
More file actions
200 lines (159 loc) · 3.95 KB
/
public.go
File metadata and controls
200 lines (159 loc) · 3.95 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package goriffle
import (
"encoding/json"
"fmt"
"log"
"strconv"
)
var sess *session
var mem chan message
var kill chan uint
// Create a global session
func PConnector(url string, domain string) string {
s, err := Start(url, domain)
sess = s
if err != nil {
fmt.Println(err)
return "NO"
}
go internalReceive()
mem = make(chan message)
kill = make(chan uint)
return "YES"
}
// func Test() map[string]interface{} {
// return map[string]interface{}{
// "id": true,
// }
// }
// Might not have to use PSubscribe at all, actually.
// Make the regular methods return their ids and toggle on
// the custom receiver loop
func PSubscribe(s string) []byte {
// Ooh, this is tricky. Can't really have this here
e := sess.Subscribe(s, nil)
if e != nil {
fmt.Println("GR: error subscribing: ", e)
}
if i, _, ok := bindingForEndpoint(sess.events, s); ok {
fmt.Println("Subscribed for endpoint: ", int(i))
return marshall(i)
} else {
fmt.Println("GR: WARN-- no subscription found for ", s)
return nil
}
}
func PRegister(s string) []byte {
sess.Register(s, nil, map[string]interface{}{})
if i, _, ok := bindingForEndpoint(sess.procedures, s); ok {
fmt.Println("Registered for endpoint: ", int(i))
return marshall(i)
} else {
fmt.Println("GR: WARN-- no registration found for ", s)
return nil
}
}
// Cant return an int safely!
func PRecieve() []byte {
var m message
//fmt.Println("GR: receive loop")
select {
case m = <-mem:
switch msg := m.(type) {
case *event:
return marshall(map[string]interface{}{
"id": msg.Subscription,
"data": msg.Arguments,
})
case *invocation:
// fmt.Println("GR: invocation: ", msg)
return marshall(map[string]interface{}{
"id": msg.Registration,
"request": msg.Request,
"data": msg.Arguments,
})
default:
log.Println("unhandled message:", msg.messageType(), msg)
panic("Unhandled message!")
}
case <-kill:
fmt.Println("GR: kill received")
return nil
}
}
func PYield(args []byte) {
var dat map[string]interface{}
if err := json.Unmarshal(args, &dat); err != nil {
panic(err)
}
strRequest, status, result := dat["id"].(string), dat["ok"].(string), dat["result"].([]interface{})
j, _ := strconv.ParseUint(strRequest, 10, 64)
request := uint(j)
//fmt.Println("Yielding with: ", dat)
var tosend message
tosend = &yield{
Request: request,
Options: make(map[string]interface{}),
Arguments: result,
}
if status != "" {
tosend = &errorMessage{
Type: iNVOCATION,
Request: request,
Details: make(map[string]interface{}),
Arguments: result,
Error: status,
}
}
if err := sess.Send(tosend); err != nil {
log.Println("error sending message:", err)
}
}
func marshall(data interface{}) []byte {
if r, e := json.Marshal(data); e == nil {
return r
} else {
fmt.Println("GR: WARN-- unable to marshall args")
return nil
}
}
func internalReceive() {
fmt.Println("Internal receive")
c := sess
for msg := range c.connection.Receive() {
fmt.Println("GR: Internal MSG: ", msg)
switch msg := msg.(type) {
case *event:
if _, ok := c.events[msg.Subscription]; ok {
mem <- msg
} else {
log.Println("no handler registered for subscription:", msg.Subscription)
}
case *invocation:
if _, ok := c.procedures[msg.Registration]; ok {
mem <- msg
} else {
log.Println("no handler registered for registration:", msg.Registration)
}
case *registered:
c.notifyListener(msg, msg.Request)
case *subscribed:
c.notifyListener(msg, msg.Request)
case *unsubscribed:
c.notifyListener(msg, msg.Request)
case *unregistered:
c.notifyListener(msg, msg.Request)
case *result:
c.notifyListener(msg, msg.Request)
case *errorMessage:
c.notifyListener(msg, msg.Request)
case *goodbye:
// fmt.Println("GR: Goodbye!")
break
default:
log.Println("unhandled message:", msg.messageType(), msg)
panic("Unhandled message!")
}
}
fmt.Println("GR: Internal receive done")
}