Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration


File:  [GForge:cvsroot/nutss] / libnutss / examples / NUTSS_app.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 11:42:45 2007 UTC (9 years, 11 months ago) by saikat
Branches: saikat, MAIN
CVS tags: version_0_1_0, initial, HEAD
Initial upload


// NUTSS_app.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <stdlib.h>
#include "nutss.h"

#ifdef WIN32
#define snprintf _snprintf
#endif

#ifdef LINUX
#include <errno.h>
#endif

#define EXCH 100000
//#define TEST_ONE_PROCESS

//int _tmain(int argc, _TCHAR* argv[])
int main(int arg, char ** argv)
{
	int sendmode=0,rcvmode=0, i, one = 1, zero = 0;
	SOCKET s, s2, s3;
	struct sockaddr_ns nct;
	int rval;
	/*
	char* proxy = "urbana:5060";  // "sip.nutss.net:5060";
	char * user =  "ari"; //"saikat";
	char *domain = "cs.cornell.edu"; // "nutss.net";
	*/

	char* proxy =  "sip.nutss.net:5060";
	char * user =  "ari1";
	char *domain = "nutss.net";
	char * pw = "password";
	char* listeningUser = user;  //person to connect to
	char * svcuname = "admin";
	char * svcdomain = "nutss.net";
	printf("test starting\n");


#ifdef NUTSS_STATIC
	printf("linking statically against nutss\n");
#else
	printf("linking dynamically against nutss\n");
#endif


#ifdef TEST_ONE_PROCESS
	sendmode=rcvmode=1;
#else
	printf("note: one arg -->send, else receive\n\n");
	if(arg > 1)
	{
		user = "ari2";   //our own username
		sendmode = 1;
	}
	else
		rcvmode =1;
#endif

	putenv("NUTSS_CERT=client.pem:password@root.pem");

	nutss_config_set(NUTSS_CONFIG_USERNAME, user, strlen(user));
	nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, user, strlen(user));
	nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, pw, strlen(pw));
	nutss_config_set(NUTSS_CONFIG_DOMAINNAME, domain, strlen(domain));
	nutss_config_set(NUTSS_CONFIG_SIGPROXY, proxy, strlen(proxy));
	nutss_config_set(NUTSS_CONFIG_OPT_SECURE, &zero, sizeof(zero));
	nutss_config_set(NUTSS_CONFIG_SVCUSERNAME, svcuname, strlen(svcuname));
	nutss_config_set(NUTSS_CONFIG_SVCDOMAINNAME, svcdomain, strlen(svcdomain));
	nutss_init();

	memset(&nct,0, sizeof(nct));
		//now set up nct for the connect
	nct.family = AF_NUTSS;

	strncpy(nct.user, listeningUser, sizeof(nct.user));
	strncpy(nct.domain, domain, sizeof(nct.domain));
	strncpy(nct.service, "nutsstestapp", sizeof(nct.service));

	if(rcvmode)
	{
		s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if(	0 != nutss_bind(s, (struct sockaddr *)&nct, sizeof(nct)))
		{
			printf("failed to bind correctly\n");
			exit(0);
		}
		else
			listen(s, 10);
	}

	if(sendmode)
	{
		char buf[255];
		memset(buf, 0, 255);
		s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

		printf("trying to connect\n");
		rval = nutss_connect(s2, &nct, sizeof(nct));
		if(rval != 0)
			printf("\nconnect failed [NUTSS_app], errno = %d \n", errno);
		else
			printf("connect returned success!\n");

		for(i=0; i<EXCH; ++i)
		{
			int randwait;
			rval = recv(s2, buf, 255, 0);
#ifdef WIN32
			randwait=  (rand() % 200);
			Sleep(randwait+ 200);  //wait an average of 300 milis per round trip.
							
#endif
			if(rval > 0)
			{
				printf("\n----\nsender received message: %s\n----\n",buf);
				snprintf(buf, 250, "hello, from the initator [%d]\n", i);
				send(s2, buf, strlen(buf)+1, 0);
			}
			else
			{
				printf("\n---\nReceive failed\n--\n\n");
				break;
			}
		}
	}

	if( rcvmode)
	{
		char buf[255];
		s3 = nutss_accept(s, NULL, 0);
		printf("connect succeeded; accept returned %08X\n", s3);

		for( i=0; i<EXCH; ++i)
		{
			snprintf(buf, 250, "hello, from the receiver [%d]",i);
			rval = send(s3, buf, strlen(buf)+1,0);
			if(rval ==0)
			{
				printf("\n---\nSend failed\n--\n\n");
				break;
			}
			memset(buf, 0, 255);
			rval = recv(s3, buf, 255, 0);
			if(rval > 0 && strlen(buf) > 0)
				printf("\n\n--\n\nreceiver received: %s\n----\n", buf);
			else
			{
				printf("\n\n--\n\nreceive failed\n---\n");
				break;
			}
		}
	}

	printf("\ndone\n");
	fflush(stdout);
#ifdef WIN32
	Sleep(3000000);
#else
	sleep(3000);
#endif
	return 0;
}


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment