Example #1
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
}
Example #2
0
func SetParent(name string) bool {
	var err tree_lib.TreeError
	err.From = tree_lib.FROM_SET_PARENT
	node_info.ParentNodeInfo, err = tree_db.GetNodeInfo(name)
	if !err.IsNull() {
		tree_log.Error(err.From, "Getting parent node info from Node database, ", err.Error())
		return false
	}
	return true
}
Example #3
0
func node_init() {
	// Getting current node name
	current_node_byte, err := tree_db.Get(tree_db.DB_RANDOM, []byte("current_node"))
	err.From = tree_lib.FROM_NODE_INIT
	if !err.IsNull() {
		tree_log.Error(err.From, "Getting current node name from Random database, ", err.Error())
		return
	}
	current_node_name = string(current_node_byte)
	node_info.CurrentNodeInfo, err = tree_db.GetNodeInfo(current_node_name)
	if !err.IsNull() {
		tree_log.Error(err.From, "Getting current node info from Node database, ", err.Error())
		return
	}

	// Setting current Node Value field from string to big.Int
	node_info.CurrentNodeValue = nil // Setting to nil for garbage collection
	node_info.CurrentNodeValue = big.NewInt(node_info.CurrentNodeInfo.Value)

	for _, child := range node_info.CurrentNodeInfo.Childs {
		node_info.ChildsNodeInfo[child], err = tree_db.GetNodeInfo(child)
		if !err.IsNull() {
			tree_log.Error(err.From, fmt.Sprintf("Getting child (%s) node info from Node database, ", child), err.Error())
			return
		}
	}

	// Setting relations
	tree_db.SetRelations(current_node_name)

	node_info.ParentNodeInfo, err = tree_db.GetParentInfo(current_node_name)
	if !err.IsNull() {
		tree_log.Error(err.From, "Getting parent node info from Node database, ", err.Error())
		return
	}

	// Setting node values based on child list
	node_info.CalculateChildParentNodeValues()
}
Example #4
0
func HandleListCommand(ev *tree_event.Event, cmd Command) {
	var (
		info           = make(map[string]node_info.NodeInfo)
		data           []byte
		ev_data        Command
		nodes          []string
		nodes_in_group []string
		nodes_in_tag   []string
		err            tree_lib.TreeError
		infos          Info
	)
	err.From = tree_lib.FROM_HANDLE_LIST_COMMAND
	err.Err = ffjson.Unmarshal(ev.Data, &ev_data)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}
	err.Err = ffjson.Unmarshal(ev_data.Data, &infos)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}
	nodes = infos.Target
	for _, g := range infos.Group {
		nodes_in_group, err = tree_db.GetGroupNodes(g)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
		for _, n := range nodes_in_group {
			nodes = append(nodes, n)
		}
	}
	for _, t := range infos.Tag {
		nodes_in_tag, err = tree_db.GetNodesByTagName(t)
		if !err.IsNull() {
			tree_log.Error(err.From, "getting Tags", err.Error())
			return
		}
		for _, n := range nodes_in_tag {
			nodes = append(nodes, n)
		}
	}
	for _, n := range nodes {
		info[n], err = tree_db.GetNodeInfo(n)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
	}
	cb_cmd := cmd
	cb_cmd.Data, err.Err = ffjson.Marshal(info)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}
	data, err.Err = ffjson.Marshal(cb_cmd)
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	SendCommandCallback(ev, data)
}