Skip to content

Commit

Permalink
patch for pull request #50
Browse files Browse the repository at this point in the history
  • Loading branch information
cyshi committed Jan 4, 2016
1 parent ee72efe commit 569e8de
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 106 deletions.
16 changes: 16 additions & 0 deletions sample/echo/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
#include <sofa/pbrpc/pbrpc.h>
#include "echo_service.pb.h"

bool WebServlet(const sofa::pbrpc::HTTPRequest& request, sofa::pbrpc::HTTPResponse& response)
{
std::map<std::string, std::string>::const_iterator it = request.headers.begin();
for (; it != request.headers.end(); ++it)
{
SLOG(INFO, "%s:%s", it->first.c_str(), it->second.c_str());
}
response.content = "<h1>Hello from sofa-pbrpc web server</h1>";
response.status_line = "HTTP/1.1 200 OK";
return true;
}

class EchoServerImpl : public sofa::pbrpc::test::EchoServer
{
public:
Expand Down Expand Up @@ -51,6 +63,9 @@ int main()
options.work_thread_dest_func = sofa::pbrpc::NewPermanentExtClosure(&thread_dest_func);
sofa::pbrpc::RpcServer rpc_server(options);

sofa::pbrpc::Servlet servlet = sofa::pbrpc::NewPermanentExtClosure(&WebServlet);
rpc_server.RegisterWebServlet("rpc", servlet);

// Start rpc server.
if (!rpc_server.Start("0.0.0.0:12321")) {
SLOG(ERROR, "start server failed");
Expand All @@ -72,6 +87,7 @@ int main()

// Delete closures.
// Attention: should delete the closures after server stopped, or may be crash.
delete servlet;
delete options.work_thread_init_func;
delete options.work_thread_dest_func;

Expand Down
42 changes: 29 additions & 13 deletions src/sofa/pbrpc/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
namespace sofa {
namespace pbrpc {

/**
* @brief request from http client, include http headers, query_params, and so
* on.
*/
struct HTTPRequest
{
enum Type
Expand All @@ -21,44 +25,56 @@ struct HTTPRequest
POST = 2
};

Type type;
Type type; // method in http header

std::map<std::string, std::string> headers;
std::map<std::string, std::string> headers; // http request headers

std::map<std::string, std::string> query_params;
std::map<std::string, std::string> query_params; // query parameters
// http://www.baidu.com/s?k=123
// will be parsed to {"k":"123"}

std::string path;
std::string path; // PATH field in http request
// for example, http://www.baidu.com/s?k=123
// path is "/s"

std::string body;

std::string decoded_path;
std::string body; // the body field describes HTTP post body

HTTPRequest() : type(GET)
, headers()
, query_params()
, path()
, body()
, decoded_path()
{ }
};

/**
* @brief http response to client or web browser
*/
struct HTTPResponse
{
std::string content;
std::string content; // page content will return to http client
// which maybe a web browser

std::string host; // server host, auto filled by web service

std::string host;
std::string ip; // server ip address, auto filled by web service

std::string ip;
uint32_t port; // server port, auto filled by web service

uint32_t port;
std::string content_type; // content-type field in http response header
// default is "text/html", return a plain text to
// http client

std::string content_type;
std::string status_line; // HTTP server status line, reference to RFC2616
// default value is 200 OK, means this request dealed
// normally

HTTPResponse() : content()
, host()
, ip()
, port(0)
, content_type("text/html; charset=UTF-8")
, status_line("HTTP/1.1 200 OK")
{ }
};

Expand Down
5 changes: 5 additions & 0 deletions src/sofa/pbrpc/rpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ bool RpcServer::RegisterWebServlet(const std::string& path, Servlet servlet)
return _impl->RegisterWebServlet(path, servlet);
}

bool RpcServer::UnregisterWebServlet(const std::string& path)
{
return _impl->UnregisterWebServlet(path);
}

} // namespace pbrpc
} // namespace sofa

Expand Down
11 changes: 11 additions & 0 deletions src/sofa/pbrpc/rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,19 @@ class RpcServer
// Return true if the server is listening on some address.
bool IsListening();

// Register a path and its dealing function
// Return true if operation success
// Return false if path already exist
// Example: see sofa-pbrpc/sample/echo
// NOTE: path will be formatted
bool RegisterWebServlet(const std::string& path, Servlet servlet);

// Delete a path and its related function from rpc server
// Return true if deleting success
// Return false if path not exist
// NOTE: path will be formatted
bool UnregisterWebServlet(const std::string& path);

public:
const sofa::pbrpc::shared_ptr<RpcServerImpl>& impl() const
{
Expand Down
16 changes: 12 additions & 4 deletions src/sofa/pbrpc/rpc_server_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ RpcServerImpl::~RpcServerImpl()
{
SOFA_PBRPC_FUNCTION_TRACE;
Stop();
_service_pool.reset();
_web_service.reset();
_service_pool.reset();
if (_event_handler) delete _event_handler;
}

Expand Down Expand Up @@ -186,11 +186,11 @@ void RpcServerImpl::Stop()
_timer_worker->stop();
_listener->close();
StopStreams();
_io_service_pool->Stop();

_timer_worker.reset();
_listener.reset();
ClearStreams();
_io_service_pool->Stop();
_maintain_thread_group->stop();

_io_service_pool.reset();
Expand Down Expand Up @@ -603,8 +603,16 @@ bool RpcServerImpl::RegisterWebServlet(const std::string& path, Servlet servlet)
{
return false;
}
_web_service->RegisterServlet(path, servlet);
return true;
return _web_service->RegisterServlet(path, servlet);
}

bool RpcServerImpl::UnregisterWebServlet(const std::string& path)
{
if (!_web_service)
{
return false;
}
return _web_service->UnregisterServlet(path);
}

} // namespace pbrpc
Expand Down
2 changes: 2 additions & 0 deletions src/sofa/pbrpc/rpc_server_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class RpcServerImpl : public sofa::pbrpc::enable_shared_from_this<RpcServerImpl>

bool RegisterWebServlet(const std::string& path, Servlet servlet);

bool UnregisterWebServlet(const std::string& path);

private:
void OnCreated(const RpcServerStreamPtr& stream);

Expand Down
Loading

0 comments on commit 569e8de

Please sign in to comment.