-
Notifications
You must be signed in to change notification settings - Fork 23
/
plugin.go
66 lines (51 loc) · 1.38 KB
/
plugin.go
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
// SPDX-FileCopyrightText: 2021 The Go-SSB Authors
//
// SPDX-License-Identifier: MIT
package ssb
import (
"net"
"sync"
"github.com/ssbc/go-muxrpc/v2"
)
type Plugin interface {
// Name returns the name and version of the plugin.
// format: name-1.0.2
Name() string
// Method returns the preferred method of the call
Method() muxrpc.Method
// Handler returns the muxrpc handler for the plugin
Handler() muxrpc.Handler
}
type PluginManager interface {
Register(Plugin)
MakeHandler(conn net.Conn) (muxrpc.Handler, error)
}
type pluginManager struct {
regLock sync.Mutex // protects the map
plugins map[string]Plugin
}
func NewPluginManager() PluginManager {
return &pluginManager{
plugins: make(map[string]Plugin),
}
}
func (pmgr *pluginManager) Register(p Plugin) {
// access race
pmgr.regLock.Lock()
defer pmgr.regLock.Unlock()
pmgr.plugins[p.Method().String()] = p
}
func (pmgr *pluginManager) MakeHandler(conn net.Conn) (muxrpc.Handler, error) {
// TODO: add authorization requirements check to plugin so we can call it here
// e.g. only allow some peers to make certain requests
pmgr.regLock.Lock()
defer pmgr.regLock.Unlock()
h := muxrpc.HandlerMux{}
// var hs []muxrpc.NamedHandler
for _, p := range pmgr.plugins {
h.Register(p.Method(), p.Handler())
// hs = append(hs, muxrpc.NamedHandler{p.Method(), p.Handler()})
}
// h.RegisterAll(hs...)
return &h, nil
}