findme 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,43 @@
1
1
  findme
2
2
  ======
3
3
 
4
- find neighborhood host and services.
4
+ Find neighborhood host and services. fineme uses `avahi-deamon` and `avahi-browse` to discover and register services. Only works with ubuntu linux for now. (12.04 is tested working)
5
+
6
+
7
+ Install
8
+ -------
9
+ sudo apt-get install avahi-utils avahi-daemon libnss-mdns
10
+ gem install findme
11
+
12
+ Example
13
+ -------
14
+
15
+ require 'findme'
16
+ require 'pp'
17
+
18
+ puts Findme.discover.count.to_s + " Service discovered."
19
+ Findme.discover.each{|service| pp service.inspect}
20
+
21
+ More advanced Example
22
+ ---------------------
23
+
24
+ on server:
25
+
26
+ require 'findme'
27
+
28
+ Findme.register "findme", 1337
29
+
30
+ on client:
31
+
32
+ require 'findme'
33
+
34
+ SERVICE = "findme"
35
+ services = Findme.discover
36
+ services.select{|s| s.service == "_#{SERVICE}._tcp"}.each{|s| puts "#{s.ip}:#{s.port}"}
37
+
38
+ results on client.rb:
39
+
40
+ ~ > ruby client.rb
41
+ fe80::a00:27ff:feeb:xxxx:1337
42
+ 192.168.xx.xx:1337
43
+
data/example/client.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'findme'
2
+ require 'pp'
3
+
4
+ SERVICE = "findme"
5
+ #services = Findme.discover
6
+ h = Findme.discover_only_earliest
7
+ h.select{|k,s| s.service == "_#{SERVICE}._tcp"}.each{|k,s| puts "#{k}|#{s.ip}:#{s.port}:#{s.txt}"}
data/example/server.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'findme.rb'
2
+
3
+ Findme.cleanup
4
+ Findme.register "findme", 1337
data/findme.gemspec CHANGED
@@ -4,8 +4,8 @@ require File.expand_path('../lib/findme/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["c2h2"]
6
6
  gem.email = ["yiling.cao@gmail.com"]
7
- gem.description = "find neighborhood host and services."
8
- gem.summary = "find neighborhood host and services."
7
+ gem.description = "find neighborhood host and services with avahi / avahi-daemon."
8
+ gem.summary = "find neighborhood host and services with avahi / avahi-daemon."
9
9
  gem.homepage = "https://github.com/c2h2/findme"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
data/lib/findme.rb CHANGED
@@ -10,7 +10,7 @@ class Findme
10
10
  files = files_with_path.map{|fwp| fwp.split("/").last}
11
11
  end
12
12
 
13
- #clean up all the services this gem registered.
13
+ #clean up all the services this gem registered. should be called by user to clean up unused services.
14
14
  def self.cleanup
15
15
  findme_services = []
16
16
  files_with_path = Dir.glob(AVAHI_SERVICE_DIR + "*.service")
@@ -28,6 +28,47 @@ class Findme
28
28
  File.delete fs
29
29
  end
30
30
  end
31
+
32
+ def self._get_startup_time str
33
+ begin
34
+ pairs = str.strip.split(" ")
35
+ pairs.each do |p|
36
+ kv=p.strip.split("=")
37
+ if kv[0] == "\"findme_startup_time"
38
+ return kv[0].to_i
39
+ end
40
+ end
41
+ rescue Exception => e
42
+ # do nothing or if you want to debug: puts e
43
+ end
44
+ nil
45
+ end
46
+
47
+ def self.discover_only_earliest
48
+ services = discover
49
+ h = {}
50
+ h_out={}
51
+
52
+ #construct the hash, group the services of the same name
53
+ services.each do |s|
54
+ if h[s.service].nil?
55
+ h[s.service]=[]
56
+ else
57
+ #existing hash, do nothing
58
+ end
59
+ h[s.service] << s
60
+ end
61
+
62
+ h.each do |k, v|
63
+ if (_get_startup_time v[0].txt).nil?
64
+ #cant find the earliest time, just return the first one.
65
+ else
66
+ v.sort! {|x,y| _get_startup_time(x.txt) <=> _get_startup_time(y.txt) }
67
+ end
68
+ h_out[k]=v[0]
69
+ end
70
+ h_out
71
+ end
31
72
 
32
73
  def self.discover
33
74
  res = `avahi-browse -arpt`
@@ -52,19 +93,22 @@ class Findme
52
93
  end
53
94
 
54
95
  #register a new service to the folder
55
- def self.register service_name, port, type = '_tcp'
96
+ def self.register service_name, port, txt="", type = '_tcp', protocol="ipv4"
97
+ txt = ("findme_startup_time=#{Time.now.to_i.to_s} " + txt).strip
56
98
  xml = '<?xml version="1.0" standalone=\'no\'?><!--*-nxml-*-->
57
99
  <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
58
100
 
59
101
  <!-- findme generated -->
60
- <!-- This file is part of udisks -->
102
+ <!-- This file is part of '+ service_name + '} -->
61
103
 
62
104
  <service-group>
63
105
  <name replace-wildcards="yes">%h</name>
64
106
 
65
- <service>
107
+ <service protocol="' + protocol + '">
108
+
66
109
  <type>'+ "_#{service_name}.#{type}" +'</type>
67
- <port>' + port.to_s + '</port>
110
+ <port>'+ port.to_s + '</port>
111
+ <txt-record>' + txt.to_s + '</txt-record>
68
112
  </service>
69
113
  </service-group>'
70
114
 
@@ -1,3 +1,3 @@
1
1
  module Findme
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: findme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-02 00:00:00.000000000 Z
12
+ date: 2012-08-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: find neighborhood host and services.
14
+ description: find neighborhood host and services with avahi / avahi-daemon.
15
15
  email:
16
16
  - yiling.cao@gmail.com
17
17
  executables: []
@@ -23,6 +23,8 @@ files:
23
23
  - LICENSE
24
24
  - README.md
25
25
  - Rakefile
26
+ - example/client.rb
27
+ - example/server.rb
26
28
  - findme.gemspec
27
29
  - lib/findme.rb
28
30
  - lib/findme/version.rb
@@ -50,6 +52,6 @@ rubyforge_project:
50
52
  rubygems_version: 1.8.24
51
53
  signing_key:
52
54
  specification_version: 3
53
- summary: find neighborhood host and services.
55
+ summary: find neighborhood host and services with avahi / avahi-daemon.
54
56
  test_files:
55
57
  - test/test.rb