chore: example code to test C wrapper

This commit is contained in:
Prem Chaitanya Prathi 2024-03-05 11:27:30 +05:30
parent c38e6e2ed8
commit 8972f3ba69
No known key found for this signature in database
5 changed files with 105 additions and 53 deletions

View File

@ -14,10 +14,10 @@ install-deps:
# Generate 'negentropy.h.gch' # Generate 'negentropy.h.gch'
precompiled-header: precompiled-header:
g++ --std=c++20 -Wall -fexceptions -g negentropy.h $(INCS) g++ -O0 --std=c++20 -Wall -fexceptions -g negentropy.h $(INCS)
shared-lib: shared-lib:
g++ --std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.c -lcrypto -lssl -L/opt/homebrew/lib/ g++ -O0 -g -std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.c -lcrypto -lssl -L/opt/homebrew/lib/
clean: clean:
rm -f $(TARGET) negentropy.h.gch libnegentropy.so rm -f $(TARGET) negentropy.h.gch libnegentropy.so

View File

@ -1,4 +1,4 @@
.PHONY: all .PHONY: all
all: all:
g++ --std=c++20 test.c -I../ -lnegentropy -lcrypto -L../ -L/opt/homebrew/lib/ -Wc++11-narrowing g++ -g -O0 --std=c++20 test.c -I../ -lnegentropy -lcrypto -L../ -L/opt/homebrew/lib/ -Wc++11-narrowing

View File

