From fbf0bd2e40f7b3309f15b056bcbabf3e1dea4981 Mon Sep 17 00:00:00 2001 From: ihsandemir Date: Tue, 16 Apr 2019 21:54:04 +0300 Subject: [PATCH] Implemented HazelcastClient::getLocalEndpoint(). fixes https://github.com/hazelcast/hazelcast-cpp-client/issues/524 --- hazelcast/include/hazelcast/client/Client.h | 51 +++++++++++++++ hazelcast/include/hazelcast/client/Endpoint.h | 65 +++++++++++++++++++ .../hazelcast/client/HazelcastClient.h | 9 +++ .../client/impl/HazelcastClientInstanceImpl.h | 3 + .../client/spi/ClientClusterService.h | 7 +- .../spi/impl/ClientClusterServiceImpl.h | 3 + hazelcast/src/hazelcast/client/Client.cpp | 27 ++++++++ hazelcast/src/hazelcast/client/Endpoint.cpp | 32 +++++++++ .../src/hazelcast/client/HazelcastClient.cpp | 4 ++ .../impl/HazelcastClientInstanceImpl.cpp | 4 ++ .../spi/impl/ClientClusterServiceImpl.cpp | 25 +++++-- .../test/src/cluster/ClientEndpointTest.cpp | 62 ++++++++++++++++++ 12 files changed, 286 insertions(+), 6 deletions(-) create mode 100644 hazelcast/include/hazelcast/client/Client.h create mode 100644 hazelcast/include/hazelcast/client/Endpoint.h create mode 100644 hazelcast/src/hazelcast/client/Client.cpp create mode 100644 hazelcast/src/hazelcast/client/Endpoint.cpp create mode 100644 hazelcast/test/src/cluster/ClientEndpointTest.cpp diff --git a/hazelcast/include/hazelcast/client/Client.h b/hazelcast/include/hazelcast/client/Client.h new file mode 100644 index 0000000000..b2f9489d7f --- /dev/null +++ b/hazelcast/include/hazelcast/client/Client.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2008-2019, Hazelcast, 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. + */ +#ifndef HAZElCAST_CLIENT_CLIENT_H_ +#define HAZElCAST_CLIENT_CLIENT_H_ + +#include "hazelcast/client/Endpoint.h" + +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) +#pragma warning(push) +#pragma warning(disable: 4251) //for dll export +#endif + +namespace hazelcast { + namespace client { + /** + * The Client interface allows to get information about + * a connected client's socket address, type and UUID. + * + */ + class HAZELCAST_API Client : public Endpoint { + public: + Client(const boost::shared_ptr &uuid, const boost::shared_ptr
&socketAddress, + const std::string &name); + + const std::string &getName() const; + + private: + std::string name; + }; + } +} + +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) +#pragma warning(pop) +#endif + +#endif //HAZElCAST_CLIENT_CLIENT_H_ + diff --git a/hazelcast/include/hazelcast/client/Endpoint.h b/hazelcast/include/hazelcast/client/Endpoint.h new file mode 100644 index 0000000000..5b70f95f7b --- /dev/null +++ b/hazelcast/include/hazelcast/client/Endpoint.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2008-2019, Hazelcast, 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. + */ +#ifndef HAZElCAST_CLIENT_ENDPOINT_H_ +#define HAZElCAST_CLIENT_ENDPOINT_H_ + +#include +#include + +#include "hazelcast/client/Address.h" + +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) +#pragma warning(push) +#pragma warning(disable: 4251) //for dll export +#endif + +namespace hazelcast { + namespace client { + /** + * Endpoint represents a peer in the cluster. + * It is the client. + */ + class HAZELCAST_API Endpoint { + public: + Endpoint(boost::shared_ptr uuid, boost::shared_ptr
socketAddress); + + /** + * Returns the UUID of this endpoint + * + * @return the UUID of this endpoint + */ + const boost::shared_ptr &getUuid() const; + + /** + * Returns the socket address for this endpoint. + * + * @return the socket address for this endpoint + */ + const boost::shared_ptr
&getSocketAddress() const; + + private: + boost::shared_ptr uuid; + boost::shared_ptr
socketAddress; + }; + } +} + +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) +#pragma warning(pop) +#endif + +#endif //HAZElCAST_CLIENT_ENDPOINT_H_ + diff --git a/hazelcast/include/hazelcast/client/HazelcastClient.h b/hazelcast/include/hazelcast/client/HazelcastClient.h index da62ba07f0..8a2b1a4e3b 100644 --- a/hazelcast/include/hazelcast/client/HazelcastClient.h +++ b/hazelcast/include/hazelcast/client/HazelcastClient.h @@ -664,6 +664,15 @@ namespace hazelcast { */ Cluster& getCluster(); + /** + * Returns the local endpoint which this HazelcastInstance belongs to. + *

