Class: Gardener

Inherits:
Object
  • Object
show all
Includes:
Toolshed
Defined in:
lib/gardener.rb

Overview

This class provides the gardener part of the Gardener,Garden,Seed natural design patern

The Gardener act as the client class for accessing and assessing the Garden ressources. Its initialization occurs through the Abundance.gardener class method. Its instance methods are fourthfold, following the 4 states of the garden. Like the 4 seasons northern hemisphere gardening cycles:

  • seed = the setting of your command cycle

  • growth = the evolution of your command growing period

  • harvest = the getting of your command results

  • close = the closing and dying cycle

Author

lp ([email protected])

Copyright

2008 Louis-Philippe Perron - Released under the terms of the MIT license

:title:Gardener

Constant Summary

Constants included from Toolshed

Toolshed::UDP_HOST

Instance Method Summary collapse

Methods included from Toolshed

available_port, block_size, block_size=, garden_port, garden_port=, socket_client_perm, #socket_client_perm_duplex, #socket_client_perm_send, socket_client_temp, #socket_client_temp, socket_server, #socket_server_recv, #socket_server_send

Constructor Details

#initialize(options, gardener_block) ⇒ Gardener

The new class method initializes the class. As part of the Abundance lib, Gardener is not initialized directly, but rather through Abundance.gardener.

Parameters

  • :seed_size = allowed seed size in bytes

  • :rows = garden rows number, the number of concurent threads

  • :init_timeout = allow to pause execution to allow for larger gardens to initialize

Example

gardener = Gardener.new({:seed_size => 1024, :rows => 6, :init_timeout => 1}) do
 Abundance.grow do |seed|
   seed.crop(`#{seed.sprout}`)
 end
end


36
37
38
39
40
41
42
43
44
# File 'lib/gardener.rb', line 36

def initialize(options,gardener_block)
  Toolshed::block_size = options[:seed_size]
  Toolshed::garden_port = Toolshed.available_port
  
  @garden = Garden.new
  @garden_rows = @garden.rows(options[:rows], options[:init_timeout], gardener_block)
  
  @socket_client_perm = Toolshed.socket_client_perm
end

Instance Method Details

#closeObject

The close method for the Gardener instance allow to safely close the Garden and its Rows. It return a hash of respective arrays for crops, sprouts and seeds at the moment of closing.

Example

final_harvest = gardener.close


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

def close
  command, data = socket_client_perm_duplex(:close,{:level => :garden, :pid => @garden_rows.pids})
  return data
end

#growth(crop = :progress) ⇒ Object

The growth method for the Gardener instance allow to get report of the growing process

Parameter

The parameter given as a symbol specifies the level of growth report you wish to get:

  • :progress = return actual progress status, scaled between 0.00 and 1.00

  • :seed = return total seeds waiting to be processed

  • :sprout = return total seeds actually in process

  • :crop = return total seeds for which process has completed

Example

progress = gardener.growth(:progress)
puts "progress is now #{progress}"  # => progress is now 0.75


68
69
70
71
# File 'lib/gardener.rb', line 68

def growth(crop=:progress)
  command, data = socket_client_perm_duplex(:growth,crop)
  return data
end

#harvest(crop) ⇒ Object

The harvest method for the Gardener instance allow to get arrays of results for each queue level

Parameter

The parameter given as a symbol specifies the level of queue results you wish to get:

  • seedID = return the result for a specific seed, if seed hasn’t processed it wait until completed

  • :crop = return an array of seed for which process has completed

  • :sprout = return an array of seed actually processing

  • :seed = return an array of seed waiting to be processed

  • :all = return a hash of respective arrays for crops, sprouts and seeds

Example

puts "result is: #{gardener.harvest(id_seed_1)}  #  => result is: ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]


84
85
86
87
# File 'lib/gardener.rb', line 84

def harvest(crop)
  command, data = socket_client_perm_duplex(:harvest,crop)
  return data
end

#seed(command) ⇒ Object

The seed method for the Gardener instance allow to sow a command in the Gardener’s Garden.

Parameter

  • command = a ruby expression or object

Example

id_seed_1 = gardener.seed('ruby -v')


52
53
54
55
# File 'lib/gardener.rb', line 52

def seed(command)
  command, data = socket_client_perm_duplex(:seed,command)
  return data
end