@ -8,6 +8,27 @@
#include <string> #include <string>
#include "../negentropy_wrapper.h" #include "../negentropy_wrapper.h"
void printHexBuffer(buffer buf){
for (uint64_t i = 0; i < buf.len; ++i) {
printf("%0hhx", buf.data[i]);
}
printf("\n");
}
void rec_callback(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output){
printf("needIds count:%llu , haveIds count: %llu \n",need_ids_len, have_ids_len);
for (int i=0; i < need_ids_len ; i++) {
printf("need ID at %d :", i);
printHexBuffer(need_ids[i]);
}
for (int j=0; j < have_ids_len ; j++) {
printf("need ID at %d :", j);
printHexBuffer(have_ids[j]);
}
}
int main(){ int main(){
void* st1 = storage_new("",""); void* st1 = storage_new("","");
@ -36,41 +57,55 @@ int main(){
b1.len = 32; b1.len = 32;
b1.data = m1; b1.data = m1;
/* char m2[] = "28798d295c30c7e6d9a4a96cdda7e020f7aa7168cce063302ed19b856332959e"; unsigned char m2[] = {0x28 ,0x79 ,0x8d ,0x29 ,0x5c ,0x30 ,0xc7 ,0xe6 \
,0xd9 ,0xa4 ,0xa9 ,0x6c ,0xdd ,0xa7 ,0xe0 ,0x20 \
,0xf7 ,0xaa ,0x71 ,0x68 ,0xcc ,0xe0 ,0x63 ,0x30 \
,0x2e ,0xd1 ,0x9b ,0x85 ,0x63 ,0x32 ,0x95 ,0x9e}; //28798d295c30c7e6d9a4a96cdda7e020f7aa7168cce063302ed19b856332959e
buffer b2 ; buffer b2 ;
b2.len = 32; b2.len = 32;
b2.data = m2; */ b2.data = m2;
bool ret = storage_insert(st1,time(NULL),&b1); bool ret = storage_insert(st1,time(NULL),&b1);
if (ret){ if (ret){
printf("inserted hash successfully in st1\n"); printf("inserted hash successfully in st1\n");
} }
/*
ret = storage_insert(st2,time(NULL),&b2); ret = storage_insert(st2,time(NULL),&b2);
if (ret){ if (ret){
printf("inserted hash %s successfully in st2\n", m2); printf("inserted hash successfully in st2\n");
} }
ret = storage_insert(st2,time(NULL),&b1); ret = storage_insert(st2,time(NULL),&b1);
if (ret){ if (ret){
printf("inserted hash %s successfully in st2\n", m1); printf("inserted hash successfully in st2\n");
} }
*/
/* std::string out; buffer b4 ;
negentropy_initiate(ngn_inst1, &out); b4.len = 153600;
if(out.size() == 0){ b4.data = (unsigned char*)malloc(153600);
size_t outSize = negentropy_initiate(ngn_inst1, &b4);
if(outSize == 0){
perror("failed to initiate negentropy instance"); perror("failed to initiate negentropy instance");
} }
printf("initiated negentropy successfully with output of len %zu \n", out.size()); printf("initiated negentropy successfully with output of len %zu \n", outSize);
b4.len = outSize;
buffer b3 ; buffer b3 ;
b3.len = out.size(); b3.len = 153600;
b3.data = (unsigned char*)malloc(b3.len); b3.data = (unsigned char*)malloc(153600);
memcpy(b3.data, out.c_str(),out.size());
const char* req2 = reconcile(ngn_inst2, &b3); */ outSize = reconcile(ngn_inst2, &b4, &b3);
if(outSize == 0){
perror("nothing to reconcile");
}
printf("reconcile returned with output of len %zu \n", outSize);
b3.len = outSize;
//free(b3.data); outSize = reconcile_with_ids(ngn_inst1, &b3, &rec_callback);
free(b3.data);
free(b4.data);
//b3.len = len; //b3.len = len;
//b3.data = (char*)malloc(len); //b3.data = (char*)malloc(len);

View File

@ -7,6 +7,13 @@
//This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code.
//TODO: Do error handling by catching exceptions //TODO: Do error handling by catching exceptions
void printHexString(std::string_view toPrint){
for (size_t i = 0; i < toPrint.size(); ++i) {
printf("%0hhx", toPrint[i]);
}
printf("\n");
}
void* storage_new(const char* db_path, const char* name){ void* storage_new(const char* db_path, const char* name){
negentropy::storage::BTreeMem* storage; negentropy::storage::BTreeMem* storage;
/* /*
@ -49,7 +56,8 @@ size_t negentropy_initiate(void* negentropy, buffer* out){
std::string* output = new std::string(); std::string* output = new std::string();
try { try {
*output = ngn_inst->initiate(); *output = ngn_inst->initiate();
std::cout << "output of initiate is, len:" << output->size() << std::hex << *output << std::endl; std::cout << "output of initiate is, len:" << output->size() << ", output:";
printHexString(std::string_view(*output));
} catch(negentropy::err e){ } catch(negentropy::err e){
//TODO:Find a way to return this error //TODO:Find a way to return this error
return 0; return 0;
@ -66,12 +74,6 @@ void negentropy_setinitiator(void* negentropy){
} }
void printHexString(std::string_view toPrint){
for (size_t i = 0; i < toPrint.size(); ++i) {
printf("%0hhx", toPrint[i]);
}
printf("\n");
}
bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ bool storage_insert(void* storage, uint64_t createdAt, buffer* id){
negentropy::storage::BTreeMem* lmdbStorage; negentropy::storage::BTreeMem* lmdbStorage;
@ -106,6 +108,8 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){
std::string* out = new std::string(); std::string* out = new std::string();
try { try {
*out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); *out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len));
std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:";
printHexString(std::string_view(*out));
} catch(negentropy::err e){ } catch(negentropy::err e){
//TODO:Find a way to return this error //TODO:Find a way to return this error
return 0; return 0;
@ -114,42 +118,53 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){
return out->size(); return out->size();
} }
char *convert(const std::string & s) void transform(std::vector<std::string> &from_ids, buffer* to_ids)
{ {
char *pc = new char[s.size()+1]; for (int i=0; i < from_ids.size(); i ++){
std::strcpy(pc, s.c_str()); to_ids[i].len = from_ids[i].size();
return pc; to_ids[i].data = (unsigned char*)from_ids[i].c_str();
}
} }
const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[], int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk){
uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len){
Negentropy<negentropy::storage::BTreeMem> *ngn_inst; Negentropy<negentropy::storage::BTreeMem> *ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy); ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
std::optional<std::string>* output; std::optional<std::string> out;
std::vector<std::string> haveIds; std::vector<std::string> haveIds, needIds;
std::vector<std::string> needIds; uint64_t have_ids_len, need_ids_len;
buffer* have_ids;
buffer* need_ids;
try { try {
*output = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds); out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds);
*have_ids_len = haveIds.size(); have_ids_len = haveIds.size();
*need_ids_len = needIds.size(); need_ids_len = needIds.size();
//TODO: Optimize to not copy and rather return memory reference. have_ids = (buffer*)malloc(have_ids_len*sizeof(buffer));
std::cout << "*have_ids_len:" << *have_ids_len << "*need_ids_len:" << *need_ids_len << "output has value" << output->has_value() << std::endl; need_ids = (buffer*)malloc(need_ids_len*sizeof(buffer));
std::transform(haveIds.begin(), haveIds.end(), have_ids, convert); std::cout << "have_ids_len:" << have_ids_len << "need_ids_len:" << need_ids_len << std::endl;
std::transform(needIds.begin(), needIds.end(), need_ids, convert); std::flush(std::cout);
transform(haveIds, have_ids);
transform(needIds, need_ids);
std::cout << "for debug" << std::endl;
} catch(negentropy::err e){ } catch(negentropy::err e){
//TODO:Find a way to return this error std::cout << "caught error "<< e.what() << std::endl;
return NULL; //TODO:Find a way to return this error and cleanup partially allocated memory if any
return -1;
} }
if (!output->has_value()) { buffer output;
//TODO: Figure out diff between error and this. if (out) {
return NULL; output.len = out.value().size();
}else { output.data = (unsigned char*)out.value().c_str();
std::cout << "output value" << output->value() << std::endl; std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:";
return output->value().c_str(); printHexString(std::string_view(out.value()));
std::cout << "invoking callback" << std::endl;
} }
cbk(have_ids, have_ids_len, need_ids, need_ids_len, &output);
free(have_ids);
free(need_ids);
return 0;
} }

View File

@ -30,8 +30,10 @@ EXTERNC bool storage_erase(void* storage, uint64_t createdAt, buffer* id);
EXTERNC size_t reconcile(void* negentropy, buffer* query, buffer* output); EXTERNC size_t reconcile(void* negentropy, buffer* query, buffer* output);
EXTERNC const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[], EXTERNC typedef void (*reconcile_cbk)(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output);
uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len);
EXTERNC int reconcile_with_ids(void* negentropy, buffer* query, reconcile_cbk cbk);
#endif #endif