//
// Lock server tester
//

#include "lock_protocol.h"
#include "lock_client.h"
#include "rpc.h"
#include <arpa/inet.h>
#include <vector>
#include "lock_client_cache.h"

int nt = 5;//R: change th[nt] cause I hardcoded
sockaddr_in dst;
int nlocks = 50;
lock_client_cache **lc = new lock_client_cache * [nt];
std::string c[50] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "X","Y", "W", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m","n", "o", "p","q", "r", "s", "t", "u", "v", "z", "x", "y" } ;
std::string a = std::string("a");
std::string b = std::string("b");

/*
std::string a[0] = std::string("a");
std::string a[1] = std::string("b");
std::string a[2] = std::string("c");
std::string a[3] = std::string("d");
std::string a[4] = std::string("e");
std::string a[5] = std::string("f");
std::string a[6] = std::string("g");
std::string a[7] = std::string("h");
std::string a[8] = std::string("i");
std::string a[9] = std::string("j");
std::string a[10] = std::string("k");
std::string a[11] = std::string("l");
std::string a[12] = std::string("m");
std::string a[13] = std::string("n");
std::string a[14] = std::string("o");
std::string a[15] = std::string("p");
std::string a[16] = std::string("q");
std::string a[17] = std::string("r");
std::string a[18] = std::string("s");
std::string a[19] = std::string("t");
std::string a[20] = std::string("aa");
std::string a[21] = std::string("ab");
std::string a[22] = std::string("ac");
std::string a[23] = std::string("ad");
std::string a[24] = std::string("ae");
std::string a[25] = std::string("af");
std::string a[26] = std::string("ag");
std::string a[27] = std::string("ah");
std::string a[28] = std::string("ai");
std::string a[29] = std::string("aj");
std::string a[30] = std::string("ak");
std::string a[31] = std::string("al");
std::string a[32] = std::string("am");
std::string a[33] = std::string("an");
std::string a[34] = std::string("ao");
std::string a[35] = std::string("ap");
std::string a[36] = std::string("aq");
std::string a[37] = std::string("ar");
std::string a[38] = std::string("as");
std::string a[39] = std::string("at");
*/
 int perm[10] = {3, 2, 0, 4, 1, 8, 7, 5, 9, 6}; 

// check_grant() and check_release() check that the lock server
// doesn't grant the same lock to both clients.
// it assumes that lock names are distinct in the first byte.
int count[256];
pthread_mutex_t count_mutex;

// convert a bit-string to a hex string for debug printouts.
std::string
hex(std::string s)
{
  char buf[64];
  unsigned int len = s.length();
  const char *p = s.c_str();
  unsigned int i;

  buf[0] = '\0';
  for(i = 0; i < len && i*2+1 < sizeof(buf); i++){
    sprintf(buf + (i * 2), "%02x", p[i] & 0xff);
  }
  
  return std::string(buf);
}

void
check_grant(std::string name)
{
  pthread_mutex_lock(&count_mutex);
  int x = name.c_str()[0] & 0xff;
  if(count[x] != 0){
    fprintf(stderr, "error: server granted %s twice\n", hex(name).c_str());
    exit(1);
  }
  count[x] += 1;
  pthread_mutex_unlock(&count_mutex);
}

void
check_release(std::string name)
{
  pthread_mutex_lock(&count_mutex);
  int x = name.c_str()[0] & 0xff;
  if(count[x] != 1){
    fprintf(stderr, "error: client released un-held lock %s\n", 
	    hex(name).c_str());
    exit(1);
  }
  count[x] -= 1;
  pthread_mutex_unlock(&count_mutex);
}

void
test1(void)
{
    printf ("acquire a release a acquire a release a\n");
    lc[0]->acquire(a);
    check_grant(a);
    lc[0]->release(a);
    check_release(a);
    lc[0]->acquire(a);
    check_grant(a);
    lc[0]->release(a);
    check_release(a);

    printf ("acquire a acquire b release b releasea \n");
    lc[0]->acquire(a);
    check_grant(a);
    lc[0]->acquire(b);
    check_grant(b);
    lc[0]->release(b);
    check_release(b);
    lc[0]->release(a);
    check_release(a);
}

