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 = 'jadams') ⇒ Session
constructor
A new instance of Session.
- #msg(msg_type, content = nil, parent = nil) ⇒ Object
- #msg_header ⇒ Object
- #pack(s) ⇒ Object
- #recv(socket, mode = ZMQ::NOBLOCK) ⇒ Object
- #send(stream, msg_or_type, content = nil, parent = nil, ident = nil, buffers = nil, subheader = nil, track = false, header = nil) ⇒ Object
- #serialize(msg, ident = nil) ⇒ Object
- #sign(msg_list) ⇒ Object
- #unserialize(msg_list, content = true, copy = true) ⇒ Object
Constructor Details
#initialize(username = 'jadams') ⇒ Session
Returns a new instance of Session.
11 12 13 14 15 16 17 |
# File 'lib/iruby/session.rb', line 11 def initialize username='jadams' @username = username @session = UUID.new.generate @msg_id = 0 @auth = nil end |
Instance Method Details
#msg(msg_type, content = nil, parent = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/iruby/session.rb', line 47 def msg(msg_type, content=nil, parent=nil) msg = {} msg['header'] = msg_header() msg['parent_header'] = parent.nil? ? {} : Message.extract_header(parent) msg['metadata'] = {} msg['header']['msg_type'] = msg_type msg['content'] = content || {} return msg end |
#msg_header ⇒ Object
41 42 43 44 45 |
# File 'lib/iruby/session.rb', line 41 def msg_header h = Message.msg_header(@msg_id, @username, @session) @msg_id += 1 return h end |
#pack(s) ⇒ Object
19 20 21 |
# File 'lib/iruby/session.rb', line 19 def pack(s) s.to_json end |
#recv(socket, mode = ZMQ::NOBLOCK) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/iruby/session.rb', line 171 def recv(socket, mode=ZMQ::NOBLOCK) begin msg = [] frame = "" rc = socket.recv_string(frame, mode) ZMQ::Util.error_check("zmq_msg_send", rc) msg << frame while socket.more_parts? begin frame = "" rc = socket.recv_string(frame, mode) ZMQ::Util.error_check("zmq_msg_send", rc) msg << frame rescue end end # Skip everything before DELIM, then munge the three json objects into the # one the rest of my code expects i = msg.index(DELIM) idents = msg[0..i-1] msg_list = msg[i+1..-1] end return idents, unserialize(msg_list) end |
#send(stream, msg_or_type, content = nil, parent = nil, ident = nil, buffers = nil, subheader = nil, track = false, header = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/iruby/session.rb', line 57 def send(stream, msg_or_type, content=nil, parent=nil, ident=nil, buffers=nil, subheader=nil, track=false, header=nil) """Build and send a message via stream or socket. The message format used by this function internally is as follows: [ident1,ident2,...,DELIM,HMAC,p_header,p_parent,p_content, buffer1,buffer2,...] The serialize/unserialize methods convert the nested message dict into this format. Parameters ---------- stream : zmq.Socket or ZMQStream The socket-like object used to send the data. msg_or_type : str or Message/dict Normally, msg_or_type will be a msg_type unless a message is being sent more than once. If a header is supplied, this can be set to None and the msg_type will be pulled from the header. content : dict or None The content of the message (ignored if msg_or_type is a message). header : dict or None The header dict for the message (ignores if msg_to_type is a message). parent : Message or dict or None The parent or parent header describing the parent of this message (ignored if msg_or_type is a message). ident : bytes or list of bytes The zmq.IDENTITY routing path. subheader : dict or None Extra header keys for this message's header (ignored if msg_or_type is a message). buffers : list or None The already-serialized buffers to be appended to the message. track : bool Whether to track. Only for use with Sockets, because ZMQStream objects cannot track messages. Returns ------- msg : dict The constructed message. (msg,tracker) : (dict, MessageTracker) if track=True, then a 2-tuple will be returned, the first element being the constructed message, and the second being the MessageTracker """ if !stream.is_a?(ZMQ::Socket) raise "stream must be Socket or ZMQSocket, not %r"%stream.class end if msg_or_type.is_a?(Hash) msg = msg_or_type else msg = self.msg(msg_or_type, content, parent) end buffers ||= [] to_send = self.serialize(msg, ident) flag = 0 if buffers.any? flag = ZMQ::SNDMORE _track = false else _track=track end if track to_send.each_with_index do |part, i| if i == to_send.length - 1 flag = 0 else flag = ZMQ::SNDMORE end stream.send_string(part, flag) end else to_send.each_with_index do |part, i| if i == to_send.length - 1 flag = 0 else flag = ZMQ::SNDMORE end stream.send_string(part, flag) end end # STDOUT.puts '-'*30 # STDOUT.puts "SENDING" # STDOUT.puts to_send # STDOUT.puts to_send.length # STDOUT.puts '-'*30 #buffers.each do |b| #stream.send(b, flag, copy=False) #end #if buffers: #if track: #tracker = stream.send(buffers[-1], copy=False, track=track) #else: #tracker = stream.send(buffers[-1], copy=False) # omsg = Message(msg) #if self.debug: #pprint.pprint(msg) #pprint.pprint(to_send) #pprint.pprint(buffers) #msg['tracker'] = tracker return msg end |
#serialize(msg, ident = nil) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/iruby/session.rb', line 197 def serialize(msg, ident=nil) """Serialize the message components to bytes. This is roughly the inverse of unserialize. The serialize/unserialize methods work with full message lists, whereas pack/unpack work with the individual message parts in the message list. Parameters ---------- msg : dict or Message The nexted message dict as returned by the self.msg method. Returns ------- msg_list : list The list of bytes objects to be sent with the format: [ident1,ident2,...,DELIM,HMAC,p_header,p_parent,p_content, buffer1,buffer2,...]. In this list, the p_* entities are the packed or serialized versions, so if JSON is used, these are utf8 encoded JSON strings. """ content = msg.fetch('content', {}) if content.nil? content = {}.to_json elsif content.is_a?(Hash) content = content.to_json #elsif isinstance(content, bytes): # content is already packed, as in a relayed message #pass #elsif isinstance(content, unicode): # should be bytes, but JSON often spits out unicode #content = content.encode('utf8') else raise "Content incorrect type: %s"%type(content) end = [self.pack(msg['header']), self.pack(msg['parent_header']), self.pack(msg['metadata']), self.pack(msg['content']), ] to_send = [] if ident.is_a?(Array) # accept list of idents to_send += ident elsif !ident.nil? to_send << ident end to_send << DELIM signature = self.sign() to_send << signature to_send += # STDOUT.puts to_send # STDOUT.puts to_send.length return to_send end |
#sign(msg_list) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/iruby/session.rb', line 23 def sign(msg_list) """Sign a message with HMAC digest. If no auth, return b''. Parameters ---------- msg_list : list The [p_header,p_parent,p_content] part of the message list. """ if @auth.nil? return '' end #h = self.auth.copy() #msg_list.each do |m| #h.update(m) #end #return str_to_bytes(h.hexdigest()) end |
#unserialize(msg_list, content = true, copy = true) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/iruby/session.rb', line 259 def unserialize(msg_list, content=true, copy=true) =begin Unserialize a msg_list to a nested message dict. This is roughly the inverse of serialize. The serialize/unserialize methods work with full message lists, whereas pack/unpack work with the individual message parts in the message list. Parameters: ----------- msg_list : list of bytes or Message objects The list of message parts of the form [HMAC,p_header,p_parent, p_content,buffer1,buffer2,...]. content : bool (True) Whether to unpack the content dict (True), or leave it packed (False). copy : bool (True) Whether to return the bytes (True), or the non-copying Message object in each place (False). Returns ------- msg : dict The nested message dict with top-level keys [header, parent_header, content, buffers]. =end minlen = 5 = {} unless copy minlen.times do |i| msg_list[i] = msg_list[i].bytes end end unless msg_list.length >= minlen raise Exception "malformed message, must have at least %i elements"%minlen end # STDERR.puts msg_list.inspect header = msg_list[1] ['header'] = JSON.parse(header) ['msg_id'] = header['msg_id'] ['msg_type'] = header['msg_type'] ['parent_header'] = JSON.parse(msg_list[2]) ['metadata'] = JSON.parse(msg_list[3]) if content ['content'] = JSON.parse(msg_list[4]) else ['content'] = msg_list[4] end ['buffers'] = msg_list[4..-1] return end |