diff --git a/respond/daemon/babel.go b/respond/daemon/babel.go index 4a19094..5a45233 100644 --- a/respond/daemon/babel.go +++ b/respond/daemon/babel.go @@ -7,7 +7,7 @@ import ( "net" "time" - "github.com/Vivena/babelweb2/parser" + "github.com/Vivena/babelweb2/state" "github.com/bdlm/log" ) @@ -31,21 +31,19 @@ func (d *Daemon) babelConnect() { conn.Close() log.Infof("Connection to %v closed\n", d.Babel) } - defer closeConn() + fmt.Fprintf(conn, "monitor\n") - r := bufio.NewReader(conn) - s := parser.NewScanner(r) - desc := parser.NewBabelDesc() - err = desc.Fill(s) + s, err := state.NewBabelState(bufio.NewReader(conn), 0) 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() return } else { - d.babelData = desc - err := d.babelDescListen(s) + d.babelData = s + err := s.ListenHistory() if err != nil { log.Warnf("Babel listen stopped: %s", err) } @@ -54,24 +52,3 @@ func (d *Daemon) babelConnect() { closeConn() } } - -func (d *Daemon) babelDescListen(s *parser.Scanner) error { - for { - upd, err := d.babelData.ParseAction(s) - if err != nil && err != io.EOF && err.Error() != "EOL" { - return err - } - if err == io.EOF { - break - } - //TODO maybe keep upd.action != none - if !(d.babelData.CheckUpdate(upd)) { - continue - } - err = d.babelData.Update(upd) - if err != nil { - return err - } - } - return nil -} diff --git a/respond/daemon/config.go b/respond/daemon/config.go index 01d6a0c..9bff26e 100644 --- a/respond/daemon/config.go +++ b/respond/daemon/config.go @@ -7,7 +7,7 @@ import ( "github.com/FreifunkBremen/yanic/data" "github.com/FreifunkBremen/yanic/lib/duration" - babelParser "github.com/Vivena/babelweb2/parser" + babelState "github.com/Vivena/babelweb2/state" ) func trim(s string) string { @@ -24,7 +24,7 @@ type Daemon struct { Batman []string `toml:"batman"` Babel string `toml:"babel"` - babelData *babelParser.BabelDesc `toml:"-"` + babelData *babelState.BabelState `toml:"-"` dataByInterface map[string]*data.ResponseData diff --git a/respond/daemon/neighbours.go b/respond/daemon/neighbours.go index 57eb8b3..ee7e20e 100644 --- a/respond/daemon/neighbours.go +++ b/respond/daemon/neighbours.go @@ -30,14 +30,13 @@ func (d *Daemon) updateNeighbours(iface string, resp *data.ResponseData) { } resp.Neighbours.Babel = make(map[string]data.BabelNeighbours) - d.babelData.Iter(func(bu parser.BabelUpdate) error { - sbu := bu.ToSUpdate() - if sbu.Table != "interface" { + d.babelData.Iter(func(t parser.Transition) error { + if t.Table != "interface" { return nil } - if sbu.EntryData["up"].(bool) { - addr := sbu.EntryData["ipv6"].(string) - resp.Neighbours.Babel[string(sbu.Entry)] = data.BabelNeighbours{ + if t.Data["up"].(bool) { + addr := t.Data["ipv6"].(string) + resp.Neighbours.Babel[string(t.Field)] = data.BabelNeighbours{ Protocol: "babel", LinkLocalAddress: addr, Neighbours: make(map[string]data.BabelLink), @@ -46,24 +45,23 @@ func (d *Daemon) updateNeighbours(iface string, resp *data.ResponseData) { return nil }) - d.babelData.Iter(func(bu parser.BabelUpdate) error { - sbu := bu.ToSUpdate() - if sbu.Table != "neighbour" { + d.babelData.Iter(func(t parser.Transition) error { + if t.Table != "neighbour" { return nil } - ifname, ok := sbu.EntryData["if"].(string) + ifname, ok := t.Data["if"].(string) if !ok { return errors.New("neighbour without if") } - addr := sbu.EntryData["address"].(string) + addr := t.Data["address"].(string) if !ok { return errors.New("neighbour without address") } if bIfname, ok := resp.Neighbours.Babel[ifname]; ok { link := data.BabelLink{ - RXCost: int(sbu.EntryData["rxcost"].(uint64)), - TXCost: int(sbu.EntryData["txcost"].(uint64)), - Cost: int(sbu.EntryData["cost"].(uint64)), + RXCost: int(t.Data["rxcost"].(uint64)), + TXCost: int(t.Data["txcost"].(uint64)), + Cost: int(t.Data["cost"].(uint64)), } bIfname.Neighbours[addr] = link return nil diff --git a/respond/daemon/nodeinfo.go b/respond/daemon/nodeinfo.go index f585f76..15d3374 100644 --- a/respond/daemon/nodeinfo.go +++ b/respond/daemon/nodeinfo.go @@ -57,7 +57,7 @@ func (d *Daemon) updateNodeinfo(iface string, resp *data.ResponseData) { resp.Nodeinfo.Software.BatmanAdv.Version = trim(string(v)) } if babel := d.babelData; babel != nil { - resp.Nodeinfo.Software.Babeld.Version = babel.Version + resp.Nodeinfo.Software.Babeld.Version = babel.Version() } if resp.Nodeinfo.Network.Mac == "" { @@ -97,13 +97,12 @@ func (d *Daemon) updateNodeinfo(iface string, resp *data.ResponseData) { meshBabel := data.NetworkInterface{} resp.Nodeinfo.Network.Mesh["babel"] = &meshBabel - d.babelData.Iter(func(bu babelParser.BabelUpdate) error { - sbu := bu.ToSUpdate() - if sbu.Table != "interface" { + d.babelData.Iter(func(t babelParser.Transition) error { + if t.Table != "interface" { return nil } - if sbu.EntryData["up"].(bool) { - addr := sbu.EntryData["ipv6"].(string) + if t.Data["up"].(bool) { + addr := t.Data["ipv6"].(string) meshBabel.Interfaces.Tunnel = append(meshBabel.Interfaces.Tunnel, addr) resp.Nodeinfo.Network.Addresses = append(resp.Nodeinfo.Network.Addresses, addr) }