Class: IRuby::Session

Inherits:
Object
  • Object
show all
Includes:
SessionSerialize
Defined in:
lib/iruby/session/rbczmq.rb,
lib/iruby/session/ffi_rzmq.rb

Constant Summary

Constants included from SessionSerialize

IRuby::SessionSerialize::DELIM

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Session

Returns a new instance of Session.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/iruby/session/rbczmq.rb', line 7

def initialize(config)
  c = ZMQ::Context.new

  connection = "#{config['transport']}://#{config['ip']}:%d"
  reply_socket = c.socket(:ROUTER)
  reply_socket.bind(connection % config['shell_port'])

  pub_socket = c.socket(:PUB)
  pub_socket.bind(connection % config['iopub_port'])

  stdin_socket = c.socket(:ROUTER)
  stdin_socket.bind(connection % config['stdin_port'])

  Thread.new do
    begin
      hb_socket = c.socket(:REP)
      hb_socket.bind(connection % config['hb_port'])
      ZMQ.proxy(hb_socket, hb_socket)
    rescue Exception => e
      IRuby.logger.fatal "Kernel heartbeat died: #{e.message}\n#{e.backtrace.join("\n")}"
    end
  end

  @sockets = { 
    publish: pub_socket, reply: reply_socket, stdin: stdin_socket
  }

  @session = SecureRandom.uuid
  unless config['key'].to_s.empty? || config['signature_scheme'].to_s.empty?
    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) ⇒ Object

Receive a message and decode it



60
61
62
# File 'lib/iruby/session/rbczmq.rb', line 60

def recv(socket)
  @last_recvd_msg = unserialize(@sockets[socket].recv_message)
end

#recv_inputObject



64
65
66
# File 'lib/iruby/session/rbczmq.rb', line 64

def recv_input 
  unserialize(@sockets[:stdin].recv_message)[:content]["value"]
end

#send(socket, type, content) ⇒ Object

Build and send a message



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iruby/session/rbczmq.rb', line 42

def send(socket, type, content)
  idents =
    if socket == :reply && @last_recvd_msg
      @last_recvd_msg[:idents]
    else
      type == :stream ? "stream.#{content[:name]}" : type
    end
  header = {
    msg_type: type,
    msg_id:   SecureRandom.uuid,
    username: 'kernel',
    session:  @session,
    version:  '5.0'
  }
  @sockets[socket].send_message(ZMQ::Message(*serialize(idents, header, content)))
end