/************************************************************************/
/* commtestclient.h - Communication Plugin Architecture Test Program	*/
/************************************************************************/
/*
 *  (c)2003, 2004 Todd Shadburn
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
/************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifndef WIN32
#include <netinet/in.h>
#else
#include <winsock2.h>
#endif

#include <cutil/cutil.h>
#include <comm/comm.h>


#define BUFFER_SIZE	65536


typedef struct {
	union {
		uint32_t	bytes;
		char		buffer[BUFFER_SIZE + sizeof (uint32_t)];
	} u;
} net_buffer_t;



int main (int argc, char ** argv)
{
	int rc;
	int count;
	int blocks;
	int size;
	int bytes;
	int pktbytes;
	int inbytes;
	int outbytes;
	net_buffer_t inbuf;
	char	*p;
	CommModule *cm = NULL;
	CommChannel *ccclient = NULL;

	if (argc < 3) {
		fprintf (stderr, "Missing arguments.\n");
		fprintf (stderr, "Usage: commtestclient.exe host blocks [blocksize]\n");
		return 2;
	}

	size = BUFFER_SIZE;
	if (argc == 4) {
		size = atoi(argv[3]);
		if (size > BUFFER_SIZE)
			size = BUFFER_SIZE;
	}
	fprintf (stderr, "calling comm_plugin_load\n");

#ifndef WIN32
	rc = comm_plugin_load (&cm, "../plugin/.libs/comm_tcp.so", NULL);
#else
	rc = comm_plugin_load (&cm, ".\\comm_tcp.dll", NULL);
#endif

	if (rc != 0) {
		fprintf (stderr, "comm_plugin_load: rc = %d\n", rc);
		return rc;
	}

	fprintf (stderr, "calling comm_connect\n");
	rc = comm_connect (cm, &ccclient, argv[1], 5050);
	if (rc != 0) {
		fprintf (stderr, "comm_connect: rc = %d\n", rc);
		rc = comm_plugin_unload (&cm);
		return rc;
	}
		
	blocks = atoi(argv[2]);
	fprintf (stderr, "Transferring %d blocks...\n", blocks);

	count = 0;
	while (count < blocks) {
		// write a packet to the remote server
		pktbytes = size;
		inbuf.u.bytes = htonl(pktbytes);
		p = inbuf.u.buffer;
		bytes = pktbytes + sizeof(uint32_t);
		while (bytes > 0) {
			//fprintf (stderr, "calling comm_write\n");
			outbytes = bytes;
			rc = comm_write (ccclient, p, &outbytes);
			if (rc != 0) {
				fprintf (stderr, "comm_write: rc = %d\n", rc);
				return rc;
			}
			bytes -= outbytes;
			p += outbytes;
		}

		// read a packet from the remote server
		p = inbuf.u.buffer;
		bytes = BUFFER_SIZE + sizeof (uint32_t);
		while (bytes > 0) {
			inbytes = bytes;
			rc = comm_read (ccclient, p, &inbytes);
			if (rc != 0) {
				fprintf (stderr, "comm_read: rc = %d\n", rc);
				return rc;
			}
			if (bytes == (BUFFER_SIZE + sizeof (uint32_t)) &&
			     inbytes >= 4) {
				pktbytes = ntohl(inbuf.u.bytes);
				bytes = pktbytes - (inbytes-4);
				p += inbytes;
			} else {
				bytes -= inbytes;
				p += inbytes;
			}
		}
							
		count++;
	}
	fprintf (stderr, "client transferred %d blocks\n", count);

	fprintf (stderr, "calling comm_close(client)\n");
	rc = comm_close (&ccclient, 0);
	if (rc != 0) {
		fprintf (stderr, "comm_close(client): rc = %d\n", rc);
		rc = comm_plugin_unload (&cm);
		return rc;
	}

fprintf (stderr, "calling comm_plugin_unload\n");
	rc = comm_plugin_unload (&cm);
	if (rc != 0) {
		fprintf (stderr, "comm_plugin_unload: rc = %d\n", rc);
		return rc;
	}


	return 0;
}


