kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
put_nonblocking.c
Go to the documentation of this file.
1 /*
2 * kinetic-c
3 * Copyright (C) 2015 Seagate Technology.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 #include "kinetic_client.h"
21 #include "kinetic_types.h"
22 #include "kinetic_semaphore.h"
23 #include <stdlib.h>
24 #include <openssl/sha.h>
25 #include <pthread.h>
26 
27 typedef struct {
28  KineticSemaphore * sem;
29  KineticStatus status;
30 } PutStatus;
31 
32 static void put_finished(KineticCompletionData* kinetic_data, void* clientData);
33 
34 int main(int argc, char** argv)
35 {
36  (void)argc;
37  (void)argv;
38 
39  // Initialize kinetic-c and establish session
40  KineticSession* session;
41  KineticClientConfig client_config = {
42  .logFile = "stdout",
43  .logLevel = 0,
44  };
45  KineticClient * client = KineticClient_Init(&client_config);
46  if (client == NULL) { return 1; }
47  const char HmacKeyString[] = "asdfasdf";
48  KineticSessionConfig config = {
49  .host = "127.0.0.1",
50  .port = KINETIC_PORT,
51  .clusterVersion = 0,
52  .identity = 1,
53  .hmacKey = ByteArray_CreateWithCString(HmacKeyString),
54  };
55  KineticStatus connect_status = KineticClient_CreateSession(&config, client, &session);
56  if (connect_status != KINETIC_STATUS_SUCCESS) {
57  fprintf(stderr, "Failed connecting to the Kinetic device w/status: %s\n",
58  Kinetic_GetStatusDescription(connect_status));
59  return 1;
60  }
61 
62  // Create structure to populate with PUT status in callback
63  // a semaphore is used to notify the main thread that it's
64  // safe to proceed.
65  PutStatus put_status = {
66  .sem = KineticSemaphore_Create(),
67  .status = KINETIC_STATUS_INVALID,
68  };
69 
70  // some dummy data to PUT
71  uint8_t value_data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
72  ByteBuffer value = ByteBuffer_MallocAndAppend(value_data, sizeof(value_data));
73 
74  // a dummy key
75  uint8_t key_data[] = {0x00, 0x01, 0x02, 0x03, 0x04};
76  ByteBuffer key = ByteBuffer_MallocAndAppend(key_data, sizeof(key_data));
77 
78  // Populate tag with SHA1
79  ByteBuffer tag = ByteBuffer_Malloc(20);
80  uint8_t sha1[20];
81  SHA1(value.array.data, value.bytesUsed, &sha1[0]);
82  ByteBuffer_Append(&tag, sha1, sizeof(sha1));
83 
84  // Because I'm passing a pointer to this entry to KineticClient_Put(), this entry must not
85  // go out of scope until the PUT completes
86  KineticEntry entry = {
87  .key = key,
88  .tag = tag,
89  .algorithm = KINETIC_ALGORITHM_SHA1,
90  .value = value,
91  .synchronization = KINETIC_SYNCHRONIZATION_WRITETHROUGH,
92  };
93 
95  session,
96  &entry,
98  .callback = put_finished,
99  .clientData = &put_status,
100  }
101  );
102 
103  if (status != KINETIC_STATUS_SUCCESS) {
104  fprintf(stderr, "PUT failed w/status: %s\n", Kinetic_GetStatusDescription(status));
105  return 1;
106  }
107 
108  // Wait for PUT to finish
110 
111  if (put_status.status != KINETIC_STATUS_SUCCESS) {
112  fprintf(stderr, "PUT failed w/status: %s\n", Kinetic_GetStatusDescription(put_status.status));
113  return 1;
114  }
115  printf("PUT completed successfully!\n");
116 
117  // Free malloc'd buffers
118  ByteBuffer_Free(value);
119  ByteBuffer_Free(key);
120  ByteBuffer_Free(tag);
121 
122  // Shutdown client connection and cleanup
124  KineticClient_Shutdown(client);
125 
126  return 0;
127 }
128 
129 static void put_finished(KineticCompletionData* kinetic_data, void* clientData)
130 {
131  PutStatus * put_status = clientData;
132 
133  // Save PUT result status
134  put_status->status = kinetic_data->status;
135  // Signal that we're done
136  KineticSemaphore_Signal(put_status->sem);
137 }
ByteBuffer * ByteBuffer_Append(ByteBuffer *buffer, const void *data, size_t len)
Definition: byte_array.c:135
Structure for an embedded ByteArray as a buffer.
Definition: byte_array.h:53
This request is made persistent before returning.
Definition: kinetic_types.h:93
ByteBuffer ByteBuffer_Malloc(size_t size)
Definition: byte_array.c:254
Operation successful.
KineticStatus KineticClient_CreateSession(KineticSessionConfig *const config, KineticClient *const client, KineticSession **session)
Creates a session with the Kinetic Device per specified configuration.
static void put_finished(KineticCompletionData *kinetic_data, void *clientData)
Structure used to specify the configuration for a session.
int main(int argc, char **argv)
KineticStatus KineticClient_DestroySession(KineticSession *const session)
Closes the connection to a host.
ByteArray array
ByteArray holding allocated array w/length = allocated size.
Definition: byte_array.h:54
Closure which can be specified for operations which support asynchronous mode.
Kinetic object instance.
char host[256]
Host name/IP address of Kinetic Device.
void KineticSemaphore_WaitForSignalAndDestroy(KineticSemaphore *sem)
Blocks until the given semaphore is signaled.
const char * Kinetic_GetStatusDescription(KineticStatus status)
Provides a string representation for a KineticStatus code.
Definition: kinetic_types.c:67
#define KINETIC_PORT
Default kinetic port.
Definition: kinetic_types.h:40
void KineticSemaphore_Signal(KineticSemaphore *sem)
Signals KineticSemaphore.
Completion data which will be provided to KineticCompletionClosure for asynchronous operations...
const char * logFile
Path to log file. Specify 'stdout' to log to STDOUT or NULL to disable logging.
ByteBuffer key
Key associated with the object stored on disk.
uint8_t * data
Pointer to an allocated array of data bytes.
Definition: byte_array.h:36
KineticStatus status
Resultant status of the operation.
Status not available (no reponse/status available)
KineticStatus
Kinetic status codes.
KineticStatus KineticClient_Put(KineticSession *const session, KineticEntry *const entry, KineticCompletionClosure *closure)
Executes a PUT operation to store/update an entry on the Kinetic Device.
ByteBuffer ByteBuffer_MallocAndAppend(const void *data, size_t len)
Definition: byte_array.c:262
Configuration values for the KineticClient connection.
void KineticClient_Shutdown(KineticClient *const client)
Performs shutdown/cleanup of the kinetic-c client library.
KineticClient * KineticClient_Init(KineticClientConfig *config)
Initializes the Kinetic API and configures logging.
size_t bytesUsed
Reflects the number of bytes used from the array
Definition: byte_array.h:55
KineticSemaphore * KineticSemaphore_Create(void)
Creates a KineticSemaphore.
void ByteBuffer_Free(ByteBuffer buffer)
Definition: byte_array.c:272
ByteArray ByteArray_CreateWithCString(const char *str)
Definition: byte_array.c:38