-
Notifications
You must be signed in to change notification settings - Fork 0
/
trial.ico
190 lines (183 loc) · 4.95 KB
/
trial.ico
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
#include "fileEdit.h"
#include "dealer.h"
#include "account.h"
#include "httpServer.h"
#include <time.h>
#include <pthread.h>
#define READ_LENGTH 100
int servfd = -1;
httpInfo tempInfo;
bool tryGzip;
char *tempo,*resulta,*finalResult,*bufPointer;
static char buf[20480];
pthread_t id1,id2,id3,id4;
pthread_mutex_t lock;
int type;
pthread_cond_t answerRequest,receiveRequest;
static const char unacceptedInfo[]="HTTP/1.1 200 OK\r\nContent-Length: 11\r\nContent-Type: text/html\r\n\r\nPlease wait";
int resultSize;
void sigint_handler(int signum) {
printf("keyboard interrupt detected with number %d, close server\n",signum);
close(servfd);
exit(0);
}
const int queueSize=10;
int volatile fdQueue[queueSize];
int volatile queueNumber=0;
int volatile queueFrontPointer,queueEndPointer;
bool isFull()
{
return queueFrontPointer==0?(queueEndPointer==9):(queueEndPointer==queueFrontPointer-1);
}
bool isEmpty(){return queueFrontPointer==queueEndPointer;}
void enQueue(int fd)
{
fdQueue[queueFrontPointer++]=fd;
queueFrontPointer%=queueSize;
queueNumber++;
}
int deQueue()
{
int ret=fdQueue[queueEndPointer];
queueEndPointer=(queueEndPointer+1)%queueSize;
queueNumber--;
return ret;
}
void initServer(int argc,char *argv[])
{
servfd = socket(AF_INET, SOCK_STREAM, 0);
signal(SIGINT, sigint_handler);
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8080);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
tryGzip=true;
int bindTimes=1;
int bindResult=bind(servfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
if(argv[1]==NULL){
tryGzip=false;
}
else if(strcmp(argv[1],"GZIP_ON")==0){
tryGzip=true;
}
else if(strcmp(argv[1],"GZIP_OFF")==0){
tryGzip=false;
}
else{
printf("Unknown command %s! Server exited\n",argv[1]);
exit(EXIT_FAILURE);
}
while(bindResult<0){
bindResult=bind(servfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
bindTimes++;
}
printf("Argc:%d\n",argc);
for(int i=0;i<3;i++){
printf("Arg #%d:%s\n",i+1,argv[i]);
}
printf("Bind done after %d times!\n",bindTimes);
}
void *ht_client(void *args)
{
while(true){
pthread_mutex_lock(&lock);
printf("QUEUE NUMBER in client:%d %u\n",queueNumber,pthread_self());
while(queueNumber==10)
pthread_cond_wait(&answerRequest,&lock);
printf("AFER CHECK ANS IN CLIENT\n");
listen(servfd, 50);
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
memset(buf,0,sizeof(buf));
int conn = accept(servfd, (struct sockaddr*)&client_addr, &length);
printf("TO HERE\n");
if(conn>=0){
printf("Accepted with conn:%d\n",conn);
enQueue(conn);
bufPointer=buf;
ssize_t readLength=read(conn,(void *)bufPointer,READ_LENGTH);
while(readLength==READ_LENGTH){
bufPointer+=readLength;
readLength=read(conn,(void *)bufPointer,READ_LENGTH);
}
pthread_cond_signal(&receiveRequest);
}
printf("DONE RECEIVING %d\n",conn);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void *ht_server(void *args)
{
printf("IN SERVER\n");
while(true){
pthread_mutex_lock(&lock);
printf("QUEUE NUMBER in server:%d %u\n",queueNumber,pthread_self());
while(queueNumber==0)
pthread_cond_wait(&receiveRequest,&lock);
int fd=deQueue();
printf("FD in server:%d\n",fd);
bufPointer=buf;
ssize_t readLength=read(fd,(void *)bufPointer,READ_LENGTH);
while(readLength==READ_LENGTH){
bufPointer+=readLength;
readLength=read(fd,(void *)bufPointer,READ_LENGTH);
}
tempInfo=analyzeExplorer(buf);
show(tempInfo);
tempo=analyzeInput(buf);
printf("Temp:%s\n",tempo);
if(strlen(buf)==0){
write(fd,unacceptedInfo,strlen(unacceptedInfo ));
close(fd);
memset(buf,0,sizeof(buf ));
continue;
}
memset(buf,0,sizeof(buf ));
prepare(tempInfo);
resulta=editMessage(tempo,&type);
if(type==LOADFILE)
finalResult=writeFileResponse(resulta,tryGzip);
else
finalResult=writeSimpleResponse(resulta);
printf("Result:\n%s afer writ respons with length %d\n",finalResult,resultSize);
[[maybe_unused]]
int len=write(fd, finalResult, resultSize);
close(fd);
pthread_cond_signal(&answerRequest);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void initThreads()
{
pthread_mutex_init(&lock,NULL);
pthread_cond_init(&answerRequest,NULL);
pthread_cond_init(&receiveRequest,NULL);
int ret;
int arg=5;
ret=pthread_create(&id1,NULL,ht_server,NULL);
assert(ret==0);
ret=pthread_create(&id2,NULL,ht_server,NULL);
assert(ret==0);
ret=pthread_create(&id3,NULL,ht_client,NULL);
assert(ret==0);
ret=pthread_create(&id4,NULL,ht_client,NULL);
assert(ret==0);
queueFrontPointer=queueEndPointer=0;
queueNumber=0;
assert(isEmpty());
}
int main(int argc,char *argv[]) {
printf("Present process id:%d\n",getpid());
init();
initServer(argc,argv);
initThreads();
printf("ALL INITIALIZATION DONE\n");
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
pthread_join(id4,NULL);
return 0;
}