Class: Moc::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/moc/controller.rb,
lib/moc/controller/tags.rb,
lib/moc/controller/player.rb,
lib/moc/controller/status.rb,
lib/moc/controller/toggle.rb,
lib/moc/controller/playlist.rb

Overview

This is the main class to manage moc.

The class also acts as a UNIXSocket if needed.

Defined Under Namespace

Classes: FileTags, Move, Player, Playlist, Status, Tags, Toggle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = '~/.moc/socket2') ⇒ Controller

Returns a new instance of Controller.



33
34
35
36
37
38
# File 'lib/moc/controller.rb', line 33

def initialize (path = '~/.moc/socket2')
	@path   = File.expand_path(path)
	@socket = UNIXSocket.new(@path)

	@queue = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/moc/controller.rb', line 44

def method_missing (id, *args, &block)
	if @socket.respond_to? id
		return @socket.__send__ id, *args, &block
	end

	super
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/moc/controller.rb', line 31

def path
  @path
end

Class Method Details

.symbol_to_code(sym) ⇒ Object



27
28
29
# File 'lib/moc/controller.rb', line 27

def self.symbol_to_code (sym)
	return Commands[sym.to_sym.upcase]
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'lib/moc/controller.rb', line 207

def active?
	send_command :ping

	read_event == :pong
rescue Exception
	false
end

#inspectObject



249
250
251
# File 'lib/moc/controller.rb', line 249

def inspect
	"#<#{self.class.name}: #{@path}>"
end

#lockObject



221
222
223
# File 'lib/moc/controller.rb', line 221

def lock
	send_command :lock
end

#loop(&block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/moc/controller.rb', line 175

def loop (&block)
	raise ArgumentError, 'no block given' unless block

	raise 'already looping' if looping?

	@looping = true

	send_command :send_events

	while event = read_event
		block.call event

		until @queue.empty?
			block.call @queue.first

			@queue.shift
		end
	end

	self
ensure
	@looping = false
end

#looping?Boolean

Returns:

  • (Boolean)


173
# File 'lib/moc/controller.rb', line 173

def looping?; !!@looping; end

#playerObject



241
242
243
# File 'lib/moc/controller.rb', line 241

def player
	@player ||= Player.new(self)
end

#quit!Object



215
216
217
218
219
# File 'lib/moc/controller.rb', line 215

def quit!
	send_command :quit

	self
end

#read_eventObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/moc/controller.rb', line 115

def read_event
	Protocol::Event.read(@socket).tap {|event|
		event.data = case event.to_sym
			when :PLIST_ADD, :QUEUE_ADD
				read_item
			
			when :PLIST_DEL, :QUEUE_DEL, :STATUS_MSG
				read_string

			when :FILE_TAGS
				FileTags.new(read_string, read_tags)

			when :PLIST_MOVE, :QUEUE_MOVE
				Move.new(read_string, read_string)
		end
	}
end

#read_integerObject



107
108
109
# File 'lib/moc/controller.rb', line 107

def read_integer
	Protocol::Integer.read(@socket)
end

#read_itemObject



137
138
139
140
141
142
143
# File 'lib/moc/controller.rb', line 137

def read_item
	return if (file = read_string.to_s).empty?

	title_tags = read_string.to_s

	Playlist::Item.new(file, title_tags.empty? ? nil : title_tags, read_tags, read_time)
end

#read_stateObject



133
134
135
# File 'lib/moc/controller.rb', line 133

def read_state
	Protocol::State.read(@socket)
end

#read_stringObject



103
104
105
# File 'lib/moc/controller.rb', line 103

def read_string
	Protocol::String.read(@socket)
end

#read_tagsObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/moc/controller.rb', line 145

def read_tags
	title  = read_string.to_s
	artist = read_string.to_s
	album  = read_string.to_s
	track  = read_integer.to_i
	time   = read_integer.to_i
	filled = read_integer.to_i

	title  = nil if title.empty?
	artist = nil if artist.empty?
	album  = nil if album.empty?
	track  = nil if track == -1

	if title.nil? && artist.nil? && album.nil? && track.nil? && time == -1 && filled == 0
		return nil
	end

	Tags.new(title, artist, album, track, time)
end

#read_timeObject



111
112
113
# File 'lib/moc/controller.rb', line 111

def read_time
	Protocol::Time.read(@socket)
end

#respond_to_missing?(id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/moc/controller.rb', line 40

def respond_to_missing? (id, include_private = false)
	@socket.respond_to? id, include_private
end

#send(object) ⇒ Object



52
53
54
55
56
# File 'lib/moc/controller.rb', line 52

def send (object)
	@socket.write object.respond_to?(:pack) ? object.pack : object.to_s

	self
end

#send_command(command) ⇒ Object



58
59
60
# File 'lib/moc/controller.rb', line 58

def send_command (command)
	send(command.is_a?(Protocol::Command) ? command : Protocol::Command.new(command))
end

#send_integer(int) ⇒ Object



62
63
64
# File 'lib/moc/controller.rb', line 62

def send_integer (int)
	send Protocol::Integer.new(int.to_i)
end

#send_item(item) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/moc/controller.rb', line 74

def send_item (item)
	if item
		send_string item.file
		send_string item.title_tags || ''
		send_tags   item.tags
		send_time   item.mtime
	else
		send_string ''
	end
end

#send_string(string) ⇒ Object



70
71
72
# File 'lib/moc/controller.rb', line 70

def send_string (string)
	send Protocol::String.new(string.to_s)
end

#send_tags(tags) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/moc/controller.rb', line 85

def send_tags (tags)
	if tags
		send_string  tags.title
		send_string  tags.artist
		send_string  tags.album
		send_integer tags.track
		send_integer tags.time
		send_integer 0x01 | 0x02
	else
		send_string  ''
		send_string  ''
		send_string  ''
		send_integer -1
		send_integer -1
		send_integer 0
	end
end

#send_time(time) ⇒ Object



66
67
68
# File 'lib/moc/controller.rb', line 66

def send_time (time)
	send Protocol::Time.at(time.to_i)
end

#status(live = false) ⇒ Object



245
246
247
# File 'lib/moc/controller.rb', line 245

def status (live = false)
	live ? (@status ||= Status::Live.new(self)) : Status.new(self)
end

#synchronize(&block) ⇒ Object



229
230
231
232
233
234
235
# File 'lib/moc/controller.rb', line 229

def synchronize (&block)
	lock

	block.call
ensure
	unlock
end

#toggleObject



237
238
239
# File 'lib/moc/controller.rb', line 237

def toggle
	@toggle ||= Toggle.new(self)
end

#unlockObject



225
226
227
# File 'lib/moc/controller.rb', line 225

def unlock
	send_command :unlock
end

#wait_for(name) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/moc/controller.rb', line 199

def wait_for (name)
	while (event = read_event) != name
		@queue << event if looping?
	end

	event
end