/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.voltdb.messaging;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.voltcore.messaging.Subject;
import org.voltcore.messaging.VoltMessage;
import org.voltcore.utils.CoreUtils;
import org.voltdb.ClientResponseImpl;

/**
 * Message from an execution site to initiator with the final response for
 * the client
 */
public class InitiateResponseMessage extends VoltMessage {

    private long m_txnId;
    private long m_spHandle;
    private long m_initiatorHSId;
    private long m_coordinatorHSId;
    private long m_clientInterfaceHandle;
    private long m_connectionId;
    private boolean m_commit;
    private boolean m_recovering;
    private boolean m_readOnly;
    private ClientResponseImpl m_response;

    /** Empty constructor for de-serialization */
    public InitiateResponseMessage()
    {
        m_initiatorHSId = -1;
        m_coordinatorHSId = -1;
        m_subject = Subject.DEFAULT.getId();
    }

    /**
     * IV2 constructor
     */
    public InitiateResponseMessage(Iv2InitiateTaskMessage task) {
        m_txnId = task.getTxnId();
        m_spHandle = task.getSpHandle();
        m_initiatorHSId = task.getInitiatorHSId();
        m_coordinatorHSId = task.getCoordinatorHSId();
        m_subject = Subject.DEFAULT.getId();
        m_clientInterfaceHandle = task.getClientInterfaceHandle();
        m_connectionId = task.getConnectionId();
        m_readOnly = task.isReadOnly();
    }

    /**
     * IV2 constructor for sentinel response
     * @param sentinel
     */
    public InitiateResponseMessage(MultiPartitionParticipantMessage sentinel) {
        m_txnId = sentinel.getTxnId();
        m_spHandle = sentinel.getSpHandle();
        m_initiatorHSId = sentinel.getInitiatorHSId();
        m_coordinatorHSId = sentinel.getCoordinatorHSId();
        m_subject = Subject.DEFAULT.getId();
        m_clientInterfaceHandle = sentinel.getClientInterfaceHandle();
        m_connectionId = sentinel.getConnectionId();
    }

    /**
     * Create a response from a request.
     * Note that some private request data is copied to the response.
     * @param task The initiation request object to collect the
     * metadata from.
     */
    public InitiateResponseMessage(InitiateTaskMessage task) {
        m_txnId = task.getTxnId();
        m_spHandle = task.getSpHandle();
        m_initiatorHSId = task.getInitiatorHSId();
        m_coordinatorHSId = task.getCoordinatorHSId();
        m_subject = Subject.DEFAULT.getId();
        m_clientInterfaceHandle = Long.MIN_VALUE;
        m_connectionId = Long.MIN_VALUE;
        m_readOnly = task.isReadOnly();
    }

    public void setClientHandle(long clientHandle) {
        m_response.setClientHandle(clientHandle);
    }

    public long getTxnId() {
        return m_txnId;
    }

    public long getSpHandle() {
        return m_spHandle;
    }

    public long getInitiatorHSId() {
        return m_initiatorHSId;
    }

    public long getCoordinatorHSId() {
        return m_coordinatorHSId;
    }

    public long getClientInterfaceHandle() {
        return m_clientInterfaceHandle;
    }

    public long getClientConnectionId() {
        return m_connectionId;
    }

    public boolean shouldCommit() {
        return m_commit;
    }

    public boolean isRecovering() {
        return m_recovering;
    }

    public void setRecovering(boolean recovering) {
        m_recovering = recovering;
    }

    public ClientResponseImpl getClientResponseData() {
        return m_response;
    }

    public void setResults(ClientResponseImpl r) {
        m_commit = (r.getStatus() == ClientResponseImpl.SUCCESS);
        m_response = r;
    }

    public boolean isReadOnly() {
        return m_readOnly;
    }

    @Override
    public int getSerializedSize()
    {
        int msgsize = super.getSerializedSize();
        msgsize += 8 // txnId
            + 8 // m_spHandle
            + 8 // initiator HSId
            + 8 // coordinator HSId
            + 8 // client interface handle
            + 8 // client connection id
            + 1 // read only
            + 1; // node recovering indication

        msgsize += m_response.getSerializedSize();

        return msgsize;
    }

    @Override
    public void flattenToBuffer(ByteBuffer buf)
    {
        buf.put(VoltDbMessageFactory.INITIATE_RESPONSE_ID);
        buf.putLong(m_txnId);
        buf.putLong(m_spHandle);
        buf.putLong(m_initiatorHSId);
        buf.putLong(m_coordinatorHSId);
        buf.putLong(m_clientInterfaceHandle);
        buf.putLong(m_connectionId);
        buf.put((byte) (m_readOnly == true ? 1 : 0));
        buf.put((byte) (m_recovering == true ? 1 : 0));
        m_response.flattenToBuffer(buf);
        assert(buf.capacity() == buf.position());
        buf.limit(buf.position());
    }

    @Override
    public void initFromBuffer(ByteBuffer buf) throws IOException
    {
        m_txnId = buf.getLong();
        m_spHandle = buf.getLong();
        m_initiatorHSId = buf.getLong();
        m_coordinatorHSId = buf.getLong();
        m_clientInterfaceHandle = buf.getLong();
        m_connectionId = buf.getLong();
        m_readOnly = buf.get() == 1;
        m_recovering = buf.get() == 1;
        m_response = new ClientResponseImpl();
        m_response.initFromBuffer(buf);
        m_commit = (m_response.getStatus() == ClientResponseImpl.SUCCESS);
        assert(buf.capacity() == buf.position());
    }

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

        sb.append("INITITATE_RESPONSE FOR TXN ");
        sb.append(m_txnId);
        sb.append("\n SP HANDLE: " + m_spHandle);
        sb.append("\n INITIATOR HSID: " + CoreUtils.hsIdToString(m_initiatorHSId));
        sb.append("\n COORDINATOR HSID: " + CoreUtils.hsIdToString(m_coordinatorHSId));
        sb.append("\n CLIENT INTERFACE HANDLE: " + m_clientInterfaceHandle);
        sb.append("\n CLIENT CONNECTION ID: " + m_connectionId);
        sb.append("\n READ-ONLY: " + m_readOnly);
        if (m_commit)
            sb.append("\n  COMMIT");
        else
            sb.append("\n  ROLLBACK/ABORT, ");

        return sb.toString();
    }
}

@@ -6860,16 +6860,107 @@
 ORT, %22);
+%0A        sb.append(%22%5Cn CLIENT RESPONSE: %5Cn%22);%0A        sb.append(m_response.toJSONString());
 %0A%0A      

Add the ClientResponse to the toString() output of InitiateResponseMessage. Helpful in debugging hash mismatches seen by SpScheduler, generally useful too.