This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
action_send.go
78 lines (66 loc) · 1.6 KB
/
action_send.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
package holochain
import (
peer "github.com/libp2p/go-libp2p-peer"
"reflect"
"time"
)
//------------------------------------------------------------
// Send
type Callback struct {
Function string
ID string
zomeType string
}
type SendOptions struct {
Callback *Callback
Timeout int
}
type ActionSend struct {
to peer.ID
msg AppMsg
options *SendOptions
}
type APIFnSend struct {
action ActionSend
}
func (fn *APIFnSend) Name() string {
return "send"
}
func (fn *APIFnSend) Args() []Arg {
return []Arg{{Name: "to", Type: HashArg}, {Name: "msg", Type: MapArg}, {Name: "options", Type: MapArg, MapType: reflect.TypeOf(SendOptions{}), Optional: true}}
}
func (fn *APIFnSend) Call(h *Holochain) (response interface{}, err error) {
var r interface{}
var timeout time.Duration
a := &fn.action
if a.options != nil {
timeout = time.Duration(a.options.Timeout) * time.Millisecond
}
msg := h.node.NewMessage(APP_MESSAGE, a.msg)
if a.options != nil && a.options.Callback != nil {
err = h.SendAsync(ActionProtocol, a.to, msg, a.options.Callback, timeout)
} else {
r, err = h.Send(h.node.ctx, ActionProtocol, a.to, msg, timeout)
if err == nil {
response = r.(AppMsg).Body
}
}
return
}
func (a *ActionSend) Name() string {
return "send"
}
func (a *ActionSend) Receive(dht *DHT, msg *Message) (response interface{}, err error) {
t := msg.Body.(AppMsg)
var r Ribosome
r, _, err = dht.h.MakeRibosome(t.ZomeType)
if err != nil {
return
}
rsp := AppMsg{ZomeType: t.ZomeType}
rsp.Body, err = r.Receive(peer.IDB58Encode(msg.From), t.Body)
if err == nil {
response = rsp
}
return
}