void *
test2(void *x) 
{
  int i = * (int *) x;
 
 printf ("test2: client %d acquire a release a\n", i);
  lc[i]->acquire(a);
  //printf ("test2: client %d sleep\n", i);
  check_grant(a);
  sleep(1);
  //printf ("test2: client %d release\n", i);
  check_release(a);
  lc[i]->release(a);
  return 0;
}

void *
test3(void *x)
{
  int i = * (int *) x;

  printf ("test3: client %d acquire a release a concurrent\n", i);
  for (int j = 0; j < 10; j++) {
    lc[i]->acquire(a);
    check_grant(a);
    //printf ("test3: client %d got lock\n", i);
    check_release(a);
    lc[i]->release(a);
  }
  return 0;
}

void *
test4(void *x)
{
  int i = * (int *) x;

  printf ("test4: client %d acquire a release a concurrent; same clnt\n", i);
  for (int j = 0; j < 10; j++) {
    lc[0]->acquire(a);
    check_grant(a);
    //printf ("test4: client %d got lock\n", i);
    check_release(a);
    lc[0]->release(a);
  }
  return 0;
}

void *
test5(void *x)
{
  int i = * (int *) x;

  printf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
  for (int k = 0; k < nlocks ; k++) {
  	std::string a = c[k];
	for (int j = 0; j < 10; j++) {
    	if (i < 5)  lc[0]->acquire(a);
    	else  lc[1]->acquire(a);
    	check_grant(a);
    	//printf ("test5: client %d got lock\n", i);
    	check_release(a);
    	if (i < 5) lc[0]->release(a);
    	else lc[1]->release(a);
 	 }
   }
  return 0;
}

int
main(int argc, char *argv[])
{
   int r;
    pthread_t th[nt];
    int test = 0;
    

    setvbuf(stdout, NULL, _IONBF, 0);

    if(argc < 2) {
      fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
      exit(1);
    }

    make_sockaddr(argv[1], &dst);

    if (argc > 2) {
      test = atoi(argv[2]);
      if(test < 1 || test > 5){
	printf("Test number must be between 1 and 5\n");
	exit(1);
      }
    }

    assert(pthread_mutex_init(&count_mutex, NULL) == 0);
time_t  dummy;
srand(time(&dummy));
int	i = rand(); i++;
	
    //printf("cache lock client\n");
    for ( i = 0; i < nt; i++) lc[i] = new lock_client_cache(dst, perm[i]*(35000/nt) +  (rand() % 4000));

    if(!test || test == 1){
      test1();
    }

    if(!test || test == 2){
      // test2
      for (int i = 0; i < nt; i++) {
	int *a = new int (i);
	r = pthread_create(&th[i], NULL, test2, (void *) a);
	assert (r == 0);
      }
      for (int i = 0; i < nt; i++) {
	pthread_join(th[i], NULL);
      }
    }

    if(!test || test == 3){
      printf("test 3\n");
      
      // test3
      for (int i = 0; i < nt; i++) {
	int *a = new int (i);
	r = pthread_create(&th[i], NULL, test3, (void *) a);
	assert (r == 0);
      }
      for (int i = 0; i < nt; i++) {
	pthread_join(th[i], NULL);
      }
    }

    if(!test || test == 4){
      printf("test 4\n");
      
      // test 4
      for (int i = 0; i < 2; i++) {
	int *a = new int (i);
	r = pthread_create(&th[i], NULL, test4, (void *) a);
	assert (r == 0);
      }
      for (int i = 0; i < 2; i++) {
	pthread_join(th[i], NULL);
      }
    }

    if(!test || test == 5){
      printf("test 5\n");
      
      // test 5
      
      for (int i = 0; i < 10; i++) {
	int *a = new int (i);
	r = pthread_create(&th[i], NULL, test5, (void *) a);
	assert (r == 0);
      }
      for (int i = 0; i < 10; i++) {
	pthread_join(th[i], NULL);
      }
    }
    lc[0]->stats();
    printf ("%s: passed all tests successfully\n", argv[0]);

}
