Class: Shuttlecraft::Mothership

Inherits:
Object
  • Object
show all
Includes:
Comms
Defined in:
lib/shuttlecraft/mothership.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Comms

#each_client, #each_service_uri, #initialize_comms, #registered_services, #update, #update!, #update?

Constructor Details

#initialize(opts = {}) ⇒ Mothership

Returns a new instance of Mothership.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shuttlecraft/mothership.rb', line 10

def initialize(opts={})
  initialize_comms(opts)

  @drb = DRb.start_service

  @protocol = opts[:protocol] || Shuttlecraft::Protocol.default
  @name = opts[:name] || @protocol.name
  @verbose = opts[:verbose] || false

  @ts = Rinda::TupleSpace.new

  renewer = Rinda::SimpleRenewer.new 10

  @provider =
    Rinda::RingProvider.new(@protocol.service_name, @name, @ts, renewer)
  @provider.provide

  notify_on_registration
  notify_on_unregistration
  notify_on_write
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/shuttlecraft/mothership.rb', line 8

def name
  @name
end

#protocolObject (readonly)

Returns the value of attribute protocol.



8
9
10
# File 'lib/shuttlecraft/mothership.rb', line 8

def protocol
  @protocol
end

#providerObject (readonly)

Returns the value of attribute provider.



8
9
10
# File 'lib/shuttlecraft/mothership.rb', line 8

def provider
  @provider
end

#tsObject (readonly)

Returns the value of attribute ts.



8
9
10
# File 'lib/shuttlecraft/mothership.rb', line 8

def ts
  @ts
end

Instance Method Details

#allow_registration?Boolean

Override this method to add custom registration restrictions

Returns:

  • (Boolean)


35
36
37
# File 'lib/shuttlecraft/mothership.rb', line 35

def allow_registration?
  true
end

#notify_on_registrationObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shuttlecraft/mothership.rb', line 39

def notify_on_registration
  @registration_observer = @ts.notify('write', Shuttlecraft::REGISTRATION_TEMPLATE)
  Thread.new do
    @registration_observer.each do |reg|
      puts "Recieved registration from #{reg[1][1]}" if @verbose
      if allow_registration?
        update!
        send(:on_registration) if respond_to? :on_registration
      else
        puts "Registration not allowed" if @verbose
        @ts.take(reg[1])
      end
    end
  end
end

#notify_on_unregistrationObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/shuttlecraft/mothership.rb', line 55

def notify_on_unregistration
  @unregistration_observer = @ts.notify('take', Shuttlecraft::REGISTRATION_TEMPLATE)
  Thread.new do
    @unregistration_observer.each do |reg|
      puts "Recieved unregistration from #{reg[1][1]}" if @verbose
      update!
      send(:on_unregistration) if respond_to? :on_unregistration
    end
  end
end

#notify_on_writeObject



66
67
68
69
70
71
# File 'lib/shuttlecraft/mothership.rb', line 66

def notify_on_write
  @write_observer = @ts.notify 'write', [nil]
  Thread.new do
    @write_observer.each {|n| p n}
  end
end