Class: PRuby::FarmNode

Inherits:
Pipeline show all
Defined in:
lib/pruby/pipeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pipeline

#>>, create, #input_channel_connected?, #join, #output_channel_connected?, #run, sink, #sink?, source, #source?, #terminated?, #wrap_around!, #|

Constructor Details

#initialize(proc, nb) ⇒ Pipeline

Cree un nouvel objet pour une ferme

Parameters:

  • proc (Proc)

    Le Proc (lambda) pour le travailleur a activer

  • nb (Fixnum)

    Le nombre d’instances desirees



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/pruby/pipeline.rb', line 457

def initialize( proc, nb )
  DBC.require proc.class == Proc, "*** Argument de FarmNode doit etre un Proc"

  # @!visibility private
  def self.emitter(nb)
    lambda do |cin, cout|
      cin.each { |v| cout << v }
      nb.times do
        cout << EOS
      end
    end
  end

  # @!visibility private
  def self.collector(nb)
    lambda do |cin, cout|
      nb_eos = 0
      while nb_eos < nb
        v = cin.get
        if v == EOS
          nb_eos += 1
        else
          cout << v
        end
      end
      cout << EOS
    end
  end

  c_ew = Channel.new
  c_wc = Channel.new.with_multiple_writers(nb)
  @emitter = mk_node emitter(nb)
  @collector = mk_node collector(nb)
  @workers = (0...nb).map do
    pn = mk_node proc
    pn.input_channel = c_ew
    pn.output_channel = c_wc
    pn
  end
  @emitter.output_channel = c_ew
  @collector.input_channel = c_wc
end

Instance Attribute Details

#input_channelChannel

Le canal d'entree.

Returns:



504
505
506
# File 'lib/pruby/pipeline.rb', line 504

def input_channel
  @emitter.input_channel
end

#output_channelChannel

Le canal de sortie.

Returns:



512
513
514
# File 'lib/pruby/pipeline.rb', line 512

def output_channel
  @collector.output_channel
end

Instance Method Details

#inner_nodesArray<Pipeline>

Retourne les differents internes noeuds de la ferme.

Returns:



526
527
528
# File 'lib/pruby/pipeline.rb', line 526

def inner_nodes
  [@emitter] + @workers + [@collector]
end

#to_sString

Returns:

  • (String)


531
532
533
# File 'lib/pruby/pipeline.rb', line 531

def to_s
  "FarmNode(#@emitter |> #{@workers.map(&:to_s).join("|")} |> #@collector) "
end

#valueArray

Returns Le tableau des valeurs produites par chacun des travailleurs

Returns:

  • (Array)

    Le tableau des valeurs produites par chacun des travailleurs

Ensures:

  • Les threads des noeuds internes ont tous termine leur activite



538
539
540
541
# File 'lib/pruby/pipeline.rb', line 538

def value
  join
  @workers.map(&:value)
end