Class: Shuttlecraft

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

Overview

Must define tuplespace to read from

def tuplespace

Must call initialize_comms inside initialize

Direct Known Subclasses

MyShuttlecraft

Defined Under Namespace

Modules: Comms Classes: Mothership, MothershipApp, Protocol, ShuttlecraftApp, Test

Constant Summary collapse

VERSION =
'0.0.1'
PROVIDER_TEMPLATE =
:name, :Mothership, name, Rinda::TupleSpace:tuplespace
[:name, :Mothership, String, nil]
REGISTRATION_TEMPLATE =
:name, name, drb_uri
[:name, String, String]

Instance Attribute Summary collapse

Class Method 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 = {}) ⇒ Shuttlecraft

Returns a new instance of Shuttlecraft.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shuttlecraft.rb', line 19

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

  @drb = DRb.start_service(nil, self)

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

  @ring_server = Rinda::RingFinger.primary
  @receive_loop = nil
end

Instance Attribute Details

#mothershipObject

Returns the value of attribute mothership.



17
18
19
# File 'lib/shuttlecraft.rb', line 17

def mothership
  @mothership
end

#nameObject

Returns the value of attribute name.



17
18
19
# File 'lib/shuttlecraft.rb', line 17

def name
  @name
end

#protocolObject

Returns the value of attribute protocol.



17
18
19
# File 'lib/shuttlecraft.rb', line 17

def protocol
  @protocol
end

#ring_serverObject

Returns the value of attribute ring_server.



17
18
19
# File 'lib/shuttlecraft.rb', line 17

def ring_server
  @ring_server
end

Class Method Details

.default_nameObject



32
33
34
# File 'lib/shuttlecraft.rb', line 32

def self.default_name
  'Shuttlecraft'
end

Instance Method Details

#find_all_mothershipsObject



42
43
44
# File 'lib/shuttlecraft.rb', line 42

def find_all_motherships
  ring_server.read_all(provider_template).collect{|_,_,name,ts| {name: name, ts: ts}}
end

#initiate_communication_with_mothership(name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/shuttlecraft.rb', line 46

def initiate_communication_with_mothership(name=nil)
  motherships = find_all_motherships

  if name
    provider = motherships.detect{|m| m[:name] == name}
  else
    provider = motherships.first
  end

  if provider
    @mothership = Rinda::TupleSpaceProxy.new provider[:ts]
  end
end

#provider_templateObject



36
37
38
39
40
# File 'lib/shuttlecraft.rb', line 36

def provider_template
  temp = PROVIDER_TEMPLATE.dup
  temp[1] = @protocol.service_name
  temp
end

#registerObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/shuttlecraft.rb', line 60

def register
  update!
  unless @mothership.nil? || registered?
    begin
      @mothership.write([:name, @name, DRb.uri])
    rescue DRb::DRbConnError
      # mothership went away =(
    end
  end
end

#registered?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/shuttlecraft.rb', line 71

def registered?
  update!
  return false unless @mothership

  !registered_services.detect{|t| !t.nil? && t[0] == @name && t[1] == DRb.uri}.nil?
end

#unregisterObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/shuttlecraft.rb', line 78

def unregister
  update!
  if registered?
    begin
      @mothership.take([:name, @name, DRb.uri])
    rescue DRb::DRbConnError
      # mothership went away =(
    end
  end
end