/*
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "gfiber.h"

static GMainLoop *loop;
static int ret;

static void
read_file_in_fiber (GFiber      *fiber,
                    gpointer     user_data)
{
  const gchar *file_name = user_data;
  GFile *file;
  GError *error;
  GFileInputStream *file_input_stream;
  gchar buf[1024];
  gssize bytes_read;

  g_print ("Going to read '%s'\n", file_name);

  file = g_file_new_for_commandline_arg (file_name);

  g_file_read_async (file,
                     G_PRIORITY_DEFAULT,
                     NULL,
                     g_fiber_get_async_ready_callback (fiber),
                     g_fiber_get_async_ready_user_data (fiber));

  g_fiber_yield (fiber);

  error = NULL;
  file_input_stream = g_file_read_finish (file,
                                          g_fiber_get_async_result (fiber),
                                          &error);
  if (file_input_stream == NULL)
    {
      g_printerr ("Error getting file input stream for %s: %s\n",
                  file_name,
                  error->message);

      goto out;
    }

  g_debug ("Got the GFileInputStream. Now sleeping 3000 msec from fiber");
  g_fiber_sleep (fiber, 3000);
  g_debug ("Done sleeping 3000 msec. Now reading data from stream.");

  g_input_stream_read_async (G_INPUT_STREAM (file_input_stream),
                             buf,
                             sizeof buf,
                             G_PRIORITY_DEFAULT,
                             NULL,
                             g_fiber_get_async_ready_callback (fiber),
                             g_fiber_get_async_ready_user_data (fiber));

  g_fiber_yield (fiber);

  bytes_read = g_input_stream_read_finish (G_INPUT_STREAM (file_input_stream),
                                           g_fiber_get_async_result (fiber),
                                           &error);
  if (bytes_read == -1)
    {
      g_printerr ("Error reading from file input stream for %s: %s\n",
                  file_name,
                  error->message);

      goto out;
    }

  g_print ("Read %" G_GSSIZE_FORMAT " bytes: '%s'\n", bytes_read, buf);

  ret = 0;

 out:
  if (file != NULL)
    g_object_unref (file);
  if (file_input_stream != NULL)
    g_object_unref (file_input_stream);
  g_main_loop_quit (loop);
}


static gboolean
do_stuff (gpointer user_data)
{
  g_print ("running fiber\n");
  g_fiber_run (read_file_in_fiber, user_data);
  g_print ("returned to main\n");
  return FALSE;
}

static gboolean
print_mark (gpointer data)
{
  g_print ("in print_mark()\n");
  return TRUE;
}

int
main (int argc, char *argv[])
{
  ret = 1;

  g_type_init ();

  if (argc != 2)
    {
      g_printerr ("Usage: gfiber <filename>\n");
      goto out;
    }

  loop = g_main_loop_new (NULL, FALSE);

  g_idle_add (do_stuff, argv[1]);
  g_timeout_add (500, print_mark, NULL);

  g_main_loop_run (loop);
  g_main_loop_unref (loop);

 out:
  return ret;
}
