dmap-ng 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jack Chen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,130 @@
1
+ # dmap-ng
2
+
3
+ dmap-ng is an improved version of the original dmap gem, rewritten from the ground up with a more detailed test suite and has awesome features like a DSL and supports a more extensive part of the DMAP protocol.
4
+
5
+ ## What is DMAP?
6
+
7
+ DMAP stands for Digital Media Access Protocol which is the data serialisation format used in the [DAAP](http://en.wikipedia.org/wiki/Digital_Audio_Access_Protocol) protocol mainly used in iTunes for media sharing and remote control.
8
+
9
+ ## Why should I care?
10
+
11
+ You should care if you want to write anything that interacts with iTunes/DAAP-compatible media server or a DAAP client.
12
+
13
+ ## What this currently being used for
14
+
15
+ I wanted to write a gem that emulates an iTunes DJ server so the Remote.app on the iPhone/iTouch/iPad can interface with any app that has voting, so it was my Railscamp 7 project. I wrote this gem over the two days at Railscamp, using the tag list from jphasting's dmap gem but writing everything from scratch. It has enough tag support to get the Remote.app to show arbitrary data with the gem I'll upload once it's in a decent state named 'deejay'.
16
+
17
+ ## How to use
18
+
19
+ Real easy. Check this out:
20
+
21
+ This is a aply (playlists) entry.
22
+
23
+ playlists = DMAP.parse("aply\000\000\001\nmstt\000\000\000\004\000\000\000\310muty\000\000\000\001\000mtco\000\000\000\004\000\000\000\002mrco\000\000\000\004\000\000\000\002mlcl\000\000\000\325mlit\000\000\000_miid\000\000\000\004\000\000(Fmper\000\000\000\b\000\000\000\000\000\000\000{minm\000\000\000\005MusicaeSP\000\000\000\001\001mpco\000\000\000\004\000\000\000\000aePS\000\000\000\001\006meds\000\000\000\004\000\000\000\000mimc\000\000\000\004\000\000\000\002mlit\000\000\000fmiid\000\000\000\004\000\000()mper\000\000\000\b\000\000\000\000\000\000\004\322minm\000\000\000\tiTunes DJmpco\000\000\000\004\000\000\000\000aePS\000\000\000\001\002meds\000\000\000\004\000\000\000\000ceJI\000\000\000\004\000\000(,mimc\000\000\000\004\000\000\000\022")
24
+
25
+ would return:
26
+
27
+ aply[266]:
28
+ mstt[4]: 200
29
+ muty[1]: 0
30
+ mtco[4]: 2
31
+ mrco[4]: 2
32
+ mlcl[213]:
33
+ mlit[95]:
34
+ miid[4]: 10310
35
+ mper[8]: 123
36
+ minm[5]: "Music"
37
+ aeSP[1]: true
38
+ mpco[4]: 0
39
+ aePS[1]: 6
40
+ meds[4]: 0
41
+ mimc[4]: 2
42
+ mlit[102]:
43
+ miid[4]: 10281
44
+ mper[8]: 1234
45
+ minm[9]: "iTunes DJ"
46
+ mpco[4]: 0
47
+ aePS[1]: 2
48
+ meds[4]: 0
49
+ ceJI[4]: 10284
50
+ mimc[4]: 18
51
+
52
+
53
+
54
+ Which is a `DMAP::Tag` object. You can access individual tags like this:
55
+
56
+ playlists.mstt # => 200
57
+ playlists.mlcl[1].minm # => "iTunes DJ"
58
+
59
+ To build a DMAP structure, you do this:
60
+
61
+ DMAP.build do
62
+ mlit do # dmap.listingitem
63
+ mikd 2 # dmap.itemkind
64
+ asal "Aquarium" # daap.songalbum
65
+ asar "Naomi" # daap.songartist
66
+ miid 3123 # dmap.itemid
67
+ minm "Relax She Said" # dmap.itemname
68
+ mper 9013529250002063060 # dmap.persistentid
69
+ mcti 10386 # dmap.containeritemid
70
+ aeHV 0 # com.apple.itunes.has-video
71
+ asai 9013529250002063588 # daap.songalbumid
72
+ ceJV 2 # com.apple.itunes.jukebox-vote
73
+ ceJC 1 # com.apple.itunes.jukebox-client-vote
74
+ end
75
+ end
76
+
77
+ There is also a `#to_dsl` method on DMAP::Tag that returns a copy-pasta-able ruby code that goes inside the `DMAP.build` block.
78
+
79
+ puts playlists.to_dsl
80
+
81
+ which gives you this:
82
+
83
+ aply do # daap.databaseplaylists
84
+ mstt 200 # dmap.status
85
+ muty 0 # dmap.updatetype
86
+ mtco 2 # dmap.specifiedtotalcount
87
+ mrco 2 # dmap.returnedcount
88
+ mlcl do # dmap.listing
89
+ mlit do # dmap.listingitem
90
+ miid 10310 # dmap.itemid
91
+ mper 123 # dmap.persistentid
92
+ minm "Music" # dmap.itemname
93
+ aeSP true # com.apple.itunes.smart-playlist
94
+ mpco 0 # dmap.parentcontainerid
95
+ aePS 6 # com.apple.itunes.special-playlist
96
+ meds 0 # dmap.editcommandssupported
97
+ mimc 2 # dmap.itemcount
98
+ end
99
+ mlit do # dmap.listingitem
100
+ miid 10281 # dmap.itemid
101
+ mper 1234 # dmap.persistentid
102
+ minm "iTunes DJ" # dmap.itemname
103
+ mpco 0 # dmap.parentcontainerid
104
+ aePS 2 # com.apple.itunes.special-playlist
105
+ meds 0 # dmap.editcommandssupported
106
+ ceJI 10284 # com.apple.itunes.jukebox-current
107
+ mimc 18 # dmap.itemcount
108
+ end
109
+ end
110
+ end
111
+
112
+ As you can see, it also puts in the tag definition at the end because I got tired of having to look up what a tag actually was.
113
+
114
+ ## Come across a new tag?
115
+
116
+ Send me an email with a pcap dump of the entire process and I'll see what I can do.
117
+
118
+ ## Note on Patches/Pull Requests
119
+
120
+ * Fork the project.
121
+ * Make your feature addition or bug fix.
122
+ * Add tests for it. This is important so I don't break it in a
123
+ future version unintentionally.
124
+ * Commit, do not mess with rakefile, version, or history.
125
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
126
+ * Send me a pull request. Bonus points for topic branches.
127
+
128
+ ## Copyright
129
+
130
+ Copyright (c) 2010 Jack "chendo" Chen. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dmap-ng"
8
+ gem.summary = %Q{An improved DMAP gem that allows easy building and parsing of DMAP structures}
9
+ gem.description = %Q{An improved DMAP gem that allows easy building and parsing of DMAP structures}
10
+ gem.email = "chendo@chendo.net"
11
+ gem.homepage = "http://github.com/chendo/dmap-ng"
12
+ gem.authors = ["Jack Chen"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.files += Dir['lib/**/*.rb']
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "dmap-ng #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.5
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dmap-ng}
8
+ s.version = "0.2.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jack Chen"]
12
+ s.date = %q{2010-04-19}
13
+ s.description = %q{An improved DMAP gem that allows easy building and parsing of DMAP structures}
14
+ s.email = %q{chendo@chendo.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "dmap-ng.gemspec",
27
+ "lib/debug_ext.rb",
28
+ "lib/dmap-ng.rb",
29
+ "lib/dmap.rb",
30
+ "lib/dmap/parser.rb",
31
+ "lib/dmap/tag.rb",
32
+ "lib/dmap/tag_builder.rb",
33
+ "lib/dmap/tag_definitions.rb",
34
+ "lib/true_false_ext.rb",
35
+ "spec/dmap_spec.rb",
36
+ "spec/dsl_spec.rb",
37
+ "spec/packet_dumps/connection_with_voting.dump",
38
+ "spec/parser_spec.rb",
39
+ "spec/selector_spec.rb",
40
+ "spec/spec.opts",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+ s.homepage = %q{http://github.com/chendo/dmap-ng}
44
+ s.rdoc_options = ["--charset=UTF-8"]
45
+ s.require_paths = ["lib"]
46
+ s.rubygems_version = %q{1.3.6}
47
+ s.summary = %q{An improved DMAP gem that allows easy building and parsing of DMAP structures}
48
+ s.test_files = [
49
+ "spec/dmap_spec.rb",
50
+ "spec/dsl_spec.rb",
51
+ "spec/parser_spec.rb",
52
+ "spec/selector_spec.rb",
53
+ "spec/spec_helper.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
62
+ else
63
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,13 @@
1
+ class Object
2
+ def tap(&block)
3
+ yield
4
+ block
5
+ end
6
+
7
+ def tapp(prefix = nil, &block)
8
+ block ||= lambda { |x| x }
9
+ str = (ret = block[self]).is_a?(String) ? ret : ret.inspect
10
+ puts [prefix, str].compact.join(': ')
11
+ self
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ require 'dmap'
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ class DMAP
5
+ VERSION = '0.2.5'
6
+ end
7
+
8
+ Dir[File.dirname(__FILE__) + "/dmap/**.rb"].each { |f| require f }
9
+
10
+ require 'true_false_ext'
11
+ require 'debug_ext'
12
+
13
+ class DMAP
14
+ class << self
15
+ def build(&block)
16
+ builder = TagBuilder.new
17
+ builder.instance_eval &block
18
+ r = builder.result
19
+ r.size == 1 ? r.first : r
20
+ end
21
+
22
+ def parse(data)
23
+ Parser.parse(data).first
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ class DMAP
2
+ class Parser
3
+ def self.parse(buffer)
4
+ buffer = StringIO.new(buffer)
5
+
6
+ ret = []
7
+ while !buffer.eof?
8
+ code = buffer.read(4).to_sym
9
+
10
+ raise "Invalid tag code format while parsing" unless code.to_s =~ /^[a-z]{4}$/i
11
+
12
+ length = buffer.read(4).unpack('N').first
13
+ data = buffer.read(length)
14
+
15
+ definition = Tag.lookup(code)
16
+
17
+ raise "No tag definition for #{code}, length: #{length}, data: #{data.inspect}" unless definition
18
+
19
+ ret << if (type = definition.last) == :list
20
+ Tag.new(code, parse(data))
21
+ else
22
+ expected_length, pack_code = STATIC_LENGTH_TYPES[type]
23
+ value = case type
24
+ when :string
25
+ data
26
+ when :version
27
+ data.unpack(pack_code)
28
+ when :long
29
+ # Some types seem to be the wrong endian
30
+ # so there are endian hacks
31
+ data.reverse.unpack(pack_code).first
32
+ when :signed_integer
33
+ data.reverse.unpack(pack_code).first
34
+ else
35
+ data.unpack(pack_code).first
36
+ end
37
+ Tag.new(code, value)
38
+ end
39
+ end
40
+
41
+ ret
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,93 @@
1
+ class DMAP
2
+ class Tag
3
+ class << self
4
+ def lookup(code)
5
+ TAGS[code]
6
+ end
7
+ end
8
+
9
+ attr_accessor :code, :value, :tag, :type
10
+
11
+ def initialize(code, value)
12
+ @code, @value = code, value
13
+ @tag, @type = lookup
14
+
15
+ raise "Cannot have nil value in tag #{code}" if value.nil? && type != :list
16
+ raise "Unknown tag #{code}" if tag.nil?
17
+ end
18
+
19
+ def to_dmap
20
+ data = case type
21
+ when :string
22
+ value
23
+ when :list
24
+ (value || []).inject('') { |mem, v| mem += v.to_dmap }
25
+ when :version
26
+ value.pack(pack_code)
27
+ when :long
28
+ # Some types seem to be the wrong endian
29
+ # so there are endian hacks
30
+ [value].pack('Q').reverse
31
+ when :signed_integer
32
+ [value].pack(pack_code).reverse
33
+ else
34
+ [value.to_i].pack(pack_code)
35
+ end
36
+ "#{code}#{[length].pack('N')}#{data}"
37
+ end
38
+
39
+ # Could be optimised later down the track
40
+ def ==(obj)
41
+ self.to_dmap == obj.to_dmap
42
+ end
43
+
44
+ def pack_code
45
+ @pack_code ||= STATIC_LENGTH_TYPES[type].last rescue raise("No pack code definition for #{code}")
46
+ end
47
+
48
+ def length
49
+ @length ||= case type
50
+ when :string
51
+ value.length
52
+ when :list
53
+ (value || []).inject(0) { |mem, v| mem += v.length + 8 }
54
+ else
55
+ STATIC_LENGTH_TYPES[type].first
56
+ end
57
+ end
58
+
59
+ def lookup
60
+ self.class.lookup(code)
61
+ end
62
+
63
+ def inspect(level = 0)
64
+ pad = ' ' * (level * 2)
65
+ case value
66
+ when Array
67
+ if value.any? { |e| e.is_a? Tag }
68
+ (["#{pad}#{code}[#{length}]:"] + value.map{ |v| v.inspect(level + 1) }).join("\n")
69
+ else
70
+ "#{pad}#{code}[#{length}]: #{value.inspect}"
71
+ end
72
+ else
73
+ "#{pad}#{code}[#{length}]: #{value.inspect}"
74
+ end
75
+ end
76
+
77
+ def to_dsl(level = 0)
78
+ pad = ' ' * (level * 2)
79
+ case value
80
+ when Array
81
+ (["#{pad}#{code} do # #{@tag}"] + value.map{ |v| v.to_dsl(level + 1) } + ["#{pad}end"]).join("\n")
82
+ else
83
+ "#{pad}#{code} #{value.inspect} # #{@tag}"
84
+ end
85
+ end
86
+
87
+ def method_missing(meth, *args)
88
+ return super unless self.class.lookup(meth)
89
+ return super unless value.is_a? Array
90
+ value.find { |v| v.code == meth }.value
91
+ end
92
+ end
93
+ end