server.go 1.62 KB
Newer Older
“李磊”'s avatar
“李磊” committed
1
2
3
4
package main

import (
	"context"
Lei Li's avatar
Lei Li committed
5
	"time"
“李磊”'s avatar
“李磊” committed
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

	"linkfog.com/pluginx/pluginrpc"
	pb "linkfog.com/pluginx/proto"
	"linkfog.com/public/lib/l"
)

type pluginServer struct {
	pb.UnimplementedPluginServer
}

func newServer() *pluginServer {
	s := &pluginServer{}
	return s
}

func (s *pluginServer) Call(ctx context.Context, req *pb.Req) (*pb.Res, error) {
	res := pluginrpc.NewRes(req, 0, "success", []byte("pong"))
	return res, nil
}

func (s *pluginServer) SendFile(stream pb.Plugin_SendFileServer) error {
	return pluginrpc.SendFileHelper(stream, SendFileHandle)
}

func SendFileHandle(fs *pb.FileStream, filePath string) {
	l.Info("recv file header:", fs.Header)
	l.Infof("recv file name:%s, purpose:%s, size:%d, path:%s", fs.Name, fs.Purpose, fs.TotalSize, filePath)
}

Lei Li's avatar
Lei Li committed
35
36
37
38
39
40
41
42
43
44
func (s *pluginServer) Chat(req *pb.Req, stream pb.Plugin_ChatServer) error {
	moduleName := req.GetHeader().GetTo()
	l.Infof("RPC:start chat with %s", moduleName)

	ch = make(chan *pb.Res, 50)
	return pluginrpc.ChatHelper(req, stream, ch)
}

var ch chan *pb.Res

“李磊”'s avatar
“李磊” committed
45
46
47
48
49
50
51
52
53
func main() {
	pluginServer := newServer()
	grpcServerHelper, err := pluginrpc.NewPluginGrpcServer("/tmp/unix_domain.sock", pluginServer)
	if err != nil {
		l.Error("NewPluginGrpcServer err:", err)
		return
	}
	defer grpcServerHelper.Close()

Lei Li's avatar
Lei Li committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	time.Sleep(10 * time.Second)
	go func() {
		msg := &pb.Res{Header: &pb.Header{
			UUID:      "123456",
			Timestamp: time.Now().Unix(),
			From:      "test-plugin",
			To:        "agent",
			Func:      "",
		}, Code: 0, Desc: "chat info",
		}
		for {
			msg.Header.Timestamp = time.Now().Unix()
			msg.Code += 1
			ch <- msg
			time.Sleep(5 * time.Second)
		}
	}()

“李磊”'s avatar
“李磊” committed
72
73
	select {}
}