Module: Archipelago::Disco::Publishable

Included in:
Tranny::Manager, Treasure::Chest
Defined in:
lib/archipelago/disco.rb

Overview

A module to simplify publishing services.

If you include it you can use the publish! method at your convenience.

If you want to customize the publishing related behaviour you can call initialize_publishable with a Hash of options.

See Archipelago::Treasure::Chest or Archipelago::Tranny::Manager for examples.

It will store the service_id of this service in a directory beside this file (publishable.rb) named as the class you include into unless you define @persistence_provider before you call initialize_publishable.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object

Also add the ClassMethods to base.



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

def self.append_features(base)
  super
  base.extend(ClassMethods)
end

Instance Method Details

#_dump(dummy_param) ⇒ Object

Dump a DRbObject refering to us.



111
112
113
# File 'lib/archipelago/disco.rb', line 111

def _dump(dummy_param)
  DRbObject.new(self)._dump(dummy_param)
end

#initialize_publishable(options = {}) ⇒ Object

Will initialize this instance with @service_description and @jockey_options and merge these with the optionally given :service_description and :jockey_options.



120
121
122
123
124
125
126
127
128
# File 'lib/archipelago/disco.rb', line 120

def initialize_publishable(options = {})
  @service_description = {
    :service_id => service_id,
    :validator => self,
    :service => self,
    :class => self.class.name
  }.merge(options[:service_description] || {})
  @jockey_options = options[:jockey_options] || {}
end

#publish!(options = {}) ⇒ Object

Create an Archipelago::Disco::Jockey for this instance using @jockey_options or optionally given :jockey_options.

Will publish this service using @service_description or optionally given :service_description.



137
138
139
140
# File 'lib/archipelago/disco.rb', line 137

def publish!(options = {})
  @jockey ||= defined?(Archipelago::Disco::MC) ? Archipelago::Disco::MC : Archipelago::Disco::Jockey.new(@jockey_options.merge(options[:jockey_options] || {}))
  @jockey.publish(Archipelago::Disco::Record.new(@service_description.merge(options[:service_description] || {})))
end

#service_idObject

Returns our semi-unique id so that we can be found again.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/archipelago/disco.rb', line 170

def service_id
  #
  # The provider of happy magic persistent hashes of different kinds.
  #
  @persistence_provider ||= Archipelago::Hashish::BerkeleyHashishProvider.new(Pathname.new(File.expand_path(__FILE__)).parent.join(self.class.name + ".db"))
  #
  # Stuff that didnt fit in any of the other databases.
  #
  @metadata ||= @persistence_provider.get_hashish("metadata")
  return @metadata["service_id"] ||= Digest::SHA1.hexdigest("#{HOST}:#{Time.new.to_f}:#{self.object_id}:#{rand(1 << 32)}").to_s
end

#stop!Object

Stops the publishing of this Publishable.



156
157
158
159
160
161
162
163
164
165
# File 'lib/archipelago/disco.rb', line 156

def stop!
  if valid?
    @valid = false
    if defined?(Archipelago::Disco::MC) && @jockey == Archipelago::Disco::MC
      @jockey.unpublish(self.service_id)
    else
      @jockey.stop!
    end
  end
end

#valid?Boolean

We are always valid if we are able to reply.

Returns:

  • (Boolean)


145
146
147
148
149
150
151
# File 'lib/archipelago/disco.rb', line 145

def valid?
  if defined?(@valid)
    @valid
  else
    @valid = true
  end
end