예제 #1
0
void test_pop() 
{
	int i = 11111;
	char intstr[10];
	sprintf(intstr, "%d", i);
	string uuid = string(intstr);

	//string uuid = getIp();
	string result = "y";
	int rc;

	while(result != "q")
	{
		cout << "UUID" << uuid << endl;
		try
		{
			rc = zc.pop(uuid, "q1", result);

			if (rc == 0)
			{
				i = i + 1;
				sprintf(intstr, "%d", i);
				uuid = string(intstr);

				printf("POP OK, rc(%d), value={%s}\n", rc, result.c_str());
			}
			else
				sleep(10);
		}
		catch(...)
		{
			sleep(10);
		}
	}	
}
예제 #2
0
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());
	}

}
예제 #3
0
void startWorker(int numThrds)
{
	int i, rc;
	char intstr[10];
	pthread_t threads[numThrds];
	string key, key1, result;

	key1 = getIp();

	sprintf(intstr, "%d", id);
	key = string(intstr) + "." + key1;

	while (true)
	{
		try
		{
			if(threadCount < numThrds)
			{
				rc = zc.pop(key, "q1", result);

				if (rc == 0)
				{
					if ( result.c_str() != NULL && strlen(result.c_str()) > 0)
					{
						printf("POP OK, rc(%d), value={%s}\n", rc, result.c_str());

						rc = pthread_create(&threads[threadCount], NULL, execTask, (void *)result.c_str());

						if (rc)
						{
							cout << "Error:unable to create thread," << rc << endl;
							exit(-1);
						}

						threadCount = threadCount + 1;
					}
				}
				else
				{
					printf("POP ERR, rc(%d), value={%s}\n", rc, result.c_str());

					if(strcmp(zht_key.c_str(),"xxx") != 0)
					{
						pthread_mutex_lock( &mutex1 );
						sendResult();
						zht_key = "xxx";
						job_count = 1000;
						pthread_mutex_unlock( &mutex1 );
					}
					else
					{
						job_count = 1000;
						sleep(5);
					}
				}
			}
		}
		catch(...)
		{
			sleep(5);
		}
		
		id = id + 1;
        sprintf(intstr, "%d", id);
        key = string(intstr) + "." + key1;
	}

	cout << "Worker completed" << endl;

}