+ * + * @return the local enpoint which this client belongs to + * @see Client + */ + Client getLocalEndpoint() const; + /** * Add listener to listen lifecycle events. * diff --git a/hazelcast/include/hazelcast/client/impl/HazelcastClientInstanceImpl.h b/hazelcast/include/hazelcast/client/impl/HazelcastClientInstanceImpl.h index ba10771fdb..077df52322 100644 --- a/hazelcast/include/hazelcast/client/impl/HazelcastClientInstanceImpl.h +++ b/hazelcast/include/hazelcast/client/impl/HazelcastClientInstanceImpl.h @@ -52,6 +52,7 @@ #include "hazelcast/client/impl/statistics/Statistics.h" #include "hazelcast/client/FlakeIdGenerator.h" #include "hazelcast/client/IExecutorService.h" +#include "hazelcast/client/Client.h" #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) #pragma warning(push) @@ -385,6 +386,8 @@ namespace hazelcast { */ Cluster& getCluster(); + Client getLocalEndpoint() const; + /** * Add listener to listen lifecycle events. * diff --git a/hazelcast/include/hazelcast/client/spi/ClientClusterService.h b/hazelcast/include/hazelcast/client/spi/ClientClusterService.h index 404b7bdd8a..afd3cd8fce 100644 --- a/hazelcast/include/hazelcast/client/spi/ClientClusterService.h +++ b/hazelcast/include/hazelcast/client/spi/ClientClusterService.h @@ -19,7 +19,7 @@ #include #include -#include "hazelcast/util/HazelcastDll.h" +#include "hazelcast/client/Client.h" #include "hazelcast/client/Member.h" #include "hazelcast/client/MembershipListener.h" @@ -36,6 +36,11 @@ namespace hazelcast { } } namespace spi { + /** + * @return The client interface representing the local client. + */ + Client getLocalClient(); + /** * Cluster service for Hazelcast clients. * diff --git a/hazelcast/include/hazelcast/client/spi/impl/ClientClusterServiceImpl.h b/hazelcast/include/hazelcast/client/spi/impl/ClientClusterServiceImpl.h index d01dedc0c8..26cb6970ef 100644 --- a/hazelcast/include/hazelcast/client/spi/impl/ClientClusterServiceImpl.h +++ b/hazelcast/include/hazelcast/client/spi/impl/ClientClusterServiceImpl.h @@ -27,6 +27,7 @@ #include "hazelcast/client/Member.h" #include "hazelcast/util/Atomic.h" #include "hazelcast/util/SynchronizedMap.h" +#include "hazelcast/client/Client.h" #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) #pragma warning(push) @@ -78,6 +79,8 @@ namespace hazelcast { virtual int getSize(); + Client getLocalClient() const; + private: ClientContext &client; boost::shared_ptr clientMembershipListener; diff --git a/hazelcast/src/hazelcast/client/Client.cpp b/hazelcast/src/hazelcast/client/Client.cpp new file mode 100644 index 0000000000..e29e461f8a --- /dev/null +++ b/hazelcast/src/hazelcast/client/Client.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2008-2019, Hazelcast, 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. + */ +#include "hazelcast/client/Client.h" + +namespace hazelcast { + namespace client { + Client::Client(const boost::shared_ptr &uuid, const boost::shared_ptr

