Skip to content

Commit

Permalink
add protorpc
Browse files Browse the repository at this point in the history
  • Loading branch information
robbinfan committed Feb 12, 2015
1 parent b858db2 commit 8be3fb0
Show file tree
Hide file tree
Showing 41 changed files with 3,897 additions and 42 deletions.
6 changes: 5 additions & 1 deletion common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ cc_library(
'./tracing/Tracing.cc',
'EscapeTables.cc',
],
deps = [':gen_escape_table', '//thirdparty/gflags:gflags', '#pthread', '#rt'],
deps = [
':gen_escape_table',
'//thirdparty/protobuf:protobuf',
'#pthread',
'#rt'],
extra_cppflags = '-std=c++0x'
)
2 changes: 1 addition & 1 deletion common/logging/LogFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string.h>
#include <unistd.h>

#include <gflags/gflags.h>
#include "thirdparty/gflags/gflags.h"

#include <claire/common/files/FileUtil.h>
#include <claire/common/time/Timestamp.h>
Expand Down
2 changes: 1 addition & 1 deletion common/logging/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#ifndef _CLAIRE_COMMON_LOGGING_LOGGING_H
#define _CLAIRE_COMMON_LOGGING_LOGGING_H

#include <gflags/gflags.h>
#include "thirdparty/gflags/gflags.h"

#include <iosfwd>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion common/metrics/CounterProvider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <claire/common/metrics/CounterProvider.h>

#include <gflags/gflags.h>
#include "thirdparty/gflags/gflags.h"

#include <boost/scoped_array.hpp>

Expand Down
8 changes: 4 additions & 4 deletions common/metrics/CounterSampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include <claire/common/metrics/CounterSampler.h>

#include <gflags/gflags.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include "thirdparty/gflags/gflags.h"
#include "thirdparty/rapidjson/document.h"
#include "thirdparty/rapidjson/stringbuffer.h"
#include "thirdparty/rapidjson/prettywriter.h"

#include <boost/bind.hpp>

Expand Down
6 changes: 3 additions & 3 deletions common/metrics/Histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include <math.h>
#include <limits.h>

#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include "thirdparty/rapidjson/document.h"
#include "thirdparty/rapidjson/stringbuffer.h"
#include "thirdparty/rapidjson/prettywriter.h"

#include <claire/common/strings/StringPrintf.h>
#include <claire/common/metrics/HistogramSamples.h>
Expand Down
12 changes: 6 additions & 6 deletions common/protobuf/ProtobufIO.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include <google/protobuf/message.h>
#include <google/protobuf/descriptor.h>
#include "thirdparty/google/protobuf/message.h"
#include "thirdparty/google/protobuf/descriptor.h"

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include "thirdparty/rapidjson/document.h"
#include "thirdparty/rapidjson/error/en.h"
#include "thirdparty/rapidjson/stringbuffer.h"
#include "thirdparty/rapidjson/prettywriter.h"

#include <vector>

Expand Down
2 changes: 0 additions & 2 deletions common/tests/Logger_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <stdio.h>
#include <sys/resource.h>

#include <gflags/gflags.h>

