Class: MPD
- Inherits:
-
Object
- Object
- MPD
- Includes:
- Parser, Plugins::Channels, Plugins::Controls, Plugins::Database, Plugins::Information, Plugins::Outputs, Plugins::PlaybackOptions, Plugins::Playlists, Plugins::Queue, Plugins::Reflection, Plugins::Stickers
- Defined in:
- lib/ruby-mpd.rb,
lib/ruby-mpd/song.rb,
lib/ruby-mpd/parser.rb,
lib/ruby-mpd/version.rb,
lib/ruby-mpd/playlist.rb,
lib/ruby-mpd/exceptions.rb,
lib/ruby-mpd/plugins/queue.rb,
lib/ruby-mpd/plugins/outputs.rb,
lib/ruby-mpd/plugins/channels.rb,
lib/ruby-mpd/plugins/controls.rb,
lib/ruby-mpd/plugins/database.rb,
lib/ruby-mpd/plugins/stickers.rb,
lib/ruby-mpd/plugins/playlists.rb,
lib/ruby-mpd/plugins/reflection.rb,
lib/ruby-mpd/plugins/information.rb,
lib/ruby-mpd/plugins/playback_options.rb
Overview
The main class/namespace of the MPD client.
Defined Under Namespace
Modules: Parser, Plugins Classes: AlreadyExists, AlreadyUpdating, ConnectionError, Error, IncorrectPassword, NotFound, NotListError, NotPlaying, PermissionError, Playlist, PlaylistLoadError, PlaylistMaxError, ServerArgumentError, ServerError, Song, SystemError
Constant Summary collapse
- VERSION =
'0.3.1'
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#connect(callbacks = nil) ⇒ true
Connect to the daemon.
-
#connected? ⇒ Boolean
Check if the client is connected.
-
#disconnect ⇒ Boolean
Disconnect from the MPD daemon.
-
#emit(event, *args) ⇒ void
Triggers an event, running it’s callbacks.
-
#initialize(hostname = 'localhost', port = 6600, opts = {}) ⇒ MPD
constructor
Initialize an MPD object with the specified hostname and port.
-
#kill ⇒ Boolean
Kills the MPD process.
-
#on(event, &block) ⇒ void
This will register a block callback that will trigger whenever that specific event happens.
-
#password(pass) ⇒ Boolean
Used for authentication with the server.
-
#ping ⇒ Boolean
Ping the server.
-
#reconnect ⇒ Boolean
Attempts to reconnect to the MPD daemon.
-
#send_command(command, *args) ⇒ true, ...
Used to send a command to the server, and to recieve the reply.
Methods included from Plugins::Channels
#channels, #readmessages, #sendmessage, #subscribe, #unsubscribe
Methods included from Plugins::Reflection
#commands, #config, #decoders, #notcommands, #tags, #url_handlers
Methods included from Plugins::Outputs
#disableoutput, #enableoutput, #outputs
Methods included from Plugins::Stickers
#delete_sticker, #find_sticker, #get_sticker, #list_stickers, #set_sticker
Methods included from Plugins::Database
#albums, #artists, #count, #directories, #files, #list, #rescan, #search, #songs, #songs_by_artist, #update, #where
Methods included from Plugins::Playlists
Methods included from Plugins::Queue
#add, #addid, #clear, #delete, #move, #queue, #queue_changes, #queue_search, #queue_where, #save, #shuffle, #song_priority, #song_with_id, #swap, #swapid
Methods included from Plugins::Controls
#next, #pause=, #play, #previous, #seek, #stop
Methods included from Plugins::PlaybackOptions
#consume=, #crossfade=, #mixrampdb=, #mixrampdelay=, #random=, #repeat=, #replay_gain_mode=, #replay_gain_status, #single=, #volume=
Methods included from Plugins::Information
#clearerror, #consume?, #crossfade, #current_song, #paused?, #playing?, #playlist_version, #random?, #repeat?, #single?, #stats, #status, #stopped?, #volume
Constructor Details
#initialize(hostname = 'localhost', port = 6600, opts = {}) ⇒ MPD
Initialize an MPD object with the specified hostname and port. When called without arguments, ‘localhost’ and 6600 are used.
When called with callbacks: true as an optional argument, callbacks will be enabled by starting a separate polling thread.
53 54 55 56 57 58 59 60 61 |
# File 'lib/ruby-mpd.rb', line 53 def initialize(hostname = 'localhost', port = 6600, opts = {}) @hostname = hostname @port = port @options = {callbacks: false}.merge(opts) reset_vars @mutex = Mutex.new @callbacks = {} end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
42 43 44 |
# File 'lib/ruby-mpd.rb', line 42 def hostname @hostname end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
42 43 44 |
# File 'lib/ruby-mpd.rb', line 42 def port @port end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
42 43 44 |
# File 'lib/ruby-mpd.rb', line 42 def version @version end |
Instance Method Details
#connect(callbacks = nil) ⇒ true
Connect to the daemon.
When called without any arguments, this will just connect to the server and wait for your commands.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ruby-mpd.rb', line 141 def connect(callbacks = nil) raise ConnectionError, 'Already connected!' if self.connected? @socket = File.exists?(@hostname) ? UNIXSocket.new(@hostname) : TCPSocket.new(@hostname, @port) # by protocol, we need to get a 'OK MPD <version>' reply # should we fail to do so, the connection was unsuccessful unless response = @socket.gets reset_vars raise ConnectionError, 'Unable to connect (possibly too many connections open)' end @version = response.chomp.gsub('OK MPD ', '') # Read the version if callbacks warn "Using 'true' or 'false' as an argument to MPD#connect has been deprecated, and will be removed in the future!" @options.merge!(callbacks: callbacks) end callback_thread if @options[:callbacks] return true end |
#connected? ⇒ Boolean
Check if the client is connected.
166 167 168 169 |
# File 'lib/ruby-mpd.rb', line 166 def connected? return false unless @socket send_command(:ping) rescue false end |
#disconnect ⇒ Boolean
Disconnect from the MPD daemon. This has no effect if the client is not connected. Reconnect using the #connect method. This will also stop the callback thread, thus disabling callbacks.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/ruby-mpd.rb', line 175 def disconnect @cb_thread[:stop] = true if @cb_thread return false unless @socket begin @socket.puts 'close' @socket.close rescue Errno::EPIPE # socket was forcefully closed end reset_vars return true end |
#emit(event, *args) ⇒ void
This method returns an undefined value.
Triggers an event, running it’s callbacks.
94 95 96 97 |
# File 'lib/ruby-mpd.rb', line 94 def emit(event, *args) return unless @callbacks[event] @callbacks[event].each { |handle| handle.call *args } end |
#kill ⇒ Boolean
Kills the MPD process.
200 201 202 |
# File 'lib/ruby-mpd.rb', line 200 def kill send_command :kill end |
#on(event, &block) ⇒ void
This method returns an undefined value.
This will register a block callback that will trigger whenever that specific event happens.
mpd.on :volume do |volume|
puts "Volume was set to #{volume}!"
end
One can also define separate methods or Procs and whatnot, just pass them in as a parameter.
method = Proc.new {|volume| puts "Volume was set to #{volume}!" }
mpd.on :volume, &method
87 88 89 |
# File 'lib/ruby-mpd.rb', line 87 def on(event, &block) (@callbacks[event] ||= []).push block end |
#password(pass) ⇒ Boolean
Used for authentication with the server.
207 208 209 |
# File 'lib/ruby-mpd.rb', line 207 def password(pass) send_command :password, pass end |
#ping ⇒ Boolean
Ping the server.
213 214 215 |
# File 'lib/ruby-mpd.rb', line 213 def ping send_command :ping end |
#reconnect ⇒ Boolean
Attempts to reconnect to the MPD daemon.
193 194 195 196 |
# File 'lib/ruby-mpd.rb', line 193 def reconnect disconnect connect end |
#send_command(command, *args) ⇒ true, ...
Used to send a command to the server, and to recieve the reply. Reply gets parsed. Synchronized on a mutex to be thread safe.
Can be used to get low level direct access to MPD daemon. Not recommended, should be just left for internal use by other methods.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/ruby-mpd.rb', line 226 def send_command(command, *args) raise ConnectionError, "Not connected to the server!" if !@socket @mutex.synchronize do begin @socket.puts convert_command(command, *args) response = handle_server_response return parse_response(command, response) rescue Errno::EPIPE reconnect retry end end end |