diff options
author | Chris <ccbrown112@gmail.com> | 2017-08-16 17:23:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-16 17:23:38 -0500 |
commit | f80d50adbddf55a043dfcab5b47d7c1e22749b7d (patch) | |
tree | 5deb606debb6322716c9cdcc6c58be4f68b74223 /plugin/rpcplugin/hooks.go | |
parent | 4f85ed985d478ddf6692fa4f7d8d98d2a412d18c (diff) | |
download | chat-f80d50adbddf55a043dfcab5b47d7c1e22749b7d.tar.gz chat-f80d50adbddf55a043dfcab5b47d7c1e22749b7d.tar.bz2 chat-f80d50adbddf55a043dfcab5b47d7c1e22749b7d.zip |
PLT-7407: Back-end plugin mechanism (#7177)
* begin backend plugin wip
* flesh out rpcplugin. everything done except for minor supervisor stubs
* done with basic plugin infrastructure
* simplify tests
* remove unused test lines
Diffstat (limited to 'plugin/rpcplugin/hooks.go')
-rw-r--r-- | plugin/rpcplugin/hooks.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/plugin/rpcplugin/hooks.go b/plugin/rpcplugin/hooks.go new file mode 100644 index 000000000..008730402 --- /dev/null +++ b/plugin/rpcplugin/hooks.go @@ -0,0 +1,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, + } +} |