Skip to content
This repository has been archived by the owner on Oct 18, 2019. It is now read-only.

Commit

Permalink
The mpg123 player client is now a non-ActiveRecord model in app/.
Browse files Browse the repository at this point in the history
  • Loading branch information
smashwilson committed Feb 26, 2012
1 parent ba4a168 commit adab374
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
4 changes: 1 addition & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require 'mpg123player/client'

class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :create_player_client

private

def create_player_client
@player = Mpg123Player::Client.new
@player = Player.new
@player.ok?

@status = @player.status
Expand Down
48 changes: 19 additions & 29 deletions lib/mpg123player/client.rb → app/models/player.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
require 'socket'

require 'mpg123player/common'
require 'active_support/json'

module Mpg123Player

class Client
# Non-ActiveRecord model. Manage status and communications with the mpg123 player process.
class Player
include Mpg123Player
include Configurable

attr_reader :error

def initialize
configure
end

# Controls

def play ; command 'play' ; end
def pause ; command 'pause' ; end
def volume percent ; command 'volume', percent ; end
def restart ; command 'restart' ; end
def skip ; command 'skip' ; end
def stop ; command 'stop' ; end
def shutdown ; command 'shutdown' ; end

# Connect to the server, issue a command, and disconnect.
def command string
unless Commands.include? string
@error = "Invalid command: #{string}"
return
end
socket = TCPSocket.new('localhost', @server_port)
rescue e
@error = "Connection failed: #{e}"
else
socket.puts string
socket.close
# Issue a command to the server. Return the command object.
def command string, parameter = nil
PlayerCommand.create!(:action => string, :parameter => parameter)
end

# Status

def ok?
unless File.readable?(@pid_path)
@error = "Can't read the pid file at #{@pid_path}!"
Expand All @@ -53,31 +45,29 @@ def ok?
else
true
end

def status
@status ||= Status.from(JSON(status_json))
end

def status_json
if File.exist?(@status_path)
File.open(@status_path) { |f| f.gets(nil) }
else
Status.stopped.to_json
end
end

def playing?
status.playback_state == 'playing'
end

def paused?
status.playback_state == 'paused'
end

def stopped?
status.playback_state == 'stopped'
end

end

end

0 comments on commit adab374

Please sign in to comment.