vlc-client 0.0.1.beta → 0.0.1.beta2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- ### Create a client and connect to a running VLC media play instance.
21
+ ### Create a client and connect to a running VLC media player instance.
22
22
 
23
23
  ```ruby
24
24
 
@@ -31,17 +31,19 @@ vlc = VLC::Client.new('192.168.1.10', 9999)
31
31
  vlc.connect
32
32
  # => true
33
33
  vlc.play('http://example.org/media.mp3') #play media
34
- => true
34
+ # => true
35
35
  vlc.playing?
36
- => true
36
+ # => true
37
37
  vlc.fullscreen
38
- => true
38
+ # => true
39
39
  #...
40
40
 
41
41
  ```
42
42
 
43
43
  ### Create a self managed client/server system.
44
- _(requires a local installation of VLC media player)_
44
+ _Most of the time we want a local client/server VLC media player system_
45
+
46
+ _(NOTE: requires a local installation of VLC media player)_
45
47
 
46
48
  ```ruby
47
49
 
@@ -49,27 +51,27 @@ vlc = VLC::System.new #A local VLC client/server system where a local VLC server
49
51
  # => "#<VLC::System:0x00000001bbb1a0 @client=#<VLC::Client:0x00000001bbade0 @server=#<VLC::Server:0x00000001bbb178 @headless=false, @port=9595, @host=\"localhost\", @pid=11225>, @connection=#<VLC::Connection:0x00000001bbacc8 @port=9595, @host=\"localhost\", @socket=#<TCPSocket:fd 5>>>>"
50
52
 
51
53
  vlc.connected? #auto_connect
52
- => true
54
+ # => true
53
55
 
54
56
  vlc.play('http://example.org/media.mp3')
55
- => true
57
+ # => true
56
58
 
57
59
  vlc.progress
58
- => 1 #%
60
+ # => 1 #%
59
61
  #...
60
62
 
61
- #Technically this the same as
63
+ #Technically this is the same as
62
64
  vlc = VLC::Client.new(VLC::Server.new('localhost', 9595, false))
63
65
  # => "#<VLC::Client:0x000000011de128 @server=#<VLC::Server:0x000000011de380 @headless=false, @port=9595, @host=\"localhost\", @pid=12656>, @connection=#<VLC::Connection:0x000000011de038 @port=9595, @host=\"localhost\", @socket=#<TCPSocket:fd 5>>>"
64
66
  ```
65
67
 
66
68
  ###Get local VLC server lifecycle management control
67
- _(requires a local installation of VLC media player)_
69
+ _(NOTE: requires a local installation of VLC media player)_
68
70
 
