// read nodes.json compatible data into database func (db *NodeDB) ImportNodes(r io.Reader, persistent bool) error { nodes, err := readNodesJSON(r) if err != nil { return err } if nodes.Version != 1 { return ErrUnknownVersion } for _, node := range nodes.Nodes { var addr alfred.HardwareAddr if err := addr.Parse(node.NodeInfo.NodeID); err != nil { log.Printf("Import: error parsing NodeID %s: %v, skipping", node.NodeInfo.NodeID, err) continue } n := &NodeInfo{NodeInfo: gluon.NodeInfo{Source: addr, Data: &node.NodeInfo}} m := store.NewMeta(n) m.Updated = time.Time(node.LastSeen).Local() m.Created = time.Time(node.FirstSeen).Local() if !persistent { m.InvalidateIn(db.validTimeGluon) } err := db.Main.Batch(func(tx *bolt.Tx) error { return db.Main.UpdateMeta(tx, store.NewMeta(&NodeInfo{}), m) }) if err != nil { log.Printf("Import: error on node %v", node.NodeInfo.NodeID) } } return err }
func (db *NodeDB) ResolveNodeID(tx *bolt.Tx, mac alfred.HardwareAddr) (string, bool) { id := &NodeID{} m := store.NewMeta(id) if db.Main.Get(tx, mac, m) == nil && m.GetItem(id) == nil { if bytes, err := id.Bytes(); err == nil { return string(bytes), true } } // when we have no nodeID, we return a synthetic one return mac.String(), false }