/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2011 Stephane Bisaro, aka Iri <iri@irizone.net>

This program 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 program 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 program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/

#ifdef __cplusplus
#error This source file is not C++ but rather C. Please use a C-compiler
#endif

#include "../include/scol_glib_net.h"

#if ((defined _WIN32) || (defined __WIN32__))
cbmachine ww;
#endif
mmachine  mm;



int SCOL_gnetConnect (mmachine m)
{
    int maddress, mport;
    gchar *address, *buffer, *url;
    int port, bufferpos = 0;
    gssize received = 0;
    GString *page = NULL;
    GSocketConnectable *addr;
    GSocket * socket;
    GSocketConnection *connection = NULL;
    GSocketClient *client;
    GError *err = NULL;

    const int sizeloop = 32*1024;

    mport = MMpull (m);
    maddress = MMpull (m);
    /*mchannel = MMget (m, 0);

    if (mchannel == NIL)
    {
        MMechostr (0, "SCOL_gnetConnect : entering\n");
        MMpull (m);
        MMpush (m, NIL);
        return 0;
    }*/

    if (maddress == NIL)
        address = "127.0.0.1";
    else
        address = MMstartstr (m, MTOP (maddress));
    if (mport == NIL)
        port = 80;
    else
        port = MTOI (mport);

    url = "index.php";

    addr = g_network_address_new (address, port);
    client = g_socket_client_new ();
    connection = g_socket_client_connect (client, addr, NULL, &err);
    if (err != NULL)
    {
        MMechostr (0, "SCOL_gnetConnect error : %d -> %s", err->code, err->message);
        MMpush (m, NIL);
        g_error_free (err);
        return 0;
    }
    socket = g_socket_connection_get_socket (connection);

    if (socket)
    {
        buffer = g_malloc (sizeof (gchar) * sizeloop);
        page = g_string_new (NULL);
        g_socket_send_with_blocking(socket, url, strlen (url), FALSE, NULL, NULL);
        while ((received = g_socket_receive_with_blocking (socket, buffer, sizeloop, FALSE, NULL, NULL)))
        {
            for (bufferpos = 0; bufferpos < received; bufferpos++)
            {
                g_string_append_c (page, buffer[bufferpos]);
            }
        }
        g_free (buffer);
    }
    else
    {
        MMechostr (0, "SCOL_gnetConnect error : no socket available\n");
        MMpush (m, NIL);
        return 0;
    }
    Mpushstrbloc (m, page->str);
    g_string_free (page, TRUE);

    g_socket_close (socket, NULL);
    return 0;
}







/* API definitions : */

char* glib_net_name[GLIB_NET_PKG_NB]=
{
    "_gnetConnect"
};

int (*glib_net_fun[GLIB_NET_PKG_NB])(mmachine m)=
{
    SCOL_gnetConnect
};

int glib_net_narg[GLIB_NET_PKG_NB]=
{
    3
};

char* glib_net_type[GLIB_NET_PKG_NB]=
{
    "fun [S S] S"
};

/**
 * \brief Load the Scol api
 */
int SCOLinitGnetClass (mmachine m)
{
    int k;

    MMechostr (0, "SCOLinitGnetClass : entering\n");

    k = PKhardpak (m, "GNETengine", GLIB_NET_PKG_NB, glib_net_name, glib_net_fun, glib_net_narg, glib_net_type);
    return k;
}


/**
 * \brief Load and free the regular expression library
 * Plateforms supported : MS Windows and GNU / Linux
 */

int GnetRelease ()
{
    MMechostr (0, "\nGNET library released !\n");
    return 0;
}





#if ((defined _WIN32) || (defined __WIN32__))
# define SCOL_GBASE_PLUGIN_EXPORT __declspec (dllexport)
#elif ((defined linux) || (defined __linux))
# define SCOL_GBASE_PLUGIN_EXPORT
#else
# error no platform supported
#endif

SCOL_GBASE_PLUGIN_EXPORT int SCOLloadGBASE(mmachine m, cbmachine w)
{
    ww = w;
    mm = m;

    SCOLinitplugin(w);
    return  SCOLinitGnetClass (m);
}

SCOL_GBASE_PLUGIN_EXPORT int SCOLfreeGBASE()
{
    GnetRelease ();
    return 0;
}
