Class: IRuby::Session

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

Constant Summary

Constants included from SessionSerialize

IRuby::SessionSerialize::DELIM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
16
17
18
19
# File 'lib/iruby/session.rb', line 10

def initialize(config, adapter_name=nil)
  @config = config
  @adapter = create_session_adapter(config, adapter_name)
  @last_recvd_msg = nil

  setup
  setup_sockets
  setup_heartbeat
  setup_security
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



21
22
23
# File 'lib/iruby/session.rb', line 21

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/iruby/session.rb', line 21

def config
  @config
end

Instance Method Details

#descriptionObject



23
24
25
# File 'lib/iruby/session.rb', line 23

def description
  "#{@adapter.name} session adapter"
end

#recv(socket) ⇒ Object

Receive a message and decode it



62
63
64
65
66
# File 'lib/iruby/session/cztop.rb', line 62

def recv(socket_type)
  sock = check_socket_type(socket_type)
  data = @adapter.recv(sock)
  @last_recvd_msg = unserialize(data)
end

#recv_inputObject



95
96
97
98
99
# File 'lib/iruby/session.rb', line 95

def recv_input
  sock = check_socket_type(:stdin)
  data = @adapter.recv(sock)
  unserialize(data)[:content]["value"]
end

#send(socket, type, content) ⇒ Object

Build and send a message



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

def send(socket_type, message_type, content)
  sock = check_socket_type(socket_type)
  idents = if socket_type == :reply && @last_recvd_msg
             @last_recvd_msg[:idents]
           else
             message_type == :stream ? "stream.#{content[:name]}" : message_type
           end
  header = {
    msg_type: message_type,
    msg_id:   SecureRandom.uuid,
    username: 'kernel',
    session:  @session_id,
    version:  '5.0'
  }
  @adapter.send(sock, serialize(idents, header, content))
end

#setupObject



27
28
# File 'lib/iruby/session.rb', line 27

def setup
end

#setup_heartbeatObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/iruby/session.rb', line 47

def setup_heartbeat
  protocol, host = config.values_at('transport', 'ip')
  hb_port = config['hb_port']
  @hb_socket, @hb_port = @adapter.make_rep_socket(protocol, host, hb_port)
  @heartbeat_thread = Thread.start do
    begin
      # NOTE: this loop is copied from CZTop's old session code
      @adapter.heartbeat_loop(@hb_socket)
    rescue Exception => e
      IRuby.logger.fatal "Kernel heartbeat died: #{e.message}\n#{e.backtrace.join("\n")}"
    end
  end
end

#setup_securityObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/iruby/session.rb', line 61

def setup_security
  @session_id = SecureRandom.uuid
  unless config['key'].empty? || config['signature_scheme'].empty?
    unless config['signature_scheme'] =~ /\Ahmac-/
      raise "Unknown signature_scheme: #{config['signature_scheme']}"
    end
    digest_algorithm = config['signature_scheme'][/\Ahmac-(.*)\Z/, 1]
    @hmac = OpenSSL::HMAC.new(config['key'], OpenSSL::Digest.new(digest_algorithm))
  end
end

#setup_socketsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/iruby/session.rb', line 30

def setup_sockets
  protocol, host = config.values_at('transport', 'ip')
  shell_port = config['shell_port']
  iopub_port = config['iopub_port']
  stdin_port = config['stdin_port']

  @shell_socket, @shell_port = @adapter.make_router_socket(protocol, host, shell_port)
  @iopub_socket, @iopub_port = @adapter.make_pub_socket(protocol, host, iopub_port)
  @stdin_socket, @stdin_port = @adapter.make_router_socket(protocol, host, stdin_port)

  @sockets = {
    publish: @iopub_socket,
    reply:   @shell_socket,
    stdin:   @stdin_socket
  }
end