#include <stdio.h>
#include <string.h>
#include <rpc/rpc.h>
#include <unistd.h>
#include <sys/param.h>
#include "../commonIncludes/Coordinator.h"
#include "../goodServer/initialize.c"
#include "_bootUp.h"

int main(int argc, char* argv[]) {

     CLIENT           *cl;
     StrArr           *strArr;
     char             hostName[MAXHOSTNAMELEN];
     char             ipAddr[50];
     int              *lresult;
     int              i;
     char			  tmp[100];
	 int			  isFirstNode = 0;

	/** Get the name of the host machine in the first argument (hostName) 
	*  and the ip-address in the second argument (ipAddr) 
	*/

    GetHostNameAndIPadd( hostName, ipAddr); 

	/*
    strArr = loadTable( NULL); // get all details of current machine
							// stored in a local file called "/kernel/src/node_inf_file".

    for( i = 0; i < strArr->StrArr_len; i++) {

        printf("%s\n", strArr->StrArr_val[i].String_val);
    }

    #ifdef PRINT
    	PrintInLogFile("Just before broadcast"); 
    #endif
	*/

		/** whether this is the first node is decided by the command line
		 * argument. We can also decide whether it is the first node  by
		 * broadcasting and asking if any other nodes are present. But this
		 * takes a long time and there are some other problems so for the
		 * time being we decide first node by a command line argument. for
		 * starting the first server give command as boot first.
		 */

	if(argc > 1) {

		if( strcmp(argv[1] , FIRST_NODE_STRING) == 0 ) { 
			
			isFirstNode = 1;  // you know that you are first one from the command line parameter
		}
		else {
			isFirstNode = 0;  // you know that you are first one from the command line parameter
		}
	}

	printf(" Just starting the server --- %s \n");
	 
	        // Start the server.....
	if ( system( "server&" ) != 0 ) {
		error("Major problem can't start the server");
		return 5;
	}

	printf(" Server STARTED ..............\n");
			
		//Wait for some time after the server has been started to give server some time
		//to do the initialization.
	sleep(5);
	
	if ( (cl = clnt_create(ipAddr, COORDINATOR, INITIALIZE, "tcp") )
					                    == NULL) {
		clnt_pcreateerror(ipAddr);
		return CLIENT_CONNECTION_FAIL;
	}

	if (clnt_call(cl, BOOT_UP, xdr_int, &( isFirstNode),
					xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
		 
		//If the RPC call to the local server has failed then
		// 
		clnt_perror(cl, ipAddr);
		exit(CLIENT_CONNECTION_FAIL);
	}

	
	/** If the command line argument does not tell that it is the first node then 
	 *  broadcast the message to find the presence of other ARC servers in the network.
	*/
	/*
  	if (replySubsFlag == true)   {

		replySubsFlag = false;

		clnt_broadcast(COORDINATOR, INITIALIZE , BROADCAST_SUBSCRIBE ,
					xdr_StrArr, strArr, xdr_NodeArr, &tmpArr, replySubscribe);

		#ifdef PRINT
			PrintInLogFile("Just after broadcast");     
		#endif
		printf("Just after broadcast\n");
	}
	
		// if you remain to be first, you will receive no replies.
    if (replySubsFlag == false) {
		printf("This is the first node \n");
        initializeFirstNode( strArr);
    }
  
    printf(" Just starting the server --- %s \n",tmp);

		// Start the server.....
    if ( system( "server&" ) != 0 ) {
       error("Major problem can't start the server");
       return 5;
    }

    printf(" Server STARTED ..............\n");

		//Wait for some time after the server has been started to give server some time 
		//to do the initialization.
    sleep(5);

	printf("\n node array  :: length = %d \n", toBePassed.NodeArr_len);

	for(i=0;i< toBePassed.NodeArr_len;i++) {
		printf("--- \n Node Id = %d \n",
		toBePassed.NodeArr_val[i].NodeId);

		printf("Node name = %s \n",
		toBePassed.NodeArr_val[i].Name.String_val);
	}

	passArgumentsToLocalCoordinator(ipAddr);

    clnt_destroy(cl); //Destroy the client handle.
}

void passArgumentsToLocalCoordinator(char ipAddr[]) {
		
	CLIENT *cl;
		//Create a client to the local co-ordinator.
    if ((cl = clnt_create( ipAddr, COORDINATOR, INITIALIZE,"tcp")) == NULL) {
			//If failed then
        clnt_pcreateerror(ipAddr);
        exit( CLIENT_CONNECTION_FAIL );
    }

		// this RPC call goes to the local server
    if (clnt_call(cl, BOOT_UP, xdr_NodeArr, &( toBePassed),  
                    xdr_int, &retVal, TIMEOUT) != RPC_SUCCESS) {

		//If the RPC call to the local server has failed then

        clnt_perror(cl, ipAddr);
        exit(CLIENT_CONNECTION_FAIL);
    }*/
}

