Example #1
0
// Getting path value
// If path value is nil then just calculating it , otherwise just returning existing path
func GetValue(p *tree_graph.Path) (final_path *big.Int, err tree_lib.TreeError) {
	if p.Path == nil {
		final_path, err = p.CalculatePath()
	}
	final_path = p.Path
	return
}
Example #2
0
func NetworkEmmit(e *tree_event.Event, path *tree_graph.Path) (err tree_lib.TreeError) {
	var (
		sdata []byte
		p     *big.Int
	)
	err.From = tree_lib.FROM_NETWORK_EMIT

	// Calling get value, because maybe some one will calculate this path before calling this functions
	// If path is not calculated yet, it will be automatically calculated in GetValue function
	p, err = path.GetValue()
	if !err.IsNull() {
		return
	}

	// If we emitting from API then we need to multiply path with connected node
	// For sending data through him
	if strings.Contains(node_info.CurrentNodeInfo.Name, tree_api.API_NAME_PREFIX) {
		p.Mul(p, node_info.ChildsNodeValue[path.From])
	}

	// If from not set, setting it before network sending
	if len(e.From) == 0 {
		e.From = node_info.CurrentNodeInfo.Name
	}

	sdata, err.Err = ffjson.Marshal(e)
	if !err.IsNull() {
		return
	}

	SendToPath(sdata, p)
	return
}
Example #3
0
func CalculatePath(p *tree_graph.Path) (final_path *big.Int, err tree_lib.TreeError) {
	var (
		node_path = make(map[string]*big.Int)
		path      []map[string]*big.Int
	)
	final_path = big.NewInt(1)
	err.From = tree_lib.FROM_GET_PATH
	nodes_info, err = tree_db.ListNodeInfos()
	if !err.IsNull() {
		return
	}

	Check()

	for _, n := range nodes_info {
		node_values[n.Name] = n.Value
	}
	if len(p.Nodes) > 0 && p.Nodes[0] == "*" {
		var val big.Int
		final_path = big.NewInt(1)
		for _, n := range nodes_info {
			val = big.Int{}
			val.SetInt64(n.Value)
			final_path.Mul(final_path, &val)
			final_path.Mul(final_path, &val)
		}
		p.Path = final_path
		return
	}

	for _, n := range p.Nodes {
		if check_node[n] {
			targets = append(targets, n)
			node_path[n], err = NodePath(p.From, n)
			if !err.IsNull() {
				return
			}
		} else {
			fmt.Println("there is no server with name ", n)
			fmt.Println("ignoring server ", n)
		}
	}
	path = append(path, node_path)
	for _, g := range p.Groups {
		if check_group[g] {
			node_path, err = GroupPath(p.From, g)
			if !err.IsNull() {
				return
			}
			path = append(path, node_path)
		} else {
			fmt.Println("there is no group with name ", g)
			fmt.Println("ignoring group ", g)
		}
	}
	for _, t := range p.Tags {
		if check_tag[t] {
			node_path, err = TagPath(p.From, t)
			if !err.IsNull() {
				return
			}
			path = append(path, node_path)
		} else {
			fmt.Println("there is no tag with name ", t)
			fmt.Println("ignoring tag ", t)
		}
	}
	final_path = merge(path)
	nodes_info = nil
	targets = []string{}
	p.Path = final_path

	//if path contains node, then final_path divides to value of node
	//if node is a target, then final path divides square of value of node
	return
}