Class: ZMachine::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/zmachine/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args.



11
12
13
# File 'lib/zmachine/connection.rb', line 11

def args
  @args
end

#blockObject

Returns the value of attribute block.



12
13
14
# File 'lib/zmachine/connection.rb', line 12

def block
  @block
end

#channelObject

Returns the value of attribute channel.



10
11
12
# File 'lib/zmachine/connection.rb', line 10

def channel
  @channel
end

Class Method Details

.new(*args, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/zmachine/connection.rb', line 14

def self.new(*args, &block)
  allocate.instance_eval do
    initialize(*args, &block)
    @args, @block = args, block
    post_init
    self
  end
end

Instance Method Details

#_not_implementedObject

EventMachine Connection API

Raises:



61
62
63
# File 'lib/zmachine/connection.rb', line 61

def _not_implemented
  raise RuntimeError.new("API call not implemented! #{caller[0]}")
end

#acceptable!Object

triggers



234
235
236
237
238
239
240
241
# File 'lib/zmachine/connection.rb', line 234

def acceptable!
  client = @channel.accept
  connection_accepted(client) if client.connected?
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self, client: client) if ZMachine.debug
  self.class.new(*@args, &@block).tap do |instance|
    instance.channel = client
  end
end

#bind(address, port_or_type) ⇒ Object

channel type dispatch



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zmachine/connection.rb', line 25

