Class: ZMachine::ZMQChannel

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

Instance Attribute Summary

Attributes inherited from Channel

#raw, #socket

Instance Method Summary collapse

Methods inherited from Channel

#can_send?, #close, #send_data, #write_outbound_data

Constructor Details

#initializeZMQChannel

Returns a new instance of ZMQChannel.



27
28
29
30
# File 'lib/zmachine/zmq_channel.rb', line 27

def initialize
  super
  @raw = true
end

Instance Method Details

#acceptObject



48
49
50
51
# File 'lib/zmachine/zmq_channel.rb', line 48

def accept
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
  self
end

#bind(address, type) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/zmachine/zmq_channel.rb', line 36

def bind(address, type)
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
  @bound = true
  @connected = true
  @socket = ZContext.create_socket(type)
  @socket.bind(address)
end

#bound?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/zmachine/zmq_channel.rb', line 44

def bound?
  @bound
end

#can_recv?Boolean

see comment in ConnectionManager#process

Returns:

  • (Boolean)


99
100
101
# File 'lib/zmachine/zmq_channel.rb', line 99

def can_recv?
  @socket.events & ZMQ::Poller::POLLIN == ZMQ::Poller::POLLIN
end

#close!Object



82
83
84
85
86
87
88
# File 'lib/zmachine/zmq_channel.rb', line 82

def close!
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
  @closed = true
  @connected = false
  @bound = false
  ZContext.destroy_socket(@socket)
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @closed
end

#connect(address, type) ⇒ Object



53
54
55
56
57
58
# File 'lib/zmachine/zmq_channel.rb', line 53

def connect(address, type)
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
  @connection_pending = true
  @socket = ZContext.create_socket(type)
  @socket.connect(address)
end

#connected?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/zmachine/zmq_channel.rb', line 70

def connected?
  @connected
end

#connection_pending?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/zmachine/zmq_channel.rb', line 60

def connection_pending?
  @connection_pending
end

#finish_connectingObject



64
65
66
67
68
# File 'lib/zmachine/zmq_channel.rb', line 64

def finish_connecting
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
  return unless connection_pending?
  @connected = true
end

#peerObject

Raises:

  • (RuntimeError)


94
95
96
# File 'lib/zmachine/zmq_channel.rb', line 94

def peer
  raise RuntimeError.new("ZMQChannel has no peer")
end

#read_inbound_dataObject



74
75
76
77
78
79
80
# File 'lib/zmachine/zmq_channel.rb', line 74

def read_inbound_data
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
  return nil unless can_recv?
  data = ZMsg.recv_msg(@socket)
  data = String.from_java_bytes(data.first.data) unless @raw
  data
end

#selectable_fdObject



32
33
34
# File 'lib/zmachine/zmq_channel.rb', line 32

def selectable_fd
  @socket.fd
end