#include <claire/common/time/Timestamp.h>
#include <claire/common/logging/Logging.h>
#include <claire/common/threading/ThreadPool.h>
Expand Down
2 changes: 1 addition & 1 deletion netty/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cc_library(
'./resolver/StaticAddressResolver.cc',
'./resolver/DnsResolver.cc',
'./loadbalancer/LoadBalancerFactory.cc',
'./inspect/PProfInspector.cc',
# './inspect/PProfInspector.cc',
'./inspect/FlagsInspector.cc',
'./inspect/StatisticsInspector.cc',
'./http/Uri.cc',
Expand Down
2 changes: 1 addition & 1 deletion netty/InetAddress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ InetAddress::InetAddress(const StringPiece& address)
{
std::vector<std::string> result;
boost::split(result, address, boost::is_any_of(":"));
CHECK_EQ(result.size(), 2);
CHECK_EQ(result.size(), 2u);

auto saved_errno = errno;
errno = 0;
Expand Down
8 changes: 4 additions & 4 deletions netty/inspect/FlagsInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include <claire/netty/inspect/FlagsInspector.h>

#include <ctemplate/template.h>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include "thirdparty/ctemplate/template.h"
#include "thirdparty/rapidjson/document.h"
#include "thirdparty/rapidjson/stringbuffer.h"
#include "thirdparty/rapidjson/prettywriter.h"

#include <vector>

Expand Down
16 changes: 2 additions & 14 deletions netty/inspect/PProfInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

#include <claire/netty/inspect/PProfInspector.h>

#ifdef HAVE_PPROF
#include <gperftools/profiler.h>
#include <gperftools/malloc_extension.h>
#endif
#include "thirdparty/gperftools/profiler.h"
#include "thirdparty/gperftools/malloc_extension.h"

#include <boost/bind.hpp>
#include <boost/algorithm/string.hpp>
Expand Down Expand Up @@ -72,7 +70,6 @@ PProfInspector::PProfInspector(HttpServer* server)
return ;
}

#ifdef HAVE_PPROF
server_->Register("/pprof/profile",
boost::bind(&PProfInspector::OnProfile, this, _1),
false);
Expand All @@ -88,12 +85,10 @@ PProfInspector::PProfInspector(HttpServer* server)
server_->Register("/pprof/symbol",
boost::bind(&PProfInspector::OnSymbol, _1),
false);
#endif
}

void PProfInspector::OnProfile(const HttpConnectionPtr& connection)
{
#ifdef HAVE_PPROF
auto request = connection->mutable_request();
if (request->method() != HttpRequest::kGet)
{
Expand Down Expand Up @@ -121,12 +116,10 @@ void PProfInspector::OnProfile(const HttpConnectionPtr& connection)
}
connections_.insert(connection->id());
}
#endif
}

void PProfInspector::OnProfileComplete()
{
#ifdef HAVE_PPROF
ProfilerFlush();
ProfilerStop();

Expand All @@ -143,27 +136,22 @@ void PProfInspector::OnProfileComplete()
server_->SendByHttpConnectionId(connection, output);
server_->Shutdown(connection);
}
#endif
}

void PProfInspector::OnHeap(const HttpConnectionPtr& connection)
{
#ifdef HAVE_PPROF
std::string output;
MallocExtension::instance()->GetHeapSample(&output);
connection->Send(output);
connection->Shutdown();
#endif
}

void PProfInspector::OnGrowth(const HttpConnectionPtr& connection)
{
#ifdef HAVE_PPROF
std::string output;
MallocExtension::instance()->GetHeapGrowthStacks(&output);
connection->Send(output);
connection->Shutdown();
#endif
}

void PProfInspector::OnCmdline(const HttpConnectionPtr& connection)
Expand Down
2 changes: 1 addition & 1 deletion netty/tests/HttpRequest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <claire/netty/TcpConnection.h>
#include <claire/netty/Buffer.h>

#include <gtest/gtest.h>
#include "thirdparty/gtest/gtest.h"

using namespace claire;

Expand Down
2 changes: 1 addition & 1 deletion netty/tests/HttpResponse_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <claire/netty/http/HttpResponse.h>
#include <claire/netty/Buffer.h>

#include <gtest/gtest.h>
#include "thirdparty/gtest/gtest.h"

using namespace claire;

Expand Down
2 changes: 1 addition & 1 deletion netty/tests/Uri_unittest.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <claire/netty/http/Uri.h>
#include <claire/common/strings/UriEscape.h>

#include <gtest/gtest.h>
#include "thirdparty/gtest/gtest.h"

using namespace claire;

Expand Down
2 changes: 2 additions & 0 deletions protorpc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pb.*
*Assets.*
44 changes: 44 additions & 0 deletions protorpc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource_library(
name = 'static_resource',
srcs = [
'./assets/forms.html',
'./assets/forms.js',
'./assets/methods.html'
]
)

