-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.go
69 lines (59 loc) · 1.53 KB
/
vm.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
67
68
69
package contract
import (
"errors"
"sync"
log "github.com/ampchain/log15"
"github.com/ampchain/go-amp/xmodel"
)
var (
// ErrVMNotExist is returned when found vm not exist
ErrVMNotExist = errors.New("Vm not exist in vm manager")
)
// ContextConfig define the config of context
type ContextConfig struct {
XMCache *xmodel.XMCache
Initiator string
AuthRequire []string
ContractName string
ResourceLimits Limits
}
// VirtualMachine define virtual machine interface
type VirtualMachine interface {
GetName() string
NewContext(*ContextConfig) (Context, error)
}
// Context define context interface
type Context interface {
Invoke(method string, args map[string][]byte) ([]byte, error)
ResourceUsed() Limits
Release() error
}
// VMManager define VMManager type
type VMManager struct {
lock *sync.Mutex
vms map[string]VirtualMachine
logger log.Logger
}
// NewVMManager new an instance of VMManager
func NewVMManager(logger log.Logger) (*VMManager, error) {
vmMgr := &VMManager{
lock: new(sync.Mutex),
vms: map[string]VirtualMachine{},
logger: logger,
}
return vmMgr, nil
}
// RegisterVM register an instance of VM into VMManager
func (vmMgr *VMManager) RegisterVM(module string, vm VirtualMachine) error {
vmMgr.lock.Lock()
defer vmMgr.lock.Unlock()
vmMgr.vms[module] = vm
return nil
}
// GetVM return specific virtual machine instance
func (vmMgr *VMManager) GetVM(module string) (VirtualMachine, error) {
if vmMgr.vms[module] == nil {
return nil, ErrVMNotExist
}
return vmMgr.vms[module], nil
}