simple_upnp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ pkg
3
+ doc
4
+ *.gem
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_upnp.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_upnp (0.0.1)
5
+ nori (~> 1.1.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ nori (1.1.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ simple_upnp!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dennis Kuczynski
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # simple_upnp
2
+
3
+ A few libraries already exist for working with Universal Plug and Play (UPnP) devices.
4
+
5
+ This library tries to provide a minimal implementation for discovery with a small amount of code and library dependencies.
6
+
7
+ For more info on uPnP, see:
8
+ http://www.upnp-hacks.org/upnp.html
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'simple_upnp'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install simple_upnp
23
+
24
+ ## Usage
25
+
26
+ The below code (which can be triggered with the included rake task: "rake simple_upnp:discover"):
27
+ ```
28
+ include_location_details = true
29
+ devices = SimpleUpnp::Discovery.search()
30
+ devices.each do |device|
31
+ puts 'Device Found: '
32
+ puts device.to_json(include_location_details)
33
+ puts ''
34
+ end
35
+ ```
36
+
37
+ Should produce output similar to the following:
38
+ ```
39
+ Searching for devices...
40
+ Device Found:
41
+ {:st=>"upnp:rootdevice", :server=>"Darwin 11.4.2 CyberLinkC/2.3 UPnP/1.0 DLNADOC/1.50", :usn=>"uuid:7DD8D98F-6577-582D-AF37-38B92EB830A4", :location=>"http://10.10.10.3:38400/description.xml", "root"=>{"specVersion"=>{"major"=>"1", "minor"=>"0"}, "device"=>{"deviceType"=>"urn:dmc-samsung-com:device:SyncServer:1", "friendlyName"=>"Adam’s MacBook Pro", "manufacturer"=>"Samsung-Electronics", "manufacturerURL"=>"http://www.samsung.com", "modelDescription"=>"Kies Sync Server", "modelName"=>"Kies Sync Server", "modelNumber"=>"1.0", "modelURL"=>"http://www.samsung.com", "UDN"=>"uuid:7DD8D98F-6577-582D-AF37-38B92EB830A4", "serviceList"=>{"service"=>{"serviceType"=>"urn:dmc-samsung-com:service:SyncManager:1", "serviceId"=>"urn:dmc-samsung-com:serviceId:SyncManager", "controlURL"=>"/KiesControll", "eventSubURL"=>"/KiesEventSub", "SCPDURL"=>"/KiesService_SCPD.xml"}}}, "URLBase"=>"http://10.10.10.3:38400", "@xmlns"=>"urn:schemas-upnp-org:device-1-0"}}
42
+
43
+ Device Found:
44
+ {:st=>"upnp:rootdevice", :server=>"FreeRTOS/6.0.5, UPnP/1.0, IpBridge/0.1", :usn=>"uuid:2f402f80-da50-11e1-9b23-001788092b7e", :location=>"http://10.10.10.104:80/description.xml", "root"=>{"specVersion"=>{"major"=>"1", "minor"=>"0"}, "URLBase"=>"http://10.10.10.104:80/", "device"=>{"deviceType"=>"urn:schemas-upnp-org:device:Basic:1", "friendlyName"=>"Philips hue (10.10.10.104)", "manufacturer"=>"Royal Philips Electronics", "manufacturerURL"=>"http://www.philips.com", "modelDescription"=>"Philips hue Personal Wireless Lighting", "modelName"=>"Philips hue bridge 2012", "modelNumber"=>"929000242503", "modelURL"=>"http://www.meethue.com", "serialNumber"=>"001788542b7e", "UDN"=>"uuid:2f402f80-da50-11e1-9b23-001788092b7e", "serviceList"=>{"service"=>{"serviceType"=>"(null)", "serviceId"=>"(null)", "controlURL"=>"(null)", "eventSubURL"=>"(null)", "SCPDURL"=>"(null)"}}, "presentationURL"=>"index.html", "iconList"=>{"icon"=>[{"mimetype"=>"image/png", "height"=>"48", "width"=>"48", "depth"=>"24", "url"=>"hue_logo_0.png"}, {"mimetype"=>"image/png", "height"=>"120", "width"=>"120", "depth"=>"24", "url"=>"hue_logo_3.png"}]}}, "@xmlns"=>"urn:schemas-upnp-org:device-1-0"}}
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ $LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/lib")
4
+ require 'simple_upnp'
5
+
6
+ namespace :simple_upnp do
7
+ task :discover do
8
+ puts "Searching for devices...\n"
9
+ include_location_details = true
10
+ devices = SimpleUpnp::Discovery.search()
11
+ devices.each do |device|
12
+ puts 'Device Found: '
13
+ puts device.to_json(include_location_details)
14
+ puts ''
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ require 'net/http'
2
+
3
+ module SimpleUpnp
4
+ class Device
5
+
6
+ attr_reader :st, :server, :usn, :location
7
+
8
+ def initialize(message)
9
+ lines = message.split(/\r?\n/)
10
+ lines.each do |line|
11
+ if line =~ /^ST:/i
12
+ @st = parse_token(line.split(': '))
13
+ elsif line =~ /^SERVER:/i
14
+ @server = parse_token(line.split(': '))
15
+ elsif line =~ /^USN:/i
16
+ @usn = parse_token(line.split(': '))
17
+ # Trim off data after ::
18
+ # For example, we don't want upnp:rootdevice from uuid:7DD8D98F-6577-582D-AF37-38B92EB830A4::upnp:rootdevice
19
+ @usn = @usn.split('::').first
20
+ elsif line =~ /^LOCATION:/i
21
+ @location = parse_token(line.split(': '))
22
+ end
23
+ end
24
+ end
25
+
26
+ def to_json(include_location_details = false)
27
+ h = {
28
+ :st => st,
29
+ :server => server,
30
+ :usn => usn,
31
+ :location => location
32
+ }
33
+ h.merge!(retrieve_location_details) if include_location_details and location
34
+ h
35
+ end
36
+
37
+ private
38
+
39
+ def parse_token(tokens)
40
+ value = nil
41
+ value = tokens[1] if tokens.length >= 2
42
+ value
43
+ end
44
+
45
+ def retrieve_location_details
46
+ url = URI.parse(location)
47
+ req = Net::HTTP::Get.new(url.path)
48
+ res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
49
+ parser = Nori.new()
50
+ parser.parse(res.body)
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,52 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+
4
+ module SimpleUpnp
5
+ class Discovery
6
+
7
+ SSDP_ADDR = '239.255.255.250'
8
+ SSDP_PORT = 1900
9
+ M_SEARCH = "M-SEARCH * HTTP/1.1\r\nHOST: #{SSDP_ADDR}:#{SSDP_PORT}\r\nMAN: \"ssdp:discover\"\r\nMX: 2\r\nST: ssdp:all\r\n\r\n"
10
+ MAX_RECEIVE_LENGTH = 65536
11
+
12
+ # Signal the uPnP Multicast address and wait for responses, returning an array of SimpleUpnp::Devices
13
+ def self.search(seconds_to_listen=5)
14
+ socket = UDPSocket.new
15
+ socket.bind('', SSDP_PORT)
16
+ socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, [1].pack('i'))
17
+ socket.send(M_SEARCH, 0, SSDP_ADDR, SSDP_PORT)
18
+ messages = receive_messages(socket, seconds_to_listen)
19
+ devices = process_messages(messages)
20
+ end
21
+
22
+ private
23
+
24
+ # Read responses from the socket until the Timout triggers
25
+ def self.receive_messages(socket, seconds_to_listen)
26
+ messages = []
27
+ begin
28
+ Timeout::timeout(seconds_to_listen) do
29
+ while true
30
+ message, sender = socket.recvfrom(MAX_RECEIVE_LENGTH)
31
+ messages << message
32
+ end
33
+ end
34
+ rescue Timeout::Error => e
35
+ # Finished Listening
36
+ end
37
+ messages
38
+ end
39
+
40
+ # Convert messages to a unique array of Devices
41
+ def self.process_messages(messages)
42
+ devices = []
43
+ messages.each do |message|
44
+ device = SimpleUpnp::Device.new(message)
45
+ index = devices.index { |x| x.usn == device.usn }
46
+ devices << device if index.nil?
47
+ end
48
+ devices
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleUpnp
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'nori'
2
+
3
+ require "simple_upnp/version"
4
+ require "simple_upnp/device"
5
+ require 'simple_upnp/discovery'
6
+
7
+ module SimpleUpnp
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/simple_upnp/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dennis Kuczynski"]
6
+ gem.email = ["dennis.kuczynski@gmail.com"]
7
+ gem.description = %q{Simple library to perform basic UPnP network discovery.}
8
+ gem.summary = %q{Simple library to perform basic UPnP network discovery.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "simple_upnp"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SimpleUpnp::VERSION
17
+
18
+ gem.add_dependency "nori", '~> 1.1.3'
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_upnp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dennis Kuczynski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nori
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.3
30
+ description: Simple library to perform basic UPnP network discovery.
31
+ email:
32
+ - dennis.kuczynski@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/simple_upnp.rb
44
+ - lib/simple_upnp/device.rb
45
+ - lib/simple_upnp/discovery.rb
46
+ - lib/simple_upnp/version.rb
47
+ - simple_upnp.gemspec
48
+ homepage: ''
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Simple library to perform basic UPnP network discovery.
72
+ test_files: []
73
+ has_rdoc: