Commit 8447962d authored by Greg Williams's avatar Greg Williams
Browse files

Completed write_file_blocking example

parent 82b830f2
Loading
Loading
Loading
Loading
+11 −16
Original line number Diff line number Diff line
@@ -289,23 +289,18 @@ run: $(UTIL_EXEC) start_simulator
# Standalone Example Executables
#===============================================================================

# EXAMPLE_SRC = ./src/examples
# EXAMPLE_BIN = $(BIN_DIR)/
# EXAMPLE_LDFLAGS += -lm -l ssl $(KINETIC_LIB) -l crypto -l pthread
EXAMPLE_SRC = ./src/examples
EXAMPLE_BIN = $(BIN_DIR)/
EXAMPLE_LDFLAGS += -lm -l ssl $(KINETIC_LIB) -l crypto -l pthread

# $(OUT_DIR)/%.o: %.c $(DEPS)
# $(OUT_DIR)/%.o: $(EXAMPLE_SRC)/%.c $(KINETIC_LIB)
# 	$(CC) -c -o $@ $< $(CFLAGS)

# $(UTIL_OBJ): $(EXAMPLE_SRC)/*.c
# 	$(CC) -c -o $@ $< $(CFLAGS) -I$(PUB_INC)

# $(UTIL_EXEC): $(UTIL_OBJ) $(KINETIC_LIB)
# 	@echo
# 	@echo --------------------------------------------------------------------------------
# 	@echo Building development test utility: $(UTIL_EXEC)
# 	@echo --------------------------------------------------------------------------------
# 	$(CC) -o $@ $< $(CFLAGS) $(UTIL_LDFLAGS) $(KINETIC_LIB)

# utility: $(UTIL_EXEC)
$(EXAMPLE_BIN)/write_file_blocking: $(EXAMPLE_SRC)/write_file_blocking.c $(KINETIC_LIB)
	@echo
	@echo --------------------------------------------------------------------------------
	@echo Building example: $@
	@echo --------------------------------------------------------------------------------
	$(CC) -o $@ $< $(CFLAGS) -I$(PUB_INC) $(UTIL_LDFLAGS) $(KINETIC_LIB)

# build: $(KINETIC_LIB) $(KINETIC_SO_DEV) utility
examples: $(KINETIC_LIB) $(EXAMPLE_BIN)/write_file_blocking
+22 −65
Original line number Diff line number Diff line
@@ -123,97 +123,54 @@
	"build_systems":
	[
		{
			"cmd":
			[
				"rake"
			],
			"cmd": "make",
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Default Build",
			"name": "kinetic-c - Default Build",
			"working_dir": "${project_path}"
		},
		{
			"cmd":
			[
				"rake",
				"clobber",
				"test_all"
			],
			"cmd": ["make", "test"],
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Run all tests (full)",
			"name": "kinetic-c - Run all tests (full)",
			"working_dir": "${project_path}"
		},
		{
			"cmd":
			[
				"rake",
				"test_all"
			],
			"cmd": ["rake", "CC=/usr/bin/CC", "test:${file_name}"],
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Run all tests (incremental)",
			"name": "kinetic-c - Test current module w/Clang",
			"selector": ["source.c", "source.c++"],
			"working_dir": "${project_path}"
		},
		{
			"cmd":
			[
				"rake",
				"CC=/usr/bin/CC",
				"test:${file_name}"
			],
			"cmd": ["rake", "CC=gcc-4.9", "test:${file_name}"],
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Test current module w/Clang",
			"selector":
			[
				"source.c",
				"source.c++"
			],
			"name": "kinetic-c - Test current module w/GCC",
			"selector": ["source.c", "source.c++"],
			"working_dir": "${project_path}"
		},
		{
			"cmd":
			[
				"rake",
				"CC=gcc-4.9",
				"test:${file_name}"
			],
			"cmd": ["make", "utility"],
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Test current module w/GCC",
			"selector":
			[
				"source.c",
				"source.c++"
			],
			"name": "kinetic-c - Build test utility",
			"working_dir": "${project_path}"
		},
		{
			"cmd":
			[
				"rake",
				"release"
			],
			"cmd": ["make", "run"],
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Build test utility",
			"name": "kinetic-c - Build and run test utility",
			"working_dir": "${project_path}"
		},
		{
			"cmd":
			[
				"rake",
				"release",
				"run"
			],
			"cmd": ["make", "all"],
			"file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Build and run test utility",
			"name": "kinetic-c - Full CI build",
			"working_dir": "${project_path}"
		},
    {
			"cmd":
			[
				"rake",
				"ci"
			],
      "cmd": ["make", "examples"],
      "file_regex": "([A-Za-z][A-Za-z0-9/_]+\\.[ch])[:,] ?l?i?n?e? ?([0-9]+)[\\.:,]*([0-9]*)",
			"name": "Full CI build",
      "name": "kinetic-c - Build Examples",
      "working_dir": "${project_path}"
		}
    },
	]
}
+34 −34
Original line number Diff line number Diff line
@@ -21,13 +21,12 @@
#include "kinetic_client.h"
#include "kinetic_types.h"
#include "byte_array.h"

#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/file.h>

