public
Description: Watches the ether for the handy *jour scripts.
Homepage:
Clone URL: git://github.com/lachie/starjour.git
Click here to lend your support to: starjour and make a donation at www.pledgie.com !
starjour / ServiceKind.rb
100644 97 lines (75 sloc) 1.788 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#
# ServiceKind.rb
# starjour
#
# Created by Lachie Cox on 21/06/08.
# Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
 
require 'osx/cocoa'
 
class ServiceKind < OSX::NSObject
  include OSX
  
attr_reader :name
attr_reader :klass
 
attr_reader :children
kvc_array_accessor :children
 
def leaf; false end
 
def self.service_lookup
@service_lookup ||= {}
  end
 
def initWithName_andClass(name,klass)
if init
puts "service kind #{name} with #{klass}"
@name = name
@klass = klass
@children = NSMutableArray.alloc.init
 
self.class.service_lookup[name] = self
@jours = {}
end
self
  end
  
  def text
    begin
      @klass.service_name
    rescue
      @name
    end.upcase
  end
  
  def service_key(service)
service.name.to_ruby + service.oc_type.to_ruby
end
 
def self.service_basename(service)
service.oc_type.to_ruby[/_([^\.]+)\._tcp/,1]
  end
  
  def service_type
    "_#{name}._tcp"
  end
  
  def self.resolve_path(sc_index,jour_index)
    
  end
  
  def self.found(service)
    puts "found service #{service.oc_type}"
    
    if srv = service_lookup[service_basename(service)]
      srv.found(service)
    end
  end
  
  def found(service)
    puts "lets go"
 
    if jour = klass.alloc.initWithService(service)
      service.delegate = jour
      service.resolveWithTimeout(60)
 
      index = @children.size
      insertObject_inChildrenAtIndex(jour, index)
      @jours[service_key(service)] = jour
    end
  end
  
  def self.lost(service)
    if srv = service_lookup[service_basename(service)]
      srv.lost(service)
    end
  end
  
  def lost(service)
    if jour = @jours[service_key(service)]
index = @children.index(jour)
removeObjectFromChildrenAtIndex(index)
end
  end
end