Class: Audioscrobbler::SubmissionQueue
- Inherits:
-
Object
- Object
- Audioscrobbler::SubmissionQueue
- Defined in:
- lib/audioscrobbler.rb
Overview
A synchronized, backed-up-to-disk queue holding tracks for submission. The synchronization is only sufficient for a single reader and writer.
Defined Under Namespace
Classes: PlayedTrack
Instance Method Summary collapse
-
#append(artist, title, length, start_time, album = "", mbid = "", track_num = nil) ⇒ Object
Append a played track to the submission queue.
-
#delete(num_tracks) ⇒ Object
Delete tracks from the beginning of the queue.
-
#initialize(filename = nil) ⇒ SubmissionQueue
constructor
constructor If a filename is supplied, it will be used to: a) load tracks that were previously played but not submitted b) save the state of the queue for later use in a).
-
#peek(max_tracks = 1) ⇒ Object
Get tracks from the beginning of the queue.
Constructor Details
#initialize(filename = nil) ⇒ SubmissionQueue
constructor If a filename is supplied, it will be used to: a) load tracks that were previously played but not submitted b) save the state of the queue for later use in a)
479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/audioscrobbler.rb', line 479 def initialize(filename=nil) @filename = filename @queue = Array.new @mutex = Mutex.new @condvar = ConditionVariable.new if @filename and File.exist?(@filename) File.open(@filename, "r").each do |line| @queue.push(PlayedTrack.deserialize(line.chomp)) end end end |
Instance Method Details
#append(artist, title, length, start_time, album = "", mbid = "", track_num = nil) ⇒ Object
Append a played track to the submission queue.
502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/audioscrobbler.rb', line 502 def append(artist, title, length, start_time, album="", mbid="", track_num=nil) @mutex.synchronize do track = PlayedTrack.new(artist, title, length, start_time, album, mbid, track_num) File.open(@filename, "a") {|f| f.puts(track.serialize) } if @filename @queue.push(track) @condvar.signal end self end |
#delete(num_tracks) ⇒ Object
Delete tracks from the beginning of the queue. If more tracks are requested than are in the queue, the queue is cleared.
536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/audioscrobbler.rb', line 536 def delete(num_tracks) if not @queue.empty? @mutex.synchronize do @queue = @queue[num_tracks..@queue.length-1] if @filename file = File.open(@filename, "w") @queue.each {|track| file.puts(track.serialize) } file.close end end end self end |
#peek(max_tracks = 1) ⇒ Object
Get tracks from the beginning of the queue. An array of PlayedTrack objects is returned. The tracks are not removed from the queue. If the queue is empty, the method blocks until a track is placed on the queue.
522 523 524 525 526 527 |
# File 'lib/audioscrobbler.rb', line 522 def peek(max_tracks=1) @mutex.synchronize do @condvar.wait(@mutex) if @queue.empty? @queue[0, max_tracks] end end |