mirror of
https://github.com/logos-messaging/negentropy.git
synced 2026-01-02 22:13:10 +00:00
chore: example code to test C wrapper
This commit is contained in:
parent
c38e6e2ed8
commit
8972f3ba69
@ -14,10 +14,10 @@ install-deps:
|
||||
|
||||
# Generate 'negentropy.h.gch'
|
||||
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:
|
||||
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:
|
||||
rm -f $(TARGET) negentropy.h.gch libnegentropy.so
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
.PHONY: 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
|
||||
|
||||
@ -8,6 +8,27 @@
|
||||
#include <string>
|
||||
#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(){
|
||||
void* st1 = storage_new("","");
|
||||
@ -36,41 +57,55 @@ int main(){
|
||||
b1.len = 32;
|
||||
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 ;
|
||||
b2.len = 32;
|
||||
b2.data = m2; */
|
||||
b2.data = m2;
|
||||
|
||||
bool ret = storage_insert(st1,time(NULL),&b1);
|
||||
if (ret){
|
||||
printf("inserted hash successfully in st1\n");
|
||||
}
|
||||
/*
|
||||
|
||||
ret = storage_insert(st2,time(NULL),&b2);
|
||||
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);
|
||||
if (ret){
|
||||
printf("inserted hash %s successfully in st2\n", m1);
|
||||
printf("inserted hash successfully in st2\n");
|
||||
}
|
||||
*/
|
||||
|
||||
buffer b4 ;
|
||||
b4.len = 153600;
|
||||
b4.data = (unsigned char*)malloc(153600);
|
||||
|
||||
/* std::string out;
|
||||
negentropy_initiate(ngn_inst1, &out);
|
||||
if(out.size() == 0){
|
||||
size_t outSize = negentropy_initiate(ngn_inst1, &b4);
|
||||
if(outSize == 0){
|
||||
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 ;
|
||||
b3.len = out.size();
|
||||
b3.data = (unsigned char*)malloc(b3.len);
|
||||
memcpy(b3.data, out.c_str(),out.size());
|
||||
b3.len = 153600;
|
||||
b3.data = (unsigned char*)malloc(153600);
|
||||
|
||||
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.data = (char*)malloc(len);
|
||||
|
||||
|
||||
@ -7,6 +7,13 @@
|
||||
//This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code.
|
||||
//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){
|
||||
negentropy::storage::BTreeMem* storage;
|
||||
/*
|
||||
@ -49,7 +56,8 @@ size_t negentropy_initiate(void* negentropy, buffer* out){
|
||||
std::string* output = new std::string();
|
||||
try {
|
||||
*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){
|
||||
//TODO:Find a way to return this error
|
||||
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){
|
||||
negentropy::storage::BTreeMem* lmdbStorage;
|
||||
@ -106,6 +108,8 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){
|
||||
std::string* out = new std::string();
|
||||
try {
|
||||
*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){
|
||||
//TODO:Find a way to return this error
|
||||
return 0;
|
||||
@ -114,42 +118,53 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){
|
||||
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];
|
||||
std::strcpy(pc, s.c_str());
|
||||
return pc;
|
||||
for (int i=0; i < from_ids.size(); i ++){
|
||||
to_ids[i].len = from_ids[i].size();
|
||||
to_ids[i].data = (unsigned char*)from_ids[i].c_str();
|
||||
}
|
||||
}
|
||||
|
||||
const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[],
|
||||
uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len){
|
||||
int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk){
|
||||
Negentropy<negentropy::storage::BTreeMem> *ngn_inst;
|
||||
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
|
||||
|
||||
std::optional<std::string>* output;
|
||||
std::vector<std::string> haveIds;
|
||||
std::vector<std::string> needIds;
|
||||
std::optional<std::string> out;
|
||||
std::vector<std::string> haveIds, needIds;
|
||||
uint64_t have_ids_len, need_ids_len;
|
||||
buffer* have_ids;
|
||||
buffer* need_ids;
|
||||
|
||||
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();
|
||||
*need_ids_len = needIds.size();
|
||||
//TODO: Optimize to not copy and rather return memory reference.
|
||||
std::cout << "*have_ids_len:" << *have_ids_len << "*need_ids_len:" << *need_ids_len << "output has value" << output->has_value() << std::endl;
|
||||
have_ids_len = haveIds.size();
|
||||
need_ids_len = needIds.size();
|
||||
have_ids = (buffer*)malloc(have_ids_len*sizeof(buffer));
|
||||
need_ids = (buffer*)malloc(need_ids_len*sizeof(buffer));
|
||||
|
||||
std::transform(haveIds.begin(), haveIds.end(), have_ids, convert);
|
||||
std::transform(needIds.begin(), needIds.end(), need_ids, convert);
|
||||
std::cout << "have_ids_len:" << have_ids_len << "need_ids_len:" << need_ids_len << std::endl;
|
||||
std::flush(std::cout);
|
||||
|
||||
transform(haveIds, have_ids);
|
||||
transform(needIds, need_ids);
|
||||
std::cout << "for debug" << std::endl;
|
||||
} catch(negentropy::err e){
|
||||
//TODO:Find a way to return this error
|
||||
return NULL;
|
||||
std::cout << "caught error "<< e.what() << std::endl;
|
||||
//TODO:Find a way to return this error and cleanup partially allocated memory if any
|
||||
return -1;
|
||||
}
|
||||
if (!output->has_value()) {
|
||||
//TODO: Figure out diff between error and this.
|
||||
return NULL;
|
||||
}else {
|
||||
std::cout << "output value" << output->value() << std::endl;
|
||||
return output->value().c_str();
|
||||
buffer output;
|
||||
if (out) {
|
||||
output.len = out.value().size();
|
||||
output.data = (unsigned char*)out.value().c_str();
|
||||
std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:";
|
||||
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;
|
||||
}
|
||||
|
||||
@ -28,10 +28,12 @@ EXTERNC bool storage_insert(void* storage, uint64_t createdAt, buffer* id);
|
||||
|
||||
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 typedef void (*reconcile_cbk)(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output);
|
||||
|
||||
EXTERNC int reconcile_with_ids(void* negentropy, buffer* query, reconcile_cbk cbk);
|
||||
|
||||
EXTERNC const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[],
|
||||
uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user