69
71
  ```ruby
70
72
 
71
73
  vlc = VLC::System.new('127.0.0.1', 9999, auto_start: false)
72
- => "#<VLC::System:0x00000001695f68 @client=#<VLC::Client:0x0000000169d718 @server=#<VLC::Server:0x00000001695ec8 @headless=false, @port=9999, @host=\"127.0.0.1\", @pid=VLC::NullObject>, @connection=#<VLC::Connection:0x0000000169d588 @port=9999, @host=\"127.0.0.1\", @socket=VLC::NullObject>>>"
74
+ # => "#<VLC::System:0x00000001695f68 @client=#<VLC::Client:0x0000000169d718 @server=#<VLC::Server:0x00000001695ec8 @headless=false, @port=9999, @host=\"127.0.0.1\", @pid=VLC::NullObject>, @connection=#<VLC::Connection:0x0000000169d588 @port=9999, @host=\"127.0.0.1\", @socket=VLC::NullObject>>>"
73
75
 
74
76
  vlc.server.running?
75
77
  # => false
data/lib/vlc-client.rb CHANGED
@@ -1,13 +1,10 @@
1
- require 'uri'
2
- require 'socket'
3
1
  require 'retryable'
4
2
 
5
- require 'vlc-client/version'
6
-
7
3
  require 'vlc-client/null_object'
8
-
9
4
  require 'vlc-client/core_ext/array'
10
5
 
6
+ require 'vlc-client/version'
7
+
11
8
  require 'vlc-client/server'
12
9
  require 'vlc-client/connection'
13
10
  require 'vlc-client/errors'
@@ -45,15 +42,17 @@ module VLC
45
42
  # @param [Hash] options
46
43
  # @option options [Boolean] :auto_start When false, the server lifecycle is not managed automatically and controll is passed to the developer
47
44
  # @option options [Integer] :conn_retries Number of connection retries (each separated by a second) to make on auto-connect. Defaults to 5.
45
+ # @option options [Boolean] :daemonize When true and only when on server auto-start mode, the server will be detached and run as a daemon process. Defaults to false.
48
46
  #
49
47
  # @example
50
48
  # vlc = VLC::Client.new(VLC::Server.new)
51
49
  # vlc.server.started?
52
- # => true
50
+ # #=> true
53
51
  #
52
+ # @example
54
53
  # vlc = VLC::Client.new(VLC::Server.new, auto_start: false)
55
54
  # vlc.server.started?
56
- # => false
55
+ # #=> false
57
56
  #
58
57
  # @return [VLC::VLC] a VLC client
59
58
  #
@@ -68,7 +67,7 @@ module VLC
68
67
  bind_server(server, options) unless server.nil?
69
68
  end
70
69
 
71
- private
70
+ private
72
71
  attr_reader :connection
73
72
 
74
73
  def bind_server(server, options = {})
@@ -77,7 +76,7 @@ module VLC
77
76
 
78
77
  if options.fetch(:auto_start, true)
79
78
  begin
80
- @server.start
79
+ options.fetch(:daemonize, false) ? @server.daemonize : @server.start
81
80
  retryable(:tries => options.fetch(:conn_retries, 5), :on => VLC::ConnectionRefused) { connect }
82
81
  rescue VLC::ConnectionRefused => e
83
82
  @server.stop
@@ -12,7 +12,6 @@ module VLC
12
12
  end
13
13
 
14
14
  # Queries if there is an active connection to VLC RC interface
15
- #
16
15
  def connected?
17
16
  connection.connected?
18
17
  end
@@ -1,3 +1,5 @@
1
+ require 'uri'
2
+
1
3
  module VLC
2
4
  class Client
3
5
  module MediaControls
@@ -24,19 +26,16 @@ module VLC
24
26
  end
25
27
 
26
28
  # Pauses playback
27
- #
28
29
  def pause
29
30
  connection.write("pause")
30
31
  end
31
32
 
32
33
  # Stops media currently playing
33
- #
34
34
  def stop
35
35
  connection.write("stop")
36
36
  end
37
37
 
38
38
  # Gets the title of the media at play
39
- #
40
39
  def title
41
40
  connection.write("get_title", false)
42
41
  end
@@ -71,18 +70,16 @@ module VLC
71
70
  end
72
71
 
73
72
  # Queries VLC if media is being played
74
- #
75
73
  def playing?
76
74
  connection.write("is_playing", false) == "1"
77
75
  end
78
76
 
79
77
  # Queries VLC if playback is currently stopped
80
- #
81
78
  def stopped?
82
79
  connection.write("is_playing", false) == "0"
83
80
  end
84
81
 
85
- private
82
+ private
86
83
  def media_arg(media)
87
84
  case media
88
85
  when File
@@ -1,5 +1,6 @@
1
+ require 'socket'
2
+
1
3
  module VLC
2
- #@ private
3
4
  #
4
5
  # Manages the connection to a VLC server
5
6
  #
@@ -9,13 +9,13 @@ module VLC
9
9
  #
10
10
  # @param [String] host The ip to bind to
11
11
  # @param [Integer] port the port
12
- # @param [Boolean] headless if true VLC media player will run headless.
12
+ # @param [Boolean] headless if true VLC media player will run in headless mode.
13
13
  # i.e. without a graphical interface. Defaults to false
14
14
  #
15
15
  def initialize(host = 'localhost', port = 9595, headless = false)
16
16
  @host, @port, @headless = host, port, headless
17
17
  @pid = NullObject.new
18
- setup_traps
18
+ @deamon = false
19
19
  end
20
20
 
21
21
  # Queries if VLC is running
@@ -36,20 +36,59 @@ module VLC
36
36
 
37
37
  # Starts a VLC instance in a subprocess
38
38
  #
39
+ # @param [Boolean] detached if true VLC will be started as a deamon process.
40
+ # Defaults to false.
41
+ #
39
42
  # @return [Integer] the subprocess PID or nil if the start command
40
43
  # as no effect (e.g. VLC already running)
41
44
  #
42
- def start
43
- return NullObject.new if running?
44
- @pid = Process.fork do
45
+ # @see #daemonize
46
+ #
47
+ def start(detached = false)
48
+ return @pid if running?
49
+ rd, wr = IO.pipe
50
+
51
+ detached ? @deamon = true : setup_traps
52
+ if Process.fork #parent
53
+ wr.close
54
+ @pid = rd.read.to_i
55
+ rd.close
56
+ return @pid
57
+ else #child
58
+ rd.close
59
+
60
+ detach if detached #daemonization
61
+
62
+ wr.write(Process.pid)
63
+ wr.close
64
+
45
65
  STDIN.reopen "/dev/null"
46
66
  STDOUT.reopen "/dev/null", "a"
47
67
  STDERR.reopen "/dev/null", "a"
48
68
 
49
- exec "#{headless? ? 'cvlc' : 'vlc'} --extraintf rc --rc-host #{@host}:#{@port}"
69
+ Kernel.exec "#{headless? ? 'cvlc' : 'vlc'} --extraintf rc --rc-host #{@host}:#{@port}"
50
70
  end
51
71
  end
52
72
 
73
+ # Start a VLC instance as a system deamon
74
+ #
75
+ #
76
+ # @return [Integer] the subprocess PID or nil if the start command
77
+ # as no effect (e.g. VLC already running)
78
+ # @see Server#start
79
+ #
80
+ def daemonize
81
+ start(true)
82
+ end
83
+
84
+ # Queries if VLC is running in daemonized mode
85
+ #
86
+ # @see #daemonize
87
+ #
88
+ def daemonized?
89
+ @deamon == true
90
+ end
91
+
53
92
  # Starts a VLC instance in a subprocess
54
93
  #
55
94
  # @return [Integer] the terminated subprocess PID or nil if the stop command
@@ -63,11 +102,21 @@ module VLC
63
102
  pid
64
103
  end
65
104
 
66
- private
105
+ private
67
106
  def setup_traps
68
107
  trap("EXIT") { stop }
69
108
  trap("INT") { stop }
70
- trap("CLD") { stop }
109
+ trap("CLD") { @pid = NullObject.new }
110
+ end
111
+
112
+ def detach
113
+ if RUBY_VERSION < "1.9"
114
+ Process.setsid
115
+ exit if Process.fork
116
+ Dir.chdir "/"
117
+ else
118
+ Process.daemon
119
+ end
71
120
  end
72
121
  end
73
- end
122
+ end
@@ -10,8 +10,10 @@ module VLC
10
10
  # @param [String] host The ip to connect to
11
11
  # @param [Integer] port the port
12
12
  # @param [Hash] options
13
+ # @option options [Boolean] :headless If true VLC run in headless mode. i.e. without a graphical interface. Defaults to false.
13
14
  # @option options [Boolean] :auto_start When false, the server lifecycle is not managed automatically and controll is passed to the developer
14
15
  # @option options [Integer] :conn_retries Number of connection retries (each separated by a second) to make on auto-connect. Defaults to 5.
16
+ # @option options [Boolean] :daemonize When true and only when on server auto-start mode, the server will be detached and run as a daemon process. Defaults to false.
15
17
  #
16
18
  # @example
17
19
  # vlc = VLC::System.new('10.10.0.10', 9000)
@@ -1,3 +1,3 @@
1
1
  module VLC
2
- VERSION = "0.0.1.beta"
2
+ VERSION = "0.0.1.beta2"
3
3
  end
data/spec/helper.rb CHANGED
@@ -25,8 +25,34 @@ module Mocks
25
25
  end
26
26
 
27
27
  def mock_system_calls(opts = {})
28
- Process.stub(:fork).and_return(1)
29
- Process.should_receive(:kill).once.with('INT', 1) if opts.fetch(:kill, true)
28
+ if opts.fetch(:daemonized, false)
29
+ Process.should_receive(:fork).once.ordered.and_return(false)
30
+
31
+ if RUBY_VERSION < "1.9"
32
+ Process.should_receive(:setsid).once.ordered
33
+ Process.should_receive(:fork).once.ordered.and_return(false)
34
+ Dir.should_receive(:chdir).once.with("/")
35
+ else
36
+ Process.should_receive(:daemon).once
37
+ end
38
+ Process.should_receive(:pid).once.and_return(99)
39
+
40
+ rd = stub('rd', :close => true)
41
+ wd = stub('wd', :write => true, :close => true)
42
+ IO.stub(:pipe).and_return([rd, wd])
43
+
44
+ [STDIN, STDOUT, STDERR].each { |std| std.stub(:reopen) }
45
+
46
+ Kernel.stub(:exec)
47
+ else
48
+ Process.stub(:fork).and_return(true)
49
+
50
+ rd = stub('rd', :read => 99, :close => true)
51
+ wd = stub('wd', :close => true)
52
+ IO.stub(:pipe).and_return([rd, wd])
53
+
54
+ Process.should_receive(:kill).once.with('INT', 99) if opts.fetch(:kill, true)
55
+ end
30
56
  end
31
57
 
32
58
  def mock_sub_systems
data/spec/system_spec.rb CHANGED
@@ -1,11 +1,8 @@
1
1
  describe VLC::System do
2
- before(:each) do
3
- mock_system_calls(:kill => false)
4
- end
5
-
6
2
  subject { VLC::System.new }
7
3
 
8
4
  it 'creates a self-managed VLC media client/server local system' do
5
+ mock_system_calls(:kill => false)
9
6
  mock_tcp_server(:close => false)
10
7
 
11
8
  subject.client.should be_a(VLC::Client)
@@ -13,6 +10,7 @@ describe VLC::System do
13
10
  end
14
11
 
15
12
  it 'delegates calls to the client' do
13
+ mock_system_calls(:kill => false)
16
14
  mock_tcp_server(:close => false).should_receive(:puts).with('is_playing').once
17
15
 
18
16
  should respond_to(:play)
@@ -20,6 +18,7 @@ describe VLC::System do
20
18
  end
21
19
 
22
20
  it 'handles server lifecycle management to client code' do
21
+ mock_system_calls(:kill => false)
23
22
  mock_tcp_server(:close => false)
24
23
 
25
24
  vlc = VLC::System.new('127.0.0.1', 9999, :auto_start => false)
@@ -28,4 +27,12 @@ describe VLC::System do
28
27
  vlc.server.host.should eq('127.0.0.1')
29
28
  vlc.server.port.should eq(9999)
30
29
  end
30
+
31
+ it 'runs server as a daemon process' do
32
+ mock_system_calls(:kill => false, :daemonized => true)
33
+ mock_tcp_server(:close => false)
34
+
35
+ vlc = VLC::System.new(:daemonize => true)
36
+ vlc.server.should be_daemonized
37
+ end
31
38
  end
@@ -1,16 +1,26 @@
1
1
  describe VLC::Server do
2
- let!(:server) do
3
- mock_system_calls
4
- server = VLC::Server.new('localhost', 9595, true)
5
- end
2
+ let(:server) { VLC::Server.new('localhost', 9595, true) }
6
3
  after(:each) { server.stop }
7
4
 
8
- it 'starts a VLC instance' do
9
- server.start.should_not be_nil
10
- server.should be_running
5
+ context 'starts a VLC instance' do
6
+ it 'as a child process' do
7
+ mock_system_calls
8
+
9
+ server.start.should_not be_nil
10
+ server.should be_running
11
+ end
12
+
13
+ it 'as a deamon' do
14
+ mock_system_calls(:daemonized => true)
15
+
16
+ server.daemonize
17
+ server.should be_daemonized
18
+ end
11
19
  end
12
20
 
13
21
  it 'stops a VLC instance' do
22
+ mock_system_calls
23
+
14
24
  server.start
15
25
 
16
26
  server.stop.should_not be_nil
@@ -14,7 +14,7 @@ describe VLC::Client do
14
14
  vlc.port.should eq(9999)
15
15
  end
16
16
 
17
- it 'is self managed if server is given' do
17
+ it 'is self managed if a server is given' do
18
18
  mock_system_calls(:kill => false)
19
19
 
20
20
  vlc = VLC::Client.new(VLC::Server.new('10.0.0.1', 9999))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vlc-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta
4
+ version: 0.0.1.beta2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-28 00:00:00.000000000 Z
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: retryable
16
- requirement: &16536620 !ruby/object:Gem::Requirement
16
+ requirement: &11273980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *16536620
24
+ version_requirements: *11273980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &16535440 !ruby/object:Gem::Requirement
27
+ requirement: &11272940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *16535440
35
+ version_requirements: *11272940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &16534580 !ruby/object:Gem::Requirement
38
+ requirement: &11271820 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *16534580
46
+ version_requirements: *11271820
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: simplecov
49
- requirement: &16533480 !ruby/object:Gem::Requirement
49
+ requirement: &11271200 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *16533480
57
+ version_requirements: *11271200
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &16532400 !ruby/object:Gem::Requirement
60
+ requirement: &11269520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *16532400
68
+ version_requirements: *11269520
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
- requirement: &16531440 !ruby/object:Gem::Requirement
71
+ requirement: &11268560 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *16531440
79
+ version_requirements: *11268560
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: maruku
82
- requirement: &16515700 !ruby/object:Gem::Requirement
82
+ requirement: &11267320 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *16515700
90
+ version_requirements: *11267320
91
91
  description: vlc-client allows to control VLC media player over TCP
92
92
  email:
93
93
  - mguinada@gmail.com