1 var DATA_TYPES = require('../../constants/DataTypes'),
  2   ErrorMessages = require('../../constants/ErrorMessages'),
  3   CAS = require('../../constants/CASConstants');
  4 
  5 module.exports = CloseQueryPacket;
  6 
  7 /**
  8  * Constructor
  9  * @param options
 10  * @constructor
 11  */
 12 function CloseQueryPacket(options) {
 13   options = options || {};
 14 
 15   this.casInfo = options.casInfo;
 16   this.reqHandle = options.reqHandle;
 17 
 18   this.responseCode = 0;
 19   this.errorCode = 0;
 20   this.errorMsg = '';
 21 }
 22 
 23 /**
 24  * Write data
 25  * @param writer
 26  */
 27 CloseQueryPacket.prototype.write = function (writer) {
 28   var bufferLength = DATA_TYPES.DATA_LENGTH_SIZEOF + DATA_TYPES.CAS_INFO_SIZE +
 29     DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF +
 30     DATA_TYPES.INT_SIZEOF + DATA_TYPES.BYTE_SIZEOF;
 31 
 32   writer._writeInt(bufferLength - DATA_TYPES.DATA_LENGTH_SIZEOF - DATA_TYPES.CAS_INFO_SIZE);
 33   writer._writeBytes(DATA_TYPES.CAS_INFO_SIZE, this.casInfo);
 34   writer._writeByte(CAS.CASFunctionCode.CAS_FC_CLOSE_REQ_HANDLE);
 35   writer._writeInt(DATA_TYPES.INT_SIZEOF);
 36   writer._writeInt(this.reqHandle);
 37   writer._writeInt(DATA_TYPES.BYTE_SIZEOF);
 38   writer._writeByte(0); //autocommit mode
 39 
 40   return writer;
 41 };
 42 
 43 /**
 44  * Read data
 45  * @param parser
 46  */
 47 CloseQueryPacket.prototype.parse = function (parser) {
 48   var reponseLength = parser._parseInt();
 49   this.casInfo = parser._parseBytes(DATA_TYPES.CAS_INFO_SIZE);
 50 
 51   this.responseCode = parser._parseInt();
 52   if (this.responseCode < 0) {
 53     this.errorCode = parser._parseInt();
 54     this.errorMsg = parser._parseNullTerminatedString(responseBuffer.length - 2 * DATA_TYPES.INT_SIZEOF);
 55     if (this.errorMsg.length == 0) {
 56       for (var iter = 0; iter < ErrorMessages.CASErrorMsgId.length; iter++) {
 57         if (this.errorCode == ErrorMessages.CASErrorMsgId[iter][1]) {
 58           this.errorMsg = ErrorMessages.CASErrorMsgId[iter][0];
 59           break;
 60         }
 61       }
 62     }
 63   }
 64 
 65   return this;
 66 };
 67 
 68