summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/hooks.go
blob: 008730402972167c9ab687f2beea9901482c5fd5 (plain)
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package rpcplugin

import (
	"io"
	"net/rpc"

	"github.com/mattermost/platform/plugin"
)

type LocalHooks struct {
	hooks     plugin.Hooks
	muxer     *Muxer
	remoteAPI *RemoteAPI
}

func (h *LocalHooks) OnActivate(args int64, reply *struct{}) error {
	stream := h.muxer.Connect(args)
	if h.remoteAPI != nil {
		h.remoteAPI.Close()
	}
	h.remoteAPI = ConnectAPI(stream, h.muxer)
	return h.hooks.OnActivate(h.remoteAPI)
}

func (h *LocalHooks) OnDeactivate(args, reply *struct{}) error {
	err := h.hooks.OnDeactivate()
	if h.remoteAPI != nil {
		h.remoteAPI.Close()
		h.remoteAPI = nil
	}
	return err
}

type RemoteHooks struct {
	client    *rpc.Client
	muxer     *Muxer
	apiCloser io.Closer
}

func ServeHooks(hooks plugin.Hooks, conn io.ReadWriteCloser, muxer *Muxer) {
	server := rpc.NewServer()
	server.Register(&LocalHooks{
		hooks: hooks,
		muxer: muxer,
	})
	server.ServeConn(conn)
}

var _ plugin.Hooks = (*RemoteHooks)(nil)

func (h *RemoteHooks) OnActivate(api plugin.API) error {
	id, stream := h.muxer.Serve()
	if h.apiCloser != nil {
		h.apiCloser.Close()
	}
	h.apiCloser = stream
	go ServeAPI(api, stream, h.muxer)
	return h.client.Call("LocalHooks.OnActivate", id, nil)
}

func (h *RemoteHooks) OnDeactivate() error {
	return h.client.Call("LocalHooks.OnDeactivate", struct{}{}, nil)
}

func (h *RemoteHooks) Close() error {
	if h.apiCloser != nil {
		h.apiCloser.Close()
	}
	return h.client.Close()
}

func ConnectHooks(conn io.ReadWriteCloser, muxer *Muxer) *RemoteHooks {
	return &RemoteHooks{
		client: rpc.NewClient(conn),
		muxer:  muxer,
	}
}