Ejemplo n.º 1
0
Archivo: group.go Proyecto: go-on/sc
// parseGroup parses information about a group from a message
// received at /g_queryTree
// it *does not* recursively query for child groups
func parseGroup(msg *osc.Message) (*Group, error) {
	// return an error if msg.Address is not right
	if msg.Address() != gQueryTreeReply {
		return nil, fmt.Errorf("msg.Address should be %s, got %s", gQueryTreeReply, msg.Address())
	}
	// g_queryTree replies should have at least 3 arguments
	g, numArgs := new(Group), msg.CountArguments()
	if numArgs < 3 {
		return nil, fmt.Errorf("expected 3 arguments for message, got %d", numArgs)
	}
	// get the id of the group this reply is for
	nodeID, err := msg.ReadInt32()
	if err != nil {
		return nil, err
	}
	g.Node.id = nodeID

	// initialize the children array
	numChildren, err := msg.ReadInt32()
	if err != nil {
		return nil, err
	}
	if numChildren < 0 {
		return nil, fmt.Errorf("expected numChildren >= 0, got %d", numChildren)
	}
	g.children = make([]*Node, numChildren)
	// get the childrens' ids
	var numControls, numSubChildren int32
	for i := 3; i < numArgs; {
		nodeID, err = msg.ReadInt32()
		if err != nil {
			return nil, err
		}
		g.children[i-3] = &Node{nodeID}
		// get the number of children of this node
		// if -1 this is a synth, if >= 0 this is a group
		numSubChildren, err = msg.ReadInt32()
		if err != nil {
			return nil, err
		}
		if numSubChildren == -1 {
			// synth
			i += 3
			numControls, err = msg.ReadInt32()
			if err != nil {
				return nil, err
			}
			i += 1 + int(numControls*2)
		} else if numSubChildren >= 0 {
			// group
			i += 2
		}
	}
	return g, nil
}