annotate Linda/ldserv.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 /**********************************************************************/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
2 /* */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
3 /* Linda Server for PlayStation */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
4 // $Id$
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
5 //
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
6 /* */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
7 /**********************************************************************/
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
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
27 #define MAX_REQ 1
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
28 #define FAIL (-1)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
29 #define MAX_USER 4
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
30 #define MAX_TUPLE 65536
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
31
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
32 char *PATHNAME = "/tmp/ldserv";
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
33
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
34 //#define DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
35
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
36
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
37 #define BUFFSIZE 65535
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
38
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
39 /*----------------------------------------------------------------------
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
40 パケットフォーマット
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
41 char short int int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
42 Mode + ID + Seq + Data_len + Padding + Data
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
43 0 1 3 7 11 12
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
44 ----------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
45 #define LINDA_MODE_OFFSET 0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
46 #define LINDA_ID_OFFSET 1
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
47 #define LINDA_SEQ_OFFSET 3
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
48 #define LINDA_DATA_LENGTH_OFFSET 7
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
49 #define LINDA_HEADER_SIZE 12
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
50
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
51
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
52 #define INT_SIZE 4 // Byte = sizeof(int)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
53
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
54 typedef struct tuple{ //構造体宣言
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
55 char *data;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
56 struct tuple *next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
57 unsigned int seq;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
58 unsigned short fd;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
59 unsigned int datalen;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
60 char mode;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
61 } TUPLE ;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
62
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
63 TUPLE *t[MAX_TUPLE]; // TUPLE Space
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
64 TUPLE *tp;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
65 int unix_port = 11511; // unix_portを指定。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
66 extern int errno;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
67
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
68 #ifdef COUNT_PACKET
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
69 // print packet count message per PRINT_INTERVAL sec
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
70 #define PRINT_INTERVAL 4
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
71
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
72 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
73 void
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
74 count_packet (char type):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
75 クライアントより受け取ったコマンドをカウントする関数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
76
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
77 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
78 type - 受け取ったコマンドの種類(char型: i,o,r,w)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
79 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
80 void count_packet(char type)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
81 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
82 static int out_packet=-1,read_packet=0,in_packet=0,wait_packet=0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
83 static struct timeval start,now,previous;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
84
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
85 if (out_packet == -1) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
86 gettimeofday(&start,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
87 gettimeofday(&previous,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
88 out_packet = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
89 printf("packet\tout\tread\tin\twait\ttime\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
90 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
91
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
92 if (type == 'o') { //受け取ったコマンドの種類によって分別。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
93 out_packet++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
94 } else if (type == 'r') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
95 read_packet++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
96 } else if (type == 'i') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
97 in_packet++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
98 } else if (type == 'w') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
99 wait_packet++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
100 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
101 fprintf(stderr,"No type in count_packet function\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
102 return;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
103 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
104
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
105 gettimeofday(&now,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
106 if ((now.tv_sec-previous.tv_sec) > PRINT_INTERVAL) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
107 printf("log\t%d\t%d\t%d\t%d\t%ld\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
108 out_packet,read_packet,in_packet,wait_packet,now.tv_sec-start.tv_sec);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
109 fflush(stdout);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
110
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
111 previous.tv_sec = now.tv_sec;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
112 out_packet = read_packet = in_packet = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
113 wait_packet = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
114 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
115 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
116 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
117
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
118 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
119 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
120 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
121 show_tuple ():
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
122 TUPLE Space にあるTUPLEをID順に表示する
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
123 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
124 void show_tuple()
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
125 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
126 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
127
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
128 static int toggle = -1;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
129 static struct timeval start,now,previous;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
130
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
131 TUPLE * tmp;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
132
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
133 if (toggle == -1) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
134 gettimeofday(&start,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
135 toggle = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
136 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
137 gettimeofday(&now,NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
138 printf("time **** %ld\n",now.tv_sec-start.tv_sec);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
139 fflush(stdout);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
140 previous.tv_sec = now.tv_sec;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
141
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
142 printf("\n******\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
143 for(i=0;i<MAX_TUPLE-1;i++){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
144 if(t[i]){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
145 printf("id: %d\t",i);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
146 for(tmp=t[i];tmp;tmp = tmp->next){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
147 printf("%c:%d\t",tmp->mode,tmp->seq);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
148 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
149 printf("\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
150 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
151 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
152 if(t[i]){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
153 printf("id: %d\t",i);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
154 for(tmp=t[i];tmp;tmp = tmp->next){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
155 printf("%c:%c%c\t",tmp->mode,tmp->data[0],tmp->data[1]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
156 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
157 printf("\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
158 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
159 printf("******\n\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
160 return;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
161 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
162 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
163
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
164 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
165 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
166 sz_send (int fd, unsigned char *buf, int size, int flag):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
167 クライアントへTUPLEを送る。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
168
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
169 引き数:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
170 fd - 送信先のファイルディスクリプタ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
171 buf - 送るデータ
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
172 size - bufのサイズ(byte)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
173 flag - 使用していない
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
174 返り値:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
175 送ったデータのbyte数
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
176 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
177 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
178 sz_send(int fd,unsigned char *buf,int size,int flag) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
179 int i,nsize;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
180 nsize = htonl(size);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
181 i = write(fd,&nsize,INT_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
182 i += write(fd,buf,size); // size は datasize + LINDA_HEADER_SIZE
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
183 return(i);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
184 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
185
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
186 /*-------------------------------------------------------------------/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
187 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
188 main (int argc,char *argv[]):
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
189 サーバのメイン。クライアントから送られて来た各コマンドに対応
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
190 した処理を行う。
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
191 /-------------------------------------------------------------------*/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
192 int
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
193 main(int argc,char *argv[])
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
194 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
195 int i, a, users;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
196 int ls,sk,fd,paddrlen,maxfds,id;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
197 int datasize;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
198 int skfg = 1;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
199 short user = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
200 unsigned char userchar[2];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
201 #ifdef UNIX_DOMAIN
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
202 struct sockaddr_un my_addr, peer;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
203 #else
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
204 struct sockaddr_in my_addr, peer;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
205 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
206 struct timeval zerotime;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
207 unsigned char ipaddr[4];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
208 unsigned char *buf = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
209 int len;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
210 int fduser[MAX_USER];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
211 fd_set readfds, suspect, tmpfd;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
212 char ch;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
213
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
214 extern char *optarg;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
215 extern int optind;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
216
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
217 while ((ch = getopt(argc, argv, "p:s:?")) != -1){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
218 switch(ch) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
219 case 'p':
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
220 unix_port = atoi(optarg);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
221 break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
222 case 's':
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
223 PATHNAME = optarg;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
224 break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
225 case '?':
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
226 default:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
227 fprintf(stderr,"usage: %s [-p port]\n",argv[0]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
228 break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
229 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
230 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
231
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
232 argc -= optind;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
233 argv += optind;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
234
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
235 zerotime.tv_sec = zerotime.tv_usec = 0L;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
236 users = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
237
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
238 #ifdef UNIX_DOMAIN
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
239 if ((ls = socket(AF_UNIX, SOCK_STREAM, 0)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
240 #else
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
241 if ((ls = socket(AF_INET, SOCK_STREAM, 0)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
242 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
243 fprintf(stderr, "socket open error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
244 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
245 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
246 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
247 if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR,(char *) &skfg, sizeof(skfg)) < 0){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
248 fprintf(stderr, "setsockopt error %d\n",errno);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
249 close(ls);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
250 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
251 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
252 #ifdef UNIX_DOMAIN
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
253 my_addr.sun_family = AF_UNIX;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
254 strcpy(my_addr.sun_path, PATHNAME);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
255 #else
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
256 my_addr.sin_family = AF_INET;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
257 my_addr.sin_port = unix_port;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
258 my_addr.sin_addr.s_addr = INADDR_ANY;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
259 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
260
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
261 if (bind(ls, (struct sockaddr *)&my_addr,sizeof(my_addr)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
262 fprintf(stderr, "socket binded address error! errno:%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
263 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
264 close(ls);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
265 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
266 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
267 if (listen(ls, MAX_REQ) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
268 fprintf(stderr, "list creat error! errno:%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
269 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
270 close(ls);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
271 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
272 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
273 paddrlen = sizeof(peer);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
274 maxfds = FD_SETSIZE;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
275 fd = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
276 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
277 maxfds = 8;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
278 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
279 FD_ZERO(&readfds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
280
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
281 // main loop
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
282 while(1){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
283 FD_SET(ls, &readfds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
284 tmpfd = readfds;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
285
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
286 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
287 show_tuple();
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
288 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
289 if ((sk = select(maxfds, &readfds, NULL, NULL, NULL)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
290 if (errno == EBADF){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
291 for(i = 0;i < maxfds;i++){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
292 FD_ZERO(&suspect);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
293 if (FD_ISSET(i, &readfds)){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
294 FD_SET(i, &suspect);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
295 if (select(maxfds, &suspect, NULL, NULL, &zerotime) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
296 fprintf(stdout, "%d descriptor clear",i);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
297 FD_CLR(i, &readfds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
298 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
299 FD_CLR(i, &suspect);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
300 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
301 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
302 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
303 fprintf(stderr, "select error! errno:%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
304 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
305 close(ls);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
306 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
307 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
308 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
309 while(1){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
310 fd = (fd + 1) % maxfds;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
311 if (FD_ISSET(fd, &readfds)) break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
312 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
313 readfds = tmpfd;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
314
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
315 // 新規の接続
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
316 if (fd == ls){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
317 if ((sk = accept(ls, (struct sockaddr *)&peer, &paddrlen)) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
318 fprintf(stderr, "connection accept error! errno:%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
319 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
320 close(ls);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
321 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
322 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
323 #ifndef UNIX_DOMAIN
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
324 if (peer.sin_family == AF_INET)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
325 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
326 int tmp = 1;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
327
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
328 setsockopt (sk, IPPROTO_TCP, TCP_NODELAY,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
329 (char *) &tmp, sizeof (int));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
330 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
331 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
332 if ((tp = t[MAX_TUPLE-1]) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
333 tp = t[MAX_TUPLE-1] = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
334 tp->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
335 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
336 while(tp->next) tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
337 tp->next = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
338 tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
339 tp->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
340 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
341 tp->mode = 'o';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
342 tp->seq = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
343 tp->data = (char *) malloc(sizeof(short));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
344 tp->datalen = sizeof(short); user++;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
345 userchar[0] = user / 10 + '0';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
346 userchar[1] = user % 10 + '0';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
347 memcpy(tp->data, &userchar ,sizeof(userchar));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
348
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
349 fprintf(stdout, "localhost connected assign id = %d\n", user);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
350 #ifndef UNIX_DOMAIN
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
351 memcpy(ipaddr, &peer.sin_addr.s_addr, sizeof(ipaddr));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
352 fprintf(stdout, "%d.%d.%d.%d connected \n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
353 ipaddr[0],ipaddr[1],ipaddr[2],ipaddr[3]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
354 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
355 FD_SET(sk,&readfds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
356
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
357 // 通信を確立しているクライアントからの要求
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
358 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
359 int tempnum;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
360 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
361 fprintf(stdout, "\n\ndata from %d descriptor\n", fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
362 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
363 if((len=read(fd,&tempnum,INT_SIZE))!=INT_SIZE) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
364 if (len==0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
365 fprintf(stdout, "fd %d closed\n", fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
366 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
367 continue;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
368 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
369 fprintf(stderr, "read error! on fd:%d ret=%d %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
370 fd,len,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
371 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
372 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
373 continue;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
374 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
375 len = ntohl(tempnum); // len は DATASIZE + LINDA_HEADER_SIZE
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
376 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
377 fprintf(stderr, "datta on fd:%d len=%d\n", fd,len);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
378 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
379 if((buf = (unsigned char*)malloc(len))==NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
380 fprintf(stderr,"allocate error! :%d %s",errno,strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
381 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
382 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
383 for(a=0;a<len;a+=i) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
384 if((i=read(fd,buf+a,len-a))<0) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
385 fprintf(stderr, "ldserv: client read error! on i=%d len=%d %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
386 i, len, strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
387 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
388 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
389 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
390 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
391 fprintf(stdout,"recv size = %d : mode = %c : strings = %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
392 len, *buf, buf + LINDA_HEADER_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
393 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
394
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
395 #ifdef COUNT_PACKET
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
396 count_packet(buf[0]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
397 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
398
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
399 if ((buf[LINDA_MODE_OFFSET] == '!') || (len == 0)){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
400 FD_CLR(fd, &readfds);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
401 for(i = 0;i < users; i++){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
402 if (fduser[i] == fd) break;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
403 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
404 fprintf(stdout, "connection closed descriptor :%d\n", fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
405 close(fd);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
406 } else if (buf[LINDA_MODE_OFFSET] == 'c'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
407 id = buf[LINDA_ID_OFFSET] * 256 + buf[LINDA_ID_OFFSET+1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
408 //buf[LINDA_MODE_OFFSET] = 'a';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
409 tp = t[id];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
410 while(tp && tp->next && (tp->mode=='w')){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
411 tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
412 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
413 if (tp && (tp->mode == 'o')){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
414 buf[LINDA_DATA_LENGTH_OFFSET] = (tp->datalen>>24) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
415 buf[LINDA_DATA_LENGTH_OFFSET+1] = (tp->datalen>>16) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
416 buf[LINDA_DATA_LENGTH_OFFSET+2] = (tp->datalen>>8) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
417 buf[LINDA_DATA_LENGTH_OFFSET+3] = (tp->datalen) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
418
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
419 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
420 /* means no out tuple */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
421 memset(&buf[LINDA_DATA_LENGTH_OFFSET],0,INT_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
422 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
423
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
424 if (sz_send(fd, buf, LINDA_HEADER_SIZE, 0) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
425 fprintf(stderr,"recv error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
426 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
427 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
428 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
429 fprintf(stdout,"send size= %d : mode = %c\n",len, *buf);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
430 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
431 } else if (buf[LINDA_MODE_OFFSET] == 'i' || buf[LINDA_MODE_OFFSET] == 'r'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
432
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
433 int mode = buf[LINDA_MODE_OFFSET];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
434
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
435 TUPLE * temp = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
436 id = buf[LINDA_ID_OFFSET] * 256 + buf[LINDA_ID_OFFSET+1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
437 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
438 fprintf(stdout, "***%c command : id = %d ***\n",mode,id);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
439 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
440 tp = t[id];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
441
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
442 // w は無視する
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
443 while(tp && tp->next && (tp->mode=='w')){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
444 temp = tp;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
445 tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
446 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
447
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
448 if (tp && (tp->mode == 'o')){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
449 if((buf = (unsigned char*)realloc(buf,tp->datalen+LINDA_HEADER_SIZE)) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
450 fprintf(stderr,"2 reallocate error! errno :%d %s",errno,strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
451 exit(1);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
452 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
453
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
454 buf[LINDA_MODE_OFFSET] = 'a';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
455 /* seq and id are already set */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
456 buf[LINDA_DATA_LENGTH_OFFSET] = (tp->datalen>>24) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
457 buf[LINDA_DATA_LENGTH_OFFSET+1] = (tp->datalen>>16) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
458 buf[LINDA_DATA_LENGTH_OFFSET+2] = (tp->datalen>>8) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
459 buf[LINDA_DATA_LENGTH_OFFSET+3] = (tp->datalen) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
460
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
461 memcpy(buf+LINDA_HEADER_SIZE, tp->data, tp->datalen);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
462 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
463 fprintf(stdout,"send size = %d : mode = %c : strings = %s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
464 len, *buf, buf + LINDA_HEADER_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
465 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
466 if (sz_send(fd, buf, (tp->datalen) + LINDA_HEADER_SIZE, 0) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
467 fprintf(stderr,"recv error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
468 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
469 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
470 if(mode == 'i') {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
471 free(tp->data);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
472 tp->data=0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
473 if(temp){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
474 temp->next = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
475 }else{
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
476 t[id] = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
477 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
478 free(tp);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
479
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
480 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
481 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
482 if (tp == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
483 tp = t[id] = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
484 tp->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
485 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
486 while(tp->next) tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
487 tp->next = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
488 tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
489 tp->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
490 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
491
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
492 tp->mode = mode;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
493 tp->seq = (buf[LINDA_SEQ_OFFSET] <<24) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
494 (buf[LINDA_SEQ_OFFSET+1]<<16) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
495 (buf[LINDA_SEQ_OFFSET+2]<<8) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
496 (buf[LINDA_SEQ_OFFSET+3]); /* seq */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
497 tp->fd = fd;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
498 tp->datalen = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
499 tp->data = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
500 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
501 fprintf(stdout, "data inserted insert seq = %d, id = %d\n"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
502 ,tp->seq,id);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
503 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
504 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
505 } else if (buf[LINDA_MODE_OFFSET] == 'w'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
506 int mode = buf[LINDA_MODE_OFFSET];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
507
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
508 id = buf[LINDA_ID_OFFSET] * 256 + buf[LINDA_ID_OFFSET+1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
509 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
510 fprintf(stdout, "***%c command : id = %d ***\n",mode,id);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
511 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
512 tp = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
513 tp->mode = mode;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
514 tp->seq = (buf[LINDA_SEQ_OFFSET] <<24) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
515 (buf[LINDA_SEQ_OFFSET+1]<<16) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
516 (buf[LINDA_SEQ_OFFSET+2]<<8) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
517 (buf[LINDA_SEQ_OFFSET+3]); /* seq */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
518 tp->fd = fd;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
519 tp->datalen = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
520 tp->data = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
521 tp->next = t[id];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
522 t[id] = tp;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
523 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
524 fprintf(stdout, "data inserted insert seq = %d, id = %d\n"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
525 ,t[id]->seq,id);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
526 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
527 } else if (buf[LINDA_MODE_OFFSET] == 'o'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
528 id = buf[LINDA_ID_OFFSET] * 256 + buf[LINDA_ID_OFFSET+1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
529
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
530 datasize = (buf[LINDA_DATA_LENGTH_OFFSET] <<24) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
531 (buf[LINDA_DATA_LENGTH_OFFSET+1]<<16) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
532 (buf[LINDA_DATA_LENGTH_OFFSET+2]<<8) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
533 (buf[LINDA_DATA_LENGTH_OFFSET+3]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
534 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
535 fprintf(stdout, "*** out command : seq = %d ***\n",id);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
536 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
537 while((t[id]) && ((t[id]->mode=='w')||(t[id]->mode == 'r'))){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
538
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
539 buf[LINDA_MODE_OFFSET] = 'a';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
540 buf[LINDA_SEQ_OFFSET] = (t[id]->seq>>24) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
541 buf[LINDA_SEQ_OFFSET+1] = (t[id]->seq>>16) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
542 buf[LINDA_SEQ_OFFSET+2] = (t[id]->seq>>8) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
543 buf[LINDA_SEQ_OFFSET+3] = (t[id]->seq) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
544 if (sz_send(t[id]->fd,buf,datasize+LINDA_HEADER_SIZE, 0) == FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
545 fprintf(stderr,"recv error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
546 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
547 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
548 tp = t[id];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
549 t[id] = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
550 free(tp);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
551 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
552 if ((t[id])&&t[id]->mode == 'i'){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
553 buf[LINDA_MODE_OFFSET] = 'a';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
554 buf[LINDA_SEQ_OFFSET] = (t[id]->seq>>24) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
555 buf[LINDA_SEQ_OFFSET+1] = (t[id]->seq>>16) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
556 buf[LINDA_SEQ_OFFSET+2] = (t[id]->seq>>8) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
557 buf[LINDA_SEQ_OFFSET+3] = (t[id]->seq) & 0xff;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
558 if (sz_send(t[id]->fd,buf,datasize+LINDA_HEADER_SIZE,0)==FAIL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
559 fprintf(stderr,"recv error! errno :%d %s\n", errno,
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
560 strerror(errno));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
561 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
562 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
563 fprintf(stdout,"sendsize = %d :mode = %c :strings=%s\n",
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
564 len, *buf, buf + LINDA_HEADER_SIZE);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
565 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
566 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
567 fprintf(stdout, "data senddata =%s,id=%d ,mode=%c,len=%d \n"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
568 ,tp->data, id, t[id]->mode, t[id]->datalen);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
569 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
570 // we should not free waiting in-packet's data
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
571 tp = t[id]; t[id] = tp->next; free(tp);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
572 } else if ((t[id]==NULL) || (t[id]->mode == 'o')){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
573 if ((tp = t[id]) == NULL){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
574 tp = t[id] = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
575 tp->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
576 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
577 while(tp->next) tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
578 tp->next = (TUPLE *) malloc(sizeof(TUPLE));
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
579 tp = tp->next;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
580 tp->next = NULL;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
581 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
582 tp->mode = 'o';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
583 tp->seq =(buf[LINDA_SEQ_OFFSET] <<24) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
584 (buf[LINDA_SEQ_OFFSET+1]<<16) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
585 (buf[LINDA_SEQ_OFFSET+2]<<8) +
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
586 (buf[LINDA_SEQ_OFFSET+3]);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
587 tp->data = (char *) malloc(datasize);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
588 tp->datalen = datasize;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
589
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
590 memcpy(tp->data, buf+LINDA_HEADER_SIZE,datasize);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
591 #ifdef DEB
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
592 fprintf(stdout, "data inserted data = %s, len = %d, id = %d\n"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
593 ,tp->data,tp->datalen,id);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
594 #endif
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
595 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
596 fprintf(stdout,"Uncorrect mode : %c\n",t[id]->mode);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
597 return(0);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
598 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
599 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
600 fprintf(stdout,"Uncorrect buffer\n");
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
601 if(buf) free(buf);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
602 return(0);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
603 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
604 free(buf);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
605 buf = 0;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
606 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
607 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
608 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
609 close(ls);/* conection terminat */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
610 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
611