Skip to content

Commit

Permalink
add examples directory
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Dec 26, 2011
1 parent 0faaea3 commit 6b36e56
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 46 deletions.
40 changes: 8 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
option(zrpc_build_tests "Build zrpc's tests." OFF)
option(zrpc_build_static "Build static library." OFF)
option(zrpc_build_examples "Build zrpc's examples." OFF)

include(library_suffix)
include(ExternalProject)
# Install GFlags
ExternalProject_Add(
GFlagsLib
URL http://google-gflags.googlecode.com/files/gflags-1.7.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
)
ExternalProject_Get_Property(GFlagsLib install_dir)
include_directories(${install_dir}/include)
set(GFLAGS_LIBRARIES ${install_dir}/lib/libgflags.${link_library_suffix})
set(GFLAGS_PREFIX ${install_dir})

# Install GLog
ExternalProject_Add(
GLogLib
URL http://google-glog.googlecode.com/files/glog-0.3.1-1.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --with-gflags=${GFLAGS_PREFIX}
)
ExternalProject_Get_Property(GLogLib install_dir)
include_directories(${install_dir}/include)
set(GLOG_LIBRARIES ${install_dir}/lib/libglog.${link_library_suffix})

# Install ZeroMQ
ExternalProject_Add(
ZeroMQ
URL http://download.zeromq.org/zeromq-2.1.11.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --with-gflags=${GFLAGS_PREFIX}
)
ExternalProject_Get_Property(ZeroMQ install_dir)
include_directories(${install_dir}/include)
set(ZEROMQ_LIBRARIES ${install_dir}/lib/libzmq.${link_library_suffix})
include(install_deps)

find_package(ProtobufPlugin REQUIRED)
find_package(Boost REQUIRED COMPONENTS thread)
Expand All @@ -48,7 +18,13 @@ set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -Werr
set(CMAKE_OSX_ARCHITECTURES x86_64)

add_subdirectory(src)

if(zrpc_build_tests)
enable_testing()
add_subdirectory(test)
endif(zrpc_build_tests)

if(zrpc_build_examples)
add_subdirectory(examples)
endif(zrpc_build_examples)

34 changes: 34 additions & 0 deletions cmake_modules/install_deps.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
include(library_suffix)
include(ExternalProject)

# Install GFlags
ExternalProject_Add(
GFlagsLib
URL http://google-gflags.googlecode.com/files/gflags-1.7.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
)
ExternalProject_Get_Property(GFlagsLib install_dir)
include_directories(${install_dir}/include)
set(GFLAGS_LIBRARIES ${install_dir}/lib/libgflags.${link_library_suffix})
set(GFLAGS_PREFIX ${install_dir})

# Install GLog
ExternalProject_Add(
GLogLib
URL http://google-glog.googlecode.com/files/glog-0.3.1-1.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --with-gflags=${GFLAGS_PREFIX}
)
ExternalProject_Get_Property(GLogLib install_dir)
include_directories(${install_dir}/include)
set(GLOG_LIBRARIES ${install_dir}/lib/libglog.${link_library_suffix})

# Install ZeroMQ
ExternalProject_Add(
ZeroMQ
URL http://download.zeromq.org/zeromq-2.1.11.tar.gz
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --with-gflags=${GFLAGS_PREFIX}
)
ExternalProject_Get_Property(ZeroMQ install_dir)
include_directories(${install_dir}/include)
set(ZEROMQ_LIBRARIES ${install_dir}/lib/libzmq.${link_library_suffix})

15 changes: 15 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include(zrpc_functions)
find_package(ProtobufPlugin REQUIRED)
PROTOBUF_GENERATE_CPP(SEARCH_PB_SRCS SEARCH_PB_HDRS search.proto)
PROTOBUF_GENERATE_ZRPC(SEARCH_ZRPC_SRCS SEARCH_ZRPC_HDRS search.proto)

add_library(example_pb ${SEARCH_PB_SRCS} ${SEARCH_PB_HDRS} ${SEARCH_ZRPC_SRCS}
${SEARCH_ZRPC_HDRS})
target_link_libraries(example_pb ${PROTOBUF_LIBRARY})
include_directories(${PROJECT_BINARY_DIR}/examples)

add_executable(example_client example_client.cc)
target_link_libraries(example_client zrpc example_pb)

add_executable(example_server example_server.cc)
target_link_libraries(example_server zrpc example_pb)
43 changes: 43 additions & 0 deletions examples/example_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: [email protected] <Nadav Samet>

