jqr-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,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+ require "lib/easyjour/version"
6
+
7
+ require 'echoe'
8
+ Echoe.new 'easyjour', Easyjour::Version do |p|
9
+ p.description = "Super simple access to service announcing and discovery using Bonjour aka DNSSD."
10
+ p.url = "http://easyjour.rubyforge.org"
11
+ p.author = "Elijah Miller"
12
+ p.email = "elijah.miller@gmail.com"
13
+ # p.extra_deps = [['net-mdns', '>= 0.4']]
14
+ p.need_tar_gz = false
15
+ end
16
+
17
+
18
+ Rake::Task[:default].prerequisites.clear
19
+ desc 'Default: run specs'
20
+ task :default => :spec
21
+ Spec::Rake::SpecTask.new do |t|
22
+ t.spec_files = FileList["spec/**/*_spec.rb"]
23
+ 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", "examples/httpjour.rb", "lib/easyjour/version.rb", "lib/easyjour.rb", "LICENSE", "Manifest", "Rakefile", "README", "spec/spec_helper.rb", "easyjour.gemspec"]
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,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jqr-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 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: echoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Super simple access to service announcing and discovery using Bonjour aka DNSSD.
25
+ email: elijah.miller@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - CHANGELOG
32
+ - lib/easyjour/version.rb
33
+ - lib/easyjour.rb
34
+ - LICENSE
35
+ - README
36
+ files:
37
+ - CHANGELOG
38
+ - examples/httpjour.rb
39
+ - lib/easyjour/version.rb
40
+ - lib/easyjour.rb
41
+ - LICENSE
42
+ - Manifest
43
+ - Rakefile
44
+ - README
45
+ - spec/spec_helper.rb
46
+ - easyjour.gemspec
47
+ has_rdoc: true
48
+ homepage: http://easyjour.rubyforge.org
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --line-numbers
52
+ - --inline-source
53
+ - --title
54
+ - Easyjour
55
+ - --main
56
+ - README
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "1.2"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: easyjour
74
+ rubygems_version: 1.2.0
75
+ signing_key:
76
+ specification_version: 2
77
+ summary: Super simple access to service announcing and discovery using Bonjour aka DNSSD.
78
+ test_files: []
79
+