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 = CommitPacket;
  7 
  8 /**
  9  * Constructor
 10  * @param options
 11  * @constructor
 12  */
 13 function CommitPacket(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 CommitPacket.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.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_END_TRAN);
 34   writer._writeInt(DATA_TYPES.BYTE_SIZEOF);
 35   writer._writeByte(CAS.CCITransactionType.CCI_TRAN_COMMIT);
 36 
 37   return writer;
 38 };
 39 
 40 /**
 41  * Read data
 42  * @param parser
 43  */
 44 CommitPacket.prototype.parse = function (parser) {
 45   var reponseLength = parser._parseInt();
 46   this.casInfo = parser._parseBytes(DATA_TYPES.CAS_INFO_SIZE);
 47 
 48   this.responseCode = parser._parseInt();
 49   if (this.responseCode < 0) {
 50     this.errorCode = parser._parseInt();
 51     this.errorMsg = parser._parseNullTerminatedString(reponseLength - 2 * DATA_TYPES.INT_SIZEOF);
 52     if (this.errorMsg.length == 0) {
 53       this.errorMsg = Helpers._resolveErrorCode(this.errorCode);
 54     }
 55   }
 56 
 57   return this;
 58 };
 59 
 60