#include <ostream>
#include "zrpc/zrpc.h"
#include "glog/logging.h"
#include "search.pb.h"
#include "search.zrpc.h"

int main() {
zrpc::Application application;
examples::SearchService_Stub search_stub(application.CreateRpcChannel(
"tcp://localhost:5555"), true);
examples::SearchRequest request;
examples::SearchResponse response;
request.set_query("gold");

zrpc::RPC rpc;
rpc.SetDeadlineMs(1000); // 1 second
std::cout << "Sending request." << std::endl;
search_stub.Search(&rpc, &request, &response, NULL);

rpc.Wait();
std::cout << "Response status: "
<< zrpc::GenericRPCResponse::Status_Name(rpc.GetStatus())
<< std::endl;
if (rpc.OK()) {
std::cout << response.DebugString() << std::endl;
}
}
45 changes: 45 additions & 0 deletions examples/example_server.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: [email protected] <Nadav Samet>

#include <iostream>
#include "zrpc/zrpc.h"
#include "search.pb.h"
#include "search.zrpc.h"

namespace examples {
class SearchServiceImpl : public SearchService {

virtual void Search(
zrpc::RPC* rpc, const SearchRequest* request,
SearchResponse* response, zrpc::Closure* done) {
std::cout << "Got request for '" << request->query() << "'" << std::endl;
response->add_results("result1 for " + request->query());
response->add_results("this is result2");
done->Run();
}
};
} // namespace examples

int main() {
zrpc::Application application;
zrpc::Server* server = application.CreateServer("tcp://*:5555");
examples::SearchServiceImpl search_service;
server->RegisterService(&search_service);
std::cout << "Serving requests on port 5555." << std::endl;
server->Start();
delete server;
std::cout << "Terminating!" << std::endl;
}
14 changes: 14 additions & 0 deletions examples/search.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package examples;

message SearchRequest {
required string query = 1;
optional int32 page_number = 2 [default = 1];
}

message SearchResponse {
repeated string results = 1;
}

service SearchService {
rpc Search(SearchRequest) returns(SearchResponse);
}
38 changes: 38 additions & 0 deletions include/zrpc/zrpc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: [email protected] <Nadav Samet>

#ifndef ZRPC_ZRPC_H
#define ZRPC_ZRPC_H

// Master include file
#include "zrpc/application.h"
#include "zrpc/callback.h"
#include "zrpc/connection_manager.h"
#include "zrpc/event_manager.h"
#include "zrpc/macros.h"
#include "zrpc/rpc.h"
#include "zrpc/rpc_channel.h"
#include "zrpc/server.h"
#include "zrpc/service.h"
#include "zrpc/sync_event.h"
#include "zrpc/zmq_utils.h"

// Two include files were intentionally left out since they rely on ZeroMQ
// headers being around and probably most people will not need this low-level
// functionality. These files are:
// zmq_utils.h
// remote_response.h
#endif
2 changes: 2 additions & 0 deletions src/zrpc/connection_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ class ConnectionImpl : public Connection {

virtual zmq::socket_t* CreateConnectedSocket(zmq::context_t* context) {
zmq::socket_t* socket = new zmq::socket_t(*context, ZMQ_DEALER);
int linger_ms = 0;
socket->setsockopt(ZMQ_LINGER, &linger_ms, sizeof(linger_ms));
socket->connect(endpoint_.c_str());
return socket;
}
Expand Down
10 changes: 5 additions & 5 deletions src/zrpc/plugin/cpp/zrpc_cpp_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void ServiceGenerator::GenerateInterface(io::Printer* printer) {
" ::zrpc::RPC* rpc,\n"
" const ::google::protobuf::Message* request,\n"
" ::google::protobuf::Message* response,\n"
" Closure* done);\n"
" ::zrpc::Closure* done);\n"
"const ::google::protobuf::Message& GetRequestPrototype(\n"
" const ::google::protobuf::MethodDescriptor* method) const;\n"
"const ::google::protobuf::Message& GetResponsePrototype(\n"
Expand Down Expand Up @@ -138,7 +138,7 @@ void ServiceGenerator::GenerateMethodSignatures(
"$virtual$void $name$(::zrpc::RPC* rpc,\n"
" const $input_type$* request,\n"
" $output_type$* response,\n"
" Closure* done);\n");
" ::zrpc::Closure* done);\n");
}
}

