Class: Archipelago::Client::Base

Inherits:
Object
  • Object
show all
Includes:
Disco::Camel
Defined in:
lib/archipelago/client.rb

Direct Known Subclasses

Pirate::Captain, Sanitation::Officer

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Finding our services dynamically.



71
72
73
74
75
76
77
# File 'lib/archipelago/client.rb', line 71

def method_missing(meth, *args)
  if @services.include?(meth)
    return @services[meth]
  else
    super
  end
end

Instance Attribute Details

#jockeyObject (readonly)

Returns the value of attribute jockey.



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

def jockey
  @jockey
end

#service_descriptionsObject (readonly)

Returns the value of attribute service_descriptions.



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

def service_descriptions
  @service_descriptions
end

#servicesObject (readonly)

Returns the value of attribute services.



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

def services
  @services
end

Instance Method Details

#around_update_services(&block) ⇒ Object

Override this if you want to do something special before or after calling update_services!



95
96
97
# File 'lib/archipelago/client.rb', line 95

def around_update_services(&block)
  yield
end

#setup_client(options = {}) ⇒ Object

Initialize a Client using Archipelago::Disco::MC or :jockey if given, or a new Archipelago::Disco::Jockey if none, that looks for new services :initial_service_update_interval or INITIAL_SERVICE_UPDATE_INTERVAL, when it starts and never slower than every :maximum_service_update_interval or MAXIMUM_SERVICE_UPDATE_INTERVAL.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/archipelago/client.rb', line 51

def setup_client(options = {})
  setup_jockey(options)

  @initial_service_update_interval = options[:initial_service_update_interval] || INITIAL_SERVICE_UPDATE_INTERVAL
  @maximum_service_update_interval = options[:maximum_service_update_interval] || MAXIMUM_SERVICE_UPDATE_INTERVAL
  @service_descriptions = options[:service_descriptions]
  @initial_lookup_timeout = options[:initial_lookup_timeout] || INITIAL_LOOKUP_TIMEOUT
  @services = {}
  @service_descriptions.each do |name, description|
    @services[name] = Archipelago::Disco::ServiceLocker.new
    @services[name].convert_to_tree!
  end

  start_service_updater
  start_subscriptions
end

#stop!Object

Stops the service update thread for this Pirate and also unpublishes and/or stops the Jockey.



83
84
85
86
87
88
89
# File 'lib/archipelago/client.rb', line 83

def stop!
  @service_update_thread.kill if @service_update_thread
  stop_subscriptions
  unless defined?(Archipelago::Disco::MC) && @jockey && @jockey == Archipelago::Disco::MC
    @jockey.stop!
  end
end

#update_services!(options = {}) ⇒ Object

Make our @jockey lookup all our services.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/archipelago/client.rb', line 102

def update_services!(options = {})
  timeout = options[:timeout] || 0
  validate = options[:validate] || false
  around_update_services do
    @service_descriptions.each do |name, description|
      #
      # This sure sounds inefficient, but hey, listen up:
      #
      # * RBTrees are nice and fast when it comes to looking up ordered stuff.
      # * They are horribly slow in all other ways.
      #   * As an example: It takes (as of this writing) 10x the time to insert 10k elements in an RBTree
      #     as it takes to sort 10k elements in an Array.
      #
      # This means that using them in Archipelago::Disco::ServiceLocker will be inefficient as hell, since they
      # are merging and creating new maps all the time. But in here, I expect us to not renew our service lists
      # more than on average once every MAXIMUM_SERVICE_UPDATE_INTERVAL, so that MAY make it worthwhile to do
      # the RBTree song and dance in here. Hopefully.
      #
      new_services = @jockey.lookup(Archipelago::Disco::Query.new(description), timeout)
      new_services.convert_to_tree!
      new_services.validate! if validate
      @services[name] = new_services
    end
  end
end