Skip to content

Commit

Permalink
generate shared library distribusions automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
ptesavol committed Oct 7, 2024
1 parent 38977a2 commit 9926de6
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 44 deletions.
25 changes: 22 additions & 3 deletions packages/streamr-libstreamrproxyclient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/monorepoPackage.cmake)

set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")

set (SHAREDLIB_VERSION 1.0.0)
set (SHAREDLIB_SOVERSION 1)

project(streamr-streamrproxyclient CXX)
add_library(streamrproxyclient SHARED
src/streamrproxyclient.cpp
include/streamrproxyclient.h
)

set_target_properties(streamrproxyclient PROPERTIES VERSION ${SHAREDLIB_VERSION} SOVERSION ${SHAREDLIB_SOVERSION})
find_package(streamr-trackerless-network CONFIG REQUIRED)
find_package(streamr-dht CONFIG REQUIRED)
find_package(streamr-logger CONFIG REQUIRED)
Expand All @@ -44,8 +48,8 @@ if (IOS)
FRAMEWORK TRUE
FRAMEWORK_VERSION A
MACOSX_FRAMEWORK_IDENTIFIER network.streamr.streamrproxyclient
VERSION 1.0.0
SOVERSION 1.0.0
VERSION ${SHAREDLIB_VERSION}
SOVERSION ${SHAREDLIB_SOVERSION}
PUBLIC_HEADER include/streamrproxyclient.h
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Petri Savolainen"
)
Expand All @@ -58,7 +62,7 @@ if (NOT IOS)
find_package(folly CONFIG REQUIRED)

add_executable(streamr-streamrproxyclient-test-integration test/integration/StreamrProxyClientTest.cpp)
target_link_directories(streamr-streamrproxyclient-test-integration PUBLIC ${CMAKE_CURRENT_LIST_DIR}/build)
target_link_directories(streamr-streamrproxyclient-test-integration PUBLIC ${SHAREDLIB_OUTPUT_DIRECTORY})

target_include_directories(streamr-streamrproxyclient-test-integration PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

Expand Down Expand Up @@ -97,5 +101,20 @@ if (NOT IOS)
)

endif()
# Install header to dist/target-triplet
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/streamrproxyclient.h
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/dist/${VCPKG_TARGET_TRIPLET}/include)

# Install library to dist/target-triplet
install(TARGETS streamrproxyclient
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/dist/${VCPKG_TARGET_TRIPLET}/lib/${CMAKE_BUILD_TYPE})

# Pack dist/target-triplet using tgz
install(CODE "
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar cvzf streamrproxyclient-${VCPKG_TARGET_TRIPLET}-${SHAREDLIB_VERSION}.tgz ${VCPKG_TARGET_TRIPLET}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist
)
")
endif()

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@ cmake_minimum_required(VERSION 3.26)

project(streamrproxyclientexample CXX)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD 26)

if(APPLE)
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
set(ARCH_DIR "arm64-osx")
else()
set(ARCH_DIR "x64-osx")
endif()
elseif(UNIX AND NOT APPLE)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
set(ARCH_DIR "arm64-linux")
else()
set(ARCH_DIR "x64-linux")
endif()
else()
message(FATAL_ERROR "Unsupported platform")
endif()

add_executable(publisherexample publisherexample.cpp)
target_include_directories(publisherexample PRIVATE ${CMAKE_SOURCE_DIR}/../../include)
target_link_directories(publisherexample PRIVATE ${CMAKE_SOURCE_DIR}/../../build)

target_include_directories(publisherexample PRIVATE ${CMAKE_SOURCE_DIR}/../../dist/${ARCH_DIR}/include)
target_link_directories(publisherexample PRIVATE ${CMAKE_SOURCE_DIR}/../../dist/${ARCH_DIR}/lib/${CMAKE_BUILD_TYPE})
target_link_libraries(publisherexample streamrproxyclient)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

set -e

cd build && cmake .. && cmake --build . && cd ..
cd build && cmake .. && cmake --build . && cmake --install . && cd ..
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
#include <cassert>
#include <chrono>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <thread>
#include "streamrproxyclient.h"

