def connect( callbacks = false )
if self.connected?
raise 'MPD Error: Already Connected'
end
@socket = TCPSocket::new @hostname, @port
ret = @socket.gets
if callbacks and (@cb_thread.nil? or !@cb_thread.alive?)
@stop_cb_thread = false
@cb_thread = Thread.new( self ) { |mpd|
old_status = {}
song = ''
connected = ''
while !@stop_cb_thread
begin
status = mpd.status
rescue
status = {}
end
begin
c = mpd.connected?
rescue
c = false
end
if connected != c
connected = c
for cb in @callbacks[CONNECTION_CALLBACK]
cb.call connected
end
end
if old_status['time'] != status['time']
if old_status['time'].nil? or old_status['time'].empty?
old_status['time'] = '0:0'
end
t = old_status['time'].split ':'
elapsed = t[0].to_i
total = t[1].to_i
for cb in @callbacks[TIME_CALLBACK]
cb.call elapsed, total
end
end
if old_status['volume'] != status['volume']
for cb in @callbacks[VOLUME_CALLBACK]
cb.call status['volume'].to_i
end
end
if old_status['repeat'] != status['repeat']
for cb in @callbacks[REPEAT_CALLBACK]
cb.call(status['repeat'] == '1')
end
end
if old_status['random'] != status['random']
for cb in @callbacks[RANDOM_CALLBACK]
cb.call(status['random'] == '1')
end
end
if old_status['playlist'] != status['playlist']
for cb in @callbacks[PLAYLIST_CALLBACK]
cb.call status['playlist'].to_i
end
end
if old_status['playlistlength'] != status['playlistlength']
for cb in @callbacks[PLAYLIST_LENGTH_CALLBACK]
cb.call status['playlistlength'].to_i
end
end
if old_status['xfade'] != status['xfade']
for cb in @callbacks[CROSSFADE_CALLBACK]
cb.call status['xfade'].to_i
end
end
if old_status['state'] != status['state']
state = (status['state'].nil? ? '' : status['state'])
for cb in @callbacks[STATE_CALLBACK]
cb.call state
end
end
begin
s = mpd.current_song
rescue
s = nil
end
if song != s
song = s
for cb in @callbacks[CURRENT_SONG_CALLBACK]
cb.call song
end
end
if old_status['songid'] != status['songid']
for cb in @callbacks[CURRENT_SONGID_CALLBACK]
cb.call status['songid'].to_i
end
end
if old_status['bitrate'] != status['bitrate']
for cb in @callbacks[BITRATE_CALLBACK]
cb.call status['bitrate'].to_i
end
end
if old_status['audio'] != status['audio']
audio = (status['audio'].nil? ? '0:0:0' : status['audio'])
a = audio.split ':'
samp = a[0].to_i
bits = a[1].to_i
chans = a[2].to_i
for cb in @callbacks[AUDIO_CALLBACK]
cb.call samp, bits, chans
end
end
old_status = status
sleep 0.1
if !connected
sleep 2
begin
mpd.connect unless @stop_cb_thread
rescue
end
end
end
}
end
return ret
end