Skip to content

Commit

Permalink
[TUBEMQ-399] expose C++ SDK method by Pybind11 (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: dockerzhang <dockerzhang@tencent.com>
  • Loading branch information
dockerzhang and dockerzhang committed Nov 2, 2020
1 parent 831f2e8 commit 0981ae2
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include <pybind11/pybind11.h>
#include "tubemq/tubemq_client.h"

namespace py = pybind11;

using namespace tubemq;
using std::string;

PYBIND11_MODULE(tubemq_client, m) {
m.def("startTubeMQService", &StartTubeMQService, "start TubeMQ Service");
m.def("stopTubeMQService", &StopTubeMQService, "stop TubeMQ Service");

py::class_<TubeMQConsumer>(m, "TubeMQConsumer")
.def(py::init<>())
.def("start", &TubeMQConsumer::Start)
.def("shutDown", &TubeMQConsumer::ShutDown)
.def("getClientId", &TubeMQConsumer::GetClientId)
.def("getMessage", &TubeMQConsumer::GetMessage)
.def("confirm", &TubeMQConsumer::Confirm)
.def("getCurConsumedInfo", &TubeMQConsumer::GetCurConsumedInfo);
}
42 changes: 42 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "tubemq/tubemq_config.h"

namespace py = pybind11;

using namespace tubemq;
using std::map;
using std::set;
using std::string;

PYBIND11_MODULE(tubemq_config, m) {
py::class_<ConsumerConfig>(m, "ConsumerConfig")
.def(py::init<>())
.def("setRpcReadTimeoutMs", &ConsumerConfig::SetRpcReadTimeoutMs)
.def("setMasterAddrInfo", &ConsumerConfig::SetMasterAddrInfo)
.def("setGroupConsumeTarget", static_cast<bool (ConsumerConfig::*)\
(string&, const string&, const set<string>&)>(&ConsumerConfig::SetGroupConsumeTarget), "")
.def("setGroupConsumeTarget", static_cast<bool (ConsumerConfig::*)\
(string&, const string&, const map<string, set<string>>&)>(&ConsumerConfig::SetGroupConsumeTarget), "")
.def("getRpcReadTimeoutMs", &ConsumerConfig::GetRpcReadTimeoutMs)
.def("getMasterAddrInfo", &ConsumerConfig::GetMasterAddrInfo);
}
58 changes: 58 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_errcode.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include <pybind11/pybind11.h>
#include "tubemq/tubemq_errcode.h"

namespace py = pybind11;

using namespace tubemq;
using namespace tubemq::err_code;
using std::string;

PYBIND11_MODULE(tubemq_errcode, m) {
py::enum_<Result>(m, "Result")
.value("kErrSuccess", Result::kErrSuccess)
.value("kErrNotReady", Result::kErrNotReady)
.value("kErrMoved", Result::kErrMoved)
.value("kErrBadRequest", Result::kErrBadRequest)
.value("kErrUnAuthorized", Result::kErrUnAuthorized)
.value("kErrForbidden", Result::kErrForbidden)
.value("kErrNotFound", Result::kErrNotFound)
.value("kErrNoPartAssigned", Result::kErrNoPartAssigned)
.value("kErrAllPartWaiting", Result::kErrAllPartWaiting)
.value("kErrAllPartInUse", Result::kErrAllPartInUse)
.value("kErrPartitionOccupied", Result::kErrPartitionOccupied)
.value("kErrHbNoNode", Result::kErrHbNoNode)
.value("kErrDuplicatePartition", Result::kErrDuplicatePartition)
.value("kErrCertificateFailure", Result::kErrCertificateFailure)
.value("kErrServerOverflow", Result::kErrServerOverflow)
.value("kErrConsumeGroupForbidden", Result::kErrConsumeGroupForbidden)
.value("kErrConsumeSpeedLimit", Result::kErrConsumeSpeedLimit)
.value("kErrConsumeContentForbidden", Result::kErrConsumeContentForbidden)
.export_values();

py::class_<ErrorCode>(m, "ErrorCode")
.def(py::init<>())
.def(py::init<int, const string&>())
.def("clear", &ErrorCode::Clear)
.def("assign", &ErrorCode::Assign)
.def("value", &ErrorCode::Value)
.def("message", &ErrorCode::Message);
}
40 changes: 40 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_message.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include <pybind11/pybind11.h>
#include <stdint.h>
#include "tubemq/tubemq_message.h"

namespace py = pybind11;

using namespace tubemq;
using std::string;

PYBIND11_MODULE(tubemq_message, m) {
py::class_<Message>(m, "Message")
.def(py::init<>())
.def(py::init<const Message&>())
.def(py::init<const string&, const char*, uint32_t>())
.def("getMessageId", &Message::GetMessageId)
.def("setMessageId", &Message::SetMessageId)
.def("getTopic", &Message::GetTopic)
.def("setTopic", &Message::SetTopic)
.def("getData", &Message::GetData)
.def("getDataLength", &Message::GetDataLength);
}
46 changes: 46 additions & 0 deletions tubemq-client-twins/tubemq-client-python/src/cpp/tubemq_return.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <stdlib.h>
#include "tubemq/tubemq_return.h"

namespace py = pybind11;

using namespace tubemq;
using std::string;

PYBIND11_MODULE(tubemq_return, m) {
py::class_<ConsumerResult>(m, "ConsumerResult")
.def(py::init<>())
.def(py::init<const ConsumerResult&>())
.def(py::init<int32_t, string>())
.def("setFailureResult", static_cast<void (ConsumerResult::*)\
(int32_t, string)>(&ConsumerResult::SetFailureResult), "set Failure Result")
.def("isSuccess", &ConsumerResult::IsSuccess)
.def("getErrCode", &ConsumerResult::GetErrCode)
.def("getErrMessage", &ConsumerResult::GetErrMessage)
.def("getTopicName", &ConsumerResult::GetTopicName)
.def("getPeerInfo", &ConsumerResult::GetPeerInfo)
.def("getConfirmContext", &ConsumerResult::GetConfirmContext)
.def("getMessageList", &ConsumerResult::GetMessageList)
.def("getPartitionKey", &ConsumerResult::GetPartitionKey)
.def("getCurrOffset", &ConsumerResult::GetCurrOffset);
}

0 comments on commit 0981ae2

Please sign in to comment.