/
alpha.c
203 lines (168 loc) · 4.48 KB
/
alpha.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include"crawler.h"
#include"queue.h"
#include"parse.h"
#include"rio.h"
char localhost[]="127.0.0.1";
int port=6379;
void *do_get_read(void*);
void home_page(char *, char*);
void write_get_cmd(struct file *);
static struct redisContext *select_database(redisContext *c) {
redisReply *reply;
reply = redisCommand(c,"SELECT 9");
freeReplyObject(reply);
reply = redisCommand(c,"DBSIZE");
if (reply == NULL)
printf("reply is null\n");
if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0) {
/* Awesome, DB 9 is empty and we can continue. */
freeReplyObject(reply);
} else {
printf("Database #9 is not empty, test can not continue\n");
exit(1);
}
return c;
}
static redisContext *connectRedis(char* host, int port) {
redisContext *c = NULL;
c = redisConnect(host,port);
if (c->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}
return select_database(c);
}
static void disconnectRedis(redisContext *c) {
redisReply *reply;
/* Make sure we're on DB 9. */
reply = redisCommand(c,"SELECT 9");
freeReplyObject(reply);
reply = redisCommand(c,"FLUSHDB");
freeReplyObject(reply);
/* Free the context as well. */
redisFree(c);
}
int main(int argc, char **argv)
{
int i, maxnconn;
pthread_t tid;
struct file *fptr;
int current;
listNode *listNode;
for (i = 0; i < MAXFILES; i++) {
file[i].f_flags = 0;
}
context = connectRedis(localhost,port);
queue = listCreate();
home_page(HOST, "/");
nlefttoread = nlefttoconn = queue->len;
nconn = 0;
while (nlefttoconn > 0 || nconn > 0) {
printf("lefttoconn: %d, conn:%d\n",nlefttoconn,nconn);
current = 0;
while (nconn < MAXFILES && nlefttoconn > 0) {
for (i = current ; i < MAXFILES; i++)
if (file[i].f_flags == 0)
{
current = i;
break;
}
pthread_mutex_lock(&queue_mutex);
listNode = queue->head;
if (listNode == NULL)
{
printf("have some but no!\n");
exit(1);
}
file[i].f_flags = F_CONNECTING;
setFileNameAndHost(listNode->value,file[i].f_name,file[i].f_host);
listDelNodeHead(queue);
pthread_create(&tid, NULL, &do_get_read, &file[i]);
file[i].f_tid = tid;
nconn++;
nlefttoconn = queue->len;
pthread_mutex_unlock(&queue_mutex);
}
pthread_mutex_lock(&ndone_mutex);
while (ndone == 0)
pthread_cond_wait(&ndone_cond, &ndone_mutex);
for (i = 0; i < MAXFILES; i++) {
if (file[i].f_flags & F_DONE) {
pthread_join(file[i].f_tid, (void **) &fptr);
if (&file[i] != fptr)
{
printf("file[i]!=ptr\n");
exit(1);
}
fptr->f_flags = 0; /* clears F_DONE */
ndone--;
nconn--;
printf("thread %d for name:%s host:%s done\n", fptr->f_tid, fptr->f_name,fptr->f_host);
}
}
pthread_mutex_unlock(&ndone_mutex);
}
listRelease(queue);
disconnectRedis(context);
exit(0);
}
void *do_get_read(void *vptr)
{
int fd, n;
char line[MAXLINE];
struct file *fptr;
rio_t rio;
fptr = (struct file *) vptr;
fd = tcpConnect(fptr->f_host, SERV);
fptr->f_fd = fd;
printf("do_get_read for name:%s host:%s, fd %d, thread %d\n",
fptr->f_name,fptr->f_host, fd, fptr->f_tid);
write_get_cmd(fptr); /* write() the GET command */
rio_readinitb(&rio,fd);
while((n = rio_readlineb(&rio,line,sizeof(line))) > 1)
{
printf("read %d bytes from name:%s host:%s\n", n, fptr->f_name,fptr->f_host);
if (strcmp(line, END_OF_HTML) == 0)
{
break;
}
parseUrl(line,fptr->f_host);
}
printf("end-of-file on name:%s host:%s\n", fptr->f_name,fptr->f_host);
close(fd);
fptr->f_flags = F_DONE; /* clears F_READING */
pthread_mutex_lock(&ndone_mutex);
ndone++;
pthread_cond_signal(&ndone_cond);
pthread_mutex_unlock(&ndone_mutex);
return(fptr); /* terminate thread */
}
void write_get_cmd(struct file *fptr)
{
int n;
char line[MAXLINE];
n = snprintf(line, sizeof(line), GET_CMD, fptr->f_name, fptr->f_host);
rio_writen(fptr->f_fd, line, n);
printf("wrote %d bytes for name:%s host:%s\n", n, fptr->f_name,fptr->f_host);
fptr->f_flags = F_READING; /* clears F_CONNECTING */
}
void home_page(char *host, char *fname)
{
int fd, n;
char line[MAXLINE];
rio_t rio;
fd = tcpConnect(host, SERV); /* blocking connect() */
n = snprintf(line, sizeof(line), GET_CMD, fname,host);
rio_writen(fd, line, n);
rio_readinitb(&rio, fd);
while ((n = rio_readlineb(&rio, line, sizeof(line))) > 1)
{
if (strcmp(line,END_OF_HTML) == 0)
{
break;
}
parseUrl(line,host);
}
printf("end-of-file on home page\n");
close(fd);
}