/****************************************************************
 *                                                              *
 *  LIBDIST V1.0						*
 *                                                              *
 *  comm-test.c -- test communication primitives                *
 *                                                              *
 *  Last changed: 01.12.98                                      *
 *  Author: Frank Kargl                                         *
 *                                                              *
 *  Restrictions: works only for Solaris 2.6 or above           *
 *                                                              *
 ****************************************************************/

#include "libdist.h"
#include "config.h"

/* simple echo server */

void *echoline(int *socket) {
	char *ptr;

	while((ptr=dl_sck_receive(*socket))) {
		if (ptr==NULL) {
			printf("\nERROR READING IN SERVER\n");
			break;
		}
		if (ptr==(char *)EOF) {
			printf("\nEOF REACHED !\n");
			break;
		}
		if (!strcmp(ptr,"QUIT")) {
			printf("\nQUIT COMMAND\n\n");
			break;
		}
		dl_sck_send(*socket,ptr);
	}

	sleep(2);
	dl_sck_close(*socket);
	dl_thr_exit();
	return NULL;
}

/* start the server using dl_sck_server */

void *servrun(void *string) {
	 
	if (dl_sck_server(string,DL_SCK_TCP,echoline) == DL_ERROR ) {
		printf("\nERROR STARTING SERVER\n\n");
		dl_thr_exit();
	}
	return (void *)NULL;
}

int main(int argc,char *argv[]) {

	if (argc!=2) {
		fprintf(stderr,"Usage: comm-test <port>\n");
		exit(1);
	}

	/* do servrun as a separate thread */
	dl_thr_create(servrun,argv[1]);

	while(1){
		sleep(10);
	};
}
