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"`
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:12:13 +08:00
|
|
|
type Connection AdaptorInterface
|
|
|
|
|
2014-06-24 11:33:59 +08:00
|
|
|
type connections struct {
|
2014-07-03 08:12:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-03 08:12:13 +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
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:12:13 +08:00
|
|
|
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 {
|
2014-07-04 10:52:31 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|