前端控制器和 dispatcher 模式

foo_catnap 源码分析

foo_catpap 是一个 foobar2000 插件,用于通过 http 协议的方式控制播放器。

此工会使用了 Poco 库,使用了 Poco::Net 模块,简单的做一个 http 服务器

CatNapServer.cpp

#include "stdafx.h"

#include "CatNapServer.h"

#include "Poco/Net/HTTPServer.h"

#include "Poco/Net/HTTPServerParams.h"

#include <iostream>

#include "CatNapRequestHandlerFactory.h"

using Poco::Net::HTTPServer;

using Poco::Net::HTTPServerParams;

namespace CatNap {

CatNapServer::CatNapServer() : server(NULL)

{

}

void CatNapServer::start()

{

unsigned short port = 20402; // 0x4FB2

HTTPServerParams *params = new HTTPServerParams();

server = new HTTPServer(new CatNapRequestHandlerFactory(), port, params);

server->start();

}

void CatNapServer::stop()

{

if (server != NULL)

{

server->stop();

delete server;

}

}

} // namespace CatNap

CatNapRequestHandlerFactory.cpp

#include "stdafx.h"

#include "CatNapRequestHandlerFactory.h"

#include "RootRequestHandler.h"

#include "PlaybackEventsRequestHandler.h"

#include "PlaylistsRequestHandler.h"

#include "PlaylistsEventsRequestHandler.h"

using Poco::Net::HTTPRequestHandler;

using Poco::Net::HTTPServerRequest;

namespace CatNap {

CatNapRequestHandlerFactory::CatNapRequestHandlerFactory()

{

}

HTTPRequestHandler* CatNapRequestHandlerFactory::createRequestHandler(const HTTPServerRequest& request)

{

if (request.getURI() == "/")

return new RootRequestHandler();

else if (request.getURI() == "/playback/events")

return new PlaybackEventsRequestHandler(playbackFacade);

else if (request.getURI() == "/playlists")

return new PlaylistsRequestHandler();

else if (request.getURI() == "/playlists/events")

return new PlaylistsEventsRequestHandler();

else

return 0;

}

} // namespace CatNap

简单的生成一个 http 服务器,使用工厂来路由。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容