annotate examples/filesend/reader.c @ 71:0352536c33fa

(example: writer) get linda server addr from commandline arg.
author kazz@e065701.local
date Fri, 23 Oct 2009 14:11:07 +0900
parents 1b86f7cfb01a
children ad401b7c97bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
68
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <stdlib.h>
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include <string.h>
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 #include <unistd.h>
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include <pthread.h>
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 #include <arpa/inet.h>
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 #include "lindaapi.h"
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #define PORT 10000
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 void
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 mainLoop(int tid, int read_id, FILE* out)
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 {
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 int rd_seq;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 char *tuple, *data;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 int len;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 /* すでにデータが書き込まれた状態で動くこと */
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 rd_seq = psx_rd(tid, read_id);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 psx_sync_n();
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 while (1) {
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 int count;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 tuple = psx_reply(rd_seq);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 if (tuple) {
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 len = psx_get_datalength(tuple);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 data = tuple+LINDA_HEADER_SIZE;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 count=0;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 while (count<len) {
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 count += fwrite(data+count, 1, len-count, out);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 free(tuple);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 break;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 }
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 usleep(500000);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 }
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 return;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 }
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 int
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 main(int argc, char *argv[]) {
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 int len = 0;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 int tspace;
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 init_linda();
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 tspace = open_linda_java("localhost", PORT);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 //printf("open socket (tupple space id) = %d\n", tspace);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 mainLoop(tspace, 10, stdout);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 //printf("mainLoop finished\n");
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 exit(EXIT_SUCCESS);
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 }
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
1b86f7cfb01a add examples
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60