mpdclient

package module
v0.0.0-...-fcdd327 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2013 License: GPL-3.0 Imports: 10 Imported by: 5

README

MPD Client for Go

Purpose

Only a subset of MPD commands is implemented: the current implementation focused on getting a robust idle events system, fetching mpd status, working with stickers and client-to-client messaging.

More commands could be added in the future, if a need arise, since the current implementation allows to add new commands fairly easily. In particular, commands not yet implemented can be run anyway using the generic Cmd() method.

However, commands lists are not implemented and the current implementation isn't suited for them.

Example usage

Basic commands

Import:

import "github.com/vincent-petithory/mpdclient"

Connect:

mpdc, err := mpdclient.Connect("localhost", 6600)
if err != nil {
    panic(err)
}
// Close client when done
defer mpdc.Close()

Print title of current song:

info, err := mpdc.CurrentSong()
if err != nil {
    panic(err)
}
fmt.Printf("Title: %s\n", (*info)["Title"])

Most commands that return data (such as currentsong) will return (*Info, error), and those who do not will simply return error.

Idle

Listening to idle events is done through an IdleListener type. It provides a channel in which idle events will be fed.

Listen forever to all player and mixer events (player pause/stop/start/seek and volume changes):

idleEvents := mpdc.Idle("player", "mixer")
for {
    subsystem := <-idleEvents.Ch
    switch subsystem {
    case "player":
        fmt.Println("player status changed.")
    case "mixer":
        statusInfo, err := mpdc.Status()
        if err != nil {
            panic(err)
        } else {
            fmt.Printf("volume is: %s\n", (*statusInfo)["volume"])
        }
    }
}

Messaging

Subscribe to a channel and write all incoming messages to stdout:

err := mpdc.Subscribe("mychannel")
if err != nil {
    panic(err)
}

mesEvents := mpdc.Idle("message")
for {
    <-mesEvents.Ch
    channelMessages, err := mpdc.ReadMessages()
    if err != nil {
        panic(err)
    }
    for _, channelMessage := range channelMessages {
        fmt.Printf("Got message \"%s\" on channel \"%s\"\n", channelMessage.Message, channelMessage.Channel)
    }
}

Run:

$ mpc sendmessage mychannel 'Did you get the message?'

More ?

  • The unit tests are also a good example.
  • Checkout mpdfav for "real world" usage.

Internals

One client instance opens 3 connections. The reason is mostly to not lose any idle event when mpd commands are sent blazing fast. Tunnelling everything in one connection was also creating races. A later implementation may improve that and lower the connections opened.

  • One connection specifically for client-to-client (subscription) idle message subsystem

  • One connection for all idle subsystems

  • One connection for all standard commands. That connection will forward the 3 following commands to the subscription idle loop: readmessages, subscribe, unsubscribe.

      subscription idle	main					idle
      |					| send noidle			|
      | wait request		|						|
      |					| send request object	|
      | process request	|						|
      | send response		|						|
    

Documentation

Index

Constants

View Source
const Debug = false
View Source
const PlaylistInfoLastModifiedTimeLayout = "2006-01-02T15:04:05Z"
View Source
const StickerSongType = "song"

Variables

This section is empty.

Functions

func CloseConn

func CloseConn(conn *textproto.Conn) error

Types

type ChannelMessage

type ChannelMessage struct {
	Channel string
	Message string
}

type Info

type Info map[string]string

func (*Info) AddInfo

func (info *Info) AddInfo(data string) error

func (*Info) Fill

func (i *Info) Fill(data []string) error

func (*Info) Progress

func (i *Info) Progress() (int, int)

type MPDClient

type MPDClient struct {
	Host            string
	Port            uint
	ProtocolVersion Version

	Logger *log.Logger
	// contains filtered or unexported fields
}

func Connect

func Connect(host string, port uint) (*MPDClient, error)

func ConnectAuth

func ConnectAuth(host string, port uint, password string) (*MPDClient, error)

func (*MPDClient) Channels

func (c *MPDClient) Channels() ([]string, error)

func (*MPDClient) Close

func (c *MPDClient) Close() error

func (*MPDClient) Cmd

func (c *MPDClient) Cmd(cmd string) *response

func (*MPDClient) CurrentSong

func (c *MPDClient) CurrentSong() (*Info, error)

func (*MPDClient) Idle

func (c *MPDClient) Idle(subsystems ...string) *idleListener

func (*MPDClient) ListPlaylist

func (c *MPDClient) ListPlaylist(name string) ([]string, error)

func (*MPDClient) ListPlaylists

func (c *MPDClient) ListPlaylists() ([]PlaylistInfo, error)

func (*MPDClient) Ping

func (c *MPDClient) Ping() error

func (*MPDClient) PlaylistAdd

func (c *MPDClient) PlaylistAdd(name, uri string) error

func (*MPDClient) PlaylistClear

func (c *MPDClient) PlaylistClear(name string) error

func (*MPDClient) ReadMessages

func (c *MPDClient) ReadMessages() ([]ChannelMessage, error)

func (*MPDClient) Rm

func (c *MPDClient) Rm(name string) error

func (*MPDClient) Save

func (c *MPDClient) Save(name string) error

func (*MPDClient) SendMessage

func (c *MPDClient) SendMessage(channel, text string) error

func (*MPDClient) Status

func (c *MPDClient) Status() (*Info, error)

func (*MPDClient) StickerFind

func (c *MPDClient) StickerFind(stype, uri, stickerName string) (SongStickerList, error)

func (*MPDClient) StickerGet

func (c *MPDClient) StickerGet(stype, uri, stickerName string) (string, error)

func (*MPDClient) StickerSet

func (c *MPDClient) StickerSet(stype, uri, stickerName, value string) error

func (*MPDClient) Subscribe

func (c *MPDClient) Subscribe(channel string) error

func (*MPDClient) Unsubscribe

func (c *MPDClient) Unsubscribe(channel string) error

type MPDError

type MPDError struct {
	Ack            uint
	CommandListNum uint
	CurrentCommand string
	MessageText    string
}

func (MPDError) Error

func (me MPDError) Error() string

type PlaylistInfo

type PlaylistInfo struct {
	Name         string
	LastModified *time.Time
}

type SongSticker

type SongSticker struct {
	Uri   string
	Name  string
	Value string
}

type SongStickerList

type SongStickerList []SongSticker

func (SongStickerList) Len

func (p SongStickerList) Len() int

func (SongStickerList) Less

func (p SongStickerList) Less(i, j int) bool

func (SongStickerList) Swap

func (p SongStickerList) Swap(i, j int)

type Version

type Version struct {
	Major    uint
	Minor    uint
	Revision uint
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL