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 }
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)) }
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)) }
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()) }
// Processes a response from the downstream service. func (h *hub) processResponse(res baps3.Message) { log.Println("New response:", res.String()) switch res.Word() { case baps3.RsEnd: // Handle, broadcast and update state h.handleRsEnd(res) fallthrough case baps3.RsTime, baps3.RsState: // Broadcast _AND_ update state h.broadcast(res) fallthrough case baps3.RsOhai, baps3.RsFeatures: // Just update state if err := h.downstreamState.Update(res); err != nil { log.Fatal("Error updating state: " + err.Error()) } default: h.broadcast(res) } }
// Handles a request from a client. // Falls through to the connector cReqCh if command is "not understood". func (h *hub) processRequest(c *Client, req baps3.Message) { log.Println("New request:", req.String()) if reqFunc, ok := REQ_FUNC_MAP[req.Word()]; ok { responses := reqFunc(h, req) for _, resp := range responses { // TODO: Add a "is fail word" func to baps3-go? if resp.Word() == baps3.RsFail || resp.Word() == baps3.RsWhat { // failures only go to sender sendInvalidCmd(c, *resp, req) } else { h.broadcast(*resp) } } } else { h.cReqCh <- req } }
func sendInvalidCmd(c *Client, errRes baps3.Message, oldCmd baps3.Message) { for _, w := range oldCmd.AsSlice() { errRes.AddArg(w) } c.resCh <- errRes }