-
Notifications
You must be signed in to change notification settings - Fork 59
/
example_test.go
62 lines (54 loc) · 1.94 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
57
58
59
60
61
62
package pie_test
import (
"log"
"os"
"strings"
"net/rpc/jsonrpc"
"github.com/natefinch/pie"
)
// This function should be called from the master program that wants to run
// plugins to extend its functionality.
//
// This example shows the master program starting a plugin at path
// "/var/lib/foo", using JSON-RPC, and writing its output to this application's
// Stderr. The application can then call methods on the rpc client returned
// using the standard rpc pattern.
func ExampleStartProviderCodec() {
client, err := pie.StartProviderCodec(jsonrpc.NewClientCodec, os.Stderr, "/var/lib/foo")
if err != nil {
log.Fatalf("failed to load foo plugin: %s", err)
}
var reply string
client.Call("Foo.ToUpper", "something", &reply)
}
// This function should be called from the plugin program that wants to provide
// functionality for the master program.
//
// This example shows the plugin starting a JSON-RPC server to be accessed by
// the master program. Server.ServeCodec() will block forever, so it is common
// to simply put this at the end of the plugin's main function.
func ExampleProvider_ServeCodec() {
p := pie.NewProvider()
if err := p.RegisterName("Foo", API{}); err != nil {
log.Fatalf("can't register api: %s", err)
}
p.ServeCodec(jsonrpc.NewServerCodec)
}
// This function should be called from the plugin program that wants to consume
// an API from the master program.
//
// This example shows the plugin creating a JSON-RPC client talks to the host
// application.
func ExampleNewConsumerCodec() {
client := pie.NewConsumerCodec(jsonrpc.NewClientCodec)
var reply string
client.Call("Foo.ToUpper", "something", &reply)
}
// API is an example type to show how to serve methods over RPC.
type API struct{}
// ToUpper is an example function that gets served over RPC. See net/rpc for
// details on how to server functionality over RPC.
func (API) ToUpper(input string, output *string) error {
*output = strings.ToUpper(input)
return nil
}