Skip to content
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PUB_INC=src/sofa/pbrpc/pbrpc.h src/sofa/pbrpc/closure_helper.h src/sofa/pbrpc/cl
src/sofa/pbrpc/locks.h src/sofa/pbrpc/mutex_lock.h src/sofa/pbrpc/spin_lock.h \
src/sofa/pbrpc/fast_lock.h src/sofa/pbrpc/rw_lock.h src/sofa/pbrpc/scoped_locker.h \
src/sofa/pbrpc/condition_variable.h src/sofa/pbrpc/wait_event.h src/sofa/pbrpc/http.h \
src/sofa/pbrpc/buffer.h src/sofa/pbrpc/buf_handle.h \
src/sofa/pbrpc/buffer.h src/sofa/pbrpc/buf_handle.h src/sofa/pbrpc/rpc_attachment.h src/sofa/pbrpc/serialize.h \
$(PROTO) $(PROTO_HEADER)

#-----------------------------------------------
Expand Down
84 changes: 84 additions & 0 deletions sample/echo_cookie/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright (c) 2014 Baidu.com, Inc. 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.

#-----------------------------------------------
## Sofa-pbrpc path containing `include'and `lib'.
##
## Check file exist:
## $(SOFA_PBRPC)/include/sofa/pbrpc/pbrpc.h
## $(SOFA_PBRPC)/lib/libsofa-pbrpc.a
##
SOFA_PBRPC=../../output
#-----------------------------------------------

#-----------------------------------------------
# Uncomment exactly one of the lines labelled (A), (B), and (C) below
# to switch between compilation modes.
#
OPT ?= -O2 # (A) Production use (optimized mode)
# OPT ?= -g2 # (B) Debug mode, w/ full line-level debugging symbols
# OPT ?= -O2 -g2 # (C) Profiling mode: opt, but w/debugging symbols
#-----------------------------------------------

#-----------------------------------------------
# !!! Do not change the following lines !!!
#-----------------------------------------------

include ../../depends.mk

CXX=g++
INCPATH=-I. -I$(SOFA_PBRPC)/include -I$(PROTOBUF_DIR)/include \
-I$(SNAPPY_DIR)/include -I$(ZLIB_DIR)/include
CXXFLAGS += $(OPT) -pipe -W -Wall -fPIC -D_GNU_SOURCE -D__STDC_LIMIT_MACROS $(INCPATH)

LIBRARY=$(SOFA_PBRPC)/lib/libsofa-pbrpc.a $(PROTOBUF_DIR)/lib/libprotobuf.a $(SNAPPY_DIR)/lib/libsnappy.a
LDFLAGS += -L$(ZLIB_DIR)/lib -lpthread -lz

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LDFLAGS += -lrt
endif

PROTO_SRC=echo_service.proto
PROTO_OBJ=$(patsubst %.proto,%.pb.o,$(PROTO_SRC))
PROTO_OPTIONS=--proto_path=. --proto_path=$(SOFA_PBRPC)/include --proto_path=$(PROTOBUF_DIR)/include

BIN=server client_sync client_async

all: check_depends $(BIN)

.PHONY: check_depends clean

check_depends:
@if [ ! -f "$(PROTOBUF_DIR)/include/google/protobuf/message.h" ]; then echo "ERROR: need protobuf header"; exit 1; fi
@if [ ! -f "$(PROTOBUF_DIR)/lib/libprotobuf.a" ]; then echo "ERROR: need protobuf lib"; exit 1; fi
@if [ ! -f "$(PROTOBUF_DIR)/bin/protoc" ]; then echo "ERROR: need protoc binary"; exit 1; fi
@if [ ! -f "$(SNAPPY_DIR)/include/snappy.h" ]; then echo "ERROR: need snappy header"; exit 1; fi
@if [ ! -f "$(SNAPPY_DIR)/lib/libsnappy.a" ]; then echo "ERROR: need snappy lib"; exit 1; fi
@if [ ! -f "$(SOFA_PBRPC)/include/sofa/pbrpc/pbrpc.h" ]; then echo "ERROR: need sofa-pbrpc header"; exit 1; fi
@if [ ! -f "$(SOFA_PBRPC)/lib/libsofa-pbrpc.a" ]; then echo "ERROR: need sofa-pbrpc lib"; exit 1; fi