&socketAddress, + const std::string &name) : Endpoint(uuid, socketAddress), name(name) {} + + const std::string &Client::getName() const { + return name; + } + } +} diff --git a/hazelcast/src/hazelcast/client/Endpoint.cpp b/hazelcast/src/hazelcast/client/Endpoint.cpp new file mode 100644 index 0000000000..1ac8ae0cf1 --- /dev/null +++ b/hazelcast/src/hazelcast/client/Endpoint.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008-2019, Hazelcast, 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. + */ +#include "hazelcast/client/Endpoint.h" + +namespace hazelcast { + namespace client { + Endpoint::Endpoint(boost::shared_ptr uuid, boost::shared_ptr
socketAddress) : uuid(uuid), + socketAddress( + socketAddress) {} + + const boost::shared_ptr &Endpoint::getUuid() const { + return uuid; + } + + const boost::shared_ptr
&Endpoint::getSocketAddress() const { + return socketAddress; + } + } +} diff --git a/hazelcast/src/hazelcast/client/HazelcastClient.cpp b/hazelcast/src/hazelcast/client/HazelcastClient.cpp index 4916f735e8..b06bc4b816 100644 --- a/hazelcast/src/hazelcast/client/HazelcastClient.cpp +++ b/hazelcast/src/hazelcast/client/HazelcastClient.cpp @@ -110,6 +110,10 @@ namespace hazelcast { boost::shared_ptr HazelcastClient::getExecutorService(const std::string &name) { return clientImpl->getExecutorService(name); } + + Client HazelcastClient::getLocalEndpoint() const { + return clientImpl->getLocalEndpoint(); + } } } diff --git a/hazelcast/src/hazelcast/client/impl/HazelcastClientInstanceImpl.cpp b/hazelcast/src/hazelcast/client/impl/HazelcastClientInstanceImpl.cpp index c4acbe7a1d..8ab311e517 100644 --- a/hazelcast/src/hazelcast/client/impl/HazelcastClientInstanceImpl.cpp +++ b/hazelcast/src/hazelcast/client/impl/HazelcastClientInstanceImpl.cpp @@ -359,6 +359,10 @@ namespace hazelcast { return boost::static_pointer_cast(proxy); } + + Client HazelcastClientInstanceImpl::getLocalEndpoint() const { + return clusterService.getLocalClient(); + } } } } diff --git a/hazelcast/src/hazelcast/client/spi/impl/ClientClusterServiceImpl.cpp b/hazelcast/src/hazelcast/client/spi/impl/ClientClusterServiceImpl.cpp index 36becd8a30..fbca849aed 100644 --- a/hazelcast/src/hazelcast/client/spi/impl/ClientClusterServiceImpl.cpp +++ b/hazelcast/src/hazelcast/client/spi/impl/ClientClusterServiceImpl.cpp @@ -15,6 +15,7 @@ */ #include +#include #include "hazelcast/client/spi/impl/ClientClusterServiceImpl.h" #include "hazelcast/client/spi/ClientContext.h" @@ -23,6 +24,7 @@ #include "hazelcast/client/InitialMembershipEvent.h" #include "hazelcast/client/spi/impl/ClientMembershipListener.h" #include "hazelcast/client/cluster/memberselector/MemberSelectors.h" +#include "hazelcast/client/connection/ClientConnectionManagerImpl.h" namespace hazelcast { namespace client { @@ -168,11 +170,11 @@ namespace hazelcast { std::vector ClientClusterServiceImpl::getMembers(const cluster::memberselector::MemberSelector &selector) { std::vector result; - BOOST_FOREACH(const Member &member , getMemberList()) { - if (selector.select(member)) { - result.push_back(member); - } - } + BOOST_FOREACH(const Member &member, getMemberList()) { + if (selector.select(member)) { + result.push_back(member); + } + } return result; } @@ -180,6 +182,19 @@ namespace hazelcast { int ClientClusterServiceImpl::getSize() { return (int) getMemberList().size(); } + + Client ClientClusterServiceImpl::getLocalClient() const { + connection::ClientConnectionManagerImpl &cm = client.getConnectionManager(); + boost::shared_ptr connection = cm.getOwnerConnection(); + boost::shared_ptr
inetSocketAddress = + connection.get() != NULL ? boost::shared_ptr
(connection->getLocalSocketAddress()) + : boost::shared_ptr
(); + const boost::shared_ptr principal = cm.getPrincipal(); + boost::shared_ptr uuid = + principal.get() != NULL ? boost::make_shared(*principal->getUuid()) + : boost::shared_ptr(); + return Client(uuid, inetSocketAddress, client.getName()); + } } } } diff --git a/hazelcast/test/src/cluster/ClientEndpointTest.cpp b/hazelcast/test/src/cluster/ClientEndpointTest.cpp new file mode 100644 index 0000000000..309a360701 --- /dev/null +++ b/hazelcast/test/src/cluster/ClientEndpointTest.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2008-2019, Hazelcast, 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. + */ +/** + * This has to be the first include, so that Python.h is the first include. Otherwise, compilation warning such as + * "_POSIX_C_SOURCE" redefined occurs. + */ +#include "HazelcastServerFactory.h" +#include "ClientTestSupport.h" +#include "HazelcastServer.h" + +#include +#include +#include +#include +#include + +namespace hazelcast { + namespace client { + namespace test { + class ClientEnpointTest : public ClientTestSupport { + }; + + TEST_F(ClientEnpointTest, testConnectedClientEnpoint) { + HazelcastServer instance(*g_srvFactory); + + HazelcastClient client; + const Client endpoint = client.getLocalEndpoint(); + spi::ClientContext context(client); + ASSERT_EQ(context.getName(), endpoint.getName()); + + boost::shared_ptr
endpointAddress = endpoint.getSocketAddress(); + ASSERT_NOTNULL(endpointAddress.get(), Address); + connection::ClientConnectionManagerImpl &connectionManager = context.getConnectionManager(); + boost::shared_ptr connection = connectionManager.getOwnerConnection(); + ASSERT_NOTNULL(connection.get(), connection::Connection); + std::auto_ptr
localAddress = connection->getLocalSocketAddress(); + ASSERT_NOTNULL(localAddress.get(), Address); + ASSERT_EQ(*localAddress, *endpointAddress); + + boost::shared_ptr principal = connectionManager.getPrincipal(); + ASSERT_NOTNULL(principal.get(), protocol::Principal); + ASSERT_NOTNULL(principal->getUuid(), std::string); + ASSERT_EQ_PTR((*principal->getUuid()), endpoint.getUuid().get(), std::string); + } + } + } +} + +