OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / examples / bidirectional / shared / interface.go
1 // Package shared contains shared data between the host and plugins.
2 package shared
3
4 import (
5         "golang.org/x/net/context"
6         "google.golang.org/grpc"
7
8         "github.com/hashicorp/go-plugin"
9         "github.com/hashicorp/go-plugin/examples/bidirectional/proto"
10 )
11
12 // Handshake is a common handshake that is shared by plugin and host.
13 var Handshake = plugin.HandshakeConfig{
14         ProtocolVersion:  1,
15         MagicCookieKey:   "BASIC_PLUGIN",
16         MagicCookieValue: "hello",
17 }
18
19 // PluginMap is the map of plugins we can dispense.
20 var PluginMap = map[string]plugin.Plugin{
21         "counter": &CounterPlugin{},
22 }
23
24 type AddHelper interface {
25         Sum(int64, int64) (int64, error)
26 }
27
28 // KV is the interface that we're exposing as a plugin.
29 type Counter interface {
30         Put(key string, value int64, a AddHelper) error
31         Get(key string) (int64, error)
32 }
33
34 // This is the implementation of plugin.Plugin so we can serve/consume this.
35 // We also implement GRPCPlugin so that this plugin can be served over
36 // gRPC.
37 type CounterPlugin struct {
38         plugin.NetRPCUnsupportedPlugin
39         // Concrete implementation, written in Go. This is only used for plugins
40         // that are written in Go.
41         Impl Counter
42 }
43
44 func (p *CounterPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
45         proto.RegisterCounterServer(s, &GRPCServer{
46                 Impl:   p.Impl,
47                 broker: broker,
48         })
49         return nil
50 }
51
52 func (p *CounterPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
53         return &GRPCClient{
54                 client: proto.NewCounterClient(c),
55                 broker: broker,
56         }, nil
57 }
58
59 var _ plugin.GRPCPlugin = &CounterPlugin{}