cc_binary(
name='protoc-gen-rpc',
srcs=['./generator/protoc-gen-rpc.cc'],
deps=['//thirdparty/protobuf:protobuf', '//thirdparty/protobuf:protoc', '#pthread'],
dynamic_link=True
)

gen_rule(
name = 'gen-rpc-proto',
srcs = ['rpcmessage.proto', 'builtin_service.proto'],
cmd = 'sh -x claire/protorpc/gen-rpc.sh $BUILD_DIR',
outs = ['builtin_service.pb.h', 'builtin_service.pb.cc', 'rpcmessage.pb.h', 'rpcmessage.pb.cc'],
deps = ['//thirdparty/protobuf-2.6.1/src:protoc.bin', ':protoc-gen-rpc']
)

cc_library(
name = 'claire_protorpc',
srcs = [
'BuiltinService.cc',
'RpcChannel.cc',
'RpcCodec.cc',
'RpcController.cc',
'RpcServer.cc',
'RpcUtil.cc',
'builtin_service.pb.cc',
'rpcmessage.pb.cc'
],
deps = [
'//claire/common:claire_common',
'//claire/netty:claire_netty',
':static_resource',
':gen-rpc-proto',
'//thirdparty/protobuf-2.6.1/src:protobuf'
]
)
67 changes: 67 additions & 0 deletions protorpc/BuiltinService.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2013 The claire-protorpc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include <claire/protorpc/BuiltinService.h>

#include "thirdparty/gflags/gflags.h"

namespace claire {
namespace protorpc {

static bool g_protorpc_healthy = true;

void BuiltinServiceImpl::HeartBeat(RpcControllerPtr& controller,
const HeartBeatRequestPtr& request,
const HeartBeatResponse* response_prototype,
const RpcDoneCallback& done)
{
HeartBeatResponse response;
if (g_protorpc_healthy)
{
response.set_status("Ok");
}
else
{
response.set_status("Bad");
}
done(controller, &response);
}

void BuiltinServiceImpl::Services(RpcControllerPtr& controller,
const ServicesRequestPtr& request,
const ServicesResponse* response_prototype,
const RpcDoneCallback& done)
{
ServicesResponse response;
for (auto it = services_.cbegin(); it != services_.cend(); ++it)
{
if (it->second == this)
{
continue;
}
response.add_services()->set_name(it->second->GetDescriptor()->full_name());
}
done(controller, &response);
}

void BuiltinServiceImpl::GetFileSet(RpcControllerPtr& controller,
const GetFileSetRequestPtr& request,
const GetFileSetResponse* response_prototype,
const RpcDoneCallback& done)
{
GetFileSetResponse response;
for (int i = 0; i < request->names_size(); i++)
{
auto it = services_.find(request->names(i));
if (it != services_.end())
{
auto file = response.mutable_file_set()->add_file();
it->second->GetDescriptor()->file()->CopyTo(file);
}
}
done(controller, &response);
}

} // namespace protorpc
} // namespace claire
44 changes: 44 additions & 0 deletions protorpc/BuiltinService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2013 The claire-protorpc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#pragma once

#include <map>
#include <string>

#include <claire/protorpc/builtin_service.pb.h> // generate by protoc

namespace claire {
namespace protorpc {

class BuiltinServiceImpl : public BuiltinService {
public:
typedef std::map<std::string, Service*> ServiceMap;

void set_services(const ServiceMap& services)
{
services_ = services;
}

virtual void HeartBeat(RpcControllerPtr& controller,
const HeartBeatRequestPtr& request,
const HeartBeatResponse* response_prototype,
const RpcDoneCallback& done);

virtual void Services(RpcControllerPtr& controller,
const ServicesRequestPtr& request,
const ServicesResponse* response_prototype,
const RpcDoneCallback& done);

virtual void GetFileSet(RpcControllerPtr& controller,
const GetFileSetRequestPtr& request,
const GetFileSetResponse* response_prototype,
const RpcDoneCallback& done);

private:
ServiceMap services_;
};

} // namespace protorpc
} // namespace claire
Loading

0 comments on commit 8be3fb0

Please sign in to comment.