中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

          Java專(zhuān)題文章博客和開(kāi)源

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          libevent庫(kù)定時(shí)器的使用

            libevent庫(kù)定時(shí)器的使用

            #include

            #include

            #include

            #include

            #include

            #include

            #include

            #include

            #define RELOAD_TIMEOUT 5

            #define DEFAULT_FILE "sample.html"

            char *filedata;

            time_t lasttime = 0;

            char filename[80];

            int counter = 0;

            void read_file()

            {

            int size = 0;

            char *data;

            struct stat buf;

            stat(filename,&buf);

            if (buf.st_mtime > lasttime)

            {

            if (counter++)

            fprintf(stderr,"Reloading file: %s",filename);

            else

            fprintf(stderr,"Loading file: %s",filename);

            FILE *f = fopen(filename, "rb");

            if (f == NULL)

            {

            fprintf(stderr,"Couldn't open file\n");

            exit(1);

            }

            fseek(f, 0, SEEK_END);

            size = ftell(f);

            fseek(f, 0, SEEK_SET);

            data = (char *)malloc(size+1);

            fread(data, sizeof(char), size, f);

            filedata = (char *)malloc(size+1);

            strcpy(filedata,data);

            fclose(f);

            fprintf(stderr," (%d bytes)\n",size);

            lasttime = buf.st_mtime;

            }

            }

            void load_file()

            {

            struct event *loadfile_event;

            struct timeval tv;

            read_file();

            tv.tv_sec = RELOAD_TIMEOUT;

            tv.tv_usec = 0;

            loadfile_event = malloc(sizeof(struct event));

            evtimer_set(loadfile_event,

            load_file,

            loadfile_event);

            evtimer_add(loadfile_event,

            &tv);

            }

            void generic_request_handler(struct evhttp_request *req, void *arg)

            {

            struct evbuffer *evb = evbuffer_new();

            evbuffer_add_printf(evb, "%s",filedata);

            evhttp_send_reply(req, HTTP_OK, "Client", evb);

            evbuffer_free(evb);

            }

            int main(int argc, char *argv[])

            {

            short http_port = 8081;

            char *http_addr = "192.168.0.22";

            struct evhttp *http_server = NULL;

            if (argc > 1)

            {

            strcpy(filename,argv[1]);

            printf("Using %s\n",filename);

            }

            else

            {

            strcpy(filename,DEFAULT_FILE);

            }

            event_init();

            load_file();

            http_server = evhttp_start(http_addr, http_port);

            evhttp_set_gencb(http_server, generic_request_handler, NULL);

            fprintf(stderr, "Server started on port %d\n", http_port);

            event_dispatch();

            }

            這個(gè)服務(wù)器的基本原理與前面的示例相同。首先,腳本設(shè)置一個(gè) HTTP 服務(wù)器,它只響應(yīng)對(duì)基本 URL 主機(jī)/端口組合的請(qǐng)求(不處理請(qǐng)求 URI)。第一步是裝載文件 (read_file())。在裝載最初的文件時(shí)和在計(jì)時(shí)器觸發(fā)回調(diào)時(shí)都使用此函數(shù)。

            read_file() 函數(shù)使用 stat() 函數(shù)調(diào)用檢查文件的修改時(shí)間,只有在上一次裝載之后修改了文件的情況下,它才重新讀取文件的內(nèi)容。此函數(shù)通過(guò)調(diào)用 fread() 裝載文件數(shù)據(jù),把數(shù)據(jù)復(fù)制到另一個(gè)結(jié)構(gòu)中,然后使用 strcpy() 把數(shù)據(jù)從裝載的字符串轉(zhuǎn)移到全局字符串中。

            load_file() 函數(shù)是觸發(fā)計(jì)時(shí)器時(shí)調(diào)用的函數(shù)。它通過(guò)調(diào)用 read_file() 裝載內(nèi)容,然后使用 RELOAD_TIMEOUT 值設(shè)置計(jì)時(shí)器,作為嘗試裝載文件之前的秒數(shù)。libevent 計(jì)時(shí)器使用 timeval 結(jié)構(gòu),允許按秒和毫秒指定計(jì)時(shí)器。計(jì)時(shí)器不是周期性的;當(dāng)觸發(fā)計(jì)時(shí)器事件時(shí)設(shè)置它,然后從事件隊(duì)列中刪除事件。sat答案

            使用與前面的示例相同的格式編譯代碼:$ gcc -o basichttpfile basichttpfile.c -levent。

            現(xiàn)在,創(chuàng)建作為數(shù)據(jù)使用的靜態(tài)文件;默認(rèn)文件是 sample.html,但是可以通過(guò)命令行上的第一個(gè)參數(shù)指定任何文件(見(jiàn) 清單 6)。

            清單 6. 創(chuàng)建作為數(shù)據(jù)使用的靜態(tài)文件

            $ ./basichttpfile

            Loading file: sample.html (8046 bytes)

            Server started on port 8081

            現(xiàn)在,程序可以接受請(qǐng)求了,重新裝載計(jì)時(shí)器也啟動(dòng)了。如果修改 sample.html 的內(nèi)容,應(yīng)該會(huì)重新裝載此文件并在日志中記錄一個(gè)消息。例如,清單 7 中的輸出顯示初始裝載和兩次重新裝載:

            清單 7. 輸出顯示初始裝載和兩次重新裝載

            $ ./basichttpfile

            Loading file: sample.html (8046 bytes)

            Server started on port 8081

            Reloading file: sample.html (8047 bytes)

            Reloading file: sample.html (8048 bytes)

            注意,要想獲得最大的收益,必須確保環(huán)境沒(méi)有限制打開(kāi)的文件描述符數(shù)量。可以使用 ulimit 命令修改限制(需要適當(dāng)?shù)臋?quán)限或根訪(fǎng)問(wèn))。具體的設(shè)置取決與您的 OS,但是在 Linux? 上可以用 -n 選項(xiàng)設(shè)置打開(kāi)的文件描述符(和網(wǎng)絡(luò)套接字)的數(shù)量:托福答案
           

          posted on 2014-01-03 23:11 好不容易 閱讀(303) 評(píng)論(1)  編輯  收藏

          評(píng)論

          # re: libevent庫(kù)定時(shí)器的使用 2014-01-10 14:59 笑面虎

          huigongzuo.com丨51fsk.com | peixunt.com | luwuyou.com
            回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          PK10開(kāi)獎(jiǎng) PK10開(kāi)獎(jiǎng)
          主站蜘蛛池模板: 三台县| 金沙县| 张家界市| 科尔| 扎赉特旗| 阳西县| 朝阳市| 如皋市| 昌邑市| 梓潼县| 遵义县| 巴彦淖尔市| 临夏县| 杭锦旗| 兴海县| 吉隆县| 河南省| 泗阳县| 黔东| 临武县| 无为县| 贵德县| 溆浦县| 广德县| 永胜县| 高邮市| 岳普湖县| 潜江市| 天祝| 泗洪县| 台北县| 昔阳县| 富蕴县| 呼和浩特市| 饶阳县| 七台河市| 永吉县| 龙南县| 福鼎市| 温州市| 广丰县|