Class: IRuby::Session
- Inherits:
-
Object
- Object
- IRuby::Session
- Defined in:
- lib/iruby/session.rb
Constant Summary collapse
- DELIM =
'<IDS|MSG>'
Instance Method Summary collapse
-
#initialize(username, config) ⇒ Session
constructor
A new instance of Session.
-
#recv(socket, mode) ⇒ Object
Receive a message and decode it.
-
#send(socket, type, content, ident = nil) ⇒ Object
Build and send a message.
Constructor Details
#initialize(username, config) ⇒ Session
Returns a new instance of Session.
5 6 7 8 9 10 11 12 13 |
# File 'lib/iruby/session.rb', line 5 def initialize(username, config) @username = username @session = SecureRandom.uuid @msg_id = 0 if config['key'] && config['signature_scheme'] raise 'Unknown signature scheme' unless config['signature_scheme'] =~ /\Ahmac-(.*)\Z/ @hmac = OpenSSL::HMAC.new(config['key'], OpenSSL::Digest.new($1)) end end |
Instance Method Details
#recv(socket, mode) ⇒ Object
Receive a message and decode it
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/iruby/session.rb', line 32 def recv(socket, mode) msg = [] while msg.empty? || socket.more_parts? begin frame = '' rc = socket.recv_string(frame, mode) ZMQ::Util.error_check('zmq_msg_send', rc) msg << frame rescue end end i = msg.index(DELIM) idents, msg_list = msg[0..i-1], msg[i+1..-1] msg = unserialize(msg_list) @last_received_header = msg[:header] return idents, msg end |
#send(socket, type, content, ident = nil) ⇒ Object
Build and send a message
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/iruby/session.rb', line 16 def send(socket, type, content, ident=nil) header = { msg_type: type, msg_id: @msg_id, username: @username, session: @session } @msg_id += 1 list = serialize(header, content, ident) list.each_with_index do |part, i| socket.send_string(part, i == list.size - 1 ? 0 : ZMQ::SNDMORE) end end |