Connection to a MPD server

Introduced properties

fun connect

mpd :: MPDConnection :: connect

Connect to the MPD server
fun current_song: nullable SongInfo

mpd :: MPDConnection :: current_song

Get information on the currently playing song on the MPD server
init defaultinit(host: String, port: Int, password: nullable String)

mpd :: MPDConnection :: defaultinit

fun error: nullable String

mpd :: MPDConnection :: error

Last occured error.
protected fun error=(error: nullable String)

mpd :: MPDConnection :: error=

Last occured error.
fun host: String

mpd :: MPDConnection :: host

Server hostname.
protected fun host=(host: String)

mpd :: MPDConnection :: host=

Server hostname.
fun load_playlist(name: String)

mpd :: MPDConnection :: load_playlist

Load playlist named name.
fun password: nullable String

mpd :: MPDConnection :: password

Server password.
protected fun password=(password: nullable String)

mpd :: MPDConnection :: password=

Server password.
fun pause

mpd :: MPDConnection :: pause

Pause music playing on the MPD server
fun play

mpd :: MPDConnection :: play

Play music playing on the MPD server
fun port: Int

mpd :: MPDConnection :: port

Server port.
protected fun port=(port: Int)

mpd :: MPDConnection :: port=

Server port.
fun relative_volume=(diff: Int)

mpd :: MPDConnection :: relative_volume=

Set the volume relatively
fun socket: nullable TCPStream

mpd :: MPDConnection :: socket

Socket connection to server.
protected fun socket=(socket: nullable TCPStream)

mpd :: MPDConnection :: socket=

Socket connection to server.
fun status: nullable ServerStatus

mpd :: MPDConnection :: status

Get MPD server status
fun stop

mpd :: MPDConnection :: stop

Stop music playing on the MPD server
fun volume=(vol: Int)

mpd :: MPDConnection :: volume=

Set MPD server volume.
protected fun write_and_check(msg: String)

mpd :: MPDConnection :: write_and_check

Write a command to the MPD server

Redefined properties

redef type SELF: MPDConnection

mpd $ MPDConnection :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun connect

mpd :: MPDConnection :: connect

Connect to the MPD server
fun current_song: nullable SongInfo

mpd :: MPDConnection :: current_song

Get information on the currently playing song on the MPD server
init defaultinit(host: String, port: Int, password: nullable String)

mpd :: MPDConnection :: defaultinit

fun error: nullable String

mpd :: MPDConnection :: error

Last occured error.
protected fun error=(error: nullable String)

mpd :: MPDConnection :: error=

Last occured error.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun host: String

mpd :: MPDConnection :: host

Server hostname.
protected fun host=(host: String)

mpd :: MPDConnection :: host=

Server hostname.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun load_playlist(name: String)

mpd :: MPDConnection :: load_playlist

Load playlist named name.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun password: nullable String

mpd :: MPDConnection :: password

Server password.
protected fun password=(password: nullable String)

mpd :: MPDConnection :: password=

Server password.
fun pause

mpd :: MPDConnection :: pause

Pause music playing on the MPD server
fun play

mpd :: MPDConnection :: play

Play music playing on the MPD server
fun port: Int

mpd :: MPDConnection :: port

Server port.
protected fun port=(port: Int)

mpd :: MPDConnection :: port=

Server port.
fun relative_volume=(diff: Int)

mpd :: MPDConnection :: relative_volume=

Set the volume relatively
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun socket: nullable TCPStream

mpd :: MPDConnection :: socket

Socket connection to server.
protected fun socket=(socket: nullable TCPStream)

mpd :: MPDConnection :: socket=

Socket connection to server.
fun status: nullable ServerStatus

mpd :: MPDConnection :: status

Get MPD server status
fun stop

mpd :: MPDConnection :: stop

Stop music playing on the MPD server
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun volume=(vol: Int)

mpd :: MPDConnection :: volume=

Set MPD server volume.
protected fun write_and_check(msg: String)

mpd :: MPDConnection :: write_and_check

