中文JAVA技術平等自由協作創造

          Java專題文章博客和開源

          常用鏈接

          統計

          最新評論

          libevent庫定時器的使用

            libevent庫定時器的使用

            #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();

            }

            這個服務器的基本原理與前面的示例相同。首先,腳本設置一個 HTTP 服務器,它只響應對基本 URL 主機/端口組合的請求(不處理請求 URI)。第一步是裝載文件 (read_file())。在裝載最初的文件時和在計時器觸發回調時都使用此函數。

            read_file() 函數使用 stat() 函數調用檢查文件的修改時間,只有在上一次裝載之后修改了文件的情況下,它才重新讀取文件的內容。此函數通過調用 fread() 裝載文件數據,把數據復制到另一個結構中,然后使用 strcpy() 把數據從裝載的字符串轉移到全局字符串中。

            load_file() 函數是觸發計時器時調用的函數。它通過調用 read_file() 裝載內容,然后使用 RELOAD_TIMEOUT 值設置計時器,作為嘗試裝載文件之前的秒數。libevent 計時器使用 timeval 結構,允許按秒和毫秒指定計時器。計時器不是周期性的;當觸發計時器事件時設置它,然后從事件隊列中刪除事件。sat答案

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

            現在,創建作為數據使用的靜態文件;默認文件是 sample.html,但是可以通過命令行上的第一個參數指定任何文件(見 清單 6)。

            清單 6. 創建作為數據使用的靜態文件

            $ ./basichttpfile

            Loading file: sample.html (8046 bytes)

            Server started on port 8081

            現在,程序可以接受請求了,重新裝載計時器也啟動了。如果修改 sample.html 的內容,應該會重新裝載此文件并在日志中記錄一個消息。例如,清單 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)

            注意,要想獲得最大的收益,必須確保環境沒有限制打開的文件描述符數量。可以使用 ulimit 命令修改限制(需要適當的權限或根訪問)。具體的設置取決與您的 OS,但是在 Linux? 上可以用 -n 選項設置打開的文件描述符(和網絡套接字)的數量:托福答案
           

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

          評論

          # re: libevent庫定時器的使用 2014-01-10 14:59 笑面虎

          huigongzuo.com丨51fsk.com | peixunt.com | luwuyou.com
            回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          PK10開獎 PK10開獎
          主站蜘蛛池模板: 普洱| 邓州市| 宝鸡市| 安宁市| 武夷山市| 连云港市| 镇远县| 丰宁| 扶沟县| 榆中县| 梨树县| 长春市| 德江县| 庄浪县| 合水县| 博湖县| 页游| 平利县| 武陟县| 鄂托克旗| 东方市| 十堰市| 买车| 金堂县| 松江区| 邢台市| 临夏县| 明水县| 湛江市| 台湾省| 上饶市| 沙坪坝区| 霍山县| 广州市| 米脂县| 宁陕县| 宜兰市| 拉孜县| 鄯善县| 旬邑县| 雅安市|