Class: Metacosm::RemoteSimulation
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, #receive, #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
13
|
# File 'lib/metacosm/remote_simulation.rb', line 13
def apply(command); fire command end
|
#fire(command) ⇒ Object
15
16
17
18
19
|
# File 'lib/metacosm/remote_simulation.rb', line 15
def fire(command)
command_dto = command.attrs.merge(handler_module: command.handler_module_name, handler_class_name: command.handler_class_name)
redis = redis_connection
redis.publish(@command_queue_name, Marshal.dump(command_dto))
end
|
#redis_connection ⇒ Object
9
10
11
|
# File 'lib/metacosm/remote_simulation.rb', line 9
def redis_connection
Redis.new
end
|
#setup_connection ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/metacosm/remote_simulation.rb', line 21
def setup_connection
@remote_listener_thread = Thread.new do
begin
redis = redis_connection
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)
end
on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from remote simulation event stream ##{channel} (#{subscriptions} subscriptions)"
end
end
rescue Redis::BaseConnectionError => error
puts "#{error}, retrying in 1s"
sleep 1
retry
end
end
end
|