forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web01.c
149 lines (120 loc) · 3.31 KB
/
web01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* include web1 */
#include "unpthread.h"
#include <thread.h> /* Solaris threads */
#define MAXFILES 20
#define SERV "80" /* port number or service name */
struct file {
char *f_name; /* filename */
char *f_host; /* hostname or IP address */
int f_fd; /* descriptor */
int f_flags; /* F_xxx below */
pthread_t f_tid; /* thread ID */
} file[MAXFILES];
#define F_CONNECTING 1 /* connect() in progress */
#define F_READING 2 /* connect() complete; now reading */
#define F_DONE 4 /* all done */
#define GET_CMD "GET %s HTTP/1.0\r\n\r\n"
int nconn, nfiles, nlefttoconn, nlefttoread;
void *do_get_read(void *);
void home_page(const char *, const char *);
void write_get_cmd(struct file *);
int
main(int argc, char **argv)
{
int i, n, maxnconn;
pthread_t tid;
struct file *fptr;
if (argc < 5)
err_quit("usage: web <#conns> <IPaddr> <homepage> file1 ...");
maxnconn = atoi(argv[1]);
nfiles = min(argc - 4, MAXFILES);
for (i = 0; i < nfiles; i++) {
file[i].f_name = argv[i + 4];
file[i].f_host = argv[2];
file[i].f_flags = 0;
}
printf("nfiles = %d\n", nfiles);
home_page(argv[2], argv[3]);
nlefttoread = nlefttoconn = nfiles;
nconn = 0;
/* end web1 */
/* include web2 */
while (nlefttoread > 0) {
while (nconn < maxnconn && nlefttoconn > 0) {
/* 4find a file to read */
for (i = 0 ; i < nfiles; i++)
if (file[i].f_flags == 0)
break;
if (i == nfiles)
err_quit("nlefttoconn = %d but nothing found", nlefttoconn);
file[i].f_flags = F_CONNECTING;
Pthread_create(&tid, NULL, &do_get_read, &file[i]);
file[i].f_tid = tid;
nconn++;
nlefttoconn--;
}
if ( (n = thr_join(0, &tid, (void **) &fptr)) != 0)
errno = n, err_sys("thr_join error");
nconn--;
nlefttoread--;
printf("thread id %d for %s done\n", tid, fptr->f_name);
}
exit(0);
}
/* end web2 */
/* include do_get_read */
void *
do_get_read(void *vptr)
{
int fd, n;
char line[MAXLINE];
struct file *fptr;
fptr = (struct file *) vptr;
fd = Tcp_connect(fptr->f_host, SERV);
fptr->f_fd = fd;
printf("do_get_read for %s, fd %d, thread %d\n",
fptr->f_name, fd, fptr->f_tid);
write_get_cmd(fptr); /* write() the GET command */
/* 4Read server's reply */
for ( ; ; ) {
if ( (n = Read(fd, line, MAXLINE)) == 0)
break; /* server closed connection */
printf("read %d bytes from %s\n", n, fptr->f_name);
}
printf("end-of-file on %s\n", fptr->f_name);
Close(fd);
fptr->f_flags = F_DONE; /* clears F_READING */
return(fptr); /* terminate thread */
}
/* end do_get_read */
/* include write_get_cmd */
void
write_get_cmd(struct file *fptr)
{
int n;
char line[MAXLINE];
n = snprintf(line, sizeof(line), GET_CMD, fptr->f_name);
Writen(fptr->f_fd, line, n);
printf("wrote %d bytes for %s\n", n, fptr->f_name);
fptr->f_flags = F_READING; /* clears F_CONNECTING */
}
/* end write_get_cmd */
/* include home_page */
void
home_page(const char *host, const char *fname)
{
int fd, n;
char line[MAXLINE];
fd = Tcp_connect(host, SERV); /* blocking connect() */
n = snprintf(line, sizeof(line), GET_CMD, fname);
Writen(fd, line, n);
for ( ; ; ) {
if ( (n = Read(fd, line, MAXLINE)) == 0)
break; /* server closed connection */
printf("read %d bytes of home page\n", n);
/* do whatever with data */
}
printf("end-of-file on home page\n");
Close(fd);
}
/* end home_page */