Class: Isono::Node

Inherits:
Object
  • Object
show all
Includes:
AmqpClient, EventObservable, Logger
Defined in:
lib/isono/node.rb

Overview

A node instance which joins AMQP network.

Direct Known Subclasses

MessagingClient

Defined Under Namespace

Classes: ValueObject

Instance Attribute Summary collapse

Attributes included from AmqpClient

#amqp_client, #mq

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventObservable

#add_observer, #add_observer_once, #fire_event, #remove_observer

Methods included from AmqpClient

#amq, #amqp_server_uri, #close, #connect, #connected?, #create_channel, #on_close, #on_connect, #on_disconnected, #publish_to

Methods included from Logger

included, initialize

Constructor Details

#initialize(manifest) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
# File 'lib/isono/node.rb', line 58

def initialize(manifest)
  initialize_event_observable
  raise ArgumentError unless manifest.is_a? Manifest
  @manifest = manifest
  @boot_token = Digest::SHA1.hexdigest(Process.pid.to_s)[0,5]
  @value_objects = {}
end

Instance Attribute Details

#boot_tokenObject (readonly)

Returns the value of attribute boot_token.



56
57
58
# File 'lib/isono/node.rb', line 56

def boot_token
  @boot_token
end

#manifestObject (readonly)

Returns the value of attribute manifest.



56
57
58
# File 'lib/isono/node.rb', line 56

def manifest
  @manifest
end

#value_objectsObject (readonly)

Returns the value of attribute value_objects.



56
57
58
# File 'lib/isono/node.rb', line 56

def value_objects
  @value_objects
end

Class Method Details

.inherited(klass) ⇒ Object



14
15
16
17
18
19
# File 'lib/isono/node.rb', line 14

def self.inherited(klass)
  super
  klass.class_eval {
    include Logger
  }
end

.instanceObject



21
22
23
# File 'lib/isono/node.rb', line 21

def self.instance
  @instance
end

.start(manifest, opts) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/isono/node.rb', line 25

def self.start(manifest, opts)
  new_instance = proc {
    stop
    @instance = new(manifest)
    @instance.connect(opts[:amqp_server_uri], *opts)
  }
  
  if EventMachine.reactor_running?
    EventMachine.schedule {
      new_instance.call
    }
  else
    EventMachine.run new_instance
  end
end

.stop(&blk) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/isono/node.rb', line 41

def self.stop(&blk)
  return if @instance.nil?
  EventMachine.schedule {
    begin
      if @instance.connected?
        @instance.close {
          blk.call(@instance) if blk
        }
      end
    ensure
      @instance = nil
    end
  }
end

Instance Method Details

#after_closeObject



100
101
102
# File 'lib/isono/node.rb', line 100

def after_close
  manifest.node_modules.reverse.each &node_hook_proc(:after_close)
end

#after_connectObject



84
85
86
87
88
89
90
91
92
# File 'lib/isono/node.rb', line 84

def after_connect
  setup_identity_queue

  manifest.node_modules.each &node_hook_proc(:after_connect)
  # TODO: remove initialize_hook
  manifest.node_modules.each &node_hook_proc(:initialize)
  
  logger.info("Started : AMQP Server=#{amqp_server_uri.to_s}, ID=#{node_id}, token=#{boot_token}")
end

#before_closeObject



94
95
96
97
98
# File 'lib/isono/node.rb', line 94

def before_close
  manifest.node_modules.reverse.each &node_hook_proc(:before_close)
  # TODO: remove terminate_hook
  manifest.node_modules.reverse.each &node_hook_proc(:terminate)
end

#before_connectObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/isono/node.rb', line 70

def before_connect
  raise "node_id is not set" if node_id.nil?

  @value_objects = {}

  manifest.node_modules.each { |modclass, *args|
    if !@value_objects.has_key?(modclass)
      @value_objects[modclass] = vo = ValueObject.new(self, modclass)

      node_hook_proc(:before_connect).call(modclass, *args)
    end
  }
end

#node_idObject



66
67
68
# File 'lib/isono/node.rb', line 66

def node_id
  manifest.node_id
end