comparison Renderer/Test/writer.c @ 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 0decff4e867b
comparison
equal deleted inserted replaced
583:530d189ebf1a 584:a13373114df4
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <string.h> 3 #include <string.h>
4 #include <unistd.h> 4 #include <unistd.h>
5 #include <pthread.h>
6 #include <arpa/inet.h> 5 #include <arpa/inet.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
7 8
8 #include "lindaapi.h" 9 #include "lindaapi.h"
9 10
10 #define PORT 10000 11 #define PORT 10000
11 12
12 void 13 #define SERIAL_REGIST_TUPLE_NO 1
13 del_callback(unsigned char *data, void *arg)
14 {
15 free(data);
16 }
17 void
18 psx_del(int t, int id)
19 {
20 psx_callback_in(t, id, del_callback, NULL);
21 }
22
23 14
24 void 15 void
25 mainLoop(int tid, int write_id) 16 mainLoop(int tid, int write_id, int fd)
26 { 17 {
27 int size=BUFSIZ; 18 void *addr;
28 char *buff = (char *)malloc(size); 19 struct stat sb;
29 int len;
30 uint32_t line_id=0;
31 20
32 if ( (buff=(char*)malloc(size))==NULL ) { 21 if (fstat(fd, &sb) == -1) {
33 perror("malloc"); 22 perror("fstat");
34 exit(EXIT_FAILURE); 23 exit(1);
24 }
25 addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
26 if (addr==NULL) {
27 perror("mmap");
28 exit(1);
35 } 29 }
36 30
37 /* '\0'も含めて送信 */ 31 printf("file size=%lld\n", sb.st_size);
38 psx_out(tid, write_id, (unsigned char *)"\0\0\0\0aiueo", 10); 32
33 psx_out(tid, write_id, (unsigned char *)addr, sb.st_size);
39 psx_sync_n(); 34 psx_sync_n();
40
41 while (1) {
42 char *data = buff+4;
43 /* 標準入力から読み込み、改行を削除 */
44 fgets(data, size-4, stdin);
45 len = strlen(data);
46 if (data[len-1]=='\n') {
47 data[--len] = '\0';
48 }
49 ((uint32_t*)buff)[0] = htonl(line_id);
50
51 if (len==size) {
52 /* realloc */
53 }
54
55 /* まえのデータを消す */
56 psx_del(tid, write_id);
57 /* '\0'も含めて送信 */
58 psx_out(tid, write_id, (unsigned char *)buff, len+4+1);
59
60 psx_sync_n();
61 printf("send data[%d] line_id=%d, `%s'\n", len, line_id, data);
62 printf("line_id =%d\n", ntohl(((uint32_t*)buff)[0]));
63
64 line_id++;
65 }
66 35
67 return; 36 return;
68 } 37 }
69 38
39 int get_serial_id(int tid) {
40 char *data;
41 int serial;
42 int seq;
43
44 seq = psx_in(tid, 65535);
45 do {
46 psx_sync_n();
47 data = (char *)psx_reply(seq);
48 } while(data==0);
49 serial = atoi(data + LINDA_HEADER_SIZE);
50 psx_free(data);
51
52 return serial;
53 }
54
55 void send_serial_id(int tid, int serial_id) {
56 int safe_id = htonl(serial_id);
57
58 psx_out(tid, SERIAL_REGIST_TUPLE_NO, (unsigned char *)&safe_id, sizeof(safe_id));
59 psx_sync_n();
60 }
70 61
71 int 62 int
72 main(int argc, char *argv[]) { 63 main(int argc, char *argv[]) {
73 int tspace; 64 int tspace;
74 65 int serial;
66 int xml_id;
67 char *linda_serv = "localhost";
68 if (argc > 1)
69 linda_serv = argv[1];
75 init_linda(); 70 init_linda();
76 tspace = open_linda_java("localhost", PORT); 71 tspace = open_linda_java(linda_serv, PORT);
77 printf("open socket (tupple space id) = %d\n", tspace); 72 printf("open socket (tupple space id) = %d\n", tspace);
78 73 serial = get_serial_id(tspace);
79 mainLoop(tspace, 10); 74 xml_id = serial * 10;
80 75 mainLoop(tspace, xml_id, fileno(stdin));
81 printf("mainLoop finished\n"); 76 sleep(1);
77 send_serial_id(tspace, serial);
78 printf("send xml_id = %d, seciral = %d\n",xml_id, serial);
82 exit(EXIT_SUCCESS); 79 exit(EXIT_SUCCESS);
83 } 80 }
84 81
85 /* end */ 82 /* end */