Module: Garden::Cycles

Included in:
Garden
Defined in:
lib/garden_cycles.rb

Overview

The Cycles module is a set of method used by the Garden’s fork. You will not need to use these methods unless you plan on refactoring the Garden.

Author

lp ([email protected])

Copyright

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

:title:Cycles

Instance Method Summary collapse

Instance Method Details

#crop_writable(writable) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/garden_cycles.rb', line 28

def crop_writable(writable)
			$log_abundance.debug('Garden::Cycles') {"crop_writable"}
  writable.each do |o_socket|
    if @writer[:buffer][o_socket.to_s] == '' || @writer[:buffer][o_socket.to_s] == nil
      remove_writable(o_socket)
    else
      write_raw(o_socket)
    end
  end
end

#route_message_blocksObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/garden_cycles.rb', line 39

def route_message_blocks
			$log_abundance.debug('Garden::Cycles') {"route_message_blocks"}
  until @message_block_queue.empty?
    message_block = @message_block_queue.shift

    case message_block[0]
    when :seed
      place_seed_in_queue(message_block)
    when :row
      this_row_is_available(message_block)  
    when :crop
      save_crop_for(message_block)
    when :growth
      report_growth(message_block)
    when :harvest
      harvest_some(message_block)
    when :close
      close_all(message_block)
    else
      message_block[2] = false
      add_writable(message_block)
    end

  end
end

#seed_available_rowsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/garden_cycles.rb', line 65

def seed_available_rows
				$log_abundance.debug('Garden::Cycles') {"seed_available_rows"}
  catch :fill_rows do
					looped = 0
    while (looped+=1) < 9
throw :fill_rows if @waiting_rows.empty?
$log_abundance.debug('Garden::Cycles') {"seed_available_rows: waiting_rows: #{@waiting_rows.inspect}"}
      if @seed_all_message_block && @seed_all_message_block[4][:row_done].size != @seed_all_message_block[1]
        $log_abundance.debug('Garden::Cycles') {"seed_available_rows: all_message_block #{@seed_all_message_block.inspect}"}
	row_socket_path = @waiting_rows.shift
        unless @seed_all_message_block[4][:row_done].include?( row_socket_path )
          add_writable([:seed,:all,@seed_all_message_block[2],row_socket_path])
          @seed_all_message_block[4][:row_done] << row_socket_path
        else
          @waiting_rows << row_socket_path
        end

      elsif @init_message_block && @init_message_block[4][:row_done].size != @init_message_block[2]
        $log_abundance.debug('Garden::Cycles') {"seed_available_rows: init_message_block #{@init_message_block.inspect}"}
	row_socket_path = @waiting_rows.shift
        unless @init_message_block[4][:row_done].include?( row_socket_path )
          add_writable([:seed,:init,'init_status',row_socket_path])
          @init_message_block[4][:row_done] << row_socket_path
        else
          @waiting_rows << row_socket_path
        end

      elsif ! @seeds.empty?
	$log_abundance.debug('Garden::Cycles') {"seed_available_rows: ! seeds.empty? #{@seeds.inspect}"}
        seed = @seeds.shift; @sprouts[seed[:id]] = seed
        add_writable([:seed,:sprout,seed,@waiting_rows.shift])

      elsif @close_message_block && ! @waiting_rows.empty?
	$log_abundance.debug('Garden::Cycles') {"seed_available_rows: close_message_block: #{@close_message_block.inspect}"}
        add_writable([:seed,:quit,nil,@waiting_rows.shift])

      else
	$log_abundance.debug('Garden::Cycles') {"seed_available_rows: else"}
        throw :fill_rows
end
               
    end
  end
end

#set_my_containersObject



10
11
12
13
14
15
# File 'lib/garden_cycles.rb', line 10

def set_my_containers
  @close_message_block = nil; @full_crop_message_block = nil
  @init_message_block = nil; @seed_all_message_block = nil
  @harvest_queue = []; @waiting_rows = []; @message_block_queue = []
  @seeds = []; @sprouts = []; @crops = []; @id = 0
end

#sprout_readable(readable) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/garden_cycles.rb', line 17

def sprout_readable(readable)
			$log_abundance.debug('Garden::Cycles') {"sprout_readable"}
  readable.each do |i_socket|
    if i_socket == @my_socket
      add_readable(i_socket)
    else
      readable_main(i_socket)
    end
  end
end