void *execTask(void *popTask)
{
	string result, update, task, key, cmd, dataTask[4] = "";
	int i, k = 0, rc, data;
	char intstr[10];

	task = (char *) popTask;

	for(i = 0; i < strlen(task.c_str()); i++)
	{
		if (task[i] == ',')
			k = k + 1;
		else
			dataTask[k] = dataTask[k] + task[i];
	}

	key = dataTask[0];
	cmd = dataTask[1] + " " + dataTask[2];

	rc = system(cmd.c_str());
	zht_key = key;
	//pthread_mutex_lock( &mutex1 );
	if ( rc == 0)
	{
		job_count = job_count + 1;
		cout << "Job Count Completed: " << (job_count - 1000) << endl;
	}
	else
	{
		cout << "Job Failed execution: " << task << endl;
		cout << "Pushing the failed job back to queue" << endl;
		id = id + 1;
		sprintf(intstr, "%d", id);
		rc = zc.push((key + "." + string(intstr) + dataTask[3]), task, "q1", result);

		if (rc == 0)
			printf("PUSH OK, rc(%d)\n", rc);
		else
			printf("PUSH ERR, rc(%d)\n", rc);
	}
	threadCount = threadCount - 1;
	//pthread_mutex_unlock( &mutex1 );
}
int main(int argc, char **argv) 
{
	extern char *optarg;

	int printHelp = 0, numThrds = 0;
	string zhtConf = "";
	string neighborConf = "";

	int c;
	while ((c = getopt(argc, argv, "z:n:t:h")) != -1) 
	{
		switch (c) 
		{
		case 'z':
			zhtConf = string(optarg);
			break;
		case 'n':
			neighborConf = string(optarg);
			break;
		case 't':
			numThrds = atoi(optarg);
			break;
		case 'h':
			printHelp = 1;
			break;
		default:
			fprintf(stderr, "Illegal argument \"%c\"\n", c);
			printUsage(argv[0]);
			exit(1);
		}
	}

	int helpPrinted = 0;
	if (printHelp) {
		printUsage(argv[0]);
		helpPrinted = 1;
	}

	try {

		if (!zhtConf.empty() && !neighborConf.empty() && numThrds != 0) {

			zc.init(zhtConf, neighborConf);
			
			cout << "Initializing Worker" << endl;
			string result;
			zc.push("temp", "test", "q1", result);
			zc.pop("xxxx", "q1", result);
			id = 1000;
			job_count = 1000;
			//test_insert();

			//test_pop();

			startWorker(numThrds);

			zc.teardown();

		} else {

			if (!helpPrinted)
				printUsage(argv[0]);
		}
	} catch (exception& e) {

		fprintf(stderr, "%s, exception caught:\n\t%s", "ZHTServer::main",
				e.what());
	}

}
void *execTask(void *popTask)
{
	string result, update, task, key, cmd, dataTask[4] = "";
	int i, k = 0, rc, data;
	char intstr[10];

	task = (char *) popTask;

	for(i = 0; i < strlen(task.c_str()); i++)
	{
		if (task[i] == ',')
			k = k + 1;
		else
			dataTask[k] = dataTask[k] + task[i];
		//key = key + task[i];
	}

	key = dataTask[0];
	cmd = dataTask[1] + " " + dataTask[2];

	//rc = system("netstat | grep 50000 | wc -l");
	rc = system(cmd.c_str());

	pthread_mutex_lock( &mutex1 );
	if ( rc == 0)
	{
		rc = zc.lookup(key, result);
		cout << "Lookup Result: " << (atoi(result.c_str()) - 1000) << endl;

		if ( rc == 0)
		{
			data = atoi(result.c_str());
			data = data + 1;

			sprintf(intstr, "%d", data);
			update = string(intstr);

			rc = zc.insert(key, update);
			cout << "Updating the Key with the Value: " << (atoi(update.c_str()) - 1000) << endl;

			if (rc == 0)
				printf("INSERT OK, rc(%d)\n", rc);
			else
				printf("INSERT ERR, rc(%d)\n", rc);
		}
		else
		{
			rc = zc.insert(key, "1001");
		}
	}
	else
	{
		cout << "********************************************************************" << endl;
		cout << "Job Failed execution: " << task << endl;
		cout << "Pushing the failed job back to queue" << endl;
		rc = zc.push((key + ".x" +dataTask[3]), task, "q1", result);

		if (rc == 0)
			printf("PUSH OK, rc(%d)\n", rc);
		else
			printf("PUSH ERR, rc(%d)\n", rc);
	}
	threadCount = threadCount - 1;
	pthread_mutex_unlock( &mutex1 );
}