Write a command to the MPD server
package_diagram mpd::MPDConnection MPDConnection core::Object Object mpd::MPDConnection->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

mpd $ MPDConnection
# Connection to a MPD server
class MPDConnection

	# Socket connection to server.
	var socket: nullable TCPStream = null

	# Server hostname.
	var host: String

	# Server port.
	var port: Int

	# Server password.
	var password: nullable String

	# Last occured error.
	var error: nullable String = null

	# Connect to the MPD server
	fun connect
	do
		var p: nullable TCPStream = null

		p = new TCPStream.connect(host, port)
		assert p.connected

		sys.nanosleep(0,5000)

		var rep = p.read(1024)
		assert not rep.is_empty
		if not rep.has_prefix("OK") then
			print "MPD responded {rep}"
			abort
		end

		socket = p

		var password = password
		if password != null then
			write_and_check("password {password}\n")
		end
	end

	# Write a command to the MPD server
	protected fun write_and_check(msg: String)
	do
		if socket == null then connect

		socket.write(msg)
		sys.nanosleep(0,5000)
		var rep = socket.read(1024)
		if not rep.has_prefix("OK") then
			print "Error: MPD responded {rep}"
			socket.close
			socket = null
		end
	end

	# Get MPD server status
	fun status: nullable ServerStatus
	do
		if socket == null then connect

		var volume: nullable Int = null
		var state: nullable String = null
		var elapsed: nullable Int = null
		var time_at: nullable Int = null
		var time_total: nullable Int = null

		# get current status
		socket.write("status\n")
		var rep = socket.read(1024)
		for l in rep.split_with("\n") do
			var words = l.split_with(" ")
			if words.length > 1 then
				var key = words[0].to_lower
				var first_space = l.chars.index_of(' ')
				var rest = l.substring_from(first_space+1)
				if  key == "volume:" then
					volume = rest.to_i
					if volume == -1 then volume = null
				else if  key == "state:" then
					state = rest
				else if  key == "elapsed:" then
					elapsed = rest.to_i
				else if  key == "time:" then
					var times = rest.split(":")
					time_at = times[0].to_i
					time_total = times[1].to_i
				end
			end
		end

		if state != null then
			var status = new ServerStatus(volume, state, elapsed, time_at, time_total)
			return status
		else
			return null
		end
	end

	# Set the volume relatively
	fun relative_volume=(diff: Int)
	do
		if socket == null then connect

		var status = status
		if status != null then
			var vol = status.volume
			if vol != null then
				var new_vol = vol + diff
				new_vol = new_vol.clamp(0, 100)
				volume = new_vol
				return
			end
		end

		error = "Cannot get volume"
	end

	# Set MPD server volume.
	fun volume=(vol: Int) do write_and_check("setvol {vol}\n")

	# Pause music playing on the MPD server
	fun pause do write_and_check("pause\n")

	# Stop music playing on the MPD server
	fun stop do write_and_check("stop\n")

	# Play music playing on the MPD server
	fun play do write_and_check("play\n")

	# Get information on the currently playing song on the MPD server
	fun current_song: nullable SongInfo
	do
		if socket == null then connect

		var album: nullable String = null
		var artist: nullable String = null
		var title: nullable String = null
		var time: nullable Int = null

		socket.write("currentsong\n")
		var rep = socket.read(1024)
		for l in rep.split_with("\n") do
			var words = l.split_with(" ")
			if words.length > 1 then
				var key = words[0].to_lower
				var first_space = l.chars.index_of(' ')
				var rest = l.substring_from(first_space+1)
				if key == "album:" then
					album = rest
				else if key == "artist:" then
					artist = rest
				else if key == "title:" then
					title = rest
				else if key == "time:" then
					time = rest.to_i
				end
			end
		end

		if album != null and artist != null and
			title != null and time != null then
			return new SongInfo(album, artist, title, time)
		else
			return null
		end
	end

	# Load playlist named `name`.
	fun load_playlist(name: String)
	do
		write_and_check "load \"{name}\""
	end
end
lib/mpd/mpd.nit:22,1--197,3