爬虫larbin主函数说明

来源:互联网 发布:vb自动登录弹出窗口 编辑:程序博客网 时间:2024/06/11 18:27

int main (int argc, char *argv[]) {
global glob(argc, argv); //创建一个global对象 亦即是初始化所要使用到的所有变量及初始配置。这里包括对larbin.conf配置文件的解析

//解析时将取得的起始url放入global::URLsPriority 或者global::URLsDisk 中以待爬取

#ifdef PROF
signal (2, handler);//如果执行即到handler函数中执行 在这里就是将主程序停止标志

#endif // PROF

#ifndef NOWEBSERVER
// launch the webserver if needeed
if (global::httpPort != 0)
startThread(startWebserver, NULL);//larbin.conf中httpPort非零 则意味着可以使用浏览器查看larbin运行状态。通过一个线程启动浏览器

#endif // NOWEBSERVER

// Start the search
printf("%s is starting its search\n", global::userAgent);
time_t old = global::now;

for (;;) {//开始不断循环执行爬取
// update time
global::now = time(NULL);
if (old != global::now) {//这意味着循环一次都要执行一次这段代码
// this block is called every second
old = global::now;
cron();//cron主要是对larbin是否终止、对url请求是否超时、以及一些统计信息用于状态输出使用

}
stateMain(-count); //where is the main loop
waitBandwidth(&old);
stateMain(1);
for (int i=0; i<global::maxFds; i++)//105
global::ansPoll[i] = 0;//初始化I/O复用监测标志组。0是没用响应

for (uint i=0; i<global::posPoll; i++)
global::ansPoll[global::pollfds[i].fd] = global::pollfds[i].revents;/*更新I/O复用监测标志组,设置为其监控对象的返回状态,

*在这里的对象就是打开的socket的句柄了。非零及可以读取了

*/
global::posPoll = 0;
stateMain(2);
input();//larbin运行过程中是否手动添加url了。有则进行手动添加url的处理
stateMain(3);
sequencer();/*将global 中url队列(global::URLsPriority || global::URLsPriorityWait || *global::URLsDisk || global::URLsDiskWait;)中的url

*/放入到这个global::namedSiteList[u->hostHashCode()] nameSIite中的url队列中

stateMain(4);
fetchDns();//从dnsSites中取得一个需要进行dns请求的站点并发起请求并取得站点的robot.txt

stateMain(5);
fetchOpen();//从OKSite队列取得一个namesite对其内的url进行爬行
stateMain(6);
checkAll();/* 检查是否已爬取的结果并对结果的处理:为robots.txt解析并筛选站点的url爬行队列;同时处理同一个站点内的rul列表进行

* 过 滤if为同一个站点内的url则将其放入到global::IPSiteList[ipHash].putUrl(u)中 同时将这个IPSite放入到OKSite列表

*否则处理的就是html,解析html和存储html的内容了。取得新的url 满足条件放入global::URLsPriority 或者global::URLsDisk

*/
// select
stateMain(count++);
poll(global::pollfds, global::posPoll, 10);//将pollfds中的句柄放入到设备内部等待队列中挂起 等待10毫秒或有事件发生则返回
stateMain(7);
}
}

原创粉丝点击