#include "../include/net_transport.h"
#include "../include/net.go.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int holder_publish_dir(net_meshnode_t* node,
net_mesh_blob_adapter_t* adapter,
const char* root_path,
uint8_t** out_ref,
size_t* out_ref_len) {
int rc = net_serve_blob_transfer(node, adapter);
if (rc != NET_TRANSPORT_OK) {
fprintf(stderr, "serve_blob_transfer failed: %d\n", rc);
return rc;
}
rc = net_store_dir(adapter, root_path, out_ref, out_ref_len);
if (rc != NET_TRANSPORT_OK) {
fprintf(stderr, "store_dir(%s) failed: %d\n", root_path, rc);
return rc;
}
printf("stored %s -> manifest ref is %zu bytes\n", root_path, *out_ref_len);
return NET_TRANSPORT_OK;
}
static int fetcher_pull_dir(net_meshnode_t* node,
net_mesh_blob_adapter_t* adapter,
uint64_t source_id,
const uint8_t* manifest_ref,
size_t manifest_ref_len,
const char* dest_path) {
int rc = net_serve_blob_transfer(node, adapter);
if (rc != NET_TRANSPORT_OK) {
fprintf(stderr, "serve_blob_transfer (fetcher) failed: %d\n", rc);
return rc;
}
char* manifest_json = NULL;
size_t json_len = 0;
rc = net_dir_manifest_read(node, source_id, manifest_ref, manifest_ref_len,
&manifest_json, &json_len);
if (rc == NET_TRANSPORT_OK) {
printf("manifest (%zu bytes JSON): %s\n", json_len, manifest_json);
net_free_string(manifest_json);
} else {
fprintf(stderr, "manifest_read failed: %d\n", rc);
}
uint64_t files = 0, bytes = 0;
rc = net_fetch_dir(node, source_id, manifest_ref, manifest_ref_len,
dest_path, &files, &bytes);
if (rc != NET_TRANSPORT_OK) {
fprintf(stderr, "fetch_dir failed: %d\n", rc);
return rc;
}
printf("reconstructed %" PRIu64 " files (%" PRIu64 " bytes) under %s\n",
files, bytes, dest_path);
return NET_TRANSPORT_OK;
}
int main(void) {
fprintf(stderr,
"transport.c is an API walkthrough; wire holder_publish_dir() "
"and fetcher_pull_dir() onto live nodes per the bring-up "
"outline in main(). See net.go.h + mesh_demo.rs.\n");
(void)holder_publish_dir;
(void)fetcher_pull_dir;
return 0;
}