Class: Tochtli::BaseClient::SyncMessageHandler

Inherits:
AbstractMessageHandler show all
Includes:
MonitorMixin
Defined in:
lib/tochtli/base_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractMessageHandler

#reconstruct_exception

Constructor Details

#initializeSyncMessageHandler

Returns a new instance of SyncMessageHandler.



84
85
86
87
# File 'lib/tochtli/base_client.rb', line 84

def initialize
  super # initialize monitor
  @cv = new_cond
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



82
83
84
# File 'lib/tochtli/base_client.rb', line 82

def error
  @error
end

#replyObject (readonly)

Returns the value of attribute reply.



82
83
84
# File 'lib/tochtli/base_client.rb', line 82

def reply
  @reply
end

Instance Method Details

#call(reply) ⇒ Object



120
121
122
123
124
125
# File 'lib/tochtli/base_client.rb', line 120

def call(reply)
  synchronize do
    @reply = reply
    @cv.signal
  end
end

#handled?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/tochtli/base_client.rb', line 102

def handled?
  @reply || @error
end

#on_error(error_message) ⇒ Object



113
114
115
116
117
118
# File 'lib/tochtli/base_client.rb', line 113

def on_error(error_message)
  synchronize do
    @error = reconstruct_exception(error_message)
    @cv.signal
  end
end

#on_timeout(original_message = nil) ⇒ Object



106
107
108
109
110
111
# File 'lib/tochtli/base_client.rb', line 106

def on_timeout(original_message=nil)
  synchronize do
    @error = Timeout::Error.new(original_message ? "Unable to send message: #{original_message.inspect}" : "Service is not responding")
    @cv.signal
  end
end

#raise_errorObject



127
128
129
130
# File 'lib/tochtli/base_client.rb', line 127

def raise_error
  error = self.error || InternalServiceError.new("Unknwon", "Unknown Error")
  raise error
end

#wait(timeout) ⇒ Object



89
90
91
92
93
94
# File 'lib/tochtli/base_client.rb', line 89

def wait(timeout)
  synchronize do
    @cv.wait(timeout) unless handled?
  end
  on_timeout unless handled?
end

#wait!(timeout) ⇒ Object



96
97
98
99
100
# File 'lib/tochtli/base_client.rb', line 96

def wait!(timeout)
  wait(timeout)
  raise_error unless @reply
  @reply
end