forked from CyCoreSystems/dispatchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.go
100 lines (83 loc) · 2.2 KB
/
static.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
95
96
97
98
99
100
package main
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
var staticSetDefinitions StaticSetDefinitions
// StaticSetMember defines the parameters of a member of a static dispatcher set
type StaticSetMember struct {
Host string
Port string
}
func (s *StaticSetMember) String() string {
return fmt.Sprintf("%s:%s", s.Host, s.Port)
}
// StaticSetDefinition defines a static dispatcher set
type StaticSetDefinition struct {
id int
members []*StaticSetMember
}
// Set configures a static dispatcher set
func (s *StaticSetDefinition) Set(raw string) (err error) {
pieces := strings.Split(raw, "=")
if len(pieces) != 2 {
return errors.New("failed to parse static set definition")
}
s.id, err = strconv.Atoi(pieces[0])
if err != nil {
return errors.Errorf("failed to parse %s as an integer", pieces[0])
}
// Handle multiple comma-delimited arguments
hostList := strings.Split(pieces[1], ",")
for _, h := range hostList {
hostPieces := strings.Split(h, ":")
switch len(hostPieces) {
case 1:
s.members = append(s.members, &StaticSetMember{
Host: hostPieces[0],
Port: "5060",
})
case 2:
s.members = append(s.members, &StaticSetMember{
Host: hostPieces[0],
Port: hostPieces[1],
})
default:
return errors.Errorf("failed to parse static set member %s", h)
}
}
return nil
}
func (s *StaticSetDefinition) String() string {
return fmt.Sprintf("%d=%s", s.id, strings.Join(s.Members(), ","))
}
// Members returns the list of set members, formatted for direct inclusion in the dispatcher set
func (s *StaticSetDefinition) Members() (list []string) {
for _, m := range s.members {
list = append(list, m.String())
}
return
}
// StaticSetDefinitions is a list of static dispatcher sets
type StaticSetDefinitions struct {
list []*StaticSetDefinition
}
// String implements flag.Value
func (s *StaticSetDefinitions) String() string {
var list []string
for _, s := range s.list {
list = append(list, s.String())
}
return strings.Join(list, ",")
}
// Set implements flag.Value
func (s *StaticSetDefinitions) Set(raw string) error {
d := new(StaticSetDefinition)
if err := d.Set(raw); err != nil {
return err
}
s.list = append(s.list, d)
return nil
}