forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
61 lines (53 loc) · 1.34 KB
/
connection.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
package gobot
import (
"errors"
"fmt"
"log"
)
// JSONConnection holds a JSON representation of a connection.
type JSONConnection struct {
Name string `json:"name"`
Adaptor string `json:"adaptor"`
}
type Connection Adaptor
type connections []Connection
// Len returns connections length
func (c *connections) Len() int {
return len(*c)
}
// Each calls function for each connection
func (c *connections) Each(f func(Connection)) {
for _, connection := range *c {
f(connection)
}
}
// Start initializes all the connections.
func (c *connections) Start() (errs []error) {
log.Println("Starting connections...")
for _, connection := range *c {
info := "Starting connection " + connection.Name()
if connection.Port() != "" {
info = info + " on port " + connection.Port()
}
log.Println(info + "...")
if errs = connection.Connect(); len(errs) > 0 {
for i, err := range errs {
errs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
}
return
}
}
return
}
// Finalize finishes all the connections.
func (c *connections) Finalize() (errs []error) {
for _, connection := range *c {
if cerrs := connection.Finalize(); cerrs != nil {
for i, err := range cerrs {
cerrs[i] = errors.New(fmt.Sprintf("Connection %q: %v", connection.Name(), err))
}
errs = append(errs, cerrs...)
}
}
return errs
}