hybridgroup.gobot/connection.go

59 lines
1.1 KiB
Go
Raw Normal View History

2014-04-30 23:10:44 +08:00
package gobot
2014-04-30 04:20:32 +08:00
import (
"errors"
"log"
)
2014-06-11 06:16:11 +08:00
type JSONConnection struct {
2014-05-16 02:50:45 +08:00
Name string `json:"name"`
Port string `json:"port"`
Adaptor string `json:"adaptor"`
}
type Connection AdaptorInterface
2014-06-24 11:33:59 +08:00
type connections struct {
connections []Connection
2014-06-24 11:33:59 +08:00
}
func (c *connections) Len() int {
return len(c.connections)
2014-04-30 23:10:44 +08:00
}
func (c *connections) Add(a Connection) Connection {
2014-06-24 11:33:59 +08:00
c.connections = append(c.connections, a)
return a
}
func (c *connections) Each(f func(Connection)) {
2014-06-24 11:33:59 +08:00
for _, connection := range c.connections {
f(connection)
}
}
2014-04-30 04:20:32 +08:00
// Start() starts all the connections.
func (c connections) Start() error {
var err error
log.Println("Starting connections...")
2014-06-24 11:33:59 +08:00
for _, connection := range c.connections {
info := "Starting connection " + connection.Name()
if connection.Port() != "" {
info = info + " on Port " + connection.Port()
}
log.Println(info + "...")
2014-04-30 04:20:32 +08:00
if connection.Connect() == false {
err = errors.New("Could not start connection")
break
}
}
return err
}
// Filanize() finalizes all the connections.
func (c connections) Finalize() {
2014-06-24 11:33:59 +08:00
for _, connection := range c.connections {
2014-04-30 04:20:32 +08:00
connection.Finalize()
}
}