changeset 584:a13373114df4

fix writer.c
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 25 Oct 2009 20:24:53 +0900
parents 530d189ebf1a
children 987380738a50
files Renderer/Test/writer.c
diffstat 1 files changed, 51 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Test/writer.c	Sun Oct 25 20:11:38 2009 +0900
+++ b/Renderer/Test/writer.c	Sun Oct 25 20:24:53 2009 +0900
@@ -2,83 +2,80 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <pthread.h>
 #include <arpa/inet.h>
+#include <sys/stat.h>
+#include <sys/mman.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);
-}
-
+#define SERIAL_REGIST_TUPLE_NO 1
 
 void
-mainLoop(int tid, int write_id)
+mainLoop(int tid, int write_id, int fd)
 {
-	int size=BUFSIZ;
-	char *buff = (char *)malloc(size);
-	int len;
-	uint32_t line_id=0;
+	void *addr;
+	struct stat sb;
 
-	if ( (buff=(char*)malloc(size))==NULL ) {
-		perror("malloc");
-		exit(EXIT_FAILURE);
+	if (fstat(fd, &sb) == -1) {
+		perror("fstat");
+		exit(1);
+	}
+	addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+	if (addr==NULL) {
+		perror("mmap");
+		exit(1);
 	}
 
-	/* '\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);
+	printf("file size=%lld\n", sb.st_size);
 
-		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++;
-	}
+	psx_out(tid, write_id, (unsigned char *)addr, sb.st_size);
+	psx_sync_n();
 
 	return;
 }
 
+int get_serial_id(int tid) {
+	char *data;
+	int serial;
+	int seq;
+
+	seq = psx_in(tid, 65535);
+	do {
+	    psx_sync_n();
+	    data = (char *)psx_reply(seq);
+	} while(data==0);
+	serial = atoi(data + LINDA_HEADER_SIZE);
+	psx_free(data);
+
+	return serial;
+}
+
+void send_serial_id(int tid, int serial_id) {
+	int safe_id = htonl(serial_id);
+
+	psx_out(tid, SERIAL_REGIST_TUPLE_NO, (unsigned char *)&safe_id, sizeof(safe_id));
+	psx_sync_n();
+}
 
 int
 main(int argc, char *argv[]) {
 	int tspace;
-
+	int serial;
+	int xml_id;
+	char *linda_serv = "localhost";
+	if (argc > 1)
+		linda_serv = argv[1];
 	init_linda();
-	tspace = open_linda_java("localhost", PORT);
+	tspace = open_linda_java(linda_serv, PORT);
 	printf("open socket (tupple space id) = %d\n", tspace);
-
-	mainLoop(tspace, 10);
-
-	printf("mainLoop finished\n");
+	serial = get_serial_id(tspace);
+	xml_id = serial * 10;
+	mainLoop(tspace, xml_id, fileno(stdin));
+	sleep(1);
+	send_serial_id(tspace, serial);
+	printf("send xml_id = %d, seciral = %d\n",xml_id, serial);
 	exit(EXIT_SUCCESS);
 }