2016-03-09 20:46:15 +00:00
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var TcpPackage = require('../systemData/tcpPackage');
|
|
|
|
var TcpCommand = require('../systemData/tcpCommand');
|
|
|
|
var TcpFlags = require('../systemData/tcpFlags');
|
|
|
|
var InspectionDecision = require('../systemData/inspectionDecision');
|
|
|
|
var ClientMessage = require('../messages/clientMessage');
|
2016-03-11 23:55:27 +00:00
|
|
|
var InspectionResult = require('./../systemData/inspectionResult');
|
2016-03-09 20:46:15 +00:00
|
|
|
var createBufferSegment = require('../common/bufferSegment');
|
|
|
|
|
|
|
|
function OperationBase(log, cb, requestCommand, responseCommand, userCredentials) {
|
|
|
|
this.log = log;
|
|
|
|
this._cb = cb;
|
|
|
|
this._requestCommand = requestCommand;
|
|
|
|
this._responseCommand = responseCommand;
|
|
|
|
this.userCredentials = userCredentials;
|
|
|
|
|
|
|
|
this._completed = false;
|
|
|
|
this._response = null;
|
|
|
|
|
|
|
|
this._responseType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationBase.prototype._createRequestDto = function() {
|
|
|
|
throw new Error('_createRequestDto not implemented.');
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._inspectResponse = function() {
|
|
|
|
throw new Error('_inspectResponse not implemented.');
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._transformResponse = function() {
|
|
|
|
throw new Error('_transformResponse not implemented.');
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype.fail = function(error) {
|
|
|
|
this._completed = true;
|
|
|
|
this._cb(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._succeed = function() {
|
|
|
|
if (!this._completed) {
|
|
|
|
this._completed = true;
|
|
|
|
|
2016-10-18 04:58:28 +00:00
|
|
|
if (this._response)
|
2016-03-09 20:46:15 +00:00
|
|
|
this._cb(null, this._transformResponse(this._response));
|
|
|
|
else
|
|
|
|
this._cb(new Error("No result."))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype.createNetworkPackage = function(correlationId) {
|
|
|
|
var dto = this._createRequestDto();
|
2016-03-11 06:57:39 +00:00
|
|
|
var buf = dto.toBuffer();
|
2016-03-09 20:46:15 +00:00
|
|
|
return new TcpPackage(
|
|
|
|
this._requestCommand,
|
|
|
|
this.userCredentials ? TcpFlags.Authenticated : TcpFlags.None,
|
|
|
|
correlationId,
|
|
|
|
this.userCredentials ? this.userCredentials.username : null,
|
|
|
|
this.userCredentials ? this.userCredentials.password : null,
|
|
|
|
createBufferSegment(buf));
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype.inspectPackage = function(pkg) {
|
|
|
|
try {
|
|
|
|
if (pkg.command === this._responseCommand) {
|
|
|
|
this._response = this._responseType.decode(pkg.data.toBuffer());
|
|
|
|
return this._inspectResponse(this._response);
|
|
|
|
}
|
|
|
|
switch (pkg.command) {
|
|
|
|
case TcpCommand.NotAuthenticated:
|
|
|
|
return this._inspectNotAuthenticated(pkg);
|
|
|
|
case TcpCommand.BadRequest:
|
|
|
|
return this._inspectBadRequest(pkg);
|
|
|
|
case TcpCommand.NotHandled:
|
|
|
|
return this._inspectNotHandled(pkg);
|
|
|
|
default:
|
|
|
|
return this._inspectUnexpectedCommand(package, this._responseCommand);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
this.fail(e);
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.EndOperation, "Error - " + e.message);
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._inspectNotAuthenticated = function(pkg)
|
|
|
|
{
|
|
|
|
var message = '';
|
|
|
|
try {
|
|
|
|
message = pkg.data.toString();
|
|
|
|
} catch(e) {}
|
|
|
|
//TODO typed error
|
|
|
|
this.fail(new Error("Authentication error: " + message));
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.EndOperation, "NotAuthenticated");
|
2016-03-09 20:46:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._inspectBadRequest = function(pkg)
|
|
|
|
{
|
|
|
|
var message = '';
|
|
|
|
try {
|
|
|
|
message = pkg.data.toString();
|
|
|
|
} catch(e) {}
|
|
|
|
//TODO typed error
|
|
|
|
this.fail(new Error("Bad request: " + message));
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.EndOperation, "BadRequest - " + message);
|
2016-03-09 20:46:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._inspectNotHandled = function(pkg)
|
|
|
|
{
|
|
|
|
var message = ClientMessage.NotHandled.decode(pkg.data.toBuffer());
|
|
|
|
switch (message.reason)
|
|
|
|
{
|
|
|
|
case ClientMessage.NotHandled.NotHandledReason.NotReady:
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.Retry, "NotHandled - NotReady");
|
2016-03-09 20:46:15 +00:00
|
|
|
|
|
|
|
case ClientMessage.NotHandled.NotHandledReason.TooBusy:
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.Retry, "NotHandled - TooBusy");
|
2016-03-09 20:46:15 +00:00
|
|
|
|
|
|
|
case ClientMessage.NotHandled.NotHandledReason.NotMaster:
|
|
|
|
var masterInfo = ClientMessage.NotHandled.MasterInfo.decode(message.additional_info);
|
2016-10-18 04:58:28 +00:00
|
|
|
return new InspectionResult(InspectionDecision.Reconnect, "NotHandled - NotMaster",
|
2016-03-09 20:46:15 +00:00
|
|
|
{host: masterInfo.external_tcp_address, port: masterInfo.external_tcp_port},
|
|
|
|
{host: masterInfo.external_secure_tcp_address, port: masterInfo.external_secure_tcp_port});
|
|
|
|
|
|
|
|
default:
|
|
|
|
this.log.error("Unknown NotHandledReason: %s.", message.reason);
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.Retry, "NotHandled - <unknown>");
|
2016-03-09 20:46:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OperationBase.prototype._inspectUnexpectedCommand = function(pkg, expectedCommand)
|
|
|
|
{
|
2016-10-18 04:58:28 +00:00
|
|
|
if (pkg.command === expectedCommand)
|
2016-03-09 20:46:15 +00:00
|
|
|
throw new Error("Command shouldn't be " + TcpCommand.getName(pkg.command));
|
|
|
|
|
|
|
|
this.log.error("Unexpected TcpCommand received.\n"
|
|
|
|
+ "Expected: %s, Actual: %s, Flags: %s, CorrelationId: %s\n"
|
|
|
|
+ "Operation (%s): %s\n"
|
|
|
|
+"TcpPackage Data Dump:\n%j",
|
|
|
|
expectedCommand, TcpCommand.getName(pkg.command), pkg.flags, pkg.correlationId,
|
|
|
|
this.constructor.name, this, pkg.data);
|
|
|
|
|
|
|
|
this.fail(new Error(util.format("Unexpected command. Expecting %s got %s.", TcpCommand.getName(expectedCommand), TcpCommand.getName(pkg.command))));
|
2016-03-11 23:55:27 +00:00
|
|
|
return new InspectionResult(InspectionDecision.EndOperation, "Unexpected command - " + TcpCommand.getName(pkg.command));
|
2016-03-09 20:46:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = OperationBase;
|