Class: Metacosm::RemoteSimulation

Inherits:
Simulation show all
Defined in:
lib/metacosm/remote_simulation.rb

Instance Method Summary collapse

Methods inherited from Simulation

#apply_event, #clear!, #command_queue, #conduct!, #construct_handler_for, #construct_listener_for, current, #disable_local_events, #event_queue, #events, #execute, #halt!, #handler_for, #listener_for, #local_events_disabled?, #mutex, #on_event, #params, #receive, #received_commands, #subscribe_for_commands

Constructor Details

#initialize(command_queue, event_stream) ⇒ RemoteSimulation

Returns a new instance of RemoteSimulation.



3
4
5
6
7
# File 'lib/metacosm/remote_simulation.rb', line 3

def initialize(command_queue, event_stream)
  @command_queue_name = command_queue
  @event_stream_name  = event_stream
  setup_connection
end

Instance Method Details

#apply(command) ⇒ Object



9
# File 'lib/metacosm/remote_simulation.rb', line 9

def apply(command); fire command end

#fire(command) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metacosm/remote_simulation.rb', line 11

def fire(command)
  puts "---> Firing command at remote sim..."
  puts "[command: #{command.inspect}]"
  command_dto = command.attrs.merge(handler_module: command.handler_module_name, handler_class_name: command.handler_class_name)

  Thread.new do
    REDIS_PUB.with do |redis|
      puts "---> Sending command over redis conn: #{redis.inspect}"
      redis.publish(@command_queue_name, Marshal.dump(command_dto))
    end
  end
  puts "---> Sent!"
  true
end

#received_eventsObject



26
27
28
# File 'lib/metacosm/remote_simulation.rb', line 26

def received_events
  @events_received ||= []
end

#setup_connectionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/metacosm/remote_simulation.rb', line 30

def setup_connection
  @remote_listener_thread = Thread.new do
    begin
      REDIS_SUB.with do |redis|
        redis.subscribe(@event_stream_name) do |on|
          on.subscribe do |channel, subscriptions|
            puts "Subscribed to remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
          end

          on.message do |channel, message|
            event = Marshal.load(message)
            listener_module_name = event.delete(:listener_module)
            listener_class_name = event.delete(:listener_class_name)
            module_name = listener_module_name
            module_name = "Object" if module_name.empty?
            listener = (module_name.constantize).const_get(listener_class_name).new(self)
            listener.receive(event)

            received_events.push(event)
          end

          on.unsubscribe do |channel, subscriptions|
            puts "Unsubscribed from remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
          end
        end
      end
    rescue ::Redis::BaseConnectionError => error
      puts "#{error}, retrying in 1s"
      sleep 1
      retry
    end
  end
end