def bind(address, port_or_type)
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  if address =~ %r{\w+://}
    @channel = ZMQChannel.new(port_or_type)
    @channel.bind(address)
  else
    @channel = TCPChannel.new
    @channel.bind(address, port_or_type)
  end
  self
end

#close_connection(after_writing = false) ⇒ Object Also known as: close



65
66
67
# File 'lib/zmachine/connection.rb', line 65

def close_connection(after_writing = false)
  @channel.close(after_writing)
end

#close_connection_after_writingObject Also known as: close_after_writing



71
72
73
# File 'lib/zmachine/connection.rb', line 71

def close_connection_after_writing
  close_connection(true)
end

#comm_inactivity_timeoutObject



77
78
79
# File 'lib/zmachine/connection.rb', line 77

def comm_inactivity_timeout
  @inactivity_timeout
end

#comm_inactivity_timeout=(value) ⇒ Object Also known as: set_comm_inactivity_timeout



81
82
83
# File 'lib/zmachine/connection.rb', line 81

def comm_inactivity_timeout=(value)
  @inactivity_timeout = value
end

#connect(address, port_or_type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zmachine/connection.rb', line 37

def connect(address, port_or_type)
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  if address.nil? or address =~ %r{\w+://}
    @channel = ZMQChannel.new(port_or_type)
    @channel.connect(address) if address
  else
    @channel = TCPChannel.new
    @channel.connect(address, port_or_type)
  end
  if @connect_timeout
    @timer = ZMachine.add_timer(@connect_timeout) do
      ZMachine.reactor.close_connection(self)
    end
  end
  self
end

#connectable!Object



243
244
245
246
247
248
249
250
251
# File 'lib/zmachine/connection.rb', line 243

def connectable!
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  @channel.finish_connecting
  @timer.cancel if @timer # cancel pending connect timer
  mark_active!
  connection_completed if @channel.connected?
  update_events
  nil
end

#connection_accepted(channel) ⇒ Object



87
88
# File 'lib/zmachine/connection.rb', line 87

def connection_accepted(channel)
end

#connection_completedObject



90
91
# File 'lib/zmachine/connection.rb', line 90

def connection_completed
end

#current_eventsObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/zmachine/connection.rb', line 282

def current_events
  if @channel.is_a?(ZMQChannel)
    return SelectionKey::OP_READ
  end

  if bound?
    return SelectionKey::OP_ACCEPT
  end

  if connection_pending?
    return SelectionKey::OP_CONNECT
  end

  events = 0

  events |= SelectionKey::OP_READ if notify_readable?
  events |= SelectionKey::OP_WRITE if notify_writable?

  return events
end

#detachObject



93
94
95
# File 'lib/zmachine/connection.rb', line 93

def detach
  _not_implemented
end

#error?Boolean

Returns:



97
98
99
# File 'lib/zmachine/connection.rb', line 97

def error?
  _not_implemented
end

#get_idle_timeObject



101
102
103
# File 'lib/zmachine/connection.rb', line 101

def get_idle_time
  (System.nano_time - @last_activity) / 1_000_000
end

#get_peer_certObject



105
106
107
# File 'lib/zmachine/connection.rb', line 105

def get_peer_cert
  _not_implemented
end

#get_peernameObject



109
110
111
112
113
# File 'lib/zmachine/connection.rb', line 109

def get_peername
  if peer = @channel.peer
    ::Socket.pack_sockaddr_in(*peer)
  end
end

#get_pidObject



115
116
117
# File 'lib/zmachine/connection.rb', line 115

def get_pid
  _not_implemented
end

#get_proxied_bytesObject



119
120
121
# File 'lib/zmachine/connection.rb', line 119

def get_proxied_bytes
  _not_implemented
end

#get_sock_opt(level, option) ⇒ Object



123
124
125
# File 'lib/zmachine/connection.rb', line 123

def get_sock_opt(level, option)
  _not_implemented
end

#get_socknameObject



127
128
129
# File 'lib/zmachine/connection.rb', line 127

def get_sockname
  _not_implemented
end

#get_statusObject



131
132
133
# File 'lib/zmachine/connection.rb', line 131

def get_status
  _not_implemented
end

#mark_active!Object



316
317
318
319
# File 'lib/zmachine/connection.rb', line 316

def mark_active!
  @last_activity = System.nano_time
  renew_timer if @inactivity_timeout
end

#notify_readable=(mode) ⇒ Object



135
136
137
# File 'lib/zmachine/connection.rb', line 135

def notify_readable=(mode)
  _not_implemented
end

#notify_readable?Boolean

Returns:



139
140
141
# File 'lib/zmachine/connection.rb', line 139

def notify_readable?
  true
end

#notify_writable=(mode) ⇒ Object



143
144
145
# File 'lib/zmachine/connection.rb', line 143

def notify_writable=(mode)
  _not_implemented
end

#notify_writable?Boolean

Returns:



147
148
149
# File 'lib/zmachine/connection.rb', line 147

def notify_writable?
  @channel.can_send?
end

#pauseObject



151
152
153
# File 'lib/zmachine/connection.rb', line 151

def pause
  _not_implemented
end

#paused?Boolean

Returns:



155
156
157
# File 'lib/zmachine/connection.rb', line 155

def paused?
  _not_implemented
end

#pending_connect_timeout=(value) ⇒ Object Also known as: set_pending_connect_timeout



159
160
161
# File 'lib/zmachine/connection.rb', line 159

def pending_connect_timeout=(value)
  @connect_timeout = value
end

#post_initObject



165
166
# File 'lib/zmachine/connection.rb', line 165

def post_init
end

#process_eventsObject



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/zmachine/connection.rb', line 303

def process_events
  return unless @channel_key
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  if @channel_key.connectable?
    connectable!
  elsif @channel_key.acceptable?
    acceptable!
  else
    writable! if @channel_key.writable?
    readable! if @channel_key.readable?
  end
end

#proxy_completedObject



168
169
170
# File 'lib/zmachine/connection.rb', line 168

def proxy_completed
  _not_implemented
end

#proxy_incoming_to(conn, bufsize = 0) ⇒ Object



172
173
174
# File 'lib/zmachine/connection.rb', line 172

def proxy_incoming_to(conn, bufsize = 0)
  _not_implemented
end

#proxy_target_unboundObject



176
177
178
# File 'lib/zmachine/connection.rb', line 176

def proxy_target_unbound
  _not_implemented
end

#readable!Object



253
254
255
256
257
258
259
# File 'lib/zmachine/connection.rb', line 253

def readable!
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  mark_active!
  data = @channel.read_inbound_data
  receive_data(data) if data
  nil
end

#receive_data(data) ⇒ Object



180
181
# File 'lib/zmachine/connection.rb', line 180

def receive_data(data)
end

#reconnect(server, port_or_type) ⇒ Object



183
184
185
# File 'lib/zmachine/connection.rb', line 183

def reconnect(server, port_or_type)
  ZMachine.reconnect(server, port_or_type, self)
end

#register(selector) ⇒ Object

selector registration



271
272
273
274
# File 'lib/zmachine/connection.rb', line 271

def register(selector)
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self, fd: @channel.selectable_fd) if ZMachine.debug
  @channel_key ||= @channel.selectable_fd.register(selector, current_events, self)
end

#renew_timerObject



321
322
323
324
325
326
# File 'lib/zmachine/connection.rb', line 321

def renew_timer
  @timer.cancel if @timer
  @timer = ZMachine.add_timer(@inactivity_timeout) do
    ZMachine.reactor.close_connection(self)
  end
end

#resumeObject



187
188
189
# File 'lib/zmachine/connection.rb', line 187

def resume
  _not_implemented
end

#send_data(data) ⇒ Object



191
192
193
194
195
# File 'lib/zmachine/connection.rb', line 191

def send_data(data)
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  @channel.send_data(data)
  update_events
end

#send_datagram(data, recipient_address, recipient_port) ⇒ Object



197
198
199
# File 'lib/zmachine/connection.rb', line 197

def send_datagram(data, recipient_address, recipient_port)
  _not_implemented
end

#send_file_data(filename) ⇒ Object



201
202
203
# File 'lib/zmachine/connection.rb', line 201

def send_file_data(filename)
  _not_implemented
end

#set_sock_opt(level, optname, optval) ⇒ Object



205
206
207
# File 'lib/zmachine/connection.rb', line 205

def set_sock_opt(level, optname, optval)
  _not_implemented
end

#ssl_handshake_completedObject



209
210
211
# File 'lib/zmachine/connection.rb', line 209

def ssl_handshake_completed
  _not_implemented
end

#ssl_verify_peer(cert) ⇒ Object



213
214
215
# File 'lib/zmachine/connection.rb', line 213

def ssl_verify_peer(cert)
  _not_implemented
end

#start_tls(args = {}) ⇒ Object



217
218
219
# File 'lib/zmachine/connection.rb', line 217

def start_tls(args = {})
  _not_implemented
end

#stop_proxyingObject



221
222
223
# File 'lib/zmachine/connection.rb', line 221

def stop_proxying
  _not_implemented
end

#stream_file_data(filename, args = {}) ⇒ Object



225
226
227
# File 'lib/zmachine/connection.rb', line 225

def stream_file_data(filename, args = {})
  _not_implemented
end

#unbindObject



229
230
# File 'lib/zmachine/connection.rb', line 229

def unbind
end

#update_eventsObject



276
277
278
279
280
# File 'lib/zmachine/connection.rb', line 276

def update_events
  return unless @channel_key
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  @channel_key.interest_ops(current_events)
end

#writable!Object



261
262
263
264
265
266
267
# File 'lib/zmachine/connection.rb', line 261

def writable!
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self) if ZMachine.debug
  mark_active!
  @channel.write_outbound_data
  update_events
  nil
end