Class: XGen::Mongo::Driver::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/message/message.rb

Constant Summary collapse

HEADER_SIZE =

size, id, response_to, opcode

16
@@class_req_id =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op) ⇒ Message

Returns a new instance of Message.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mongo/message/message.rb', line 16

def initialize(op)
  @op = op
  @message_length = HEADER_SIZE
  @data_length = 0
  @request_id = (@@class_req_id += 1)
  @response_id = 0
  @buf = ByteBuffer.new
  
  @buf.put_int(16)      # holder for length
  @buf.put_int(@request_id)
  @buf.put_int(0)       # response_to
  @buf.put_int(op)
end

Instance Attribute Details

#bufObject (readonly)

for testing



14
15
16
# File 'lib/mongo/message/message.rb', line 14

def buf
  @buf
end

Instance Method Details

#dumpObject



54
55
56
# File 'lib/mongo/message/message.rb', line 54

def dump
  @buf.dump
end

#to_aObject



50
51
52
# File 'lib/mongo/message/message.rb', line 50

def to_a
  @buf.to_a
end

#update_message_lengthObject

Do not call. Private, but kept public for testing.



59
60
61
62
63
# File 'lib/mongo/message/message.rb', line 59

def update_message_length
  pos = @buf.position
  @buf.put_int(@buf.size, 0)
  @buf.position = pos
end

#write_doc(hash) ⇒ Object



45
46
47
48
# File 'lib/mongo/message/message.rb', line 45

def write_doc(hash)
  @buf.put_array(BSON.new.serialize(hash).to_a)
  update_message_length
end

#write_int(i) ⇒ Object



30
31
32
33
# File 'lib/mongo/message/message.rb', line 30

def write_int(i)
  @buf.put_int(i)
  update_message_length
end

#write_long(i) ⇒ Object



35
36
37
38
# File 'lib/mongo/message/message.rb', line 35

def write_long(i)
  @buf.put_long(i)
  update_message_length
end

#write_string(s) ⇒ Object



40
41
42
43
# File 'lib/mongo/message/message.rb', line 40

def write_string(s)
  BSON.serialize_cstr(@buf, s)
  update_message_length
end