typedef struct {
    char ip[16];
@@ -41,9 +40,8 @@ typedef struct {
    ByteBuffer data;
    KineticStatus status;
} write_args;
static write_args WriteArgs;

void kinetic_put(write_args* args)
void store_data(write_args* args)
{
    KineticEntry* entry = &(args->entry);
    int32_t objIndex = 0;
@@ -52,7 +50,7 @@ void kinetic_put(write_args* args)

        // Configure entry meta-data
        ByteBuffer_Reset(&entry->key);
        ByteBuffer_AppendCString(&entry->key, WriteArgs.keyPrefix);
        ByteBuffer_AppendCString(&entry->key, args->keyPrefix);
        char keySuffix[8];
        snprintf(keySuffix, sizeof(keySuffix), "%02d", objIndex);
        ByteBuffer_AppendCString(&entry->key, keySuffix);
@@ -80,64 +78,66 @@ void kinetic_put(write_args* args)
    printf("File stored on Kinetic Device across %d entries\n", objIndex);
}

int main(const char * argc, const int argv)
int main(int argc, char** argv)
{
    KineticSessionHandle kinetic_client;
    const char HmacKeyString[] = "asdfasdf";
    const KineticSession sessionConfig = {
        .host = "localhost",
        .port = KINETIC_PORT,
        .clusterVersion = 0,
        .identity = 1,
        .nonBlocking = false,
        .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
    };

    // Read in file contents to store
    const char* dataFile = "test/support/data/test.data";
    struct stat st;
    stat(dataFile, &st);
    char* buf = malloc(st.st_size);
    int fd = open(dataFile, O_RDONLY);
    long dataLen = read(fd, buf, BUFSIZE);
    long dataLen = read(fd, buf, st.st_size);
    close(fd);
    if (dataLen <= 0) {
        fprinf(stderr, "Failed reading data file to store: %s", dataFile);
        fprintf(stderr, "Failed reading data file to store: %s", dataFile);
        exit(-1);
    }

    // Establish connection
    KineticClient_Connect(&sessionConfig, &kinetic_client));
    KineticStatus status;
    const char HmacKeyString[] = "asdfasdf";
    const KineticSession sessionConfig = {
        .host = "localhost",
        .port = KINETIC_PORT,
        .clusterVersion = 0,
        .identity = 1,
        .nonBlocking = false,
        .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
    };
    write_args* writeArgs = calloc(1, sizeof(write_args));
    status = KineticClient_Connect(&sessionConfig, &writeArgs->sessionHandle);
    if (status != KINETIC_STATUS_SUCCESS) {
        fprintf(stderr, "Connection to host '%s' failed w/ status: %s",
            sessionConfig.host, Kinetic_GetStatusDescription(status));
    }

    // Create a ByteBuffer for consuming chunks of data out of for overlapped PUTs
    WriteArgs.data = ByteBuffer_Create(buf, dataLen, 0);
    writeArgs->data = ByteBuffer_Create(buf, dataLen, 0);

    // Configure common meta-data for the entries
    struct timeval now;
    gettimeofday(&now, NULL);
    snprintf(WriteArgs.keyPrefix, sizeof(WriteArgs.keyPrefix), "%010llu_%02d%02d_",
        now.tv_sec, iteration, i);
    ByteBuffer verBuf = ByteBuffer_Create(WriteArgs.version, sizeof(WriteArgs.version), 0);
    snprintf(writeArgs->keyPrefix, sizeof(writeArgs->keyPrefix), "%010ld_", now.tv_sec);
    ByteBuffer verBuf = ByteBuffer_Create(writeArgs->version, sizeof(writeArgs->version), 0);
    ByteBuffer_AppendCString(&verBuf, "v1.0");
    ByteBuffer tagBuf = ByteBuffer_Create(WriteArgs.tag, sizeof(WriteArgs.tag), 0);
    ByteBuffer tagBuf = ByteBuffer_Create(writeArgs->tag, sizeof(writeArgs->tag), 0);
    ByteBuffer_AppendCString(&tagBuf, "some_value_tag...");
    WriteArgs.entry = (KineticEntry) {
        .key = ByteBuffer_Create(WriteArgs.key, sizeof(WriteArgs.key), 0),
    writeArgs->entry = (KineticEntry) {
        .key = ByteBuffer_Create(writeArgs->key, sizeof(writeArgs->key), 0),
        // .newVersion = verBuf,
        .tag = tagBuf,
        .algorithm = KINETIC_ALGORITHM_SHA1,
        .value = ByteBuffer valBuf = ByteBuffer_Create(WriteArgs.value, sizeof(WriteArgs.value), 0),
        .value = ByteBuffer_Create(writeArgs->value, sizeof(writeArgs->value), 0),
        .synchronization = KINETIC_SYNCHRONIZATION_WRITEBACK,
    };
    strcpy(WriteArgs.ip, sessionConfig.host);
    strcpy(writeArgs->ip, sessionConfig.host);

    // Store the data
    printf("\nWriting data file to the Kinetic device...\n");
    kinetic_put(WriteArgs);
    store_data(writeArgs);

    // Shutdown client connection and cleanup
    KineticClient_Disconnect(&kinetic_client[i]);
    free(kinetic_client);
    free(kt_arg);
    KineticClient_Disconnect(&writeArgs->sessionHandle);
    free(writeArgs);
    free(buf);
}