⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 17 - a remote-controlled jukebox.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
#!/usr/bin/ruby# rinda_server.rbrequire 'rinda/ring'        # for RingServerrequire 'rinda/tuplespace'  # for TupleSpaceDRb.start_service# Create a TupleSpace to hold named services, and start running.Rinda::RingServer.new(Rinda::TupleSpace.new)DRb.thread.join#---#!/usr/bin/ruby -w# jukebox_server.rbrequire 'drb'require 'rinda/ring'require 'rinda/tuplespace'require 'thread'require 'find'DRb.start_serviceclass Jukebox  include DRbUndumped  attr_reader :now_playing, :running  def initialize(files)    @files = files    @songs = @files.keys    @now_playing = nil    @queue = []  end  def play_queue    Thread.new(self) do      @running = true      while @running         if @queue.empty?          play songs[rand(songs.size)]        else          play @queue.shift        end      end    end  end#---  # Adds a song to the queue. Returns the new size of the queue.  def <<(song)    raise ArgumentError, 'No such song' unless @files[song]    @queue.push song    return @queue.size  end    # Returns the current queue of songs.  def queue    return @queue.clone.freeze  end  # Returns the titles of songs that match the given regexp.  def songs(regexp=/.*/)    return @songs.grep(regexp).sort  end  # Turns the jukebox on or off.  def running=(value)    @running = value    play_queue if @running  end#---  private  # Play the given through this computer's sound system, using a  # previously installed music player.  def play(song)    @now_playing = song    path = @files[song]    player = path[-4..path.size] == '.mp3' ? 'mpg123' : 'ogg123'         command = %{#{player} "#{path}"}    # The player and path both come from local data, so it's safe to    # untaint them.    command.untaint    system(command)  endend#---if ARGV.empty?  puts "Usage: #{__FILE__} [directory full of MP3s and/or OGGs] ..."  exitelse  songs = {}    Find.find(*ARGV) do |path|    if path =~ /\.(mp3|ogg)$/      name = File.split(path)[1][0..-5]      songs[name] = path    end  endendjukebox = Jukebox.new(songs)#---# Set safe before we start accepting connections from outside.  $SAFE = 1puts "Registering..."# Register the Jukebox with the local RingServer, under its class name.ring_server = Rinda::RingFinger.primaryring_server.write([:name, :Jukebox, jukebox, "Remote-controlled jukebox"],                  Rinda::SimpleRenewer.new)#---jukebox.play_queueDRb.thread.join#---require 'rinda/ring'require 'rinda/tuplespace'DRb.start_servicering_server = Rinda::RingFinger.primaryjukebox = ring_server.read([:name, :Jukebox, nil, nil])[2]jukebox.now_playing                                # => "Chickadee"jukebox.songs(/D/)# => ["ID 3", "Don't Leave Me Here (Over There Would Be Fine)"]jukebox << 'ID 3'                                  # => 1jukebox << "Attack of the Good Ol' Boys from Planet Honky-Tonk"# ArgumentError: No such songjukebox.queue                                      # => ["ID 3"]#---#!/usr/bin/ruby -w# jukebox_client.rbrequire 'rinda/ring'NO_ARG_COMMANDS = %w{start stop now-playing queue}ARG_COMMANDS = %w{grep append grep-and-append}COMMANDS = NO_ARG_COMMANDS + ARG_COMMANDSdef usage  puts "Usage: #{__FILE__} [#{COMMANDS.join('|')}] [ARG]"  exitendusage if ARGV.size < 1 or ARGV.size > 2command = ARGV[0]argument = nilusage unless COMMANDS.index(command)if ARG_COMMANDS.index(command)  if ARGV.size == 1    puts "Command #{command} takes an argument."    exit  else    argument = ARGV[1]  endelsif ARGV.size == 2  puts "Command #{command} takes no argument."  exitend#---DRb.start_servicering_server = Rinda::RingFinger.primaryjukebox = ring_server.read([:name, :Jukebox, nil, nil])[2]#---case commandwhen 'start' then  if jukebox.running    puts 'Already running.'  else    jukebox.running = true    puts 'Started.'  endwhen 'stop' then  if jukebox.running    jukebox.running = false    puts 'Jukebox will stop after current song.'  else    puts 'Already stopped.'  endwhen 'now-playing' then   puts "Currently playing: #{jukebox.now_playing}"when 'queue' then  jukebox.queue.each { |song| puts song }when 'grep'   jukebox.songs(Regexp.compile(argument)).each { |song| puts song }when 'append' then  jukebox  << argument  jukebox.queue.each { |song| puts song }when 'grep-and-append' then  jukebox.songs(Regexp.compile(argument)).each { |song| jukebox << song }  jukebox.queue.each { |song| puts song }end#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -