コード例 #1
0
func (this *filtercollectionhelper) GetFilterCollections() ([]FilterCollection, error) {
	filterCollections := make([]FilterCollection, 0, 0)

	cursor, err := this.collection.NewCursor()
	defer cursor.Close()

	if err != nil {
		return filterCollections, err
	}

	err = cursor.First()
	if err != nil {
		//You Get -28 When There are no records.
		if err == unqlitego.UnQLiteError(-28) {
			return filterCollections, nil
		} else {
			return filterCollections, err
		}

	}

	for {
		if !cursor.IsValid() {
			break
		}

		filterCollection := FilterCollection{}
		value, err := cursor.Value()

		if err != nil {

			log.Println("Error: Cursor Get Value Error:" + err.Error())

		} else {

			err := this.collection.Unmarshal()(value, &filterCollection)
			if err != nil {
				key, err := cursor.Key()
				if err != nil {
					log.Println("Error: Cursor Get Key Error:" + err.Error())
				} else {
					log.Println("Invalid Filter Collection in Datastore:" + string(key))
					filterCollection.Name = string(key)
					this.SetFilterCollection(filterCollection)
				}
			}

			filterCollections = append(filterCollections, filterCollection)
		}

		err = cursor.Next()
		if err != nil {
			break
		}
	}

	err = cursor.Close()

	return filterCollections, err
}
コード例 #2
0
ファイル: user[helper].go プロジェクト: d2g/cloudpathway
/**
 * Return an array of all users.
 */
