Exemplo n.º 1
0
func SetPathValues() (err tree_lib.TreeError) {
	var (
		nodes []node_info.NodeInfo
		prime int64
		value []byte
	)
	err.From = tree_lib.FROM_SET_PATH_VALUES
	nodes, err = ListNodeInfos()
	if !err.IsNull() {
		return
	}
	prime = 2
	for _, n := range nodes {
		n.Value = prime
		value, err.Err = ffjson.Marshal(n)
		if !err.IsNull() {
			return
		}
		err = Set(DB_NODE, []byte(n.Name), value)
		if !err.IsNull() {
			return
		}
		prime = tree_lib.NextPrimeNumber(prime)
	}
	return
}
Exemplo n.º 2
0
// Init API node for connection to targets
func API_INIT(targets ...string) bool {
	var err tree_lib.TreeError
	err.From = tree_lib.FROM_API_INIT
	if len(targets) == 0 {
		tree_log.Error(err.From, "For running API client you need to specify target node(s) to connect")
		return false
	}
	for _, n := range targets {
		node_info.ChildsNodeInfo[n], err = tree_db.GetNodeInfo(n)
		if !err.IsNull() {
			tree_log.Error(err.From, fmt.Sprintf("Unable Getting target (%s) node info from Node database, ", n), err.Error())
			return false
		}
	}

	rand.Seed(time.Now().UnixNano())
	node_info.CurrentNodeInfo = node_info.NodeInfo{
		Name:   fmt.Sprintf("%s|%s", API_NAME_PREFIX, tree_lib.RandomString(10)),
		Childs: targets,
		// Getting next prime number based on Unix Timestamp nanoseconds and
		// TODO: Think about making this in a different way
		Value: tree_lib.NextPrimeNumber((1 * rand.Int63n(100)) + int64(100)),
	}

	node_info.CurrentNodeValue = big.NewInt(node_info.CurrentNodeInfo.Value)

	// Setting node values based on child list
	node_info.CalculateChildParentNodeValues()

	// After we have child information lets connect to them
	node_info.ChildsConnectionUpdate()

	return true
}
Exemplo n.º 3
0
func DBFromConfig() {
	var (
		err        tree_lib.TreeError
		prev_prime = int64(1)
	)
	err.From = tree_lib.FROM_DB_FROM_CONFIG

	for n, nf := range GLOBAL_CONFIG.TreeNode {
		nf.Value = tree_lib.NextPrimeNumber(prev_prime)
		prev_prime = nf.Value
		err = tree_db.SetNodeInfo(n, nf)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
		}
	}

	// After having All nodes information now we can set related things for every node
	for n, _ := range GLOBAL_CONFIG.TreeNode {
		// Setting relations for every Node
		err = tree_db.SetRelations(n)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
		}

		// Setting Groups with node lists in Group database
		err = tree_db.AddNodeToHisGroups(n)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
		}

		// Setting Tags with node lists in Group database
		err = tree_db.AddNodeToHisTags(n)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
		}
	}

	// Setting Balancers
	for b, b_conf := range GLOBAL_CONFIG.Balancer {
		var b_data []byte
		b_data, err.Err = ffjson.Marshal(b_conf)
		if !err.IsNull() {
			tree_log.Error(err.From, "Error encoding balancer config", b, " -> ", err.Error())
			continue
		}
		err = tree_db.Set(tree_db.DB_BALANCER, []byte(b), b_data)
		if !err.IsNull() {
			tree_log.Error(err.From, "Error setting balancer config", b, " -> ", err.Error())
		}
	}

	// Setting Registry
	for r, r_conf := range GLOBAL_CONFIG.Registry {
		var r_data []byte
		r_data, err.Err = ffjson.Marshal(r_conf)
		if !err.IsNull() {
			tree_log.Error(err.From, "Error encoding registry config", r, " -> ", err.Error())
			continue
		}
		err = tree_db.Set(tree_db.DB_REGISTRY, []byte(r), r_data)
		if !err.IsNull() {
			tree_log.Error(err.From, "Error setting registry config", r, " -> ", err.Error())
		}
	}
}