/************************************************************************/
/* commtestserver.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 bytes;
	int pktbytes;
	int inbytes;
	int outbytes;
	int total;
	net_buffer_t inbuf;
	char	*p;
	CommModule *cm = NULL;
	CommChannel *ccserver = NULL;
	CommChannel *ccclient = NULL;

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_bind\n");
	rc = comm_bind (cm, &ccserver, NULL, 5050);
	if (rc != 0) {
		fprintf (stderr, "comm_bind: rc = %d\n", rc);
		rc = comm_plugin_unload (&cm);
		return rc;
	}

	fprintf (stderr, "Accepting connections...\n");
//	count = 0;
	while (1) {

		//fprintf (stderr, "calling comm_accept\n");
		rc = comm_accept (ccserver, &ccclient, 0);
		if (rc != 0) {
			fprintf (stderr, "comm_accept: rc = %d\n", rc);
			rc = comm_close (&ccserver, 0);
			rc = comm_plugin_unload (&cm);
			return rc;
		}
		total = 0;
		count = 0;
		while (1) {
			// 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);
					goto client_close;
				}
				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;
				}
			}

			// write a packet to the remote server
			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);
					goto client_close;
				}
				bytes -= outbytes;
				p += outbytes;
			}

/*
			//fprintf (stderr, "calling comm_read (header)\n");
			inbytes = 4;
			rc = comm_read (ccclient, inbuf.u.buffer, &inbytes);
			if (rc != 0) {
				fprintf (stderr, "comm_read: rc = %d\n", rc);
				break;
			}
			pktbytes = ntohl(inbuf.u.bytes);

			//fprintf (stderr, "calling comm_read\n");
			p = inbuf.buffer;
			bytes = pktbytes;
			while (bytes > 0) {
				inbytes = bytes;
				rc = comm_read (ccclient, p, &inbytes);
				if (rc != 0) {
					fprintf (stderr, "comm_read: rc = %d\n", rc);
					break;
				}
				bytes -= inbytes;
				p += inbytes;
			}
								
			//fprintf (stderr, "calling comm_write (header)\n");
			
			outbytes = 4;
			rc = comm_write (ccclient, inbuf.u.buffer, &outbytes);
			if (rc != 0) {
				fprintf (stderr, "comm_write: rc = %d\n", rc);
				break;
			}

			p = inbuf.buffer;
			bytes = pktbytes;
			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);
					break;
				}
				bytes -= outbytes;
				p += outbytes;
			}
*/

			count++;
			total += pktbytes;
		}
client_close:
		fprintf (stderr, "client transferred %d blocks (%d bytes)\n", count, total);
		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_close (&ccserver, 0);
			rc = comm_plugin_unload (&cm);
			return rc;
		}
//	count++;
	}

fprintf (stderr, "calling comm_close(server)\n");
	rc = comm_close (&ccserver, 0);
	if (rc != 0) {
		fprintf (stderr, "comm_close(server): 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;
}