func (t *userhelper) GetUsers() ([]User, error) {

	users := make([]User, 0, 0)

	cursor, err := t.collection.NewCursor()
	defer cursor.Close()

	if err != nil {
		return users, err
	}

	err = cursor.First()
	if err != nil {
		if err == unqlitego.UnQLiteError(-28) {
			return users, nil
		} else {
			return users, err
		}
	}

	for {
		if !cursor.IsValid() {
			break
		}

		user := User{}
		value, err := cursor.Value()

		if err != nil {

			log.Println("Error: Cursor Get Value Error:" + err.Error())

		} else {

			err := t.collection.Unmarshal()(value, &user)
			if err != nil {
				key, err := cursor.Key()
				if err != nil {
					log.Println("Error: Cursor Get Key Error:" + err.Error())
				} else {
					log.Println("Invalid Lease in Datastore:" + string(key))
					user.Username = string(key)
					t.SetUser(&user)
				}
			}

			users = append(users, user)
		}

		err = cursor.Next()
		if err != nil {
			break
		}
	}

	err = cursor.Close()

	return users, err
}
コード例 #3
0
ファイル: cache.go プロジェクト: d2g/cloudpathway
func (t *cache) GC() error {

	cursor, err := t.collection.NewCursor()
	defer cursor.Close()

	if err != nil {
		return err
	}

	err = cursor.First()
	if err != nil {
		if err == unqlitego.UnQLiteError(-28) {
			return nil
		} else {
			return err
		}
	}

	for {
		if !cursor.IsValid() {
			break
		}

		key, err := cursor.Key()
		if err != nil {
			log.Println("Error: Cursor Get Key Error:" + err.Error())
			continue
		}

		value, err := cursor.Value()
		if err != nil {
			log.Println("Error: Cursor Get Value Error:" + err.Error())
			continue
		}

		cacherecord := CacheRecord{}
		err = t.collection.Unmarshal()(value, &cacherecord)
		if err != nil {
			log.Println("Error: Unmarshalling \"" + string(key) + "\":" + err.Error())
		}

		if cacherecord.Expiry.Before(time.Now()) {
			//Record Has Expired
			err = cursor.Delete()
			if err != nil {
				log.Println("Error: Unable to Delete Cache Record \"" + string(key) + "\":" + err.Error())
			}
		}

		err = cursor.Next()
		if err != nil {
			break
		}
	}

	err = cursor.Close()

	return err
}
コード例 #4
0
ファイル: lease[helper].go プロジェクト: d2g/cloudpathway
func (t *leasehelper) GetLeases() ([]leasepool.Lease, error) {
	leases := make([]leasepool.Lease, 0, 0)

	cursor, err := t.collection.NewCursor()
	defer cursor.Close()

	if err != nil {
		return leases, err
	}

	err = cursor.First()
	if err != nil {
		if err == unqlitego.UnQLiteError(-28) {
			return leases, nil
		} else {
			return leases, err
		}
	}

	for {
		if !cursor.IsValid() {
			break
		}

		lease := leasepool.Lease{}
		value, err := cursor.Value()

		if err != nil {

			log.Println("Error: Cursor Get Value Error:" + err.Error())

		} else {

			err := t.collection.Unmarshal()(value, &lease)
			if err != nil {
				key, err := cursor.Key()
				if err != nil {
					log.Println("Error: Cursor Get Key Error:" + err.Error())
				} else {
					log.Println("Invalid Lease in Datastore:" + string(key))
					lease.IP = net.ParseIP(string(key))
					t.SetLease(&lease)
				}
			}

			leases = append(leases, lease)
		}

		err = cursor.Next()
		if err != nil {
			break
		}
	}

	err = cursor.Close()

	return leases, err
}
コード例 #5
0
func (t *blockroutehelper) IsBlockedRoute(source net.IP, destination net.IP) (bool, error) {

	_, err := t.collection.Fetch(append([]byte(source.To4()), []byte(destination.To4())...))
	if err != nil {
		if err == unqlitego.UnQLiteError(-6) {
			//Not Found
			return false, nil
		}
		return false, err
	}

	return true, nil
}
コード例 #6
0
func (t *blockroutehelper) GetBlockedRoutesByIP(clientIP net.IP) (routes []BlockedRoute, err error) {
	cursor, err := t.collection.NewCursor()
	defer cursor.Close()
	if err != nil {
		return
	}

	err = cursor.First()
	if err != nil {
		//You Get -28 When There are no records.
		if err == unqlitego.UnQLiteError(-28) {
			//No Records in the DB.
			err = nil
			return
		} else {
			return
		}
	}

	for {
		if !cursor.IsValid() {
			break
		}

		value, err := cursor.Value()
		if err != nil {
			log.Println("Error: Cursor Get Value Error:" + err.Error())
		} else {
			tmpRecord := BlockedRoute{}
			err = t.collection.Unmarshal()(value, &tmpRecord)
			if err != nil {
				log.Println("Error: Can't Unmarshal Block Record (Deleting)")
				cursor.Delete()
			}

			if tmpRecord.Source.Equal(clientIP) || tmpRecord.Destination.Equal(clientIP) {
				//err = cursor.Delete()
				//We could Optimise this to delete the record from the datastore now rather than using the function.
				routes = append(routes, tmpRecord)
			}
		}

		err = cursor.Next()
		if err != nil {
			break
		}
	}

	err = cursor.Close()
	return
}
コード例 #7
0
ファイル: device[helper].go プロジェクト: d2g/cloudpathway
/**
 * Gets an array of all devices within the database.
 */
func (this *devicehelper) GetDevices() ([]Device, error) {

	devices := make([]Device, 0, 0)

	cursor, err := this.collection.NewCursor()
	defer cursor.Close()

	if err != nil {
		return devices, err
	}

	err = cursor.First()
	if err != nil {
		//You Get -28 When There are no records.
		if err == unqlitego.UnQLiteError(-28) {
			return devices, nil
		} else {
			return devices, err
		}
	}

	for {
		if !cursor.IsValid() {
			break
		}

		device := Device{}
		value, err := cursor.Value()

		if err != nil {

			log.Println("Error: Cursor Get Value Error:" + err.Error())

		} else {

			err := this.collection.Unmarshal()(value, &device)
			if err != nil {
				key, err := cursor.Key()
				if err != nil {
					log.Println("Error: Cursor Get Key Error:" + err.Error())
				} else {
					log.Println("Invalid Device in Datastore:" + string(key))
					device.MACAddress, err = net.ParseMAC(string(key))
					if err != nil {
						this.SetDevice(&device)
					}
				}
			}

			devices = append(devices, device)
		}

		err = cursor.Next()
		if err != nil {
			break
		}
	}

	err = cursor.Close()
	if err != nil {
		log.Println("3:" + err.Error())
	}
	return devices, err
}