view Renderer/Test/writer.c @ 583:530d189ebf1a

add writer
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 25 Oct 2009 20:11:38 +0900
parents
children a13373114df4
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>

#include "lindaapi.h"

#define PORT 10000

void
del_callback(unsigned char *data, void *arg)
{
	free(data);
}
void
psx_del(int t, int id)
{
	psx_callback_in(t, id, del_callback, NULL);
}


void
mainLoop(int tid, int write_id)
{
	int size=BUFSIZ;
	char *buff = (char *)malloc(size);
	int len;
	uint32_t line_id=0;

	if ( (buff=(char*)malloc(size))==NULL ) {
		perror("malloc");
		exit(EXIT_FAILURE);
	}

	/* '\0'も含めて送信  */
	psx_out(tid, write_id, (unsigned char *)"\0\0\0\0aiueo", 10);
	psx_sync_n();

	while (1) {
		char *data = buff+4;
		/* 標準入力から読み込み、改行を削除  */
		fgets(data, size-4, stdin);
		len = strlen(data);
		if (data[len-1]=='\n') {
			data[--len] = '\0';
		}
		((uint32_t*)buff)[0] = htonl(line_id);

		if (len==size) {
			/* realloc  */
		}

		/* まえのデータを消す  */
		psx_del(tid, write_id);
		/* '\0'も含めて送信  */
		psx_out(tid, write_id, (unsigned char *)buff, len+4+1);

		psx_sync_n();
		printf("send data[%d] line_id=%d, `%s'\n", len, line_id, data);
		printf("line_id =%d\n", ntohl(((uint32_t*)buff)[0]));

		line_id++;
	}

	return;
}


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

	init_linda();
	tspace = open_linda_java("localhost", PORT);
	printf("open socket (tupple space id) = %d\n", tspace);

	mainLoop(tspace, 10);

	printf("mainLoop finished\n");
	exit(EXIT_SUCCESS);
}

/* end */