Module: Easyjour

Defined in:
lib/easyjour.rb,
lib/easyjour/version.rb

Defined Under Namespace

Classes: Search, Service

Constant Summary collapse

Version =
'0.0.4'

Class Method Summary collapse

Class Method Details

.search(service, protocol = :tcp, &block) ⇒ Object

Initiaites a service search, returns an Easyjour::Search. Search will continue until stop is called. Can be given a block for immediate result processing.

# Find the HTTP servers that respond in 5 seconds or less
search = Easyjour.search('http')
sleep 5
search.stop
search.results.each do |result|                      # access the entire result set
  puts "http://#{result.target}:#{result.port}/"
end  

# Continously find HTTP servers
search = Easyjour.search('http') do |result|
  puts "http://#{result.target}:#{result.port}/"
end
search.results                                       # result set is updated as servers respond


57
58
59
# File 'lib/easyjour.rb', line 57

def self.search(service, protocol = :tcp, &block)
  Search.new(service, protocol, &block)
end

.serve(name, service, port, text_record = {}, protocol = :tcp) ⇒ Object

Makes a new service discoverable. Service is discoverable until stop is called.

# garbage_files is an HTTP server available on port 3000
Easyjour.serve("garbage_files", 'http', 3000)


20
21
22
# File 'lib/easyjour.rb', line 20

def self.serve(name, service, port, text_record = {}, protocol = :tcp)
  Service.new(name, service, port, text_record, protocol = :tcp)
end

.synchronous_search(timeout, service, protocol = :tcp, &block) ⇒ Object

Searches for a service for timeout seconds and returns the results after stopping the search. Can be given a block for immediate result processing.

# Find the Git servers that respond in 5 seconds or less
results = Easyjour.search(5, 'git')
results.each do |result|
  puts "git://#{result.target}:#{result.port}"
end


69
70
71
72
73
74
# File 'lib/easyjour.rb', line 69

def self.synchronous_search(timeout, service, protocol = :tcp, &block)
  search = Search.new(service, protocol, &block)
  sleep timeout
  search.stop
  search.results
end

.type_from_parts(service, protocol = :tcp) ⇒ Object

Turns a service and protocol into _service._protocol format, automatically defaults to TCP if no protocol is specified. Also gracefully handles a service that is already in the _service._protocol format format.



8
9
10
11
12
13
14
# File 'lib/easyjour.rb', line 8

def self.type_from_parts(service, protocol = :tcp)
  if service =~ /^_\w+\._\w+$/
    service
  else
    "_#{service}._#{protocol}"
  end
end