-
Notifications
You must be signed in to change notification settings - Fork 3
/
reconcile.go
103 lines (92 loc) · 2.96 KB
/
reconcile.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
101
102
103
package main
import (
"sort"
)
// Key = username@domain
// We use the <user> value from "sofia xmlstatus profile internal reg" to populate.
type Registrations map[string]KvBackendValue
// The format we receive from FreeSWITCH.
func generateCurrentRegistrationsType(users *[]string, advertise_ip string, advertise_port int) *Registrations {
result := make(Registrations)
for _, v := range *users {
// TODO: duplicate user handling?
result[v] = KvBackendValue{
Host: advertise_ip,
Port: advertise_port,
}
}
return &result
}
// The format we receive from a K/V backend.
func generateLastRegistrationsType(input *map[string]string) (*Registrations, error) {
result := make(Registrations)
for k, v := range *input {
parse_v, err := getKvBackendValueJsonType(v)
if err != nil {
return new(Registrations), err
}
result[k] = parse_v
}
return &result, nil
}
// Parses out multiple K/V backend result sets into just the user@domain list,
// and filter on this advertise IP and port only (this instance).
func generateRegistrationListForThisInstance(input *Registrations, advertise_ip string, advertise_port int) *Registrations {
result := make(Registrations)
for k, v := range *input {
if v.Host != advertise_ip || v.Port != advertise_port {
continue
}
result[k] = v
}
return &result
}
// add_registrations []string, remove_registrations []string
func reconcileRegistrations(last_active_registrations *Registrations, current_active_registrations *Registrations) (*[]string, *[]string, error) {
var add_registrations []string
var remove_registrations []string
// First in one direction.
for k1, _ := range *last_active_registrations {
exists_in_current := false
for k2, _ := range *current_active_registrations {
if k1 == k2 {
exists_in_current = true
break
}
}
if exists_in_current == false {
remove_registrations = append(remove_registrations, k1)
}
}
// And in reverse.
for k3, _ := range *current_active_registrations {
exists_in_last := false
for k4, _ := range *last_active_registrations {
if k3 == k4 {
exists_in_last = true
break
}
}
if exists_in_last == false {
add_registrations = append(add_registrations, k3)
}
}
// Reconcile our adds and removes, if we find any that are "added" and also "removed", they cancel eachother out and can be removed from both.
// This is normally a sign they already exist.
var add_registrations_results []string
var remove_registrations_results []string
for _, v5 := range add_registrations {
if stringInSlice(v5, remove_registrations) == false {
add_registrations_results = append(add_registrations_results, v5)
}
}
for _, v6 := range remove_registrations {
if stringInSlice(v6, add_registrations) == false {
remove_registrations_results = append(remove_registrations_results, v6)
}
}
// Sort the results
sort.Strings(add_registrations_results)
sort.Strings(remove_registrations_results)
return &add_registrations_results, &remove_registrations_results, nil
}