/
plugin.go
94 lines (72 loc) · 2.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2016-2017 Christian Saide <supernomad>
// Licensed under the MPL-2.0, for details see https://github.com/supernomad/quantum/blob/master/LICENSE
package plugin
import (
"errors"
"github.com/supernomad/quantum/common"
)
const (
// CompressionPlugin configures and injects a compression based plugin.
CompressionPlugin = "compression"
// EncryptionPlugin configures and injects an encryption based plugin.
EncryptionPlugin = "encryption"
// MockPlugin configures and injects a mock plugin for testing.
MockPlugin = "mock"
)
const (
// CompressionPluginOrder is the location of the compression plugin in the overall plugins enabled within quantum.
CompressionPluginOrder = iota
// EncryptionPluginOrder is the location of the encryption plugin in the overall plugins enabled within quantum.
EncryptionPluginOrder
// MockPluginOrder is the location of the mock plugin in the overall plugins enabled within quantum.
MockPluginOrder
)
// Direction of the packet when supplied to the plugin in question.
type Direction int
const (
// Incoming a packet is coming from a remote node destined for the local node.
Incoming = iota
// Outgoing a packet is coming from the local node destined for a remote node.
Outgoing
)
// Plugin interface for a generic multi-queue network device.
type Plugin interface {
// Apply should apply the plugin to the specified payload and mapping.
Apply(direction Direction, payload *common.Payload, mapping *common.Mapping) (*common.Payload, *common.Mapping, bool)
// Close should gracefully destroy the plugin.
Close() error
// Name should return the name of the plugin.
Name() string
// Order returns the location of the specified plugin in the overall plugins enabled within quantum.
Order() int
}
// Plugins is a collection of plugin structs for quantum to use.
type Plugins []Plugin
// Len returns the number of plugins.
func (plugins Plugins) Len() int {
return len(plugins)
}
// Swap will swap the specified plugins in place.
func (plugins Plugins) Swap(i, j int) {
plugins[i], plugins[j] = plugins[j], plugins[i]
}
// Sorter is used to sort the various enabled plugins within quantum.
type Sorter struct {
Plugins
}
// Less returns whether or not the plugin at index 'i' is less than the that of the plugin at index 'j'.
func (sorter Sorter) Less(i, j int) bool {
return sorter.Plugins[i].Order() < sorter.Plugins[j].Order()
}
// New will generate a new Plugin struct based on the supplied device pluginType and user configuration.
func New(pluginType string, cfg *common.Config) (Plugin, error) {
switch pluginType {
case CompressionPlugin:
return newCompression(cfg)
case EncryptionPlugin:
return newEncryption(cfg)
case MockPlugin:
return newMock(cfg)
}
return nil, errors.New("specified plugin is not supported")
}