int main() {
static constexpr const char* ownEthereumAddress =
"0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";

static constexpr const char* tsEthereumAddress =
"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
static constexpr const char* tsProxyUrl = "ws://127.0.0.1:44211";
static constexpr const char* tsStreamPartId =
"0xa000000000000000000000000000000000000000#01";

static constexpr const char* productionProxyUrl =
"wss://95.216.15.80:32200";
static constexpr const char* productionStreamPartId =
"0xa000000000000000000000000000000000000000#01";
static constexpr const char* productionEthereumAddress =
"0x5bb9deae7df3d9d7f1ddb2f73d29134127ef4b1b";
std::string generateRandomEthereumAddress() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 15); // NOLINT

std::stringstream ss;
ss << "0x";
for (int i = 0; i < 40; ++i) { // NOLINT
ss << std::hex << dis(gen);
}
return ss.str();
}

int main(int argc, char* argv[]) {
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <proxy_server_url>"
<< " <proxy_server_ethereum_address>"
<< " <stream_part_id>"
<< "\n";
return 1;
}
const char* proxyUrl = argv[1];
const char* proxyServerEthereumAddress = argv[2];
const char* streamPartId = argv[3];

const std::string ownEthereumAddressString =
generateRandomEthereumAddress();
const char* ownEthereumAddress = ownEthereumAddressString.c_str();

Error* errors = nullptr;
uint64_t numErrors = 0;

uint64_t clientHandle =
proxyClientNew(&errors, &numErrors, ownEthereumAddress, tsStreamPartId);
proxyClientNew(&errors, &numErrors, ownEthereumAddress, streamPartId);

assert(numErrors == 0);
assert(errors == nullptr);

Proxy proxy{
.websocketUrl = tsProxyUrl, .ethereumAddress = tsEthereumAddress};
.websocketUrl = proxyUrl,
.ethereumAddress = proxyServerEthereumAddress};

proxyClientConnect(&errors, &numErrors, clientHandle, &proxy, 1);

