资源简介
可以拿到满分的网络代理,采用读者优先的方式避免竞争,避免了pipe broken造成的服务器中断。
代码片段和文件信息
#include
#include “csapp.h“
#include
//the const variable below are requested in the reference
static const char *user_agent_hdr = “User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n“;
static const char *connect_hdr = “Connection: close\r\n“;
static const char *proxy_hdr = “Proxy-Connection: close\r\n“;
static const char *connection_key = “Connection“;
static const char *user_agent_key= “User-Agent“;
static const char *proxy_connection_key = “Proxy-Connection“;
static const char *host_key = “Host“;
//the micro below show some string type and cache size
#define EOF_TYPE 1
#define HOST_TYPE 2
#define OTHER_TYPE 3
#define MAX_CACHE_SIZE 1049000
#define MAX_object_SIZE 102400
#define CACHE_NUM 10
int getType(char *buf);
void doit(int connfd);
void parseUri(char uri[]char hostname[]char path[]char port[]);
void build_http
(char *server_httpchar *hostnamechar*pathchar* portrio_t *clientrio);
void* thread(void *vargp);
void initCache();
void PreRead(int index);
void afterRead(int index);
void preWrite(int index);
void afterWrite(int index);
int findCache(char *url);
void updateLRU(int index);
int findSuitCache();
void writeCacheContent(char *urlchar* buf);
//the struct of a cacheLine for a file
typedef struct
{
char content[MAX_object_SIZE];
char url[MAXLINE];
int time;
int isEmpty;
int readCount;
int writeCount;
sem_t mutex;
sem_t w;
}cacheunit;
//the cache struct consist of ten cacheLine
typedef struct
{
cacheunit cacheUnit[CACHE_NUM];
}Cache;
Cache cache;
//record the overall time to update the cacheline time
int allTime=0;
//the flag is used to void compete of “allTime“
sem_t flag;
/*
the main function to connect the client and it is same to
the code in the page 672 of textbook
*/
int main(int argcchar **argv)
{
int listenfd*connfd;
char hostname[MAXLINE]port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
pthread_t tid;
Signal(SIGPIPE SIG_IGN);
Sem_init(&flag01);
// the cmd parameters fail
if (argc!=2){
fprintf(stderr “usage: %s \n“argv[0]);
exit(1);
}
initCache();
listenfd= open_listenfd(argv[1]);
while (1)
{
clientlen=sizeof(clientaddr);
connfd=malloc(sizeof(int));
*connfd=Accept(listenfd(SA*)&clientaddr&clientlen);
Getnameinfo((SA*)&clientaddrclientlenhostnameMAXLINE
portMAXLINE0);
printf(“Accepted connection from (%s %s)\n“hostnameport);
Pthread_create(&tidNULLthread(void*)connfd);
}
}
//create a new thread to run for servering the client
void *thread(void *vargo)
{
int connfd = *((int *)vargo);
Pthread_detach(pthread_self());
free(vargo);
doit(connfd);
Close(connfd);
return NULL;
}
//the main process to get the cmdparse the cmdfind the cache
//build the headerconnect to the server and return the content
//to th
相关资源
- labview魔方程序
- 一个labview仿真的函数信号发生器.vi
- labview编程软件滤波器以及编写程序设
- labview语音输入输出
- labview中的DAQ助手采集多个通道电压
- Labview与CH372CH375的通信测试
- LabVIEW钢琴实例
- 编程实现二维DCT变换
- silicon lab公司的收音IC SI47XX全套开发工
- 图像二值化
- 用FFT对信号进行频谱分析
- nachos-lab 02
- Tone-Reservation
- QGA 量子遗传算法
- 差分形式的阻滞增长模型
- 遗传算法的M文件
- LabVIEW入门与实战开发100例190153
- LABVIEW做的蓝牙数据发送程序.vi
- VCS lab 和 VCS lab guide
- 基于labview的变声器
- 基于labview的方差标准差计算(求方差
- smart和labview通讯(smart_connet.vi)
- LabVIEW 数据采集 模拟量+编码器(Daq
- LabVIEW 声音信号的采集与存储.vi
- LabVIEW步进电机控制.vi
- 多通道数据采集.vi(labview)
- labview智能电梯(6层电梯.vi)
- 用labview编写的一个神经网络Vi图
- 基于Labview的电子秤的设计.docx
- labview汽车仪表盘
评论
共有 条评论