Class: Estore::ConnectionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/estore/connection_context.rb

Overview

Registry storing handlers for the pending commands

Instance Method Summary collapse

Constructor Details

#initializeConnectionContext

Returns a new instance of ConnectionContext.



6
7
8
9
# File 'lib/estore/connection_context.rb', line 6

def initialize
  @mutex = Mutex.new
  @commands = {}
end

Instance Method Details

#dispatch(uuid, message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/estore/connection_context.rb', line 23

def dispatch(uuid, message)
  command = @commands[uuid]
  command.handle(message) if command
rescue => error
  command.reject! error
  remove(command)

  puts "[DISPATCH] #{error.message}"
  puts error.backtrace
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/estore/connection_context.rb', line 34

def empty?
  @commands.empty?
end

#on_error(error) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/estore/connection_context.rb', line 38

def on_error(error)
  # TODO: Error handling
  @mutex.synchronize do
    @commands.each { |_uuid, command| command.reject! error }
    @commands = {}
  end
end

#register(command) ⇒ Object



11
12
13
14
15
# File 'lib/estore/connection_context.rb', line 11

def register(command)
  @mutex.synchronize do
    @commands[command.uuid] = command
  end
end

#remove(command) ⇒ Object



17
18
19
20
21
# File 'lib/estore/connection_context.rb', line 17

def remove(command)
  @mutex.synchronize do
    @commands.delete(command.uuid)
  end
end