iruby 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/iruby/ostream.rb CHANGED
@@ -27,7 +27,7 @@ module IRuby
27
27
 
28
28
  def write(s)
29
29
  raise 'I/O operation on closed file' unless @session
30
- @session.send(:publish, 'stream', { name: @name, text: s.to_s })
30
+ @session.send(:publish, :stream, name: @name, text: s.to_s)
31
31
  nil
32
32
  end
33
33
  alias_method :<<, :write
data/lib/iruby/session.rb CHANGED
@@ -2,11 +2,28 @@ module IRuby
2
2
  class Session
3
3
  DELIM = '<IDS|MSG>'
4
4
 
5
- def initialize(username, config, sockets)
6
- @username = username
7
- @sockets = sockets
5
+ def initialize(config)
6
+ c = ZMQ::Context.new
7
+
8
+ connection = "#{config['transport']}://#{config['ip']}:%d"
9
+ reply_socket = c.socket(:ROUTER)
10
+ reply_socket.bind(connection % config['shell_port'])
11
+
12
+ pub_socket = c.socket(:PUB)
13
+ pub_socket.bind(connection % config['iopub_port'])
14
+
15
+ Thread.new do
16
+ begin
17
+ hb_socket = c.socket(:REP)
18
+ hb_socket.bind(connection % config['hb_port'])
19
+ ZMQ.proxy(hb_socket, hb_socket)
20
+ rescue Exception => ex
21
+ IRuby.logger.fatal "Kernel heartbeat died: #{ex.message}\n#{ex.backtrace.join("\n")}"
22
+ end
23
+ end
24
+
25
+ @sockets = { publish: pub_socket, reply: reply_socket }
8
26
  @session = SecureRandom.uuid
9
- @msg_id = 0
10
27
  if config['key'] && config['signature_scheme']
11
28
  raise 'Unknown signature scheme' unless config['signature_scheme'] =~ /\Ahmac-(.*)\Z/
12
29
  @hmac = OpenSSL::HMAC.new(config['key'], OpenSSL::Digest.new($1))
@@ -14,35 +31,37 @@ module IRuby
14
31
  end
15
32
 
16
33
  # Build and send a message
17
- def send(socket, type, content, ident=nil)
34
+ def send(socket, type, content)
35
+ idents =
36
+ if socket == :reply && @last_recvd_msg
37
+ @last_recvd_msg[:idents]
38
+ else
39
+ type == :stream ? "stream.#{content[:name]}" : type
40
+ end
18
41
  header = {
19
42
  msg_type: type,
20
- msg_id: @msg_id,
21
- username: @username,
43
+ msg_id: SecureRandom.uuid,
44
+ username: 'kernel',
22
45
  session: @session,
23
46
  version: '5.0'
24
47
  }
25
- @msg_id += 1
26
-
27
- @sockets[socket].send_message(serialize(header, content, ident))
48
+ @sockets[socket].send_message(serialize(idents, header, content))
28
49
  end
29
50
 
30
51
  # Receive a message and decode it
31
52
  def recv(socket)
32
- idents, msg = unserialize(@sockets[socket].recv_message)
33
- @last_received_header = msg[:header]
34
- return idents, msg
53
+ @last_recvd_msg = unserialize(@sockets[socket].recv_message)
35
54
  end
36
55
 
37
56
  private
38
57
 
39
- def serialize(header, content, ident)
58
+ def serialize(idents, header, content)
40
59
  msg = [MultiJson.dump(header),
41
- MultiJson.dump(@last_received_header || {}),
60
+ MultiJson.dump(@last_recvd_msg ? @last_recvd_msg[:header] : {}),
42
61
  '{}',
43
62
  MultiJson.dump(content || {})]
44
- #STDERR.puts "SEND #{(([ident].flatten.compact << DELIM << sign(msg)) + msg).inspect}"
45
- ZMQ::Message(*(([ident].flatten.compact << DELIM << sign(msg)) + msg))
63
+ #STDERR.puts "SEND #{(([*idents].compact << DELIM << sign(msg)) + msg).inspect}"
64
+ ZMQ::Message(*(([*idents].compact.map(&:to_s) << DELIM << sign(msg)) + msg))
46
65
  end
47
66
 
48
67
  def unserialize(msg)
@@ -60,12 +79,13 @@ module IRuby
60
79
  raise 'malformed message, must have at least #{minlen} elements' unless msg_list.length >= minlen
61
80
  s, header, parent_header, metadata, content, buffers = *msg_list
62
81
  raise 'Invalid signature' unless s == sign(msg_list[1..-1])
63
- return idents, {
64
- header: MultiJson.load(header),
82
+ {
83
+ idents: idents,
84
+ header: MultiJson.load(header),
65
85
  parent_header: MultiJson.load(parent_header),
66
- metadata: MultiJson.load(metadata),
67
- content: MultiJson.load(content),
68
- buffers: buffers
86
+ metadata: MultiJson.load(metadata),
87
+ content: MultiJson.load(content),
88
+ buffers: buffers
69
89
  }
70
90
  end
71
91
 
data/lib/iruby/utils.rb CHANGED
@@ -4,7 +4,7 @@ module IRuby
4
4
  end
5
5
 
6
6
  def self.display(obj, options = {})
7
- Kernel.instance.session.send(:publish, 'display_data', data: Display.display(obj, options), metadata: {}) unless obj.nil?
7
+ Kernel.instance.session.send(:publish, :display_data, data: Display.display(obj, options), metadata: {}, source: 'ruby') unless obj.nil?
8
8
  end
9
9
 
10
10
  def self.table(s, **options)
data/lib/iruby/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module IRuby
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Mendler
@@ -114,6 +114,7 @@ files:
114
114
  - Rakefile
115
115
  - bin/iruby
116
116
  - examples/display.ipynb
117
+ - examples/history.ipynb
117
118
  - examples/stdout.ipynb
118
119
  - examples/table.ipynb
119
120
  - iruby.gemspec