clean:
@rm -f $(BIN) *.o *.pb.* cookie_plugin/*.o

rebuild: clean all

server: $(PROTO_OBJ) server.o cookie_plugin/rpc_cookie.o
$(CXX) $^ -o $@ $(LIBRARY) $(LDFLAGS)

client_sync: $(PROTO_OBJ) client_sync.o cookie_plugin/rpc_cookie.o
$(CXX) $^ -o $@ $(LIBRARY) $(LDFLAGS)

client_async: $(PROTO_OBJ) client_async.o cookie_plugin/rpc_cookie.o
$(CXX) $^ -o $@ $(LIBRARY) $(LDFLAGS)

%.pb.o: %.pb.cc
$(CXX) $(CXXFLAGS) -c $< -o $@

%.pb.cc: %.proto
$(PROTOBUF_DIR)/bin/protoc $(PROTO_OPTIONS) --cpp_out=. $<

%.o: %.cc $(PROTO_OBJ)
$(CXX) $(CXXFLAGS) -c $< -o $@

87 changes: 87 additions & 0 deletions sample/echo_cookie/client_async.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2014 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: qinzuoyan01@baidu.com (Qin Zuoyan)

#include <unistd.h>
#include <sofa/pbrpc/pbrpc.h>
#include "cookie_plugin/rpc_cookie.h"
#include "echo_service.pb.h"


typedef sofa::pbrpc::shared_ptr<sofa::pbrpc::Cookie> CookiePtr;
sofa::pbrpc::RpcCookieManager cookie_manager;

void EchoCallback(sofa::pbrpc::RpcController* cntl,
sofa::pbrpc::test::EchoRequest* request,
sofa::pbrpc::test::EchoResponse* response,
bool* callbacked)
{
SLOG(NOTICE, "RemoteAddress=%s", cntl->RemoteAddress().c_str());
SLOG(NOTICE, "IsRequestSent=%s", cntl->IsRequestSent() ? "true" : "false");
if (cntl->IsRequestSent())
{
SLOG(NOTICE, "LocalAddress=%s", cntl->LocalAddress().c_str());
SLOG(NOTICE, "SentBytes=%ld", cntl->SentBytes());
}

if (cntl->Failed()) {
SLOG(ERROR, "request failed: %s", cntl->ErrorText().c_str());
}
else {
SLOG(NOTICE, "request succeed: %s", response->message().c_str());
CookiePtr cookie(new sofa::pbrpc::Cookie(&cookie_manager));
cntl->GetResponseAttachment(cookie.get());
std::string version;
cookie->Get("version", version);
SLOG(NOTICE, "cookie version : %s", version.c_str());
cookie->Store();
}

delete cntl;
delete request;
delete response;
*callbacked = true;
}

int main()
{
SOFA_PBRPC_SET_LOG_LEVEL(NOTICE);

// Define an rpc server.
sofa::pbrpc::RpcClientOptions client_options;
sofa::pbrpc::RpcClient rpc_client(client_options);

// Define an rpc channel.
sofa::pbrpc::RpcChannelOptions channel_options;
sofa::pbrpc::RpcChannel rpc_channel(&rpc_client, "127.0.0.1:12300", channel_options);

CookiePtr cookie(new sofa::pbrpc::Cookie(&cookie_manager));
cookie->Set("type", "async");
cookie->Set("logid", "123456");

// Prepare parameters.
sofa::pbrpc::RpcController* cntl = new sofa::pbrpc::RpcController();
cntl->SetTimeout(3000);
cntl->SetRequestAttachment(cookie.get());
sofa::pbrpc::test::EchoRequest* request = new sofa::pbrpc::test::EchoRequest();
request->set_message("Hello from qinzuoyan01");
sofa::pbrpc::test::EchoResponse* response = new sofa::pbrpc::test::EchoResponse();
bool callbacked = false;
google::protobuf::Closure* done = sofa::pbrpc::NewClosure(
&EchoCallback, cntl, request, response, &callbacked);

// Async call.
sofa::pbrpc::test::EchoServer_Stub stub(&rpc_channel);
stub.Echo(cntl, request, response, done);

// Wait call done.
while (!callbacked) {
usleep(100000);
}

return EXIT_SUCCESS;
}

/* vim: set ts=4 sw=4 sts=4 tw=100 */
79 changes: 79 additions & 0 deletions sample/echo_cookie/client_sync.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2014 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: qinzuoyan01@baidu.com (Qin Zuoyan)

#include <sofa/pbrpc/pbrpc.h>
#include "cookie_plugin/rpc_cookie.h"
#include "echo_service.pb.h"
#include <iostream>

typedef sofa::pbrpc::shared_ptr<sofa::pbrpc::Cookie> CookiePtr;
sofa::pbrpc::RpcCookieManager cookie_manager;

// Using global RpcClient object can help share resources such as threads and buffers.
sofa::pbrpc::RpcClient g_rpc_client;

int main()
{
SOFA_PBRPC_SET_LOG_LEVEL(NOTICE);

// Define an rpc channel.
sofa::pbrpc::RpcChannelOptions channel_options;
sofa::pbrpc::RpcChannel rpc_channel(&g_rpc_client, "127.0.0.1:12300", channel_options);

// Prepare parameters.
sofa::pbrpc::RpcController* cntl = new sofa::pbrpc::RpcController();
cntl->SetTimeout(30000);
CookiePtr cookie(new sofa::pbrpc::Cookie(&cookie_manager));
cookie->Set("type", "sync");
cookie->Set("logid", "123456");
cntl->SetRequestAttachment(cookie.get());
sofa::pbrpc::test::EchoRequest* request =
new sofa::pbrpc::test::EchoRequest();
request->set_message("Hello from qinzuoyan01");
sofa::pbrpc::test::EchoResponse* response =
new sofa::pbrpc::test::EchoResponse();

// Sync call.
sofa::pbrpc::test::EchoServer_Stub* stub =
new sofa::pbrpc::test::EchoServer_Stub(&rpc_channel);
stub->Echo(cntl, request, response, NULL);

// Check if the request has been sent.
// If has been sent, then can get the sent bytes.
SLOG(NOTICE, "RemoteAddress=%s", cntl->RemoteAddress().c_str());
SLOG(NOTICE, "IsRequestSent=%s", cntl->IsRequestSent() ? "true" : "false");
if (cntl->IsRequestSent())
{
SLOG(NOTICE, "LocalAddress=%s", cntl->LocalAddress().c_str());
SLOG(NOTICE, "SentBytes=%ld", cntl->SentBytes());
}

// Check if failed.
if (cntl->Failed())
{
SLOG(ERROR, "request failed: %s", cntl->ErrorText().c_str());
}
else
{
SLOG(NOTICE, "request succeed: %s", response->message().c_str());
cookie.reset(new sofa::pbrpc::Cookie(&cookie_manager));
cntl->GetResponseAttachment(cookie.get());
std::string version;
cookie->Get("version", version);
SLOG(NOTICE, "cookie version : %s", version.c_str());
cookie->Store();
}

// Destroy objects.
delete cntl;
delete request;
delete response;
delete stub;

return EXIT_SUCCESS;
}

/* vim: set ts=4 sw=4 sts=4 tw=100 */
Loading