Expand All @@ -42,8 +57,9 @@ int main() {
std::string message = "Hello from libstreamrproxyclient!";

while (true) {
std::cout << "Publishing message" << "\n";
proxyClientPublish(
std::cout << "Publishing message"
<< "\n";
uint64_t numProxiesPublishedTo = proxyClientPublish(
&errors,
&numErrors,
clientHandle,
Expand All @@ -52,10 +68,16 @@ int main() {

assert(numErrors == 0);
assert(errors == nullptr);
std::cout << "Published message" << "\n";
std::cout << "Sleeping for 15 seconds" << "\n";

std::cout << ownEthereumAddress << " published message "
<< "\"" << message << "\""
<< " to " << numProxiesPublishedTo << " proxies"
<< "\n";
std::cout << "Sleeping for 15 seconds"
<< "\n";
std::this_thread::sleep_for(std::chrono::seconds(15)); // NOLINT
std::cout << "Sleeping done" << "\n";
std::cout << "Sleeping done"
<< "\n";
}

proxyClientDelete(&errors, &numErrors, clientHandle);
Expand Down
10 changes: 9 additions & 1 deletion packages/streamr-libstreamrproxyclient/examples/unix/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

set -e

./build/publisherexample

PROXY_URL="ws://127.0.0.1:44211"
# Read proxy server ethereum address from file
PROXY_ETHEREUM_ADDRESS=$(cat ../../../streamr-trackerless-network/test/integration/ts-integration/proxyEthereumAddress.txt)
STREAM_PART_ID="0xd7278f1e4a946fa7838b5d1e0fe50c5725fb23de/nativesdktest#01"

echo "Running the publisher example: ./build/publisherexample $PROXY_URL $PROXY_ETHEREUM_ADDRESS $STREAM_PART_ID"
# Run the publisher example with the proxy address
./build/publisherexample $PROXY_URL $PROXY_ETHEREUM_ADDRESS $STREAM_PART_ID
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,75 @@ typedef struct Error {
const char* code;
} Error;

/**
* @brief Create a new proxy client.
*
* @param errors The array of errors or NULL.
* @param numErrors The number of errors.
* @param ownEthereumAddress The Ethereum address of the client in format
* 0x1234567890123456789012345678901234567890.
* @param streamPartId The stream part id in format
* 0xa000000000000000000000000000000000000000#01.
* @return The handle of the created client.
*/

EXTERN_C SHARED_EXPORT uint64_t proxyClientNew(
Error** error,
Error** errors,
uint64_t* numErrors,
const char* ownEthereumAddress,
const char* streamPartId);

/**
* @brief Delete a proxy client.
*
* @param errors The array of errors or NULL.
* @param numErrors The number of errors.
* @param clientHandle The client handle of the client to delete.
*/

EXTERN_C SHARED_EXPORT void proxyClientDelete(
Error** errors, uint64_t* numErrors, uint64_t clientHandle);

/**
* @brief Connect a proxy client to a list of proxies.
*
* @param errors The array of errors or NULL.
* @param numErrors The number of errors.
* @param clientHandle The client handle of the client to connect.
* @param proxies The array of proxies.
* @param numProxies The number of proxies.
* @return The number of proxies connected to.
*/

EXTERN_C SHARED_EXPORT uint64_t proxyClientConnect(
Error** errors,
uint64_t* numErrors,
uint64_t clientHandle,
const Proxy* proxies,
uint64_t numProxies);

/**
* @brief Disconnect a proxy client from all proxies.
*
* @param errors The array of errors or NULL.
* @param numErrors The number of errors.
* @param clientHandle The client handle of the client to disconnect.
*/

EXTERN_C SHARED_EXPORT void proxyClientDisconnect(
Error** errors, uint64_t* numErrors, uint64_t clientHandle);

/**
* @brief Publish a message to the stream.
*
* @param errors The array of errors or NULL.
* @param numErrors The number of errors.
* @param clientHandle The client handle of the client to publish.
* @param content The content to publish.
* @param contentLength The length of the content.
* @return The number of proxies to which the message was published to.
*/

EXTERN_C SHARED_EXPORT uint64_t proxyClientPublish(
Error** errors,
uint64_t* numErrors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cd ../streamr-trackerless-network/test/integration/ts-integration

#Run the server

./run-server.sh
./run-server-for-tests.sh

cd -

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cd ../streamr-trackerless-network/test/integration/ts-integration

#Run the server

./run-server.sh
./run-server-for-tests.sh

cd -

Expand Down
17 changes: 10 additions & 7 deletions packages/streamr-libstreamrproxyclient/src/LibProxyClientApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class LibProxyClientApi {
return result.first;
}

void proxyClientPublish(
uint64_t proxyClientPublish(
Error** errors,
uint64_t* numErrors,
uint64_t clientHandle,
Expand All @@ -341,7 +341,7 @@ class LibProxyClientApi {
"Proxy client not found", ERROR_PROXY_CLIENT_NOT_FOUND);
*errors = this->errors.data();
*numErrors = 1;
return;
return 0;
}

this->errors.clear();
Expand All @@ -361,16 +361,19 @@ class LibProxyClientApi {
proxyClient->second->getNextSequenceNumber());
message.mutable_messageid()->CopyFrom(messageId);
try {
proxyClient->second->getProxyClient()->broadcast(message);
auto result =
proxyClient->second->getProxyClient()->broadcast(message);
*errors = nullptr;
*numErrors = 0;
return result;
} catch (const std::exception& e) {
SLogger::error("Error in proxyClientPublish: " + std::string(e.what()));
SLogger::error(
"Error in proxyClientPublish: " + std::string(e.what()));
this->addError(e.what(), ERROR_PROXY_BROADCAST_FAILED);
*errors = this->errors.data();
*numErrors = this->errors.size();
return 0;
}

*errors = nullptr;
*numErrors = 0;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ uint64_t proxyClientConnect(
errors, numErrors, clientHandle, proxies, numProxies);
}

void proxyClientPublish(
uint64_t proxyClientPublish(
Error** errors,
uint64_t* numErrors,
uint64_t clientHandle,
const char* content,
uint64_t contentLength) {
getProxyClientApi().proxyClientPublish(
return getProxyClientApi().proxyClientPublish(
errors, numErrors, clientHandle, content, contentLength);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cd test/integration/ts-integration

#Run the server

./run-server.sh
./run-server-for-tests.sh

cd -

Expand Down

0 comments on commit 9926de6

Please sign in to comment.