client.go 1.92 KB
Newer Older
“李磊”'s avatar
“李磊” committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
	"context"
	"path/filepath"
	"time"

	"linkfog.com/public/lib/l"

	"linkfog.com/pluginx/pluginrpc"
)

func main() {
	pluginConf := make(map[string]string)
	pluginConf["test-plugin"] = "/tmp/unix_domain.sock"
	client, err := pluginrpc.NewPluginGrpcClient(pluginConf)
	if err != nil {
		l.Error("NewGrpcClientHelper err:", err)
		return
	}
	defer client.Close()

	call(client)
	sendFile(client)
Lei Li's avatar
Lei Li committed
25
	chat(client)
“李磊”'s avatar
“李磊” committed
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
}

func call(client *pluginrpc.PluginGrpcClient) {
	req := pluginrpc.NewReq("agent", "test-plugin", "Ping", []byte("ping"))
	l.Info("Call req header:", req.Header)
	l.Info("Call req data:", string(req.Data))
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	res, err := client.Call(ctx, req)
	if err != nil {
		l.Error("Call err:", err)
		return
	}
	l.Info("Call res header:", res.Header)
	l.Infof("Call res code:%d, desc:%s, data:%s", res.Code, res.Desc, string(res.Data))
}

func sendFile(client *pluginrpc.PluginGrpcClient) {
	filePath := "client"
	fs := pluginrpc.NewFileStream("agent", "test-plugin", "SendFile", filepath.Base(filePath), "testdata")
	l.Info("SendFile req header:", fs.Header)
	l.Infof("SendFile req name:%s, purpose:%s", fs.Name, fs.Purpose)
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	res, err := client.SendFile(ctx, fs, filePath)
	if err != nil {
		l.Error("SendFile err:", err)
		return
	}
	l.Info("SendFile res header:", res.Header)
	l.Infof("SendFile res code:%d, desc:%s, data:%s", res.Code, res.Desc, string(res.Data))
}
Lei Li's avatar
Lei Li committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

func chat(client *pluginrpc.PluginGrpcClient) {
	req := pluginrpc.NewReq("agent", "test-plugin", "chat", []byte("chat"))
	chatClient, err := client.Chat(context.Background(), req)
	if err != nil {
		l.Error("ChatClient err:", err)
		return
	}
	for {
		res, err := chatClient.Recv()
		if err != nil {
			l.Error("ChatClient receive err:", err)
			return
		}
		l.Infof("receive message: %v", res)
	}
}