-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmessage.js
More file actions
155 lines (143 loc) · 4.89 KB
/
message.js
File metadata and controls
155 lines (143 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
/* This file is part of VoltDB.
* Copyright (C) 2008-2017 VoltDB Inc.
*
* This file contains original code and/or modifications of original code.
* Any modifications made by VoltDB Inc. are licensed under the following
* terms and conditions:
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
var Parser = require('./parser').Parser,
util = require('util'),
PRESENT = require('./voltconstants').PRESENT,
MESSAGE_TYPE = require('./voltconstants').MESSAGE_TYPE,
STATUS_CODE_STRINGS = require('./voltconstants').STATUS_CODE_STRINGS,
STATUS_CODES = require('./voltconstants').STATUS_CODES;
function Message(buffer) {
this.type = MESSAGE_TYPE.UNDEFINED;
this.error = false;
Parser.call(this, buffer);
if(!buffer) {
this.writeInt(0);
this.writeByte(0);
} else {
this.readHeader();
}
}
Message.prototype = Object.create(Parser.prototype);
Message.prototype.readHeader = function() {
this.length = this.readInt();
this.protocol = this.readByte();
};
Message.prototype.writeHeader = function() {
var pos = this.position;
this.position = 0;
this.writeInt(this.buffer.length - 4);
this.writeByte(0);
this.position = pos;
};
Message.prototype.toBuffer = function() {
this.writeHeader();
return new Buffer(this.buffer);
};
// for getting lengths from incoming data
Message.readInt = function(buffer, offset) {
return Parser.readInt(buffer, offset);
};
LoginMessage = function(buffer) {
Message.call(this, buffer);
this.type = MESSAGE_TYPE.LOGIN;
this.status = this.readByte();
this.error = (this.status !== 0 );
if(this.error === false) {
this.serverId = this.readInt();
this.connectionId = this.readLong();
this.clusterStartTimestamp = new Date(parseInt(this.readLong().toString()));
// not microseonds, milliseconds
this.leaderIP = this.readByte() + '.' + this.readByte() + '.' + this.readByte() + '.' + this.readByte();
this.build = this.readString();
}
}
util.inherits(LoginMessage, Message);
var lm = LoginMessage.prototype;
lm.toString = function() {
return {
length : this.length,
protocol : this.protocol,
status : this.status,
error : this.error,
serverId : this.serverId,
connectionId : this.connectionId,
clusterStartTimestamp : this.clusterStartTimestamp,
leaderIP : this.leaderIP,
build : this.build
};
}
QueryMessage = function(buffer) {
Message.call(this, buffer);
this.type = MESSAGE_TYPE.QUERY;
this.uid = this.readBinary(8).toString();
this.fieldsPresent = this.readByte();
// bitfield, use PRESENT values to check
this.status = this.readByte();
this.statusString = STATUS_CODE_STRINGS[this.status];
if(this.fieldsPresent & PRESENT.STATUS) {
this.statusString = this.readString();
}
this.appStatus = this.readByte();
this.appStatusString = '';
if(this.fieldsPresent & PRESENT.APP_STATUS) {
this.appStatusString = this.readString();
}
this.exceptionLength = this.readInt();
if(this.fieldsPresent & PRESENT.EXCEPTION) {
this.exception = this.readException(1);
// seems size doesn't matter, always 1
} else {
// don't parse the rest if there was an exception. Bad material there.
var resultCount = this.readShort();
// there can be more than one table with rows
this.table = new Array(resultCount);
for(var i = 0; i < resultCount; i++) {
this.table[i] = this.readVoltTable();
}
}
}
util.inherits(QueryMessage, Message);
var qm = QueryMessage.prototype;
qm.toString = function() {
return {
length : this.length,
protocol : this.protocol,
status : this.status,
error : this.error,
uid : this.uid,
fieldsPresent : this.fieldsPresent,
statusString : this.statusString,
appStatus : this.appStatus,
appStatusString : this.appStatusString,
exception : this.exception,
exceptionLength : this.exceptionLength,
results : this.results
};
}
exports.Message = Message;
exports.LoginMessage = LoginMessage;
exports.QueryMessage = QueryMessage;