#include <stdio.h>
#include <rpc/rpc.h>
#include <unistd.h>
#include <sys/param.h>
#include "../commonIncludes/defn.h"
#include "../commonIncludes/Coordinator.h"

/*
    Boot strapping will initialize the data which is global for
    remote procedures.

    This is the client program which will call the server at this
    node itself.
*/

int main(int argc, char* argv[]) {
     CLIENT           *cl;
     char             server[MAXHOSTNAMELEN];
     int              numOfLocks;  
     void*            dum;
	 static struct timeval TIMEOUT = {25,0};

			
	 if(argc < 2) {
		printf("Usage is : deposit <num_of_locks> <machine_name> \n");
	 	exit(0);
	 }
	 
     if (argc == 2) {
         numOfLocks = DEFAULT_NUMBER_OF_LOCKS;
     }
     else {
         numOfLocks  = atoi(argv[1]);
     }
	
		// if machine name is given on command line use it. Otherwise get
		// the name of machine on which it is run and use it

		if (argc == 3) {
			strcpy(server,argv[2]);
		 	numOfLocks  = atoi(argv[1]); 
		}
		else if (gethostname(server, MAXHOSTNAMELEN)) {
        	error("main: gethostname: couldn't get the name of host");
        	error("main: Sorry can't boot up");
        	return GET_HOST_NAME_FAIL;
     	}


     if ((cl = clnt_create(server, COORDINATOR, ADMINISTRATION, "tcp"))
                                            == NULL) {
        clnt_pcreateerror(server);
        return CLIENT_CONNECTION_FAIL;
     }

     //deposit_locks_6(&numOfLocks, cl);
	 if(clnt_call(cl,DEPOSIT_LOCKS,xdr_int,&numOfLocks,
				xdr_void,NULL,TIMEOUT) != RPC_SUCCESS) {
	 	printf("Deposit lock process : The process of depositing the lock has FAILED");
		clnt_perror(cl,server);
		exit(CLIENT_CONNECTION_FAIL);
	 }
     clnt_destroy(cl);
}

