Esempio n. 1
0
func (h *hub) processReqSelect(req baps3.Message) (resps []*baps3.Message) {
	args := req.Args()
	if len(args) == 0 {
		if h.pl.HasSelection() {
			// Remove current selection
			h.cReqCh <- *baps3.NewMessage(baps3.RqEject)
			h.pl.selection = -1
			resps = append(resps, baps3.NewMessage(baps3.RsSelect))
		} else {
			// TODO: Should we care about there not being an existing selection?
			resps = append(resps, baps3.NewMessage(baps3.RsFail).AddArg("No selection to remove"))
		}
	} else if len(args) == 2 {
		iStr, hash := args[0], args[1]

		i, err := strconv.Atoi(iStr)
		if err != nil {
			return append(resps, baps3.NewMessage(baps3.RsWhat).AddArg("Bad index"))
		}

		newIdx, newHash, err := h.pl.Select(i, hash)
		if err != nil {
			return append(resps, baps3.NewMessage(baps3.RsFail).AddArg(err.Error()))
		}

		h.cReqCh <- *baps3.NewMessage(baps3.RqLoad).AddArg(h.pl.items[h.pl.selection].Data)
		resps = append(resps, baps3.NewMessage(baps3.RsSelect).AddArg(strconv.Itoa(newIdx)).AddArg(newHash))
	} else {
		resps = makeBadCommandMsgs()
	}
	return
}
Esempio n. 2
0
func (h *hub) processReqEnqueue(req baps3.Message) (resps []*baps3.Message) {
	args := req.Args()
	if len(args) != 4 {
		return makeBadCommandMsgs()
	}
	iStr, hash, itemType, data := args[0], args[1], args[2], args[3]

	i, err := strconv.Atoi(iStr)
	if err != nil {
		return append(resps, baps3.NewMessage(baps3.RsWhat).AddArg("Bad index"))
	}

	if itemType != "file" && itemType != "text" {
		return append(resps, baps3.NewMessage(baps3.RsWhat).AddArg("Bad item type"))
	}

	oldSelection := h.pl.selection
	item := &PlaylistItem{Data: data, Hash: hash, IsFile: itemType == "file"}
	newIdx, err := h.pl.Enqueue(i, item)
	if err != nil {
		return append(resps, baps3.NewMessage(baps3.RsFail).AddArg(err.Error()))
	}
	if oldSelection != h.pl.selection {
		resps = append(resps, baps3.NewMessage(baps3.RsSelect).AddArg(strconv.Itoa(h.pl.selection)).AddArg(h.pl.items[h.pl.selection].Hash))
	}
	return append(resps, baps3.NewMessage(baps3.RsEnqueue).AddArg(strconv.Itoa(newIdx)).AddArg(item.Hash).AddArg(itemType).AddArg(item.Data))
}
Esempio n. 3
0
func (h *hub) processReqDequeue(req baps3.Message) (resps []*baps3.Message) {
	args := req.Args()
	if len(args) != 2 {
		return makeBadCommandMsgs()
	}
	iStr, hash := args[0], args[1]

	i, err := strconv.Atoi(iStr)
	if err != nil {
		return append(resps, baps3.NewMessage(baps3.RsWhat).AddArg("Bad index"))
	}

	oldSelection := h.pl.selection
	rmIdx, rmHash, err := h.pl.Dequeue(i, hash)
	if err != nil {
		return append(resps, baps3.NewMessage(baps3.RsFail).AddArg(err.Error()))
	}
	if oldSelection != h.pl.selection {
		if !h.pl.HasSelection() {
			resps = append(resps, baps3.NewMessage(baps3.RsSelect))
		} else {
			resps = append(resps, baps3.NewMessage(baps3.RsSelect).AddArg(strconv.Itoa(h.pl.selection)).AddArg(h.pl.items[h.pl.selection].Hash))
		}
	}
	return append(resps, baps3.NewMessage(baps3.RsDequeue).AddArg(strconv.Itoa(rmIdx)).AddArg(rmHash))
}
Esempio n. 4
0
// Collates all the responses that comprise a dump response.
// Exists as this is used by the dump response handler /and/ is sent on client connection
func (h *hub) makeDumpResponses() (msgs []*baps3.Message) {
	msgs = append(msgs, baps3.NewMessage(baps3.RsState).AddArg(h.downstreamState.State.String()))
	if h.downstreamState.State != baps3.StEjected {
		msgs = append(msgs, baps3.NewMessage(baps3.RsTime).AddArg(
			strconv.FormatInt(h.downstreamState.Time.Nanoseconds()/1000, 10)))
	}
	msgs = append(msgs, h.makeRsAutoAdvance())
	msgs = append(msgs, h.makeListResponses()...)
	return
}
Esempio n. 5
0
// Collates all the responses that comprise a list reponse.
// Exists as this is used by the list response handler and makeDumpResponse.
func (h *hub) makeListResponses() (msgs []*baps3.Message) {
	msgs = append(msgs, baps3.NewMessage(baps3.RsCount).AddArg(strconv.Itoa(len(h.pl.items))))
	for i, item := range h.pl.items {
		typeStr := "file"
		if !item.IsFile {
			typeStr = "text"
		}
		msgs = append(msgs, baps3.NewMessage(baps3.RsItem).AddArg(strconv.Itoa(i)).AddArg(item.Hash).AddArg(typeStr).AddArg(item.Data))
	}
	return
}
Esempio n. 6
0
func (h *hub) makeRsAutoAdvance() (msg *baps3.Message) {
	var autoadvancestate string
	if h.autoAdvance {
		autoadvancestate = "on"
	} else {
		autoadvancestate = "off"
	}
	return baps3.NewMessage(baps3.RsAutoAdvance).AddArg(autoadvancestate)
}
Esempio n. 7
0
func (h *hub) processReqAutoadvance(req baps3.Message) (msgs []*baps3.Message) {
	if len(req.Args()) != 1 {
		return makeBadCommandMsgs()
	}
	onoff, _ := req.Arg(0)
	switch onoff {
	case "on":
		h.autoAdvance = true
	case "off":
		h.autoAdvance = false
	default:
		return append(msgs, baps3.NewMessage(baps3.RsWhat).AddArg("Bad argument"))
	}
	return append(msgs, h.makeRsAutoAdvance())
}
Esempio n. 8
0
// Appends the downstream service's version (from the OHAI) to the listd version.
func (h *hub) makeRsOhai() *baps3.Message {
	return baps3.NewMessage(baps3.RsOhai).AddArg("listd " + LD_VERSION + "/" + h.downstreamState.Identifier)
}
Esempio n. 9
0
func makeBadCommandMsgs() []*baps3.Message {
	return []*baps3.Message{baps3.NewMessage(baps3.RsWhat).AddArg("Bad command")}
}