Beispiel #1
0
// FindByCloudAddress returns an item which matches with cloud address.
func (db *DB) FindByCloudAddress(c common.CloudAddress) (common.MapItem, error) {
	n, ok := db.cloudIndex[c.IDString()]
	if !ok {
		return common.MapItem{}, common.ErrUnknownEndnode
	}
	return db.items[n], nil
}
Beispiel #2
0
// RegisterEndnode registers cloud address for a end-node.
func (m *Mapper) RegisterEndnode(ea common.EndnodeAddress, ca common.CloudAddress) error {
	item, err := m.Store.UpdateCloud(ea, ca)
	if err != nil {
		return err
	}
	m.Log.Printf("mapped: endnodeID=%s cloudID=%s\n", ea.IDString(), ca.IDString())
	return m.Cloud.SendState(*item.CloudAddress, item.States)
}
Beispiel #3
0
// GetEndnodeToken returns a stored token for the endnode.
func (db *DB) GetEndnodeToken(a common.CloudAddress) (common.Token, error) {
	db.lock.Lock()
	defer db.lock.Unlock()
	t, ok := db.endnodeTokens[a.IDString()]
	if !ok {
		return common.Token{}, common.ErrTokenNotFound
	}
	return t, nil
}
Beispiel #4
0
// GetEndnodeToken returns a stored token for the endnode.
func (m *DB) GetEndnodeToken(a common.CloudAddress) (common.Token, error) {
	var t common.Token
	err := m.db.View(func(tx *bolt.Tx) (err error) {
		t, err = m.getToken(tx, a.IDString())
		return err
	})
	if err != nil {
		return common.Token{}, err
	}
	return t, nil
}
Beispiel #5
0
// UpdateCloud updates CloudAddress field of a end-node.
func (db *DB) UpdateCloud(ea common.EndnodeAddress, caddr common.CloudAddress) (common.MapItem, error) {
	db.lock.Lock()
	defer db.lock.Unlock()

	n, ok := db.endnodeIndex[ea.IDString()]
	if !ok {
		return common.MapItem{}, common.ErrUnknownEndnode
	}
	db.items[n].CloudAddress = &caddr
	db.cloudIndex[caddr.IDString()] = n
	db.dumpTable()
	return db.items[n], nil
}
Beispiel #6
0
func testUpdateCloud(t *testing.T, s mapper.Store, ea common.EndnodeAddress, ca common.CloudAddress) common.MapItem {
	m, err := s.UpdateCloud(ea, ca)
	if err != nil {
		t.Fatal("UpdateCloud failed:", err)
	}
	if m.EndnodeAddress == nil || m.EndnodeAddress.IDString() != ea.IDString() {
		t.Fatalf("UpdateCloud modified EndnodeAddress: %#v", m.EndnodeAddress)
	}
	if m.CloudAddress == nil || m.CloudAddress.IDString() != ca.IDString() {
		t.Fatalf("UpdateCloud returned unexpected CloudAddress: %#v",
			m.CloudAddress)
	}
	return m
}
Beispiel #7
0
// FindByCloudAddress returns an item which matches with cloud address.
func (m *DB) FindByCloudAddress(c common.CloudAddress) (common.MapItem, error) {
	var item common.MapItem
	err := m.db.View(func(tx *bolt.Tx) (err error) {
		ck := []byte(c.IDString())
		cloud := tx.Bucket([]byte(boltCloudBucket))
		k := cloud.Get(ck)
		if k == nil {
			return common.ErrUnknownEndnode
		}
		item, err = m.getMapItem(tx, cloud.Get(ck))

		return err
	})
	if err != nil {
		return common.MapItem{}, err
	}
	return item, nil
}
Beispiel #8
0
// UpdateEndnode update EndnodeAddress for existing endnode
func (db *DB) UpdateEndnode(newEA common.EndnodeAddress, caddr common.CloudAddress) (common.MapItem, error) {
	db.lock.Lock()
	defer db.lock.Unlock()

	// retrieve mapItem by cloudAddress
	item, err := db.FindByCloudAddress(caddr)
	if err != nil {
		return common.MapItem{}, err
	}
	n := db.cloudIndex[caddr.IDString()]
	// update endnodeIndex
	delete(db.endnodeIndex, item.EndnodeAddress.IDString())
	db.endnodeIndex[newEA.IDString()] = n

	// update mapitem
	db.items[n].EndnodeAddress = &newEA
	db.dumpTable()
	return db.items[n], nil
}
Beispiel #9
0
// PutEndnodeToken puts a token as endnode's token.
func (db *DB) PutEndnodeToken(a common.CloudAddress, t common.Token) error {
	db.lock.Lock()
	defer db.lock.Unlock()
	db.endnodeTokens[a.IDString()] = t
	return nil
}
Beispiel #10
0
// PutEndnodeToken puts a token as endnode's token.
func (m *DB) PutEndnodeToken(a common.CloudAddress, t common.Token) error {
	err := m.db.Update(func(tx *bolt.Tx) (err error) {
		return m.putToken(tx, a.IDString(), t)
	})
	return err
}