easyjour 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ v0.0.2. First gem release.
2
+
3
+ v0.0.1. Initial release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Elijah Miller
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.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ CHANGELOG
2
+ easyjour.gemspec
3
+ examples/httpjour.rb
4
+ lib/easyjour/version.rb
5
+ lib/easyjour.rb
6
+ LICENSE
7
+ Manifest
8
+ Rakefile
9
+ README
10
+ spec/spec_helper.rb
data/README ADDED
@@ -0,0 +1,34 @@
1
+ Easyjour
2
+ ========
3
+
4
+ Super simple access to service announcing and discovery using Bonjour aka DNSSD.
5
+
6
+
7
+ Examples
8
+ ========
9
+
10
+ # garbage_files is an HTTP server available on port 3000
11
+ Easyjour.serve("garbage_files", 'http', 3000)
12
+
13
+
14
+ # Continously find HTTP servers and print the url
15
+ search = Easyjour::Search.new('http') do |result|
16
+ puts "http://#{result.target}:#{result.port}/"
17
+ end
18
+
19
+
20
+ # Find the Git servers that respond in 5 seconds or less
21
+ results = Easyjour.synchronous_search(5, 'git')
22
+ results.each do |result|
23
+ puts "git://#{result.target}:#{result.port}"
24
+ end
25
+
26
+
27
+ Install
28
+ =======
29
+
30
+ gem sources -a http://gems.github.com
31
+ sudo gem install jqr-easyjour
32
+
33
+ Homepage: http://github.com/jqr/easyjour/tree/master
34
+ Copyright (c) 2008 Elijah Miller <elijah.miller@gmail.com>, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ project_name = 'easyjour'
2
+ website_path = "jqr@rubyforge.org:/var/www/gforge-projects/#{project_name}"
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+ require 'spec/rake/spectask'
8
+ require "lib/#{project_name}/version"
9
+
10
+ require 'echoe'
11
+ Echoe.new project_name, Easyjour::Version do |p|
12
+ p.description = "Super simple access to service announcing and discovery using Bonjour aka DNSSD."
13
+ p.url = "http://#{project_name}.rubyforge.org"
14
+ p.author = "Elijah Miller"
15
+ p.email = "elijah.miller@gmail.com"
16
+ # p.extra_deps = [['net-mdns', '>= 0.4']]
17
+ p.need_tar_gz = false
18
+ p.docs_host = website_path
19
+ end
20
+
21
+
22
+ Rake::Task[:default].prerequisites.clear
23
+ desc 'Default: run specs'
24
+ task :default => :spec
25
+ Spec::Rake::SpecTask.new do |t|
26
+ t.spec_files = FileList["spec/**/*_spec.rb"]
27
+ end
data/easyjour.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{easyjour}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Elijah Miller"]
9
+ s.date = %q{2008-12-06}
10
+ s.description = %q{Super simple access to service announcing and discovery using Bonjour aka DNSSD.}
11
+ s.email = %q{elijah.miller@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/easyjour/version.rb", "lib/easyjour.rb", "LICENSE", "README"]
13
+ s.files = ["CHANGELOG", "easyjour.gemspec", "examples/httpjour.rb", "lib/easyjour/version.rb", "lib/easyjour.rb", "LICENSE", "Manifest", "Rakefile", "README", "spec/spec_helper.rb"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://easyjour.rubyforge.org}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Easyjour", "--main", "README"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{easyjour}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Super simple access to service announcing and discovery using Bonjour aka DNSSD.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<echoe>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<echoe>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<echoe>, [">= 0"])
33
+ end
34
+ end
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'easyjour')
4
+
5
+ class HTTPjour
6
+ def self.serve
7
+ # This code is webrick example code, slightly modified
8
+ require 'webrick'
9
+ include WEBrick
10
+
11
+ port = rand(10_000) + 1025
12
+ s = HTTPServer.new(
13
+ :Port => port,
14
+ :DocumentRoot => Dir::pwd
15
+ )
16
+ Easyjour.serve("webrick-#{port}", 'http', port)
17
+
18
+ trap("INT") { s.shutdown }
19
+ s.start
20
+ end
21
+
22
+
23
+ def self.search(open = false)
24
+ puts "Searching for servers... (press control-c to stop)\n\n"
25
+ search = Easyjour.search('http') do |service|
26
+ puts " * http://#{service.target}:#{service.port}/"
27
+ system('open', "http://#{service.target}:#{service.port}/") if open
28
+ end
29
+ trap("INT") { exit }
30
+ loop { sleep 10 }
31
+ end
32
+
33
+
34
+ def self.help
35
+ puts "usage: "
36
+ puts " serve # serves the current directory over HTTP"
37
+ puts " search # looks for HTTP servers"
38
+ puts " show # opens every HTTP server as it finds them"
39
+ exit
40
+ end
41
+ end
42
+
43
+ case ARGV[0]
44
+ when 'serve'
45
+ HTTPjour.serve
46
+ when 'search'
47
+ HTTPjour.search
48
+ when 'open'
49
+ HTTPjour.search(true)
50
+ else
51
+ HTTPjour.help
52
+ end
@@ -0,0 +1,3 @@
1
+ module Easyjour
2
+ Version = '0.0.2'
3
+ end
data/lib/easyjour.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'net/dns/mdns-sd'
2
+
3
+ module Easyjour
4
+ # Turns a service and protocol into _service._protocol format, automatically
5
+ # defaults to TCP if no protocol is specified. Also gracefully handles a service
6
+ # that is already in the _service._protocol format format.
7
+ def self.type_from_parts(service, protocol = :tcp)
8
+ if service =~ /^_\w+\._\w+$/
9
+ service
10
+ else
11
+ "_#{service}._#{protocol}"
12
+ end
13
+ end
14
+
15
+ # Makes a new service discoverable. Service is discoverable until stop is called.
16
+ #
17
+ # # garbage_files is an HTTP server available on port 3000
18
+ # Easyjour.serve("garbage_files", 'http', 3000)
19
+ def self.serve(name, service, port, text_record = {}, protocol = :tcp)
20
+ Service.new(name, service, port, text_record, protocol = :tcp)
21
+ end
22
+
23
+ class Service
24
+ def initialize(name, service, port, text_record = {}, protocol = :tcp)
25
+ @service = Net::DNS::MDNSSD.register(name, Easyjour.type_from_parts(service, protocol), 'local', port, text_record)
26
+ end
27
+
28
+ # Stops a service from being discoverable.
29
+ def stop
30
+ @service.stop
31
+ end
32
+ end
33
+
34
+ # Initiaites a service search, returns an Easyjour::Search. Search will continue until stop
35
+ # is called. Can be given a block for immediate result processing.
36
+ #
37
+ # # Find the HTTP servers that respond in 5 seconds or less
38
+ # search = Easyjour.search('http')
39
+ # sleep 5
40
+ # search.stop
41
+ # search.results.each do |result| # access the entire result set
42
+ # puts "http://#{result.target}:#{result.port}/"
43
+ # end
44
+ #
45
+ # # Continously find HTTP servers
46
+ # search = Easyjour.search('http') do |result|
47
+ # puts "http://#{result.target}:#{result.port}/"
48
+ # end
49
+ # search.results # result set is updated as servers respond
50
+ def self.search(service, protocol = :tcp, &block)
51
+ Search.new(service, protocol, &block)
52
+ end
53
+
54
+ # Searches for a service for timeout seconds and returns the results after stopping the search.
55
+ # Can be given a block for immediate result processing.
56
+ #
57
+ # # Find the Git servers that respond in 5 seconds or less
58
+ # results = Easyjour.search(5, 'git')
59
+ # results.each do |result|
60
+ # puts "git://#{result.target}:#{result.port}"
61
+ # end
62
+ def self.synchronous_search(timeout, service, protocol = :tcp, &block)
63
+ search = Search.new(service, protocol, &block)
64
+ sleep timeout
65
+ search.stop
66
+ search.results
67
+ end
68
+
69
+
70
+ class Search
71
+ def initialize(service, protocol = :tcp)
72
+ @results = []
73
+ @results_mutex = Mutex.new
74
+
75
+ @query = Net::DNS::MDNSSD.browse(Easyjour.type_from_parts(service, protocol), 'local') do |reply|
76
+ Net::DNS::MDNSSD.resolve(reply.name, reply.type, reply.domain) do |reply|
77
+ yield(reply) if block_given?
78
+
79
+ @results_mutex.synchronize do
80
+ @results << reply
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ # Returns the current the results.
87
+ def results
88
+ @results_mutex.synchronize do
89
+ @results
90
+ end
91
+ end
92
+
93
+ # Stop the search.
94
+ def stop
95
+ @query.stop
96
+ end
97
+
98
+ end
99
+
100
+ end
File without changes
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easyjour
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Elijah Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-06 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Super simple access to service announcing and discovery using Bonjour aka DNSSD.
26
+ email: elijah.miller@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - CHANGELOG
33
+ - lib/easyjour/version.rb
34
+ - lib/easyjour.rb
35
+ - LICENSE
36
+ - README
37
+ files:
38
+ - CHANGELOG
39
+ - easyjour.gemspec
40
+ - examples/httpjour.rb
41
+ - lib/easyjour/version.rb
42
+ - lib/easyjour.rb
43
+ - LICENSE
44
+ - Manifest
45
+ - Rakefile
46
+ - README
47
+ - spec/spec_helper.rb
48
+ has_rdoc: true
49
+ homepage: http://easyjour.rubyforge.org
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --line-numbers
53
+ - --inline-source
54
+ - --title
55
+ - Easyjour
56
+ - --main
57
+ - README
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "1.2"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project: easyjour
75
+ rubygems_version: 1.3.1
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: Super simple access to service announcing and discovery using Bonjour aka DNSSD.
79
+ test_files: []
80
+