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