提交 2125d96e 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

plugin communication sample

上级 2d857816
package main
import (
zapPlugin "github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/plugin"
zapService "github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/service"
"github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/shared"
"github.com/hashicorp/go-plugin"
)
func main() {
zapPlugin := zapShared.ZapPlugin{
zapPlugin := zapPlugin.ZapPlugin{
Impl: &zapService.ZapService{},
}
......
package zapShared
package zapPlugin
import (
"context"
......@@ -7,10 +7,12 @@ import (
)
// GRPCClient is an implementation of ZapInterface that talks over RPC.
type GRPCClient struct{ client zapProto.ZapClient }
type GRPCClient struct {
Client zapProto.ZapClient
}
func (m *GRPCClient) Put(key string, value []byte) error {
_, err := m.client.Put(context.Background(), &zapProto.PutRequest{
_, err := m.Client.Put(context.Background(), &zapProto.PutRequest{
Key: key,
Value: value,
})
......@@ -18,7 +20,7 @@ func (m *GRPCClient) Put(key string, value []byte) error {
}
func (m *GRPCClient) Get(key string) ([]byte, error) {
resp, err := m.client.Get(context.Background(), &zapProto.GetRequest{
resp, err := m.Client.Get(context.Background(), &zapProto.GetRequest{
Key: key,
})
if err != nil {
......
package zapShared
package zapPlugin
import (
"context"
......@@ -25,5 +25,5 @@ func (p *ZapPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error
}
func (p *ZapPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{client: zapProto.NewZapClient(c)}, nil
return &GRPCClient{Client: zapProto.NewZapClient(c)}, nil
}
package zapShared
import (
zapPlugin "github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/plugin"
"github.com/hashicorp/go-plugin"
)
......@@ -8,6 +9,10 @@ const (
PluginNameZap = "ZAP"
)
var PluginMap = map[string]plugin.Plugin{
PluginNameZap: &zapPlugin.ZapPlugin{},
}
// Handshake is a common handshake that is shared by plugin and host.
var Handshake = plugin.HandshakeConfig{
// This isn't required when using VersionedPlugins
......@@ -15,8 +20,3 @@ var Handshake = plugin.HandshakeConfig{
MagicCookieKey: "DEEPTEST_PLUGIN",
MagicCookieValue: "hello",
}
// PluginMap is the map of plugins we can dispense.
var PluginMap = map[string]plugin.Plugin{
PluginNameZap: &ZapPlugin{},
}
......@@ -19,9 +19,9 @@ type PluginService struct {
func (s *PluginService) Start() (err error) {
s.zapClient = plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: zapShared.Handshake,
Plugins: zapShared.PluginMap,
Cmd: shellUtils.GetCmd(ZapPath),
HandshakeConfig: zapShared.Handshake,
AllowedProtocols: []plugin.Protocol{plugin.ProtocolNetRPC, plugin.ProtocolGRPC},
})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册