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.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongo/message/message.rb', line 32

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



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

def buf
  @buf
end

Instance Method Details

#dumpObject



70
71
72
# File 'lib/mongo/message/message.rb', line 70

def dump
  @buf.dump
end

#to_aObject



66
67
68
# File 'lib/mongo/message/message.rb', line 66

def to_a
  @buf.to_a
end

#update_message_lengthObject

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



75
76
77
78
79
# File 'lib/mongo/message/message.rb', line 75

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

#write_doc(hash, check_keys = false) ⇒ Object



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

def write_doc(hash, check_keys=false)
  @buf.put_array(BSON.new.serialize(hash, check_keys).to_a)
  update_message_length
end

#write_int(i) ⇒ Object



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

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

#write_long(i) ⇒ Object



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

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

#write_string(s) ⇒ Object



56
57
58
59
# File 'lib/mongo/message/message.rb', line 56

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