File: activeset.go

package info (click to toggle)
slinkwatch 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 180 kB
  • sloc: makefile: 17; sh: 14
file content (112 lines) | stat: -rw-r--r-- 3,054 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package slinkwatch

// This file is part of slinkwatch.
//
// slinkwatch is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// slinkwatch is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with slinkwatch; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

import (
	"bytes"
	"math"
	"runtime"
	"text/template"

	yaml "gopkg.in/yaml.v2"
)

// InterfaceInfo contains basic information about a specific interface entry.
type InterfaceInfo struct {
	Threads      int
	ClusterID    int
	ExtraOptions map[string]string
}

// ActiveSet contains descriptions of the currently connected interfaces.
type ActiveSet struct {
	Config *Config
	Ifaces map[string]InterfaceInfo
}

// MakeActiveSet returns a new empty ActiveSet.
func MakeActiveSet(config *Config) *ActiveSet {
	return &ActiveSet{
		Config: config,
		Ifaces: make(map[string]InterfaceInfo),
	}
}

// Reset clears the interfaces associated with an ActiveSet.
func (a *ActiveSet) Reset() {
	a.Ifaces = make(map[string]InterfaceInfo)
}

// String returns a string representation of an ActiveSet, listing its
// interfaces.
func (a *ActiveSet) String() string {
	out := ""
	i := 0
	if len(a.Ifaces) == 0 {
		return "<empty>"
	}
	for k := range a.Ifaces {
		out += k
		if i != len(a.Ifaces)-1 {
			out += "/"
			i++
		}
	}
	return out
}

// Add adds an interface to a given ActiveSet.
func (a *ActiveSet) Add(iface string) {
	if _, ok := a.Config.Ifaces[iface]; ok {
		a.Ifaces[iface] = InterfaceInfo{}
	}
	// from here we can assume that we only handle the intersection of the
	// configured interfaces and the observed interfaces
}

// Len returns the number of interfaces in the active set.
func (a *ActiveSet) Len() int {
	return len(a.Ifaces)
}

// ToYAML returns the YAML representation of an ActiveSet, given a template and
// a configuration with thread weights.
func (a *ActiveSet) ToYAML(tmpl *template.Template, config *Config) (string, error) {
	threads := runtime.NumCPU()
	totalweight := 0
	for intf := range a.Ifaces {
		totalweight += config.Ifaces[intf].ThreadWeight
	}
	for intf := range a.Ifaces {
		a.Ifaces[intf] = InterfaceInfo{
			Threads:      int(math.Ceil(float64(threads) * (float64(config.Ifaces[intf].ThreadWeight) / float64(totalweight)))),
			ClusterID:    config.Ifaces[intf].ClusterID,
			ExtraOptions: config.Ifaces[intf].ExtraOptions,
		}
	}
	buf := new(bytes.Buffer)
	err := tmpl.Execute(buf, a.Ifaces)
	if err != nil {
		return "", err
	}
	var t interface{}
	err = yaml.Unmarshal(buf.Bytes(), &t)
	if err != nil {
		return "", err
	}
	return buf.String(), nil
}