OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / examples / basic / commons / greeter_interface.go
1 package example
2
3 import (
4         "net/rpc"
5
6         "github.com/hashicorp/go-plugin"
7 )
8
9 // Greeter is the interface that we're exposing as a plugin.
10 type Greeter interface {
11         Greet() string
12 }
13
14 // Here is an implementation that talks over RPC
15 type GreeterRPC struct{ client *rpc.Client }
16
17 func (g *GreeterRPC) Greet() string {
18         var resp string
19         err := g.client.Call("Plugin.Greet", new(interface{}), &resp)
20         if err != nil {
21                 // You usually want your interfaces to return errors. If they don't,
22                 // there isn't much other choice here.
23                 panic(err)
24         }
25
26         return resp
27 }
28
29 // Here is the RPC server that GreeterRPC talks to, conforming to
30 // the requirements of net/rpc
31 type GreeterRPCServer struct {
32         // This is the real implementation
33         Impl Greeter
34 }
35
36 func (s *GreeterRPCServer) Greet(args interface{}, resp *string) error {
37         *resp = s.Impl.Greet()
38         return nil
39 }
40
41 // This is the implementation of plugin.Plugin so we can serve/consume this
42 //
43 // This has two methods: Server must return an RPC server for this plugin
44 // type. We construct a GreeterRPCServer for this.
45 //
46 // Client must return an implementation of our interface that communicates
47 // over an RPC client. We return GreeterRPC for this.
48 //
49 // Ignore MuxBroker. That is used to create more multiplexed streams on our
50 // plugin connection and is a more advanced use case.
51 type GreeterPlugin struct {
52         // Impl Injection
53         Impl Greeter
54 }
55
56 func (p *GreeterPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
57         return &GreeterRPCServer{Impl: p.Impl}, nil
58 }
59
60 func (GreeterPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
61         return &GreeterRPC{client: c}, nil
62 }