Class: Flatware::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/flatware/socket.rb

Defined Under Namespace

Classes: Monitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Socket



90
91
92
# File 'lib/flatware/socket.rb', line 90

def initialize(socket)
  @socket = socket
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



88
89
90
# File 'lib/flatware/socket.rb', line 88

def socket
  @socket
end

Instance Method Details

#bind(port) ⇒ Object



145
146
147
148
149
150
# File 'lib/flatware/socket.rb', line 145

def bind(port)
  @type = 'bound'
  @port = port
  error unless socket.bind(port) == 0
  Flatware.log "bind #@port"
end

#closeObject



152
153
154
155
# File 'lib/flatware/socket.rb', line 152

def close
  error unless socket.close == 0
  Flatware.log "close #@type #@port"
end

#connect(port) ⇒ Object



109
110
111
112
113
114
# File 'lib/flatware/socket.rb', line 109

def connect(port)
  @type = 'connected'
  @port = port
  error unless socket.connect(port) == 0
  Flatware.log "connect #@port"
end

#errorObject



157
158
159
# File 'lib/flatware/socket.rb', line 157

def error
  Flatware::socket_error name
end

#monitorObject



116
117
118
119
120
# File 'lib/flatware/socket.rb', line 116

def monitor
  name = "inproc://monitor#{SecureRandom.hex(10)}"
  LibZMQ.zmq_socket_monitor(socket.socket, name, ZMQ::EVENT_ALL)
  Monitor.new(name)
end

#nameObject



98
99
100
# File 'lib/flatware/socket.rb', line 98

def name
  socket.name
end

#recv(block: true, marshal: true) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/flatware/socket.rb', line 161

def recv(block: true, marshal: true)
  message = ''
  if block
   result = socket.recv_string(message)
   error if result == -1
  else
    socket.recv_string(message, ZMQ::NOBLOCK)
  end
  if message != '' and marshal
    message = Marshal.load(message)
  end
  Flatware.log "#@type #@port recv #{message}"
  message
end

#send(message) ⇒ Object



102
103
104
105
106
107
# File 'lib/flatware/socket.rb', line 102

def send(message)
  result = socket.send_string(Marshal.dump(message))
  error if result == -1
  Flatware.log "#@type #@port send #{message}"
  message
end

#setsockopt(*args) ⇒ Object



94
95
96
# File 'lib/flatware/socket.rb', line 94

def setsockopt(*args)
  socket.setsockopt(*args)
end