Expand Down Expand Up @@ -203,7 +203,7 @@ void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) {
"void $classname$::$name$(::zrpc::RPC* rpc,\n"
" const $input_type$*,\n"
" $output_type$*,\n"
" Closure* done) {\n"
" ::zrpc::Closure* done) {\n"
" rpc->SetFailed(\"Method $name$() not implemented.\");\n"
" done->Run();\n"
"}\n"
Expand All @@ -217,7 +217,7 @@ void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
" ::zrpc::RPC* rpc,\n"
" const ::google::protobuf::Message* request,\n"
" ::google::protobuf::Message* response,\n"
" Closure* done) {\n"
" ::zrpc::Closure* done) {\n"
" GOOGLE_DCHECK_EQ(method->service(), $classname$_descriptor_);\n"
" switch(method->index()) {\n");

Expand Down Expand Up @@ -301,7 +301,7 @@ void ServiceGenerator::GenerateStubMethods(io::Printer* printer) {
"void $classname$_Stub::$name$(::zrpc::RPC* rpc,\n"
" const $input_type$* request,\n"
" $output_type$* response,\n"
" Closure* done) {\n"
" ::zrpc::Closure* done) {\n"
" channel_->CallMethod(descriptor()->method($index$),\n"
" rpc, request, response, done);\n"
"}\n");
Expand Down
24 changes: 18 additions & 6 deletions src/zrpc/rpc_channel_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ void RpcChannelImpl::CallMethodFull(
const std::string& service_name,
const std::string& method_name,
RPC* rpc,
const ::google::protobuf::Message* request_msg,
const std::string& request,
std::string* response_str,
::google::protobuf::Message* response_msg,
std::string* response_str,
Closure* done) {
CHECK_EQ(rpc->GetStatus(), GenericRPCResponse::INACTIVE);
GenericRPCRequest generic_request;
Expand All @@ -65,8 +66,17 @@ void RpcChannelImpl::CallMethodFull(
zmq::message_t* msg_out = new zmq::message_t(msg_size);
CHECK(generic_request.SerializeToArray(msg_out->data(), msg_size));

zmq::message_t* payload_out = new zmq::message_t(request.size());
memcpy(payload_out->data(), request.c_str(), request.size());
zmq::message_t* payload_out = NULL;
if (request_msg != NULL) {
size_t bytes = request_msg->ByteSize();
payload_out = new zmq::message_t(bytes);
request_msg->SerializeToArray(payload_out->data(),
bytes);
}
else {
payload_out = new zmq::message_t(request.size());
memcpy(payload_out->data(), request.c_str(), request.size());
}

MessageVector* msg_vector = new MessageVector;
msg_vector->push_back(msg_out);
Expand Down Expand Up @@ -96,9 +106,10 @@ void RpcChannelImpl::CallMethod0(const std::string& service_name,
CallMethodFull(service_name,
method_name,
rpc,
NULL,
request,
response,
NULL,
response,
done);
}

Expand All @@ -111,9 +122,10 @@ void RpcChannelImpl::CallMethod(
CallMethodFull(method->service()->name(),
method->name(),
rpc,
request->SerializeAsString(),
NULL,
request,
"",
response,
NULL,
done);
}

Expand Down
3 changes: 2 additions & 1 deletion src/zrpc/rpc_channel_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ class RpcChannelImpl: public RpcChannel {
const std::string& service_name,
const std::string& method_name,
RPC* rpc,
const ::google::protobuf::Message* request_msg,
const std::string& request,
std::string* response_str,
::google::protobuf::Message* response_msg,
std::string* response_str,
Closure* done);

Connection* connection_;
Expand Down
4 changes: 2 additions & 2 deletions test/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ include(zrpc_functions)
find_package(ProtobufPlugin REQUIRED)

PROTOBUF_GENERATE_PYTHON(SEARCH_PB_PY_SRCS search.proto)
PROTOBUF_GENERATE_CPP(SEARCH_PB_SRCS SEARCH_PB_HDRS search.proto)

PROTOBUF_GENERATE_PYTHON_ZRPC(SEARCH_PB_PYZRPC_SRCS search.proto)

PROTOBUF_GENERATE_CPP(SEARCH_PB_SRCS SEARCH_PB_HDRS search.proto)
PROTOBUF_GENERATE_ZRPC(SEARCH_ZRPC_SRCS SEARCH_ZRPC_HDRS search.proto)

add_library(search_pb ${SEARCH_PB_SRCS} ${SEARCH_PB_HDRS} ${SEARCH_ZRPC_SRCS}
Expand Down

0 comments on commit 6b36e56

Please sign in to comment.