findme 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/findme.rb +24 -8
  2. data/lib/findme/version.rb +1 -1
  3. metadata +2 -2
@@ -1,9 +1,9 @@
1
1
  module Findme
2
-
2
+
3
3
  AVAHI_SERVICE_DIR = "/etc/avahi/services/"
4
4
  AVAHI_BROWSE = "/usr/bin/avahi-browse"
5
5
 
6
-
6
+
7
7
  #return all the services running by avahi
8
8
  def self.services
9
9
  files_with_path = Dir.glob(AVAHI_SERVICE_DIR + "*.service")
@@ -28,7 +28,7 @@ module Findme
28
28
  File.delete fs
29
29
  end
30
30
  end
31
-
31
+
32
32
  def self._get_startup_time str
33
33
  begin
34
34
  pairs = str.strip.split(" ")
@@ -48,7 +48,7 @@ module Findme
48
48
  services = discover
49
49
  h = {}
50
50
  h_out={}
51
-
51
+
52
52
  #construct the hash, group the services of the same name
53
53
  services.each do |s|
54
54
  if h[s.service].nil?
@@ -57,7 +57,7 @@ module Findme
57
57
  #existing hash, do nothing
58
58
  end
59
59
  h[s.service] << s
60
- end
60
+ end
61
61
 
62
62
  h.each do |k, v|
63
63
  if (_get_startup_time v[0].txt).nil?
@@ -112,7 +112,7 @@ module Findme
112
112
  end
113
113
 
114
114
 
115
- def self.discover
115
+ def self.discover
116
116
  res = `avahi-browse -arpt`
117
117
  services = []
118
118
  lines = res.split("\n")
@@ -147,7 +147,7 @@ module Findme
147
147
  <name replace-wildcards="yes">%h</name>
148
148
 
149
149
  <service protocol="' + protocol + '">
150
-
150
+
151
151
  <type>'+ "_#{service_name}.#{type}" +'</type>
152
152
  <port>'+ port.to_s + '</port>
153
153
  <txt-record>' + txt.to_s + '</txt-record>
@@ -157,6 +157,18 @@ module Findme
157
157
  File.open(AVAHI_SERVICE_DIR + service_name + ".service", "w" ){|f| f.write xml}
158
158
  end
159
159
 
160
+ # Unregister a service, just remove .service file from avahi service directory.
161
+ # result: BOOLEAN, In most cases we don't care it.
162
+ def self.unregister service_name
163
+ service_file = AVAHI_SERVICE_DIR + service_name + ".service"
164
+ if File.exist? service_file
165
+ File.delete service_file
166
+ return true
167
+ end
168
+ rescue # Errno::EACCES: Permission denied
169
+ false
170
+ end
171
+
160
172
  #find the mac address
161
173
  def self.mac
162
174
  return @mac_address if defined? @mac_address
@@ -170,7 +182,7 @@ module Findme
170
182
  candidates = lines.map{|l| re.match( l )[1] rescue nil }.compact
171
183
  @mac_address = candidates.first
172
184
  end
173
-
185
+
174
186
  def self.mac_short
175
187
  self.mac.split(":").join("")
176
188
  end
@@ -184,4 +196,8 @@ class AvahiService
184
196
  [ip, port]
185
197
  end
186
198
 
199
+ def inspect
200
+ "#<eth: #{eth}, ipv4: #{ipv4}, ip: #{ip}, hosttxt: #{hosttxt}, hostname: #{hostname}, port: #{port}, txt: #{txt}, service: #{service}>"
201
+ end
202
+
187
203
  end
@@ -1,3 +1,3 @@
1
1
  module Findme
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
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.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-24 00:00:00.000000000 Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: find neighborhood host and services with avahi / avahi-daemon. run this
15
15
  for the first time to allow normal user to register services `sudo chmod a+w /etc/avahi/services/`