testclient.c 2.07 KB
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "trbase.h"
#include "trcomm.h"
#include "trio.h"
#include "trevent.h"

#include "test_handler.h"

#define ITERATIONS 10000000
int spinner[] = {'|', '/', '-', '\\'};

TR_INSTANCE(TR_LoggerSyslog, mylogger, {TR_LOGGER_INFO});
TR_INSTANCE(TR_LoggerStderr, mylogger2, {TR_LOGGER_INFO});

int
main (int argc, char * argv[])
{
#if UDP
	TR_UdpSocket       socket;
	TR_DatagramService connection;
#else
	TR_TcpSocket       socket;
	TR_Connection      connection;
#endif
	TR_SimpleClient    client;
	TR_Protocol        protocol;
	TR_ProtoMessageRaw message;
	int                i, j=0;

	TR_logger  = TR_INSTANCE_CAST(TR_Logger, mylogger2);
	protocol   = TR_new(TR_ProtocolRaw);
#if UDP
	socket     = TR_new(TR_UdpSocket, TR_logger, "127.0.0.1", 5678, 0);
	connection = TR_new(TR_DatagramService, socket, protocol, 2048);
	TR_socketOpen((TR_Socket)socket);
#else
	socket     = TR_new(TR_TcpSocket, TR_logger, "127.0.0.1", 5678, 0);
	connection = TR_new(TR_Connection, socket, protocol, 2048);
	TR_socketConnect((TR_Socket)socket);
#endif

	TR_socketNonblock((TR_Socket)socket);

	client = TR_new(TR_SimpleClient, connection);

	for (i=0; i<ITERATIONS; i++) {
		message = (TR_ProtoMessageRaw)TR_protoCreateRequest(
				protocol, (TR_Socket)socket);
		message->size = sizeof("test");
		message->data = TR_malloc(message->size);
		memcpy(message->data, "test", sizeof("test"));

		message = (TR_ProtoMessageRaw)TR_simpleClientIssue(
				client,
				(TR_ProtoMessage)message,
				1000);

		if (! message) break;
#if 0
		printf("%s\n", message->data);
#else
		if (0 == strncmp("test", message->data, sizeof("test")-1)) {
			if (i % (ITERATIONS/80) == 0) {
				if (j != 0) putchar('\b');
				printf("%c%c", '.', spinner[j%4]);
			} else {
				printf("%c%c", '\b', spinner[j%4]);
			}
			j++;
		} else {
			printf("%c%c", 'f', spinner[i%4]);
		}
#endif
		fflush(stdout);

		TR_delete(message);
	}
	puts("");

	puts("cleanup...");

	TR_delete(client);
	TR_delete(protocol);

	TR_simpleClientClassCleanup();

	TR_cleanup();

	return 0;
}

// vim: set ts=4 sw=4: