Skip to content

Commit

Permalink
version 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lynncui00 committed Aug 26, 2016
1 parent b5401ee commit 81f0bfc
Show file tree
Hide file tree
Showing 30 changed files with 376 additions and 369 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ licenses applicable to the third-party components included within
PhxRPC.

A copy of the BSD 3-Clause License is included in this file.
Other dependencies and licenses:

Open Source Software Licensed under the BSD 3-Clause License:
--------------------------------------------------------------------
1. protobuf 3.0.0-beta-1
Copyright 2014, Google Inc.
All rights reserved.

Terms of the BSD 3-Clause License:
--------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions codegen/client_code_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void ClientCodeRender::GenerateStubCpp(SyntaxTree * stree, FILE * write) {

{
fprintf(write, "%s :: %s( phxrpc::BaseTcpStream & socket, phxrpc::ClientMonitor & client_monitor )\n",
clasname, clasname);
clasname, clasname);

fprintf(write, " : socket_( socket ), client_monitor_(client_monitor), keep_alive_(false)\n");
fprintf(write, "{\n");
Expand Down Expand Up @@ -188,7 +188,7 @@ void ClientCodeRender::GenerateStubFunc(SyntaxTree * stree, SyntaxFunc * func, F
fprintf(write, "{\n");
fprintf(write, " phxrpc::HttpCaller caller( socket_, client_monitor_ );\n");
fprintf(write, " caller.SetURI( \"/%s/%s\", %d );\n", stree->GetPackageName(), func->GetName(),
func->GetCmdID());
func->GetCmdID());
fprintf(write, " caller.SetKeepAlive( keep_alive_ );\n");
fprintf(write, " return caller.Call( req, resp );\n");

Expand Down
40 changes: 20 additions & 20 deletions codegen/client_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class $ClientClass$
public:
static bool Init( const char * config_file );
static const char * GetPackageName();
static const char * GetPackageName();
public:
$ClientClass$();
Expand Down Expand Up @@ -65,25 +65,25 @@ bool $ClientClass$ :: Init( const char * config_file )
}
const char * $ClientClass$ :: GetPackageName() {
const char * ret = global_$ClientClassLower$_config_.GetPackageName();
if (strlen(ret) == 0) {
ret = "$PackageName$";
}
return ret;
const char * ret = global_$ClientClassLower$_config_.GetPackageName();
if (strlen(ret) == 0) {
ret = "$PackageName$";
}
return ret;
}
$ClientClass$ :: $ClientClass$()
{
static std::mutex monitor_mutex;
if ( !global_$ClientClassLower$_monitor_.get() ) {
monitor_mutex.lock();
if ( !global_$ClientClassLower$_monitor_.get() ) {
global_$ClientClassLower$_monitor_ = phxrpc::MonitorFactory::GetFactory()
->CreateClientMonitor( GetPackageName() );
}
global_$ClientClassLower$_config_.SetClientMonitor( global_$ClientClassLower$_monitor_ );
monitor_mutex.unlock();
}
static std::mutex monitor_mutex;
if ( !global_$ClientClassLower$_monitor_.get() ) {
monitor_mutex.lock();
if ( !global_$ClientClassLower$_monitor_.get() ) {
global_$ClientClassLower$_monitor_ = phxrpc::MonitorFactory::GetFactory()
->CreateClientMonitor( GetPackageName() );
}
global_$ClientClassLower$_config_.SetClientMonitor( global_$ClientClassLower$_monitor_ );
monitor_mutex.unlock();
}
}
$ClientClass$ :: ~$ClientClass$()
Expand All @@ -102,16 +102,16 @@ const char * PHXRPC_CLIENT_FUNC_TEMPLATE =
if(ep != nullptr) {
phxrpc::BlockTcpStream socket;
bool open_ret = phxrpc::PhxrpcTcpUtils::Open(&socket, ep->ip, ep->port,
bool open_ret = phxrpc::PhxrpcTcpUtils::Open(&socket, ep->ip, ep->port,
global_$ClientClassLower$_config_.GetConnectTimeoutMS(), NULL, 0,
*(global_$ClientClassLower$_monitor_.get()));
if ( open_ret ) {
*(global_$ClientClassLower$_monitor_.get()));
if ( open_ret ) {
socket.SetTimeout(global_$ClientClassLower$_config_.GetSocketTimeoutMS());
$StubClass$ stub(socket, *(global_$ClientClassLower$_monitor_.get()));
return stub.$Func$(req, resp);
}
}
}
return -1;
}
Expand Down
42 changes: 21 additions & 21 deletions codegen/server_code_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,32 @@ void ServerCodeRender::GenerateServerConfigCpp(SyntaxTree * stree, FILE * write)
fprintf(write, "\n");

