/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activeblaze.wire;

import java.io.IOException;
import java.net.SocketAddress;

/**
 * Base class for
 */
public class Packet implements Cloneable {
    private transient SocketAddress from;
    private transient SocketAddress to;
    private String id;
    private String correlationId;
    private boolean responseRequired;
    private boolean response;
    private boolean replayed;
    private String producerId;
    private long messageSequence;
    private transient boolean reliable;
    private transient boolean noLocal;
    protected ByteBool byteBool = new ByteBool();

    public Packet clone() {
        Packet result = new Packet();
        copy(result);
        return result;
    }

    protected void copy(Packet copy) {
        copy.from = this.from;
        copy.to = this.to;
        copy.id = this.id;
        copy.correlationId = this.correlationId;
        copy.responseRequired = this.responseRequired;
        copy.response = this.response;
        copy.replayed = this.replayed;
        copy.producerId = this.producerId;
        copy.messageSequence = this.messageSequence;
        copy.reliable = this.reliable;
    }

    /**
     * Get the packetType
     *
     * @return the packetType
     */
    public int getPacketType() {
        return PacketType.PACKET.getNumber();
    }

    /**
     * Get the from
     *
     * @return the from
     */
    public SocketAddress getFrom() {
        return this.from;
    }

    /**
     * Set the from
     *
     * @param from the from to set
     */
    public void setFrom(SocketAddress from) {
        this.from = from;
    }

    /**
     * Get the to
     *
     * @return the to
     */
    public SocketAddress getTo() {
        return this.to;
    }

    /**
     * Set the to
     *
     * @param to the to to set
     */
    public void setTo(SocketAddress to) {
        this.to = to;
    }

    /**
     * Get the id
     *
     * @return the id
     */
    public String getId() {
        return this.id;
    }

    /**
     * Set the id
     *
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the correlationId
     */
    public String getCorrelationId() {
        return this.correlationId;
    }

    /**
     * @param correlationId the correlationId to set
     */
    public void setCorrelationId(String correlationId) {
        this.correlationId = correlationId;
    }

    /**
     * Get the reponseRequired
     *
     * @return the reponseRequired
     */
    public boolean isResponseRequired() {
        return this.responseRequired;
    }

    /**
     * Set the reponseRequired
     *
     * @param responseRequired the reponseRequired to set
     */
    public void setResponseRequired(boolean responseRequired) {
        this.responseRequired = responseRequired;
    }

    /**
     * Get the response
     *
     * @return the response
     */
    public boolean isResponse() {
        return this.response;
    }

    /**
     * Set the response
     *
     * @param response the response to set
     */
    public void setResponse(boolean response) {
        this.response = response;
    }

    /**
     * Get the replayed
     *
     * @return the replayed
     */
    public boolean isReplayed() {
        return this.replayed;
    }

    /**
     * Set the replayed
     *
     * @param replayed the replayed to set
     */
    public void setReplayed(boolean replayed) {
        this.replayed = replayed;
    }

    /**
     * Get the producerId
     *
     * @return the producerId
     */
    public String getProducerId() {
        return this.producerId;
    }

    /**
     * Set the producerId
     *
     * @param producerId the producerId to set
     */
    public void setProducerId(String producerId) {
        this.producerId = producerId;
    }

    /**
     * Get the messageSequence
     *
     * @return the messageSequence
     */
    public long getMessageSequence() {
        return this.messageSequence;
    }

    /**
     * Set the messageSequence
     *
     * @param messageSequence the messageSequence to set
     */
    public void setMessageSequence(long messageSequence) {
        this.messageSequence = messageSequence;
    }

    /**
     * Get the reliable
     *
     * @return the reliable
     */
    public boolean isReliable() {
        return this.reliable;
    }

    /**
     * Set the reliable
     *
     * @param reliable the reliable to set
     */
    public void setReliable(boolean reliable) {
        this.reliable = reliable;
    }

    /**
     * Get the noLocal
     *
     * @return the noLocal
     */
    public boolean isNoLocal() {
        return this.noLocal;
    }

    /**
     * Set the noLocal
     *
     * @param noLocal the noLocal to set
     */
    public void setNoLocal(boolean noLocal) {
        this.noLocal = noLocal;
    }

    /**
     * Initialize from a Buffer
     */
    public void read(BufferInputStream in) throws IOException {
        this.id = in.readString();
        this.correlationId = in.readString();
        this.producerId = in.readString();
        this.messageSequence = in.readLong();
        this.byteBool.setData(in.readByte());
        this.responseRequired = this.byteBool.readBoolean();
        this.response = this.byteBool.readBoolean();
        this.replayed = this.byteBool.readBoolean();
    }

    protected void preWrite() {
        this.byteBool.clear();
        this.byteBool.writeBoolean(this.responseRequired);
        this.byteBool.writeBoolean(this.response);
        this.byteBool.writeBoolean(this.replayed);
    }

    /**
     * Write state to a Buffer
     */
    public void write(BufferOutputStream out) throws IOException {
        preWrite();
        out.writeString(this.id);
        out.writeString(this.correlationId);
        out.writeString(this.producerId);
        out.writeLong(this.messageSequence);
        out.writeByte(this.byteBool.getData());
    }
}