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.



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

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.



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

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Instance Method Details

#descriptionObject



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

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



97
98
99
100
101
# File 'lib/iruby/session.rb', line 97

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
60
# 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,
    date:     Time.now.utc.iso8601,
    username: 'kernel',
    session:  @session_id,
    version:  '5.0'
  }
  @adapter.send(sock, serialize(idents, header, content))
end

#setupObject



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

def setup
end

#setup_heartbeatObject



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

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



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

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



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

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