char classname[128] = { 0 };
char message_file[128] = {0};
char message_file[128] = {0};
name_render_.GetServerConfigClasname(stree->GetName(), classname, sizeof(classname));
name_render_.GetMessageFileName(stree->GetProtoFile(), message_file, sizeof(message_file));

std::string content = PHXRPC_EPOLL_SERVER_CONFIG_CPP_TEMPLATE;

std::string package_name = "\"" + std::string(stree->GetPackageName()) + "\"";

{
std::string message_name = "";
for( auto itr : *(stree->GetFuncList()) ) {
if ( std::string(itr.GetReq()->GetType()).find( stree->GetPackageName() ) != std::string::npos ) {
message_name = itr.GetReq()->GetType();
break;
} else if ( std::string(itr.GetResp()->GetType()).find( stree->GetPackageName() ) != std::string::npos ) {
message_name = itr.GetResp()->GetType();
break;
}
}
if( message_name != "" ) {
int package_name_len = strlen(stree->GetPackageName());
message_name = message_name.substr( package_name_len + 1, message_name.size() - package_name_len - 1 );
package_name = "\n" + std::string(stree->GetPackageName()) + "::" + message_name
+ "::default_instance().GetDescriptor()->file()->package().c_str()";
}
}
std::string package_name = "\"" + std::string(stree->GetPackageName()) + "\"";

{
std::string message_name = "";
for( auto itr : *(stree->GetFuncList()) ) {
if ( std::string(itr.GetReq()->GetType()).find( stree->GetPackageName() ) != std::string::npos ) {
message_name = itr.GetReq()->GetType();
break;
} else if ( std::string(itr.GetResp()->GetType()).find( stree->GetPackageName() ) != std::string::npos ) {
message_name = itr.GetResp()->GetType();
break;
}
}
if( message_name != "" ) {
int package_name_len = strlen(stree->GetPackageName());
message_name = message_name.substr( package_name_len + 1, message_name.size() - package_name_len - 1 );
package_name = "\n" + std::string(stree->GetPackageName()) + "::" + message_name
+ "::default_instance().GetDescriptor()->file()->package().c_str()";
}
}

StrTrim(&content);
StrReplaceAll(&content, "$MessageFile$", message_file);
Expand Down
76 changes: 38 additions & 38 deletions codegen/server_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ using namespace std;
void HttpDispatch( const phxrpc::HttpRequest & request, phxrpc::HttpResponse * response, phxrpc::DispatcherArgs_t * args ) {
ServiceArgs_t * service_args = (ServiceArgs_t *)(args->service_args);
ServiceArgs_t * service_args = (ServiceArgs_t *)(args->service_args);
$ServiceImplClass$ service( service_args );
$DispatcherClass$ dispatcher( service, args );
$ServiceImplClass$ service( service_args );
$DispatcherClass$ dispatcher( service, args );
phxrpc::HttpDispatcher<$DispatcherClass$> http_dispatcher(
dispatcher, $DispatcherClass$::GetURIFuncMap() );
Expand All @@ -52,46 +52,46 @@ void HttpDispatch( const phxrpc::HttpRequest & request, phxrpc::HttpResponse * r
}
void showUsage( const char * program ) {
printf( "\n" );
printf( "Usage: %s [-c <config>] [-v]\n", program );
printf( "\n" );
printf( "\n" );
printf( "Usage: %s [-c <config>] [-v]\n", program );
printf( "\n" );
exit( 0 );
exit( 0 );
}
void LogImpl(int priority, const char * format, va_list args) {
//or implement your logmode here
}
int main( int argc, char * argv[] ) {
const char * config_file = NULL;
extern char *optarg ;
int c ;
while( ( c = getopt( argc, argv, "c:v" ) ) != EOF ) {
switch ( c ) {
case 'c' : config_file = optarg; break;
case 'v' :
default: showUsage( argv[ 0 ] ); break;
}
}
const char * config_file = NULL;
extern char *optarg ;
int c ;
while( ( c = getopt( argc, argv, "c:v" ) ) != EOF ) {
switch ( c ) {
case 'c' : config_file = optarg; break;
case 'v' :
default: showUsage( argv[ 0 ] ); break;
}
}
assert(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
//set your logfunc
//phxrpc::setvlog(LogImpl);
//phxrpc::MonitorFactory::SetFactory( new YourSelfsMonitorFactory() );
//phxrpc::MonitorFactory::SetFactory( new YourSelfsMonitorFactory() );
if( NULL == config_file ) showUsage( argv[0] );
$ServerConfigClass$ config;
if( ! config.Read( config_file ) ) showUsage( argv[0] );
if( NULL == config_file ) showUsage( argv[0] );
$ServerConfigClass$ config;
if( ! config.Read( config_file ) ) showUsage( argv[0] );
ServiceArgs_t service_args;
service_args.config = &config;
ServiceArgs_t service_args;
service_args.config = &config;
phxrpc::HshaServer server( config.GetHshaServerConfig(), HttpDispatch, &service_args );
server.RunForever();
return 0;
phxrpc::HshaServer server( config.GetHshaServerConfig(), HttpDispatch, &service_args );
server.RunForever();
return 0;
}
)";
Expand All @@ -106,16 +106,16 @@ const char * PHXRPC_EPOLL_SERVER_CONFIG_HPP_TEMPLATE =
class $ServerConfigClass$
{
public:
$ServerConfigClass$();
$ServerConfigClass$();
~$ServerConfigClass$();
~$ServerConfigClass$();
bool Read( const char * config_file );
bool Read( const char * config_file );
const phxrpc::HshaServerConfig & GetHshaServerConfig();
const phxrpc::HshaServerConfig & GetHshaServerConfig();
private:
phxrpc::HshaServerConfig ep_server_config_;
phxrpc::HshaServerConfig ep_server_config_;
};
)";
Expand All @@ -138,18 +138,18 @@ const char * PHXRPC_EPOLL_SERVER_CONFIG_CPP_TEMPLATE =
bool $ServerConfigClass$ :: Read( const char * config_file )
{
bool ret = ep_server_config_.Read( config_file );
bool ret = ep_server_config_.Read( config_file );
if ( strlen( ep_server_config_.GetPackageName() ) == 0 ) {
ep_server_config_.SetPackageName( $PackageName$ );
}
if ( strlen( ep_server_config_.GetPackageName() ) == 0 ) {
ep_server_config_.SetPackageName( $PackageName$ );
}
return ret;
return ret;
}
const phxrpc::HshaServerConfig & $ServerConfigClass$ :: GetHshaServerConfig()
{
return ep_server_config_;
return ep_server_config_;
}
)";
Expand Down
10 changes: 5 additions & 5 deletions codegen/service_code_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ void ServiceCodeRender::GenerateServiceImplHpp(SyntaxTree * stree, FILE * write)
fprintf(write, "class %s;\n", config_name);
fprintf(write, "\n");

