Class: Handlers::SenderHandler

Inherits:
SRCommonHandler show all
Defined in:
lib/handlers/sender_handler.rb

Overview

Sender events handler for sender client

Instance Attribute Summary collapse

Attributes inherited from SRCommonHandler

#auto_settle_off, #idle_timeout, #log_lib, #log_msgs, #max_frame_size, #msg_content_hashed, #sasl_enabled

Attributes inherited from BasicHandler

#broker, #exit_timer, #idle_timeout, #log_lib, #max_frame_size, #sasl_enabled, #sasl_mechs

Instance Method Summary collapse

Methods inherited from SRCommonHandler

#on_error, #print_message

Constructor Details

#initialize(broker, log_msgs, msg_content_hashed, count, msg_properties, msg_content, msg_content_type, msg_durable, msg_ttl, msg_correlation_id, msg_reply_to, msg_group_id, msg_to, msg_priority, msg_id, msg_user_id, msg_subject, anonymous, sasl_mechs, idle_timeout, max_frame_size, sasl_enabled, log_lib, auto_settle_off, exit_timer, duration, duration_mode) ⇒ SenderHandler

Initialization of sender events handler

Sender events handler arguments

broker

URI of broker

log_msgs

format of message(s) log

count

number of messages to send

msg_content

message content

msg_durable

message durability

msg_ttl

message TTL (ms)

msg_correlation_id

message correlation ID

msg_reply_to

address to send reply to

msg_group_id

message group ID

msg_to

message destination

sasl_mechs

allowed SASL mechanisms



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/handlers/sender_handler.rb', line 69

def initialize(
  broker,
  log_msgs,
  msg_content_hashed,
  count,
  msg_properties,
  msg_content,
  msg_content_type,
  msg_durable,
  msg_ttl,
  msg_correlation_id,
  msg_reply_to,
  msg_group_id,
  msg_to,
  msg_priority,
  msg_id,
  msg_user_id,
  msg_subject,
  anonymous,
  sasl_mechs,
  idle_timeout,
  max_frame_size,
  sasl_enabled,
  log_lib,
  auto_settle_off,
  exit_timer,
  duration,
  duration_mode
)
  super(
    broker,
    log_msgs,
    msg_content_hashed,
    sasl_mechs,
    idle_timeout,
    max_frame_size,
    sasl_enabled,
    log_lib,
    auto_settle_off,
    exit_timer
  )
  # Save count of messages to be send
  @count = count
  # Save message properties
  @msg_properties = msg_properties
  # Save message content
  @msg_content = msg_content
  # Save message content type
  @msg_content_type = msg_content_type
  # Save message durability
  @msg_durable = msg_durable
  # Save message TTL (ms)
  @msg_ttl = msg_ttl
  # Save message correlation ID
  @msg_correlation_id = msg_correlation_id
  # Save reply to address
  @msg_reply_to = msg_reply_to
  # Save message group ID
  @msg_group_id = msg_group_id
  # Save message destination
  @msg_to = msg_to
  # Save message priority
  @msg_priority = msg_priority
  # Save message ID
  @msg_id = msg_id
  # Save user ID
  @msg_user_id = msg_user_id
  # Save message subject
  @msg_subject = msg_subject
  # Save anonymous
  @anonymous = anonymous
  # Number of sent messages
  @sent = 0
  # Number of accepted messages
  @accepted = 0
  # Duration
  @duration = Duration.new(duration, count, duration_mode)
  # True if a send has been scheduled
  @scheduled = false
end

Instance Attribute Details

#anonymousObject

Anonymous



54
55
56
# File 'lib/handlers/sender_handler.rb', line 54

def anonymous
  @anonymous
end

#countObject

Count of messages to be send



26
27
28
# File 'lib/handlers/sender_handler.rb', line 26

def count
  @count
end

#msg_contentObject

Message content



30
31
32
# File 'lib/handlers/sender_handler.rb', line 30

def msg_content
  @msg_content
end

#msg_content_typeObject

Message content type



32
33
34
# File 'lib/handlers/sender_handler.rb', line 32

def msg_content_type
  @msg_content_type
end

#msg_correlation_idObject

Message correlation ID



38
39
40
# File 'lib/handlers/sender_handler.rb', line 38

def msg_correlation_id
  @msg_correlation_id
end

#msg_durableObject

Message durability



34
35
36
# File 'lib/handlers/sender_handler.rb', line 34

def msg_durable
  @msg_durable
end

#msg_group_idObject

Message group ID



42
43
44
# File 'lib/handlers/sender_handler.rb', line 42

def msg_group_id
  @msg_group_id
end

#msg_idObject

Message ID



48
49
50
# File 'lib/handlers/sender_handler.rb', line 48

def msg_id
  @msg_id
end

#msg_priorityObject

Message priority



46
47
48
# File 'lib/handlers/sender_handler.rb', line 46

def msg_priority
  @msg_priority
