Skip to content

Instantly share code, notes, and snippets.

@tdfischer
Created April 18, 2014 10:51
Show Gist options
  • Save tdfischer/11037402 to your computer and use it in GitHub Desktop.
Save tdfischer/11037402 to your computer and use it in GitHub Desktop.
entropy-control
#include <glib.h>
#include <entropy/client.h>
#include <entropy/resource.h>
#include <stdio.h>
GMainLoop *loop = NULL;
void
cb_download_file (EntropyClient *client, gchar **argv)
{
const gchar *collectionID = argv[2];
const gchar *resourceID = argv[3];
EntropyCollection *collection = entropy_client_get_collection (client, collectionID);
if (collection) {
g_debug ("Downloading resource");
EntropyResource *resource = entropy_collection_get_resource (collection, resourceID);
GravitonNodeStream *stream = entropy_resource_get_stream (resource);
GIOStream *ioStream = graviton_node_stream_open (stream);
GInputStream *input = g_io_stream_get_input_stream (ioStream);
gchar buf[1024];
GError *error = NULL;
gssize read_size = g_input_stream_read (input, &buf, sizeof (buf), NULL, &error);
if (error) {
g_error ("Error while reading: %s", error->message);
}
g_debug ("Read %i into %i", read_size, sizeof(buf));
while (read_size > 0) {
fwrite (buf, read_size, 1, stdout);
read_size = g_input_stream_read (input, &buf, sizeof (buf), NULL, &error);
g_debug ("Read %i", read_size);
}
g_debug ("Done!");
if (error)
g_print ("Error: %s", error->message);
} else {
g_error ("Could not find collection %s", collectionID);
}
g_main_loop_quit (loop);
}
void
cb_browse_collection (EntropyClient *client, gchar *collectionID)
{
EntropyCollection *collection = entropy_client_get_collection (client, collectionID);
if (collection) {
g_printf ("Contents of %s:\n", collectionID);
EntropyCollectionCursor *cursor = entropy_collection_get_cursor (collection);
EntropyResource *resource = NULL;
while (resource = entropy_collection_cursor_next (cursor, NULL)) {
GError *error = NULL;
gchar *url;
gchar *guid;
g_object_get (resource, "guid", &guid, NULL);
entropy_resource_get_metadata (resource, &error, "nie:url", &url, NULL);
g_printf ("%s: %s\n", guid, url);
g_free (guid);
}
} else {
g_printf ("Could not find collection %s\n", collectionID);
}
g_free (collectionID);
g_main_loop_quit (loop);
}
void
cb_list_collections (EntropyClient *client, gpointer *data)
{
g_printf ("Found collections:\n");
GList *collections = entropy_client_list_collections (client);
GList *cur = collections;
while (cur) {
GravitonNode *node = entropy_collection_get_node (cur->data);
GInetSocketAddress *socket_address;
GInetAddress *address;
gchar *addr_str;
g_object_get (node, "address", &socket_address, NULL);
address = g_inet_socket_address_get_address (socket_address);
addr_str = g_inet_address_to_string (address);
g_printf ("%s on %s\n", entropy_collection_get_guid (cur->data), addr_str);
g_free (addr_str);
g_object_unref (node);
g_object_unref (address);
g_object_unref (socket_address);
cur = cur->next;
}
g_main_loop_quit (loop);
}
int main(int argc, char** argv)
{
g_type_init ();
loop = g_main_loop_new (NULL, FALSE);
EntropyClient *client = entropy_client_new ();
const gchar *command;
if (argc == 1)
command = "ls";
else
command = argv[1];
g_debug ("command %s %d", command, argc);
if (strcmp (command, "ls") == 0) {
if (argc == 3) {
const gchar *collectionID = argv[2];
g_debug ("browse %s", collectionID);
g_signal_connect (client,
"all-collections-found",
G_CALLBACK (cb_browse_collection),
g_strdup (collectionID));
} else {
g_signal_connect (client,
"all-collections-found",
G_CALLBACK (cb_list_collections),
NULL);
}
} else if (strcmp (command, "cat") == 0) {
g_signal_connect (client,
"all-collections-found",
G_CALLBACK (cb_download_file),
argv);
} else if (strcmp (command, "add") == 0) {
GravitonNode *node = graviton_node_new_from_id (
EntropyManager *manager = entropy_client_get_manager (client, argv[2]);
entropy_manager_add_collection (manager, argv[3]);
}
g_main_loop_run (loop);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment