/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.messaging;

import java.io.IOException;

import org.voltcore.messaging.VoltMessage;
import org.voltdb.utils.DBBPool;

public class CompleteTransactionResponseMessage extends VoltMessage
{
    int m_executionSiteId;
    long m_txnId;

    /** Empty constructor for de-serialization */
    CompleteTransactionResponseMessage() {
        super();
    }

    public CompleteTransactionResponseMessage(CompleteTransactionMessage msg,
                                              int siteId)
    {
        m_executionSiteId = siteId;
        m_txnId = msg.getTxnId();
    }

    public long getTxnId()
    {
        return m_txnId;
    }

    public int getExecutionSiteId()
    {
        return m_executionSiteId;
    }

    @Override
    protected void flattenToBuffer(DBBPool pool) throws IOException
    {
        int msgsize = 4 + 8;

        if (m_buffer == null) {
            m_container = pool.acquire(msgsize + 1 + HEADER_SIZE);
            m_buffer = m_container.b;
        }
        setBufferSize(msgsize + 1, pool);

        m_buffer.position(HEADER_SIZE);
        m_buffer.put(COMPLETE_TRANSACTION_RESPONSE_ID);

        m_buffer.putInt(m_executionSiteId);
        m_buffer.putLong(m_txnId);
        m_buffer.limit(m_buffer.position());
    }

    @Override
    protected void initFromBuffer()
    {
        m_buffer.position(HEADER_SIZE + 1); // skip the msg id

        m_executionSiteId = m_buffer.getInt();
        m_txnId = m_buffer.getLong();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("COMPLETE_TRANSACTION_RESPONSE");
        sb.append(" (FROM EXEC SITE: ");
        sb.append(m_executionSiteId);
        sb.append(") FOR TXN ID: ");
        sb.append(m_txnId);

        return sb.toString();
    }
}

@@ -758,16 +758,44 @@
 ception;
+%0Aimport java.nio.ByteBuffer;
 %0A%0Aimport
@@ -850,24 +850,28 @@
 volt
-db
+core
 .utils.
-DBBPool
+MiscUtils
 ;%0A%0Ap
@@ -943,19 +943,20 @@
 e%0A%7B%0A    
-int
+long
  m_execu
@@ -1227,19 +1227,20 @@
         
-int
+long
  siteId)
@@ -1398,19 +1398,20 @@
  public 
-int
+long
  getExec
@@ -1494,353 +1494,248 @@
    p
-rotected void flattenToBuffer(DBBPool pool) throws IOException%0A    %7B%0A        int msgsize = 4 + 8;%0A%0A        if (m_buffer == null) %7B%0A            m_container = pool.acquire(msgsize + 1 + HEADER_SIZE);%0A            m_buffer = m_container.b
+ublic int getSerializedSize()%0A    %7B%0A        int msgsize = super.getSerializedSize();%0A        msgsize += 8 + 8;%0A        return msgsize
 ;%0A    
+%7D%0A%0A
     
-%7D%0A        setBufferSize(msgsize + 1, pool);%0A%0A        m_buffer.position(HEADER_SIZE);
+@Override%0A    public void flattenToBuffer(ByteBuffer buf) throws IOException
 %0A    
+%7B%0A
     
-m_buffer
+    buf
 .put
@@ -1774,32 +1774,57 @@
 D);%0A
-%0A
+ 
        
- m_buffer.putInt
+int msgsize = 4 + 8;%0A%0A        buf.putLong
 (m_e
@@ -1848,24 +1848,19 @@
         
-m_
 buf
-fer
 .putLong
@@ -1882,31 +1882,71 @@
     
-m_buffer
+assert(buf.capacity() == buf.position());%0A        buf
 .limit(
-m_
 buf
-fer
 .pos
@@ -2014,79 +2014,29 @@
 fer(
-)%0A    %7B%0A        m_buffer.position(HEADER_SIZE + 1); // skip the msg id%0A
+ByteBuffer buf)%0A    %7B
 %0A   
@@ -2064,23 +2064,19 @@
 d = 
-m_
 buf
-fer
 .get
-Int
+Long
 ();%0A
@@ -2097,16 +2097,11 @@
 d = 
-m_
 buf
-fer
 .get
@@ -2104,24 +2104,74 @@
 .getLong();%0A
+        assert(buf.capacity() == buf.position());%0A
     %7D%0A%0A    @
@@ -2370,16 +2370,39 @@
 .append(
+MiscUtils.hsIdToString(
 m_execut
@@ -2403,32 +2403,33 @@
 executionSiteId)
+)
 ;%0A        sb.app

Fix CompleteTransactionResponseMessage to conform to new message creation pattern.