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 / slinkapp.c
Revision (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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <poll.h>
#include <sys/socket.h>
#include <pthread.h>
#include "nutss.h"
#include "swaplinks.h"

#ifdef WIN32
#define sleep(sec) Sleep((sec)*1000)
#define snprintf   _snprintf

int num;
int q, r;

void *recvthread(void *arg) {
    swaplinks_p swp = (swaplinks_p)arg;
    struct sockaddr_ns peer;
    socklen_t len = sizeof(peer);
    char tmp[256];

    for (;;) {
        if (swaplinks_recvfrom(swp, tmp, sizeof(tmp), 0, &peer, &len) > 0) {
            if (f) fprintf(f, "node-%d RCVD %s\n", num, tmp);
            if (!strncmp(tmp, "REQUEST", 7)) {
                snprintf(tmp, sizeof(tmp), "RESPONSE Response %d from node-%d", r++, num);
                swaplinks_sendto(swp, tmp, strlen(tmp)+1, 0, &peer, sizeof(peer));
                if (f) fprintf(f, "node-%d SENT %s\n", num, tmp);

int main(int argc, char **argv) {
    struct sockaddr_ns reg;
	swaplinks_p swp;
    char tmp[256];
    int i = -1;
    pthread_t tid;
    struct timespec ts;

    if (argc < 2) {
        printf("Usage:\n  slinkapp num\n");

    num = atoi(argv[1]);

    if (argc > 2)
        i = atoi(argv[2]);

    snprintf(tmp, sizeof(tmp), "test%d", num);
    nutss_config_set(NUTSS_CONFIG_USERNAME, tmp, strlen(tmp));
    nutss_config_set(NUTSS_CONFIG_PROXYUSERNAME, tmp, strlen(tmp));
    nutss_config_set(NUTSS_CONFIG_PROXYPASSWORD, tmp, strlen(tmp));
    strncpy(tmp, "nutss.net", sizeof(tmp));
    nutss_config_set(NUTSS_CONFIG_DOMAINNAME, tmp, strlen(tmp));
    strncpy(tmp, "sip.nutss.net:5060", sizeof(tmp));
    nutss_config_set(NUTSS_CONFIG_SIGPROXY, tmp, strlen(tmp));

    sprintf(tmp, "output/output_node%d.txt", num);
    f = fopen(tmp,"w");
    if (f == NULL) {
        f = stderr;

    memset(&reg, 0, sizeof(reg));
    reg.family = AF_NUTSS;
    strncpy(reg.user, "ths1", sizeof(reg.user));
    strncpy(reg.domain, "nutss.net", sizeof(reg.domain));
    strncpy(reg.service, "swaplinksd", sizeof(reg.service));

    swp = swaplinks("cloud9", &reg);

    pthread_create(&tid, NULL, recvthread, swp);

    while (i-- != 0) {
        snprintf(tmp, sizeof(tmp), "REQUEST Query %d from node-%d", q++, num);
        if (swaplinks_sendtoany(swp, tmp, strlen(tmp)+1, 0) != -1) {
            if (f) {
                fprintf(f, "node-%d SENT %s\n", num, tmp);
        } else q--;

    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