fprintf(write, "typedef struct tagServiceArgs {\n");
fprintf(write, " %s * config;\n", config_name);
fprintf(write, " //You can add other arguments here and initiate in main().\n");
fprintf(write, "}ServiceArgs_t;\n");
fprintf(write, "typedef struct tagServiceArgs {\n");
fprintf(write, " %s * config;\n", config_name);
fprintf(write, " //You can add other arguments here and initiate in main().\n");
fprintf(write, "}ServiceArgs_t;\n");
fprintf(write, "\n");

fprintf(write, "class %s : public %s\n", clasname, base_name);
Expand Down Expand Up @@ -428,7 +428,7 @@ void ServiceCodeRender::GenerateDispatcherFunc(SyntaxTree * stree, SyntaxFunc *
clasname, func->GetName());

fprintf(write, "{\n");
fprintf(write, " dispatcher_args_->server_monitor->SvrCall(%d, \"%s\", 1);\n", func->GetCmdID(), func->GetName() );
fprintf(write, " dispatcher_args_->server_monitor->SvrCall(%d, \"%s\", 1);\n", func->GetCmdID(), func->GetName() );
fprintf(write, "\n");

fprintf(write, " int ret = 0;\n");
Expand Down
6 changes: 3 additions & 3 deletions codegen/syntax_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const char * SyntaxParam::GetType() const {
SyntaxFunc::SyntaxFunc() {
memset(opt_string_, 0, sizeof(opt_string_));
memset(usage_, 0, sizeof(usage_));
cmdid_ = -1;
cmdid_ = -1;
}

SyntaxFunc::~SyntaxFunc() {
Expand Down Expand Up @@ -104,11 +104,11 @@ const char * SyntaxFunc::GetUsage() const {
}

void SyntaxFunc::SetCmdID(int32_t cmdid) {
cmdid_ = cmdid;
cmdid_ = cmdid;
}

int32_t SyntaxFunc::GetCmdID() {
return cmdid_;
return cmdid_;
}

//====================================================================
Expand Down
6 changes: 3 additions & 3 deletions codegen/syntax_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ class SyntaxFunc : public SyntaxNode {
void SetUsage(const char * usage);
const char * GetUsage() const;

void SetCmdID(int32_t cmdid );
int32_t GetCmdID();
void SetCmdID(int32_t cmdid );
int32_t GetCmdID();

private:
SyntaxParam req_;
SyntaxParam resp_;
char opt_string_[_SYNTAX_DESC_LEN];
char usage_[_SYNTAX_DESC_LEN];
int32_t cmdid_;
int32_t cmdid_;
};

//------------------------------------------------------------
Expand Down
Loading

0 comments on commit 81f0bfc

Please sign in to comment.