Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #52 from ali-ince/1.7-add-encapsulation
Browse files Browse the repository at this point in the history
Introduce encapsulation
  • Loading branch information
ali-ince authored Oct 25, 2018
2 parents b445976 + e3a6ce9 commit 116a161
Show file tree
Hide file tree
Showing 87 changed files with 2,354 additions and 1,386 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ include(InstallRequiredSystemLibraries)
install(
TARGETS
${SEABOLT_SHARED}
${SEABOLT_STATIC}
EXPORT
${SEABOLT_NAME}Targets
ARCHIVE
Expand Down Expand Up @@ -161,7 +162,6 @@ Description: The C Connector Library for Neo4j\n\
Version: ${PROJECT_VERSION}\n\
CFlags: -I\${includedir}\n\
Libs: -L\${libdir} -l\${name}\n\
Libs.private: $<TARGET_PROPERTY:${SEABOLT_SHARED},INTERFACE_LINK_LIBRARIES>\n\
")

install(
Expand Down
35 changes: 27 additions & 8 deletions cmake/Internals.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,36 @@ macro(fix_compiler_settings)
endif ()
endmacro()

macro(find_openssl_both)
macro(clear_openssl_cached_vars)
unset(OPENSSL_FOUND CACHE)
unset(OPENSSL_INCLUDE_DIR CACHE)
unset(OPENSSL_SSL_LIBRARY CACHE)
unset(OPENSSL_CRYPTO_LIBRARY CACHE)
unset(OPENSSL_LIBRARIES CACHE)
unset(OPENSSL_VERSION CACHE)
unset(_openssl_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES CACHE)
unset(_OPENSSL_MSVC_RT_MODE CACHE)
unset(_OPENSSL_MSVC_ARCH_SUFFIX CACHE)
unset(_OPENSSL_PATH_SUFFIXES CACHE)
unset(LIB_EAY_DEBUG CACHE)
unset(LIB_EAY_RELEASE CACHE)
unset(SSL_EAY_DEBUG CACHE)
unset(SSL_EAY_RELEASE CACHE)
unset(LIB_EAY_LIBRARY_DEBUG CACHE)
unset(LIB_EAY_LIBRARY_RELEASE CACHE)
unset(SSL_EAY_LIBRARY_DEBUG CACHE)
unset(SSL_EAY_LIBRARY_RELEASE CACHE)
unset(CMAKE_FIND_LIBRARY_SUFFIXES CACHE)
endmacro()

macro(find_openssl_both)
clear_openssl_cached_vars()
find_package(OpenSSL REQUIRED)
set(OPENSSL_SHARED_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
set(OPENSSL_SHARED_LIBRARIES ${OPENSSL_LIBRARIES})
message(STATUS "Discovered OpenSSL shared libraries: ${OPENSSL_SHARED_LIBRARIES}")

unset(OPENSSL_FOUND CACHE)
unset(OPENSSL_INCLUDE_DIR CACHE)
unset(OPENSSL_SSL_LIBRARY CACHE)
unset(OPENSSL_CRYPTO_LIBRARY CACHE)
unset(OPENSSL_LIBRARIES CACHE)
unset(OPENSSL_VERSION CACHE)
clear_openssl_cached_vars()
set(OPENSSL_USE_STATIC_LIBS ON)
if (MSVC)
set(OPENSSL_MSVC_STATIC_RT ON)
Expand All @@ -47,6 +60,7 @@ macro(find_openssl_both)

set(OPENSSL_STATIC_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
set(OPENSSL_STATIC_LIBRARIES ${OPENSSL_LIBRARIES})
message(STATUS "Discovered OpenSSL static libraries: ${OPENSSL_STATIC_LIBRARIES}")
endmacro()

macro(set_version)
Expand Down Expand Up @@ -87,6 +101,11 @@ endmacro()

macro(set_names)
set(SEABOLT_NAME "seabolt${PROJECT_VERSION_MAJOR}${PROJECT_VERSION_MINOR}" CACHE STRING "Seabolt version suffixed name")
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(SEABOLT_STATIC_NAME ${SEABOLT_NAME}-static)
else ()
set(SEABOLT_STATIC_NAME ${SEABOLT_NAME})
endif ()
set(SEABOLT_SHARED "seabolt-shared")
set(SEABOLT_STATIC "seabolt-static")
set(SEABOLT_TEST "seabolt-test")
Expand All @@ -96,4 +115,4 @@ macro(configure_rpath)
set(CMAKE_SKIP_RPATH OFF)
set(CMAKE_BUILD_RPATH ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_MACOSX_RPATH ON)
endmacro()
endmacro()
8 changes: 0 additions & 8 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@ add_library(catch2 INTERFACE)
target_include_directories(catch2
INTERFACE
${CMAKE_CURRENT_LIST_DIR}/tests)

add_library(string-builder
OBJECT
${CMAKE_CURRENT_LIST_DIR}/utils/string-builder.c)

target_include_directories(string-builder
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/utils)
9 changes: 2 additions & 7 deletions src/seabolt-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ add_executable(seabolt-cli "")

target_sources(seabolt-cli
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/src/main.c
$<TARGET_OBJECTS:string-builder>)
${CMAKE_CURRENT_LIST_DIR}/src/main.c)

target_link_libraries(seabolt-cli
PRIVATE
${SEABOLT_SHARED})

target_include_directories(seabolt-cli
PRIVATE
$<TARGET_PROPERTY:string-builder,INTERFACE_INCLUDE_DIRECTORIES>)
${SEABOLT_STATIC})
131 changes: 61 additions & 70 deletions src/seabolt-cli/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,8 @@

#include <inttypes.h>

#include "auth.h"
#include "connector.h"
#include "connections.h"
#include "lifecycle.h"
#include "mem.h"
#include "values.h"
#include "platform.h"
#include "logging.h"
#include "protocol.h"
#include "string-builder.h"
#include "bolt/bolt.h"
#include "bolt/platform.h"

#ifdef WIN32

Expand Down Expand Up @@ -78,7 +70,7 @@ enum Command {
struct Application {
struct BoltConnector* connector;
struct BoltConnection* connection;
enum BoltAccessMode access_mode;
BoltAccessMode access_mode;
struct {
struct timespec connect_time;
struct timespec init_time;
Expand Down Expand Up @@ -117,18 +109,21 @@ void log_to_stderr(int state, const char* message)
fprintf(stderr, "%s\n", message);
}

struct BoltLog* create_logger()
struct BoltLog* create_logger(int enabled)
{
struct BoltLog* log = BoltLog_create();
log->state = 0;
log->debug_enabled = 0;
log->info_enabled = 0;
log->warning_enabled = 0;
log->error_enabled = 0;
log->debug_logger = log_to_stderr;
log->info_logger = log_to_stderr;
log->warning_logger = log_to_stderr;
log->error_logger = log_to_stderr;
struct BoltLog* log = BoltLog_create(0);
BoltLog_set_debug_func(log, enabled ? log_to_stderr : NULL);
BoltLog_set_warning_func(log, enabled ? log_to_stderr : NULL);
BoltLog_set_info_func(log, enabled ? log_to_stderr : NULL);
BoltLog_set_error_func(log, log_to_stderr);
// log->debug_enabled = enabled;
// log->info_enabled = enabled;
// log->warning_enabled = enabled;
// log->error_enabled = enabled;
// log->debug_logger = log_to_stderr;
// log->info_logger = log_to_stderr;
// log->warning_logger = log_to_stderr;
// log->error_logger = log_to_stderr;
return log;
}

Expand All @@ -142,26 +137,7 @@ struct Application* app_create(int argc, char** argv)
const char* BOLT_CONFIG_USER = getenv_or_default("BOLT_USER", "neo4j");
const char* BOLT_CONFIG_PASSWORD = getenv("BOLT_PASSWORD");

struct Application* app = BoltMem_allocate(sizeof(struct Application));

struct BoltConfig config;
config.mode = (strcmp(BOLT_CONFIG_ROUTING, "1")==0 ? BOLT_ROUTING : BOLT_DIRECT);
config.transport = (strcmp(BOLT_CONFIG_SECURE, "1")==0) ? BOLT_SECURE_SOCKET : BOLT_SOCKET;
config.routing_context = NULL;
config.user_agent = "seabolt/1.0.0a";
config.max_pool_size = 10;
config.log = create_logger();
config.address_resolver = NULL;
config.trust = NULL;
config.sock_opts = NULL;

struct BoltValue* auth_token = BoltAuth_basic(BOLT_CONFIG_USER, BOLT_CONFIG_PASSWORD, NULL);

app->connector = BoltConnector_create(&BoltAddress_of((char*) BOLT_CONFIG_HOST, (char*) BOLT_CONFIG_PORT),
auth_token, &config);

BoltValue_destroy(auth_token);
BoltLog_destroy(config.log);
struct Application* app = malloc(sizeof(struct Application));

app->access_mode = (strcmp(BOLT_CONFIG_ACCESS_MODE, "WRITE")==0 ? BOLT_ACCESS_MODE_WRITE : BOLT_ACCESS_MODE_READ);
app->with_allocation_report = 0;
Expand Down Expand Up @@ -212,13 +188,32 @@ struct Application* app_create(int argc, char** argv)
}
}

BoltLog* log = create_logger(app->command==CMD_DEBUG);
BoltConfig* config = BoltConfig_create();
BoltConfig_set_mode(config, (strcmp(BOLT_CONFIG_ROUTING, "1")==0) ? BOLT_MODE_ROUTING : BOLT_MODE_DIRECT);
BoltConfig_set_transport(config,
(strcmp(BOLT_CONFIG_SECURE, "1")==0) ? BOLT_TRANSPORT_ENCRYPTED : BOLT_TRANSPORT_PLAINTEXT);
BoltConfig_set_user_agent(config, "seabolt/" SEABOLT_VERSION);
BoltConfig_set_max_pool_size(config, 10);
BoltConfig_set_log(config, log);

struct BoltValue* auth_token = BoltAuth_basic(BOLT_CONFIG_USER, BOLT_CONFIG_PASSWORD, NULL);
struct BoltAddress* address = BoltAddress_create((char*) BOLT_CONFIG_HOST, (char*) BOLT_CONFIG_PORT);

app->connector = BoltConnector_create(address, auth_token, config);

BoltValue_destroy(auth_token);
BoltAddress_destroy(address);
BoltLog_destroy(log);
BoltConfig_destroy(config);

return app;
}

void app_destroy(struct Application* app)
{
BoltConnector_destroy(app->connector);
BoltMem_deallocate(app, sizeof(struct Application));
free(app);
}

void app_connect(struct Application* app)
Expand All @@ -242,11 +237,6 @@ int app_debug(struct Application* app, const char* cypher)

BoltUtil_get_time(&t[1]); // Checkpoint 1 - right at the start

app->connector->config->log->debug_enabled = 1;
app->connector->config->log->info_enabled = 1;
app->connector->config->log->warning_enabled = 1;
app->connector->config->log->error_enabled = 1;

app_connect(app);

BoltUtil_get_time(&t[2]); // Checkpoint 2 - after handshake and initialisation
Expand All @@ -255,11 +245,11 @@ int app_debug(struct Application* app, const char* cypher)
BoltConnection_load_begin_request(app->connection);
BoltConnection_set_run_cypher(app->connection, cypher, strlen(cypher), 0);
BoltConnection_load_run_request(app->connection);
bolt_request run = BoltConnection_last_request(app->connection);
BoltRequest run = BoltConnection_last_request(app->connection);
BoltConnection_load_pull_request(app->connection, -1);
bolt_request pull = BoltConnection_last_request(app->connection);
BoltRequest pull = BoltConnection_last_request(app->connection);
BoltConnection_load_commit_request(app->connection);
bolt_request commit = BoltConnection_last_request(app->connection);
BoltRequest commit = BoltConnection_last_request(app->connection);

BoltConnection_send(app->connection);

Expand Down Expand Up @@ -319,38 +309,39 @@ int app_run(struct Application* app, const char* cypher)

BoltConnection_set_run_cypher(app->connection, cypher, strlen(cypher), 0);
BoltConnection_load_run_request(app->connection);
bolt_request run = BoltConnection_last_request(app->connection);
BoltRequest run = BoltConnection_last_request(app->connection);
BoltConnection_load_pull_request(app->connection, -1);
bolt_request pull = BoltConnection_last_request(app->connection);
BoltRequest pull = BoltConnection_last_request(app->connection);

BoltConnection_send(app->connection);

BoltConnection_fetch_summary(app->connection, run);
char string_buffer[4096];
if (app->with_header) {
const struct BoltValue* fields = BoltConnection_field_names(app->connection);
for (int i = 0; i<fields->size; i++) {
for (int i = 0; i<BoltValue_size(fields); i++) {
if (i>0) {
putc('\t', stdout);
}
struct StringBuilder* builder = StringBuilder_create();
BoltValue_write(builder, BoltList_value(fields, i), app->connection->protocol->structure_name);
fprintf(stdout, "%s", StringBuilder_get_string(builder));
StringBuilder_destroy(builder);
if (BoltValue_string(BoltList_value(fields, i), string_buffer, 4096, app->connection)>4096) {
string_buffer[4095] = 0;
}
fprintf(stdout, "%s", string_buffer);
}
putc('\n', stdout);
}

while (BoltConnection_fetch(app->connection, pull)) {
const struct BoltValue* field_values = BoltConnection_field_values(app->connection);
for (int i = 0; i<field_values->size; i++) {
for (int i = 0; i<BoltValue_size(field_values); i++) {
struct BoltValue* value = BoltList_value(field_values, i);
if (i>0) {
putc('\t', stdout);
}
struct StringBuilder* builder = StringBuilder_create();
BoltValue_write(builder, value, app->connection->protocol->structure_name);
fprintf(stdout, "%s", StringBuilder_get_string(builder));
StringBuilder_destroy(builder);
if (BoltValue_string(value, string_buffer, 4096, app->connection)>4096) {
string_buffer[4095] = 0;
}
fprintf(stdout, "%s", string_buffer);
}
putc('\n', stdout);
}
Expand All @@ -367,11 +358,11 @@ long run_fetch(const struct Application* app, const char* cypher)
BoltConnection_load_begin_request(app->connection);
BoltConnection_set_run_cypher(app->connection, cypher, strlen(cypher), 0);
BoltConnection_load_run_request(app->connection);
bolt_request run = BoltConnection_last_request(app->connection);
BoltRequest run = BoltConnection_last_request(app->connection);
BoltConnection_load_pull_request(app->connection, -1);
bolt_request pull = BoltConnection_last_request(app->connection);
BoltRequest pull = BoltConnection_last_request(app->connection);
BoltConnection_load_commit_request(app->connection);
bolt_request commit = BoltConnection_last_request(app->connection);
BoltRequest commit = BoltConnection_last_request(app->connection);

BoltConnection_send(app->connection);

Expand Down Expand Up @@ -457,13 +448,13 @@ int main(int argc, char* argv[])

if (with_allocation_report) {
fprintf(stderr, "=====================================\n");
fprintf(stderr, "current allocation : %" PRIu64 " bytes\n", (int64_t) BoltMem_current_allocation());
fprintf(stderr, "peak allocation : %" PRId64 " bytes\n", (int64_t) BoltMem_peak_allocation());
fprintf(stderr, "allocation events : %" PRId64 "\n", BoltMem_allocation_events());
fprintf(stderr, "current allocation : %" PRIu64 " bytes\n", (int64_t) BoltStat_memory_allocation_current());
fprintf(stderr, "peak allocation : %" PRId64 " bytes\n", (int64_t) BoltStat_memory_allocation_peak());
fprintf(stderr, "allocation events : %" PRId64 "\n", BoltStat_memory_allocation_events());
fprintf(stderr, "=====================================\n");
}

if (BoltMem_current_allocation()==0) {
if (BoltStat_memory_allocation_current()==0) {
return EXIT_SUCCESS;
}
else {
Expand Down
1 change: 1 addition & 0 deletions src/seabolt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_library(${SEABOLT_SHARED} SHARED "")
add_library(${SEABOLT_STATIC} STATIC "")
add_executable(${SEABOLT_TEST} "")

include(${CMAKE_CURRENT_LIST_DIR}/src/CMakeLists.txt)
Expand Down
Loading

0 comments on commit 116a161

Please sign in to comment.