-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
72 lines (64 loc) · 1.6 KB
/
api.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
package zmey
import (
"log"
)
// API lets the process to communicate the data to the framework.
type API interface {
// Send should be called whenever a process needs to send a message to
// another process.
Send(to int, payload interface{})
// Return should be called whenever a process needs to return a call to its client.
Return(payload interface{})
// Trace used for logging
Trace(payload interface{})
// ReportError should be used for any errors to be escalated to the upper layer
// There is no specific error handling implemented, currently the errors are
// just printed via log package
ReportError(error)
}
// api implements exported API interface
type api struct {
// scale int
pid int
net *Net
returnC chan interface{}
traceC chan interface{}
debug bool
}
func (a *api) BindNet(net *Net) {
a.net = net
}
func (a *api) Send(to int, payload interface{}) {
if a.debug {
log.Printf("[%4d] Send: sending message %+v", a.pid, payload)
}
if a.net != nil {
err := a.net.Send(a.pid, to, payload)
if err != nil {
log.Printf("[%4d] Send: Error: %s", a.pid, err)
}
} else {
log.Printf("[%4d] Send: Error: network is nil", a.pid)
}
if a.debug {
log.Printf("[%4d] Send: done", a.pid)
}
}
func (a *api) Return(c interface{}) {
if a.debug {
log.Printf("[%4d] Return: returning call %+v", a.pid, c)
}
a.returnC <- c
if a.debug {
log.Printf("[%4d] Return: done", a.pid)
}
}
func (a *api) Trace(t interface{}) {
if a.debug {
log.Printf("[%4d] T: %+v", a.pid, t)
}
a.traceC <- t
}
func (a *api) ReportError(err error) {
log.Printf("[%4d] ReportError: %s", a.pid, err)
}