Module: Findme

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

Constant Summary collapse

AVAHI_SERVICE_DIR =
"/etc/avahi/services/"
AVAHI_BROWSE =
"/usr/bin/avahi-browse"
VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

._get_startup_time(str) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/findme.rb', line 33

def self._get_startup_time str
  begin
    pairs = str.strip.split(" ")
    pairs.each do |p|
      kv=p.strip.split("=")
      if kv[0] == "\"findme_startup_time"
        return kv[0].to_i
      end
    end
  rescue Exception => e
    # do nothing or if you want to debug: puts e
  end
  nil
end

.cleanupObject

clean up all the services this gem registered. should be called by user to clean up unused services.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/findme.rb', line 15

def self.cleanup
  findme_services = []
  files_with_path = Dir.glob(AVAHI_SERVICE_DIR + "*.service")
  files_with_path.each do |fwp|
    File.open(fwp, 'r') do |infile|
      while (line = infile.gets)
        if line.strip == "<!-- findme generated -->"
          findme_services << fwp
        end
      end
    end
  end
  findme_services.each do |fs|
    puts "Removing #{fs}"
    File.delete fs
  end
end

.discover(custom_cmd = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/findme.rb', line 116

def self.discover custom_cmd=nil

  if custom_cmd.nil?
    res = `avahi-browse -arpt`
  else
    res = `#{custom_cmd}`
  end

  services = []
  lines = res.scan(/\S+/)
  lines.each do |line|
    if line[0] == "="
      elems=line.split(";")
      as = AvahiService.new
      as.eth      = elems[1]
      as.ipv4     = elems[2]
      as.hosttxt  = elems[3]
      as.service  = elems[4]
      as.hostname = elems[6]
      as.ip       = elems[7]
      as.port     = elems[8]
      as.txt      = elems[9]
      services << as
    end
  end
  services
end

.discover_only_earliestObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/findme.rb', line 48

def self.discover_only_earliest
  services = discover
  h = {}
  h_out={}

  #construct the hash, group the services of the same name
  services.each do |s|
    if h[s.service].nil?
      h[s.service]=[]
    else
      #existing hash, do nothing
    end
    h[s.service] << s
  end

  h.each do |k, v|
    if (_get_startup_time v[0].txt).nil?
      #cant find the earliest time, just return the first one.
    else
      v.sort! {|x,y| _get_startup_time(x.txt) <=> _get_startup_time(y.txt) }
    end
    h_out[k]=v.last #fix a bug here.
  end
  h_out
end

.discover_only_latestObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/findme.rb', line 74

def self.discover_only_latest
  services = discover
  h = {}
  h_out={}

  #construct the hash, group the services of the same name
  services.each do |s|
    if h[s.service].nil?
      h[s.service]=[]
    else
      #existing hash, do nothing
    end
    h[s.service] << s
  end

  h.each do |k, v|
    if (_get_startup_time v[0].txt).nil?
      #cant find the earliest time, just return the first one.
    else
      v.sort! {|x,y| _get_startup_time(x.txt) <=> _get_startup_time(y.txt) }
    end
    h_out[k]=v.first #latest registerd.
  end
  h_out
end

.discover_service(service, ip = nil) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/findme.rb', line 100

def self.discover_service(service, ip=nil)
  services = discover

  result = services.detect {|x| x.service == "_#{service}._tcp" && x.ip == ip}
  return result if result #here we choose best match if previous is this ip
  #todo, using other factor to determine rank algorithm
  return services.detect {|x| x.service == "_#{service}._tcp"}[0]
end

.discover_services(service) ⇒ Object



109
110
111
112
113
# File 'lib/findme.rb', line 109

def self.discover_services(service)
  services = discover

  result = services.select {|x| x.service == "_#{service}._tcp" }
end

.macObject

find the mac address



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/findme.rb', line 180

def self.mac
  return @mac_address if defined? @mac_address
  re = %r<(?:hwaddr|:)\s+((?:[0-9a-f]{1,2}[-:]){5}[0-9a-f]{1,2})\s*$>i
  lines =
    begin
      IO.popen('ifconfig'){|fd| fd.readlines}
    rescue
      IO.popen('ipconfig /all'){|fd| fd.readlines}
    end
  candidates = lines.map{|l| re.match( l )[1] rescue nil }.compact
  @mac_address = candidates.first
end

.mac_shortObject



193
194
195
# File 'lib/findme.rb', line 193

def self.mac_short
  self.mac.split(":").join("")
end

.register(service_name, port, txt = "", type = '_tcp', protocol = "ipv4") ⇒ Object

register a new service to the folder



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/findme.rb', line 145

def self.register service_name,  port, txt="", type = '_tcp', protocol="ipv4"
  txt = ("findme_startup_time=#{Time.now.to_i.to_s}#" + txt).strip
  xml = '<?xml version="1.0" standalone=\'no\'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">

<!-- findme generated -->
<!-- This file is part of '+ service_name + '} -->

<service-group>
<name replace-wildcards="yes">%h</name>

<service protocol="' + protocol + '">

  <type>'+ "_#{service_name}.#{type}" +'</type>
  <port>'+ port.to_s + '</port>
  <txt-record>' + txt.to_s + '</txt-record>
</service>
</service-group>'

  File.open(AVAHI_SERVICE_DIR  + service_name + ".service", "w" ){|f| f.write xml}
end

.servicesObject

return all the services running by avahi



9
10
11
12
# File 'lib/findme.rb', line 9

def self.services
  files_with_path = Dir.glob(AVAHI_SERVICE_DIR + "*.service")
  files = files_with_path.map{|fwp| fwp.split("/").last}
end

.unregister(service_name) ⇒ Object

Unregister a service, just remove .service file from avahi service directory. result: BOOLEAN, In most cases we don’t care it.



169
170
171
172
173
174
175
176
177
# File 'lib/findme.rb', line 169

def self.unregister service_name
  service_file = AVAHI_SERVICE_DIR + service_name + ".service"
  if File.exist? service_file
    File.delete service_file
    return true
  end
rescue # Errno::EACCES: Permission denied
  false
end