forked from ProtonMail/tobubus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
56 lines (48 loc) · 1.07 KB
/
example_test.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
package tobubus_test
import (
"fmt"
"github.com/shibukawa/tobubus"
)
type Calculator struct {
}
func (c Calculator) Fib(n int64) int64 {
if n < 2 {
return n
}
return c.Fib(n-2) + c.Fib(n-1)
}
func plugin(wait chan string) {
// First argument is pipe name
// Second argument is a ID of plugin
plugin, err := tobubus.NewPlugin("tobubus.test", "github.com/shibukawa/tobubus/example")
if err != nil {
wait <- "error"
}
// Start communication with host and start event loop
plugin.Publish("/calculator", &Calculator{})
err = plugin.Connect()
// Publish object. Now host can call this instance's method.
wait <- "ready client"
<-wait // wait finish
plugin.Close()
}
func Example() {
wait := make(chan string)
host := tobubus.NewHost("tobubus.test")
host.Listen()
defer host.Close()
go plugin(wait)
pStatus := <-wait // ready client
if pStatus == "error" {
return
}
// Call plugin's method
results, err := host.Call("/calculator", "Fib", int(10))
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(results[0].(int64))
// Output:
// 55
wait <- "finish"
}