Example #1
0
func HandleNodeCommand(cmd *cobra.Command, args []string) {
	var (
		name string
		err  tree_lib.TreeError
	)
	err.From = tree_lib.FROM_HANDLE_NODE_COMMAND
	name, err.Err = cmd.Flags().GetString("set-name")
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
	}

	// If we have set-name flag then we just setting current_node in database and exiting
	if len(name) > 0 {
		tree_db.Set(tree_db.DB_RANDOM, []byte("current_node"), []byte(name))
		return
	}
	daemon := false
	daemon, err.Err = cmd.Flags().GetBool("daemon")
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	if daemon {
		cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("%s node > %s 2>&1 &", os.Args[0], tree_log.LogFile))
		err.Err = cmd.Run()
		if !err.IsNull() {
			log.Fatal(err.Err)
		}
		return
	}

	name, err.Err = cmd.Flags().GetString("name")
	if !err.IsNull() {
		tree_log.Error(err.From, err.Error())
		return
	}

	if len(name) == 0 {
		current_node_byte, err := tree_db.Get(tree_db.DB_RANDOM, []byte("current_node"))
		if !err.IsNull() {
			tree_log.Error(err.From, "Getting current node name from Random database, ", err.Error())
			return
		}
		if len(current_node_byte) == 0 {
			fmt.Println("Name is important for the first time run")
			return
		}
	} else {
		err = tree_node.SetCurrentNode(name)
		if !err.IsNull() {
			tree_log.Error(err.From, err.Error())
			return
		}
	}

	tree_node.Start()
}
Example #2
0
// Trigger specific event
func Trigger(name string, out io.Writer) (err tree_lib.TreeError) {
	var (
		event_byte []byte
		handlers   []Handler
	)
	event_byte, err = tree_db.Get(tree_db.DB_EVENT, []byte(name))
	if !err.IsNull() {
		return
	}
	err.Err = ffjson.Unmarshal(event_byte, &handlers)
	if !err.IsNull() {
		return
	}

	// calling handlers for this event
	for _, h := range handlers {
		ExecuteHandler(h, out)
	}

	return
}
Example #3
0
// Adding new custom event handler
// if event doesn't exists it will be created
func ON(name string, handlers ...Handler) (err tree_lib.TreeError) {
	var (
		handlers_data []byte
		event_byte    []byte
		old_handlers  []Handler
	)
	event_byte, err = tree_db.Get(tree_db.DB_EVENT, []byte(name))
	if !err.IsNull() {
		return
	}
	if len(event_byte) > 0 {
		err.Err = ffjson.Unmarshal(event_byte, &old_handlers)
		if !err.IsNull() {
			return
		}
	}

	old_handlers = append(old_handlers, handlers...)
	handlers_data, err.Err = ffjson.Marshal(old_handlers)
	err = tree_db.Set(tree_db.DB_EVENT, []byte(name), handlers_data)
	return
}
Example #4
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()
}