yanic/respond/daemon/babel.go

55 lines
1.0 KiB
Go
Raw Normal View History

2019-05-18 23:33:13 +02:00
package respondd
import (
"bufio"
"fmt"
"io"
"net"
"time"
"github.com/Vivena/babelweb2/state"
2019-05-18 23:33:13 +02:00
"github.com/bdlm/log"
)
func (d *Daemon) babelConnect() {
var conn net.Conn
var err error
for {
log.Debug("Trying", d.Babel)
for {
conn, err = net.Dial("tcp6", d.Babel)
if err != nil {
log.Println(err)
time.Sleep(time.Second * 5)
} else {
break
}
}
log.Info("Connected to ", d.Babel)
closeConn := func() {
conn.Close()
log.Infof("Connection to %v closed\n", d.Babel)
}
2019-05-18 23:33:13 +02:00
fmt.Fprintf(conn, "monitor\n")
s, err := state.NewBabelState(bufio.NewReader(conn), 0)
2019-05-18 23:33:13 +02:00
if err == io.EOF {
log.Warnf("Something wrong with %v:\n\tcouldn't get router id.\n", d.Babel)
} else if err != nil {
// Don't you even dare to reconnect to this unholy node!
log.Warnf("Oh, boy! %v is doomed:\n\t%v.\t", d.Babel, err)
closeConn()
2019-05-18 23:33:13 +02:00
return
} else {
d.babelData = s
err := s.ListenHistory()
2019-05-18 23:33:13 +02:00
if err != nil {
log.Warnf("Babel listen stopped: %s", err)
}
d.babelData = nil
}
closeConn()
}
}