Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pd branch #67

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ FROM mcr.microsoft.com/devcontainers/cpp:0-debian-11
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install curl

COPY ./install-mariadb.sh /tmp/
RUN chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh
# COPY ./install-mariadb.sh /tmp/
# RUN chmod +x /tmp/install-mariadb.sh && /tmp/install-mariadb.sh && rm -f /tmp/install-mariadb.sh

ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="3.21.5"

Expand All @@ -33,4 +33,7 @@ RUN chmod +x /tmp/install-protobuf.sh && /tmp/install-protobuf.sh && rm -f /tmp/
# install tiny xml
ARG NUM_COMPILE_JOBS="3"
COPY ./tinyxml.sh /tmp/
RUN chmod +x /tmp/tinyxml.sh && /tmp/tinyxml.sh && rm -f /tmp/tinyxml.sh
RUN chmod +x /tmp/tinyxml.sh && /tmp/tinyxml.sh && rm -f /tmp/tinyxml.sh

COPY ./install-mysql.sh /tmp/
RUN chmod +x /tmp/install-mysql.sh && /tmp/install-mysql.sh && rm -f /tmp/install-mysql.sh
17 changes: 7 additions & 10 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp-mariadb
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp
{
"name": "TinyRPC",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"features": {
// "ghcr.io/devcontainers-contrib/features/protoc-asdf:1":{},
// "ghcr.io/balazs23/devcontainers-features/bazel:1":{},
"ghcr.io/devcontainers-contrib/features/apt-get-packages:1": {}
"build": {
"dockerfile": "Dockerfile"
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// This is the recommended way to access the container from the host
// "forwardPorts": [3306],
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "gcc -v",

// Configure tool-specific properties.
// "customizations": {},
Expand Down
34 changes: 0 additions & 34 deletions .devcontainer/docker-compose.yml

This file was deleted.

13 changes: 13 additions & 0 deletions .devcontainer/install-mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
echo "Building mysql connector"

sudo apt-get update
# sudo apt-get install libssl-dev libmysqlclient-dev
sudo apt-get install libmysqlcppconn-dev
wget https://dev.mysql.com/get/Downloads/Connector-C++/mysql-connector-c++-8.0.26-src.tar.gz
tar -xvf mysql-connector-c++-8.0.26-src.tar.gz
cd mysql-connector-c++-8.0.26-src
cmake .
make
sudo make install
cd ..
rm -rf /mysql-connector-c++-8.0.26-src.tar.gz
Empty file modified .devcontainer/reinstall-cmake.sh
100644 → 100755
Empty file.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(PATH_BIN bin)
set(PATH_TESTCASES testcases)

include_directories(${PROJECT_SOURCE_DIR})
include_directories(/usr/include)
# *.cc
aux_source_directory(${PROJECT_SOURCE_DIR}/tinyrpc/comm COMM)
aux_source_directory(${PROJECT_SOURCE_DIR}/tinyrpc/coroutine COR)
Expand All @@ -32,6 +33,9 @@ else ()
message(FATAL_ERROR "Cannot find Protobuf")
endif ()

find_package(Boost REQUIRED)
find_library(MYSQLCPPCONN_LIBRARY mysqlcppconn)

set(tinyxml /usr/lib/libtinyxml.a)

set(LIBS
Expand Down Expand Up @@ -81,6 +85,16 @@ add_executable(test_coroutine ${test_coroutine})
target_link_libraries(test_coroutine ${LIBS})
install(TARGETS test_coroutine DESTINATION ${PATH_BIN})

# db tool
set(
db_tool
${PROJECT_SOURCE_DIR}/${PATH_TESTCASES}/db_tool.cc
)
add_executable(db_tool ${db_tool})
target_link_libraries(db_tool ${LIBS})
target_link_libraries(db_tool ${MYSQLCPPCONN_LIBRARY})
install(TARGETS db_tool DESTINATION ${PATH_BIN})

# install *.h
add_subdirectory(tinyrpc/comm)
add_subdirectory(tinyrpc/coroutine)
Expand Down
9 changes: 4 additions & 5 deletions conf/test_tinypb_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@
</server>

<database>
<db_key name="test_db_key1">
<!-- <ip>127.0.0.1</ip> -->
<ip>192.168.245.7</ip>
<db_key name="entrytask">
<ip>10.53.48.90</ip>
<port>3306</port>
<user>root</user>
<passwd>Ikerli20220517!!</passwd>
<select_db></select_db>
<passwd>123456</passwd>
<select_db>userinfo</select_db>
<char_set>utf8mb4</char_set>
</db_key>
</database>
Expand Down
46 changes: 46 additions & 0 deletions testcases/db_tool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

int main(int argc, char* argv[]) {
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;

// Create a MySQL driver instance
driver = sql::mysql::get_mysql_driver_instance();

// Establish a connection to the MySQL server
con = driver->connect("tcp://10.53.48.90:3306", "root", "123456");

// Select the database
con->setSchema("entrytask");

// Create a statement object
stmt = con->createStatement();

// Execute the query and retrieve the result set
res = stmt->executeQuery("select * from userinfo where uid = 1");

// Iterate over the result set and retrieve the row data
while (res->next()) {
// int id = res->getInt("id");
std::string name = res->getString("username");
// Retrieve other column values as needed
// ...

// Process the row data
// ...
std::cout << "selected name ="<< name << std::endl;

}

// Clean up resources
delete res;
delete stmt;
delete con;

return 0;
}
47 changes: 30 additions & 17 deletions testcases/test_tinypb_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
#include "tinyrpc/comm/start.h"
#include "test_tinypb_server.pb.h"

#include <sys/stat.h>

static int i = 0;
tinyrpc::CoroutineMutex g_cor_mutex;

class QueryServiceImpl : public QueryService {
public:
class QueryServiceImpl : public QueryService
{
public:
QueryServiceImpl() {}
~QueryServiceImpl() {}

Expand All @@ -30,18 +32,21 @@ class QueryServiceImpl : public QueryService {

AppInfoLog("QueryServiceImpl.query_name, res={%s}", response->ShortDebugString().c_str());

if (done) {

if (done)
{
done->Run();
}

}

void query_age(google::protobuf::RpcController* controller,
const ::queryAgeReq* request,
::queryAgeRes* response,
::google::protobuf::Closure* done) {
void query_age(google::protobuf::RpcController *controller,
const ::queryAgeReq *request,
::queryAgeRes *response,
::google::protobuf::Closure *done)
{

AppInfoLog("QueryServiceImpl.query_age, req={%s}", request->ShortDebugString().c_str());

// AppInfoLog << "QueryServiceImpl.query_age, sleep 6 s begin";
// sleep(6);
// AppInfoLog << "QueryServiceImpl.query_age, sleep 6 s end";
Expand All @@ -59,31 +64,39 @@ class QueryServiceImpl : public QueryService {
AppDebugLog("end i = %d", i);
g_cor_mutex.unlock();

if (done) {
if (done)
{
done->Run();
}
// printf("response = %s\n", response->ShortDebugString().c_str());

AppInfoLog("QueryServiceImpl.query_age, res={%s}", response->ShortDebugString().c_str());

}

};

int main(int argc, char *argv[])
{

struct stat attr;

auto config = "../conf/test_tinypb_server.xml";

int main(int argc, char* argv[]) {
if (argc != 2) {
if (::lstat(config, &attr) != 0 && argc != 2)
{
printf("Start TinyRPC server error, input argc is not 2!");
printf("Start TinyRPC server like this: \n");
printf("./server a.xml\n");
return 0;
}

tinyrpc::InitConfig(argv[1]);
if (argc == 2)
{
config = argv[1];
}

tinyrpc::InitConfig(config);

REGISTER_SERVICE(QueryServiceImpl);

tinyrpc::StartRpcServer();

return 0;
}
2 changes: 1 addition & 1 deletion testcases/test_tinypb_server_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

void test_client() {

tinyrpc::IPAddress::ptr addr = std::make_shared<tinyrpc::IPAddress>("127.0.0.1", 39999);
tinyrpc::IPAddress::ptr addr = std::make_shared<tinyrpc::IPAddress>("127.0.0.1", 20000);

tinyrpc::TinyPbRpcChannel channel(addr);
QueryService_Stub stub(&channel);
Expand Down
Loading