view examples/filesend/reader.c @ 68:1b86f7cfb01a

add examples
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Wed, 21 Oct 2009 21:11:43 +0900
parents
children ad401b7c97bb
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
mainLoop(int tid, int read_id, FILE* out)
{
	int rd_seq;
	char *tuple, *data;
	int len;

	/* すでにデータが書き込まれた状態で動くこと  */
	rd_seq = psx_rd(tid, read_id);
	psx_sync_n();

	while (1) {
		int count;
		tuple = psx_reply(rd_seq);
		if (tuple) {
			len = psx_get_datalength(tuple);
			data = tuple+LINDA_HEADER_SIZE;

			count=0;
			while (count<len) {
				count += fwrite(data+count, 1, len-count, out);
			}
			free(tuple);
			break;

		}
		usleep(500000);
	}

	return;
}


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

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

	mainLoop(tspace, 10, stdout);

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