package com.example.jorgepalacios.threewayhandshake;

import android.support.v7.app.AppCompatActivity;

import java.io.Serializable;

/**
 * Created by jorgepalacios on 3/26/16.
 */
public class HostB extends AppCompatActivity implements Serializable
{
    // instance variables - replace the example below with your own
    private int sequenceNum, acknowledgeNum, respondSeqNum;
    private int sequenceA = 0;
    private boolean passiveOpen, connection;

    /**
     * Constructor for objects of class HostA
     */
    public HostB()
    {
        // initialise instance variables
        sequenceNum = 0;
        acknowledgeNum = 0;
        respondSeqNum = 0;
        passiveOpen = false;
        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 boolean getPassiveOpen()
    {
        return passiveOpen;
    }

    public boolean getConnection()
    {
        return connection;
    }

    public String recieve(int s){
        sequenceA = s;
        acknowledgeNum = sequenceA +1;
        passiveOpen = true;
        connection = true;
        return request();

        //System.out.println(request());
    }

    public String CheckSeq(int a, int r, boolean t){

        if (sequenceNum+1==a && acknowledgeNum==r && connection == true){
            respondSeqNum=a;
            return established();
        }
        else{
            return error();
        }
    }

    public String connectionCheck(String q){
        if(q.equals("Y") || q.equals("y")){
            return connection();
        }
        else{
          return connectionNo();
        }
    }

    public String terminateCon(String w){
        if(w.equals("Y") || w.equals("y")){
            if (connection != false){
                connection = false;
                return terConnection();
            }
            else{
                return connectionNo();
            }
        }
        else {
            return connectionStill();
        }
    }

    public int setRespondSeqNum()
    {
        return respondSeqNum;
    }

    public String connectionStill(){
        return "Connection is still running";
    }

    public String terConnection(){
        return "\n\n\nConnection\n terminated";
    }

    public String connection(){
        return "Connection works";
    }

    public String connectionNo(){
        return "No connection";
    }

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

    public String request(){
        return "SYN ( SEQ: "+ sequenceA +" )";
    }

    public String established(){
        return "ACK ( SEQ: " + acknowledgeNum +", ACK: "+ respondSeqNum +" ) Connection Acknowledged";
    }

    public String ackTerm(){
        return "ACK(Request \nSEQ:" +respondSeqNum+ " ACK:"+acknowledgeNum+")";
    }

    public String sendTerm(){
        return "FIN\n(SEQ:" +respondSeqNum+ " ACK:"+acknowledgeNum+")";
    }
}