end

#msg_propertiesObject

Message properties



28
29
30
# File 'lib/handlers/sender_handler.rb', line 28

def msg_properties
  @msg_properties
end

#msg_reply_toObject

Reply to address



40
41
42
# File 'lib/handlers/sender_handler.rb', line 40

def msg_reply_to
  @msg_reply_to
end

#msg_subjectObject

Message subject



52
53
54
# File 'lib/handlers/sender_handler.rb', line 52

def msg_subject
  @msg_subject
end

#msg_toObject

Message destination



44
45
46
# File 'lib/handlers/sender_handler.rb', line 44

def msg_to
  @msg_to
end

#msg_ttlObject

Message TTL (ms)



36
37
38
# File 'lib/handlers/sender_handler.rb', line 36

def msg_ttl
  @msg_ttl
end

#msg_user_idObject

Message user ID



50
51
52
# File 'lib/handlers/sender_handler.rb', line 50

def msg_user_id
  @msg_user_id
end

Instance Method Details

#delayObject



176
177
178
179
180
# File 'lib/handlers/sender_handler.rb', line 176

def delay
  before = @duration.delay("before-send")
  after = @duration.delay("after-send") if @sent > 0 # No after-delay on first send
  [before, after].compact.inject(:+)
end

#on_container_start(container) ⇒ Object

Called when the event loop starts, connects sender client to SRCommonHandler#broker and creates sender



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/handlers/sender_handler.rb', line 153

def on_container_start(container)
  # Connecting to broker and creating sender
  container.connect(
    # Set broker URI
    @broker,
    # Enable SASL authentication
    sasl_enabled: @sasl_enabled,
    # Enable insecure SASL mechanisms
    sasl_allow_insecure_mechs: true,
    # Set allowed SASL mechanisms
    sasl_allowed_mechs: @sasl_mechs,
    # Set idle timeout
    idle_timeout: @idle_timeout,
    # Set max frame size
    max_frame_size: @max_frame_size,
  ).open_sender({
      # Set target address
      :target => anonymous ? nil : @broker.amqp_address,
      # Set auto settle
      :auto_settle => @auto_settle_off ? false : true,
    })
end

#on_sendable(sender) ⇒ Object

Called when the sender link has credit and messages can therefore be transferred, sending SenderHandler#count messages



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/handlers/sender_handler.rb', line 185

def on_sendable(sender)
  if @duration.zero?        # Send immediately
    send(sender) while (sender.credit > 0) && (@sent < @count)
  elsif (sender.credit > 0) && (@sent < @count) && !@scheduled # Schedule to send after delay
    @scheduled = true
    c = sender.connection.container
    c.schedule(delay) do
      send(sender)
      @scheduled = false    # Need to re-schedule for another send
    end
  end
end

#on_tracker_accept(tracker) ⇒ Object

Called when the remote peer accepts an outgoing message, accepting SenderHandler#count messages



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/handlers/sender_handler.rb', line 253

def on_tracker_accept(tracker)
  # Increase number of accepted messages
  @accepted = @accepted + 1
  # If all messages to be send are sent and accepted
  if @accepted == @count
    # Close sender
    tracker.sender.close
    # Close connection
    tracker.sender.connection.close
  end # if
end

#send(sender) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/handlers/sender_handler.rb', line 198

def send(sender)
  exit_timer.reset if exit_timer
  # Create new message
  msg = Qpid::Proton::Message.new
  # Set message destination
   msg.address = @msg_to
  # If message destination is not set
  unless msg.address
    # Set message destination if anonymous mode
    msg.address = @broker.amqp_address if @anonymous
  end
  # Set message properties
  if @msg_properties
    @msg_properties.each { |k, v| msg[k] = v }
  end
  # If message content is set
  if @msg_content
    # If message content is string and contains formatting part
    if @msg_content.is_a? String and @msg_content =~ /%[0-9]*d/
      # Format message content with number of sent messages
      msg.body = sprintf(@msg_content, @sent)
    else
      # Set message content as it is
      msg.body = @msg_content
    end
  end # if
  # Set message content type if specified
  msg.content_type = @msg_content_type if @msg_content_type
  # Set message durability
  msg.durable = @msg_durable
  # Set message TTL (ms)
  msg.ttl = @msg_ttl
  # If message correlation ID is set
  if @msg_correlation_id
    msg.correlation_id = @msg_correlation_id
  end # if
  # Set reply to address
  msg.reply_to = @msg_reply_to
  # If message group ID is set
  if @msg_group_id
    msg.group_id = @msg_group_id
  end
  msg.priority = @msg_priority if @msg_priority
  msg.id = @msg_id if @msg_id
  msg.user_id = @msg_user_id if @msg_user_id
  msg.subject = @msg_subject if @msg_subject
  # Send message
  sender.send(msg)
  # Increase number of sent messages
  @sent = @sent + 1
  print_message(msg)
end