package com.example.jorgepalacios.threewayhandshake;

import java.io.Serializable;

/**
 * Created by jorgepalacios on 3/26/16.
 */
public class HostA implements Serializable
{
    // instance variables - replace the example below with your own
    private int sequenceNum, acknowledgeNum, respondSeqNum, sequenceB;
    private boolean connection;

    /**
     * Constructor for objects of class HostA
     */
    public HostA()
    {
        // initialise instance variables
        sequenceNum = 0;
        acknowledgeNum = 0;
        respondSeqNum = 0;
        sequenceB = 0;
        connection = false;
    }

    public void setSequenceNum(int sequenceNum)
    {
        this.sequenceNum = sequenceNum;
    }

    public int getSequenceNum()
    {
        return sequenceNum;
    }

    public void setAcknowledgeNum(int acknowledgeNum)
    {
        this.acknowledgeNum = acknowledgeNum;
    }

    public int getAcknowledgeNum()
    {
        return acknowledgeNum;
    }

    public int getRespondSeqNum()
    {
        return respondSeqNum;
    }

    public int getSequenceB()
    {
        return sequenceB;
    }

    public boolean getConnection()
    {
        return connection;
    }

    public String CheckSeq(int a, int r, boolean t){
        sequenceB=r;
        connection = t;
        if (sequenceNum+1==a){
            acknowledgeNum=sequenceB+1;
            respondSeqNum=a;
            return responds();
        }
        else{
            return error();
        }
    }

    public String error(){
        return "wrong sequence number";
    }

    public String responds(){
        return "SYN + ACK ( ACK: " +respondSeqNum+ ", "+ "SEQ: " +sequenceB+ ") Established!";
    }

    public String termReq(){
        return "FIN: Request \n[SEQ:" +respondSeqNum+ " ACK:"+sequenceB+"]";
    }

    public String recAck(){
        return "Receive ACK";
    }

    public String recFin(){
        return "Receive FIN\n Send ACK";
    }
}
