annotate Linda/lindaapi.c @ 122:5219bf3d5e71

title_draw
author e085768
date Mon, 27 Jun 2011 19:44:46 +0900
parents 0fae5658fb0b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
1 // $Id$
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
2 //
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
3
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
4 /*----------------------------------------------------------------------
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
5 インクルードファイル読み込み
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
6 ----------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
7 #include <sys/file.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
8 #include <stdio.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
9 #include <stdlib.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
10 #include <string.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
11 #include <unistd.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
12 #include <fcntl.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
13 #include <sys/uio.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
14 #include <sys/time.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
15 #include <sys/select.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
16 #include <sys/stat.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
17 #include <sys/types.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
18 #include <sys/socket.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
19 #include <sys/un.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
20 #include <netinet/in.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
21 #include <netinet/tcp.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
22 #include <signal.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
23 #include <termios.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
24 #include <netdb.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
25 #include <errno.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
26 #include <sys/select.h>
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
27
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
28 #include "lindaapi.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
29
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
30 #define TIMEDELTA 10
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
31 #if 0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
32 #define PSX_Debug(deb) (putchar(PS_DEB)),\
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
33 (printf deb ),\
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
34 (putchar(PS_DEB))
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
35 #define DEB(a)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
36 #else
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
37 #define PSX_Debug(deb)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
38 #define DEB(a) /* a */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
39 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
40
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
41 COMMAND *q_top, *q_end;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
42 REPLY *reply, *r_end;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
43
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
44 int qsize, ps;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
45 unsigned short seq;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
46
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
47 #ifdef COUNT_PACKET
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
48 // print packet count message per PRINT_INTERVAL sec
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
49 #define PRINT_INTERVAL 4
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
50
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
51 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
52 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
53 count_packet (char type):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
54 パケットの送受信カウントする
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
55
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
56 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
57 type - 送信、受信 (char型: s,r)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
58 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
59 void count_packet(char type)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
60 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
61 static int send_packet=-1,receive_packet=0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
62 static struct timeval start,now,previous;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
63
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
64 if (out_packet == -1) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
65 gettimeofday(&start,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
66 gettimeofday(&previous,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
67 send_packet = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
68 printf("packet\tout\tread\t\ttime\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
69 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
70
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
71 if (type == 's') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
72 send_packet++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
73 } else if (type == 'r') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
74 receive_packet++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
75 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
76 fprintf(stderr,"No type in count_packet function\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
77 return;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
78 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
79
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
80 gettimeofday(&now,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
81 if ((now.tv_sec-previous.tv_sec) > PRINT_INTERVAL) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
82 printf("log\t%d\t%d\t%ld\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
83 send_packet,receive_packet,now.tv_sec-start.tv_sec);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
84 fflush(stdout);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
85
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
86 previous.tv_sec = now.tv_sec;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
87 send_packet = receive_packet = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
88 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
89 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
90 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
91
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
92
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
93 #define unix_open open
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
94 #define unix_read_w read
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
95 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
96 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
97 unix_read (int fd, char *buf, unsigned int size):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
98 サーバからTUPLEを読みこむ。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
99 現在は使われていない。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
100
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
101 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
102 fd - サーバのファイルディスクリプタ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
103 buf - 受け取るデータの格納場所(TUPLEヘッダ含む)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
104 size - bufのbyte数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
105 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
106 読みこんだbyte数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
107 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
108 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
109 unix_read(int fd,char *buf,unsigned int size) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
110 int len,a,i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
111 if(read(fd,buf,INT_SIZE)!=INT_SIZE) { // INT_SIZE is sizeof(int)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
112 fprintf(stderr, "read error! on fd:%d len=%d %s\n", fd,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
113 *(unsigned int*)&buf[0],
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
114 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
115 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
116 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
117 len = ntohl(*(unsigned int*)&buf[0]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
118 if(len>size) len=size;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
119 for(a=0;a<len;a+=i) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
120 if((i=read(fd,buf+a,len-a))<0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
121 fprintf(stderr, "ldserv: client read error! on i=%d len= %d %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
122 i, len, strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
123 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
124 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
125 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
126 return len;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
127 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
128
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
129 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
130 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
131 unix_write (int fd, unsigned char *buf, unsigned int size):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
132 サーバへTUPLEを送る。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
133
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
134 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
135 fd - サーバのファイルディスクリプタ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
136 buf - サーバへ送るデータ(TUPLEヘッダ含む)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
137 size - bufのbyte数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
138 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
139 送った(書きこんだ)データのbyte数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
140 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
141 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
142 unix_write(int fd,unsigned char *buf,unsigned int size) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
143 int i,nsize;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
144 nsize = htonl(size);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
145 i = write(fd,&nsize,INT_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
146 i += write(fd,buf,size); // size == datasize + LINDA_HEADER_SIZE
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
147 #ifdef COUNT_PACKET
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
148 count_packet('s');
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
149 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
150 return(i);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
151 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
152
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
153 #define unix_write_w unix_write
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
154
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
155 #define SERV_NAME unix_port
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
156 #define PROTO_NAME "tcp"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
157 #define SERVER_NAME hostname
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
158 #define MAX_REQ 16
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
159
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
160 int fd,paddrlen;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
161 struct hostent *hoste;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
162 struct sockaddr_in serv_addr;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
163 struct sockaddr_un serv_addr_un;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
164 unsigned char ipaddr[4];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
165
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
166
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
167 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
168 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
169 start_linda (char * hostname):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
170 サーバとのコネクションを確立し、COMMANDキューとREPLYキューの
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
171 初期化を行なう。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
172
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
173 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
174 hostname - サーバのホスト名
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
175 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
176 コネクション確立が成功するとそのファイルディスクリプタを返す。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
177 失敗すると -1 を返す。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
178 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
179 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
180 start_linda(char * hostname){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
181 char *p;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
182
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
183 if (! hostname) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
184 hostname = "/tmp/ldserv";
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
185 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
186 if (hostname[0]=='/') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
187 /* Unix domain */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
188 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
189 fprintf(stderr, "socket open error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
190 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
191 return(-1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
192 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
193 serv_addr_un.sun_family = AF_UNIX;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
194 strcpy(serv_addr_un.sun_path, hostname);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
195 fprintf(stdout,"connecting ... %d\n", serv_addr.sin_port);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
196 if (connect(fd, (struct sockaddr *)&serv_addr_un,sizeof(serv_addr_un)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
197 fprintf(stderr,"connection error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
198 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
199 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
200 return(-1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
201 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
202
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
203 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
204 /* INET domain */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
205 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
206 fprintf(stderr, "socket open error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
207 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
208 return(-1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
209 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
210 /* check optional port number */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
211 serv_addr.sin_port = 11511;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
212 p = (char *)malloc(strlen(hostname));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
213 strcpy(p,hostname);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
214 hostname = p;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
215 while(*p) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
216 if (*p==':') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
217 serv_addr.sin_port = atoi(p+1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
218 *p = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
219 break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
220 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
221 p++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
222 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
223 if ((hoste = gethostbyname(hostname)) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
224 fprintf(stderr,"hostname error\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
225 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
226 return(-1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
227 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
228 free(hostname); hostname=0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
229 serv_addr.sin_family = AF_INET;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
230 serv_addr.sin_addr.s_addr = ((struct in_addr *)(hoste->h_addr))->s_addr;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
231 if (serv_addr.sin_family == AF_INET) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
232 int tmp = 1;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
233 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
234 (char *) &tmp, sizeof (int));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
235 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
236 fprintf(stdout,"connecting ... %d\n", serv_addr.sin_port);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
237 if (connect(fd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
238 fprintf(stderr,"connection error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
239 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
240 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
241 return(-1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
242 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
243 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
244
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
245 ps = fd;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
246 fprintf(stdout," connect middle server %d\n", fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
247 q_end = q_top = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
248 r_end = reply = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
249 qsize = seq = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
250 seq = 120;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
251 return ps;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
252 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
253
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
254 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
255 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
256 psx_out (unsigned int id, unsigned char *data, unsigned int size):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
257 outコマンドをCOMMANDキューへ溜める。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
258
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
259 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
260 id - TUPLE SpaceのID
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
261 data - 送信するデータ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
262 size - dataのサイズ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
263 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
264 シーケンス番号
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
265 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
266 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
267 psx_out(unsigned int id, unsigned char *data, unsigned int size){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
268 if (psx_queue(id, size, data, 'o', NULL, NULL) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
269 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
270 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
271 DEB( fprintf(stdout, "psx_out: size = %d, command = %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
272 q_end->size, q_end->command+LINDA_HEADER_SIZE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
273 return(seq);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
274 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
275
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
276 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
277 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
278 psx_ld (unsigned int id, char mode, void(*callback)(char*,void*),
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
279 void * obj):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
280 in,read,waitなどの受信コマンドをCOMMANDキューへ溜める。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
281 psx_in,psx_rd,psx_wait_rdなどに置き換えられている。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
282
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
283 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
284 id - TUPLE SpaceのID
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
285 mode - i,r,w の文字を取り、各々in,read,waitを表している。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
286 callback - コールバックを使用する場合の関数へのポインタ。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
287 使用しない場合はNULLをいれる。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
288 obj - コールバックで用いる関数の引き数。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
289 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
290 psx_queue内でmallocされたREPLY構造体へのポインタ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
291 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
292 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
293 psx_ld(unsigned int id, char mode,void(*callback)(char *,void *),void * obj){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
294 int r;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
295 if ((r=psx_queue(id, 0, NULL, mode, callback, obj)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
296 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
297 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
298 return(r);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
299 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
300
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
301 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
302 unsigned char *
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
303 psx_reply (int seq):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
304 サーバから答えが来たデータを返す。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
305
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
306 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
307 seq - psx_ld()が返した値。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
308 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
309 seqに対応したデータを返す。データをまだ受信していない場合は
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
310 NULLを返す。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
311 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
312 unsigned char *
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
313 psx_reply(int seq){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
314 REPLY *p, *q;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
315 char *ans;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
316
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
317 DEB(fprintf(stdout, "psx_reply: search of seq = %d\n", seq));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
318 PSX_Debug(("psx_reply: seq %d", seq));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
319 for(q = NULL,p = reply;p;q = p,p = p->next){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
320 if (p->seq == seq){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
321 DEB(fprintf(stdout, "psx_reply: match of seq = %d\n", seq));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
322 if (p->mode == '!'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
323 ans = p->answer;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
324 if (q == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
325 reply = p->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
326 if(p==r_end) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
327 r_end = p->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
328 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
329 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
330 q->next = p->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
331 if(p==r_end) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
332 r_end = q;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
333 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
334 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
335 PSX_Debug(("psx_reply: reply %x r_end %x p %x q %x",reply,r_end,p,q));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
336 free(p);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
337 DEB( for(p=reply;p;p=p->next) { PSX_Debug(("psx_queue dump: seq %d mode %c %x %x",p->seq,p->mode,p,p->next))});
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
338 DEB( fprintf(stdout, "psx_reply: returned answer = %s\n", ans));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
339 PSX_Debug(("psx_reply: answer %s",ans));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
340 return(ans);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
341 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
342 if (p->mode == '?'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
343 DEB(fprintf(stdout, "psx_reply: don't accept anser\n"));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
344 return(NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
345 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
346 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
347 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
348
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
349 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
350 PSX_Debug(("psx_reply: no match seq %d",seq));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
351 DEB(fprintf(stdout, "psx_reply: no match of seq\n"));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
352 return(NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
353 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
354
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
355 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
356 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
357 psx_sync_n ():
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
358 サーバとデータの送受信をする。COMMANDキューに溜まったデータを
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
359 送信し、サーバから送られて来たデータを対応するREPLYへいれる。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
360 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
361 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
362 psx_sync_n(){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
363 int acount;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
364 COMMAND *c, *t;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
365
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
366 fd_set tmp, fds;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
367 struct timeval timeout;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
368 timeout.tv_sec=0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
369 timeout.tv_usec=TIMEDELTA * 1000;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
370
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
371 acount = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
372 while (q_top != NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
373 c = q_top;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
374 unix_write_w(ps, c->command, c->size);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
375 free(c->command);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
376 t = c->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
377 free(c);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
378 q_top = c = t;qsize--;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
379 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
380 FD_ZERO(&fds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
381 FD_SET(ps, &fds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
382 tmp = fds;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
383 while(select(32, &tmp, NULL, NULL, &timeout) > 0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
384 if(FD_ISSET(ps, &tmp)) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
385 unix_chkserv();
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
386 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
387 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
388 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
389
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
390 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
391 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
392 psx_queue (unsigned int id, unsigned int size, unsigned char *data,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
393 char mode, void(*callback)(char*,void*), void * obj):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
394 out,in,read,waitなどのコマンドをCOMMANDキューに溜める。データを
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
395 受信するコマンド(in,read,wait)のときは受け取ったときにデータを
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
396 格納するREPLY構造体を作る。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
397
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
398 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
399 id - アクセスするTUPLE SpaceのID
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
400 size - dataのサイズ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
401 data - 送信するデータ。受信時はNULL。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
402 mode - コマンドのモード(out,in,read,wait は各々char型: o,i,r,w)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
403 callback - コールバックを使用する場合の関数へのポインタ。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
404 使用しない場合はNULL。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
405 obj - コールバックで用いる関数に引き渡すデータ。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
406 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
407 成功した場合 - mallocしたREPLY構造体へのポインタ。outの場合は
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
408 0が返る。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
409 失敗した場合 - FAIL(-1)が返る。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
410 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
411 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
412 psx_queue(unsigned int id, unsigned int size, unsigned char *data, char mode,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
413 void(*callback)(char *,void *), void * obj){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
414 REPLY *p;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
415 COMMAND *c;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
416
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
417 seq++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
418 if (qsize >= MAX_QUEUE){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
419 // PSX_Debug(("max queue: qsize=%d",qsize));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
420 psx_sync_n();
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
421 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
422
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
423 for(p=reply;p;p=p->next){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
424 if(p->seq == seq){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
425 printf("same seq number: %d\n",seq);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
426 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
427 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
428
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
429 if (q_top == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
430 if ((q_top = (COMMAND *) malloc (sizeof(COMMAND))) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
431 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
432 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
433 c = q_end = q_top;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
434 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
435 if ((q_end->next = (COMMAND *) malloc (sizeof(COMMAND))) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
436 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
437 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
438 c = q_end;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
439 q_end = q_end->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
440 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
441
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
442 // size は DATASIZE
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
443 if ((q_end->command = (unsigned char *) malloc(size+LINDA_HEADER_SIZE)) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
444 free(q_end);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
445 c->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
446 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
447 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
448
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
449 if (mode != 'o'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
450 if (reply == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
451 if ((reply = (REPLY *) malloc (sizeof(REPLY))) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
452 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
453 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
454 p = r_end = reply; p->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
455 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
456 if ((r_end->next = (REPLY *) malloc (sizeof(REPLY))) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
457 return(FAIL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
458 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
459 p = r_end->next; r_end = p; p->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
460 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
461 p->mode = '?';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
462 p->seq = (int)p; // 構造体のアドレスで識別
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
463 p->callback = callback;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
464 p->obj = obj;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
465 PSX_Debug(("psx_queue: seq %d reply %x p %x r_end %x",seq,reply,p,r_end));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
466 }else{
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
467 p=0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
468 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
469 q_end->command[LINDA_MODE_OFFSET] = mode;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
470
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
471 q_end->command[LINDA_ID_OFFSET] = id >> 8;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
472 q_end->command[LINDA_ID_OFFSET+1] = id & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
473
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
474 q_end->command[LINDA_SEQ_OFFSET] = ((int)p>>24) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
475 q_end->command[LINDA_SEQ_OFFSET+1] = ((int)p>>16) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
476 q_end->command[LINDA_SEQ_OFFSET+2] = ((int)p>>8) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
477 q_end->command[LINDA_SEQ_OFFSET+3] = ((int)p) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
478
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
479 q_end->command[LINDA_DATA_LENGTH_OFFSET] = (size>>24) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
480 q_end->command[LINDA_DATA_LENGTH_OFFSET+1] = (size>>16) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
481 q_end->command[LINDA_DATA_LENGTH_OFFSET+2] = (size>>8) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
482 q_end->command[LINDA_DATA_LENGTH_OFFSET+3] = (size) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
483
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
484 q_end->size = size+LINDA_HEADER_SIZE; /* command size */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
485 q_end->next = NULL; qsize++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
486 if (data && size>0) memcpy(q_end->command+LINDA_HEADER_SIZE, data, size);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
487 return((int)p);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
488 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
489
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
490 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
491 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
492 unix_chkserv ():
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
493 サーバからデータ(TUPLE)を受け取る。REPLY構造体にコールバック関数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
494 が指定されていればその関数を実行し、REPLY構造体をキューから取り
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
495 除く。コールバック関数が指定されていなければREPLY構造体にデータ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
496 を引き渡す。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
497 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
498 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
499 unix_chkserv(){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
500 int i,k,pkt,npkt;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
501 REPLY *r,*prev;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
502 int a;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
503 unsigned char * tuple = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
504
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
505 if((i=read(ps,&npkt,INT_SIZE))<0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
506 fprintf(stderr, "size read error! on fd:%d %s\n", ps,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
507 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
508 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
509 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
510 pkt = ntohl(npkt);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
511 DEB(printf("pkt: %d\n",pkt));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
512 DEB(fprintf(stdout, "psx_chkserv: queue number: %d , size = %d\n", i, pkt));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
513 if((tuple = (unsigned char *)malloc(pkt))==NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
514 fprintf(stderr,"allocate error! errno :%d %s",errno,strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
515 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
516 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
517 for(a=0;a<pkt;a+=i) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
518 if((i=unix_read_w(ps,tuple+a,pkt-a))<0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
519 fprintf(stderr, "psx_chkserv: read error! on i=%d pkt=%d %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
520 i, pkt, strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
521 exit(1);//close(ps);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
522 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
523 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
524
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
525 #ifdef COUNT_PACKET
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
526 count_packet('r');
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
527 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
528
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
529 i = tuple[LINDA_ID_OFFSET] * 256 + tuple[LINDA_ID_OFFSET+1]; /* id */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
530 k = (tuple[LINDA_SEQ_OFFSET] <<24) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
531 (tuple[LINDA_SEQ_OFFSET+1]<<16) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
532 (tuple[LINDA_SEQ_OFFSET+2]<<8) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
533 (tuple[LINDA_SEQ_OFFSET+3]); /* seq */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
534 PSX_Debug(("psx_chkserv: anser packet size = %d id %d seq %d", pkt,i,k));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
535 DEB(fprintf(stdout, "psx_chkserv: data from server: %s id=%d seq = %d\n", tuple, i, k));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
536 DEB (
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
537 for(p=reply;p;p=p->next) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
538 PSX_Debug(printf("psx_queue dump: seq %d mode %c %x %x",p->seq,p->mode,p,p->next));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
539 })
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
540
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
541 for(prev = NULL,r = reply; r; prev = r,r = r->next){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
542 DEB(fprintf(stdout,"seq: %d\n",r->seq);)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
543 if (r->seq == k){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
544 if(r->callback){ // call callback function
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
545 (*r->callback)(tuple,r->obj);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
546 if (prev == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
547 reply = r->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
548 if(r == r_end) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
549 r_end = r->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
550 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
551 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
552 prev->next = r->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
553 if(r == r_end) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
554 r_end = prev;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
555 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
556 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
557 free(r);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
558 }else{ // normal reply
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
559 PSX_Debug(("psx_chkserv: copy answer r %x seq %d",r,k));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
560 r->answer = tuple;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
561 r->mode = '!';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
562 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
563 break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
564 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
565 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
566 tuple = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
567 if (!r){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
568 DEB(fprintf(stdout, "unix_chkserv: accepted seq %d does not match. \n",k));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
569 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
570 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
571
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
572 void psx_free(unsigned char * tuple)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
573 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
574 // free(tuple - LINDA_HEADER_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
575 free(tuple);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
576 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
577
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
578 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
579 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
580 get_int(unsigned char * tuple, int offset):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
581 TUPLEのヘッダに格納された int型 のデータを得るための関数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
582 psx_get_datalength() と psx_get_seq() から呼ばれる。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
583
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
584 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
585 tuple - ヘッダ情報も含んだTUPLE。psx_reply()で得たものでもいい。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
586 offset - 取りだすデータのオフセット。LINDA_DATA_LENGTH_OFFSET
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
587 か LINDA_SEQ_OFFSET。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
588
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
589 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
590 指定したオフセットに格納されていた数値(int型)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
591 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
592 static
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
593 int get_int(unsigned char * tuple, int offset){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
594 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
595 i = (tuple[offset] <<24) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
596 (tuple[offset+1]<<16) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
597 (tuple[offset+2]<<8) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
598 (tuple[offset+3]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
599 return i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
600 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
601
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
602 int psx_get_datalength(unsigned char * tuple){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
603 return get_int(tuple,LINDA_DATA_LENGTH_OFFSET);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
604 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
605
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
606 unsigned char *psx_get_data(unsigned char * tuple){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
607 return tuple+LINDA_HEADER_SIZE;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
608 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
609
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
610 int psx_get_seq(unsigned char * tuple){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
611 return get_int(tuple,LINDA_SEQ_OFFSET);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
612 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
613
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
614 short psx_get_id(unsigned char * tuple){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
615 short s;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
616 s = tuple[LINDA_ID_OFFSET] * 256 +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
617 tuple[LINDA_ID_OFFSET+1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
618 return s;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
619 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
620
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
621 char psx_get_mode(unsigned char * tuple){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
622 return tuple[LINDA_MODE_OFFSET];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
623 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
624
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
625
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
626 /* end */