/*
 * myserver-impl.c
 *
 * Function:
 * This is the 'meat' of your server.  This is where you get 
 * your server to do its tricks.  Some of the code in this file
 * is 'cut-n-paste', which you'll want to copy into your 
 * application. But the important parts that you need to 
 * design for yerself are all here as well.
 *
 * Author:
 *   Miguel de Icaza (miguel@helixcode.com)
 * Modifications by Linas Vepstas <linas@linas.org>
 */


#include <bonobo.h>
#include "iface.h"
#include "myserver.h"

#define PARENT_TYPE BONOBO_X_OBJECT_TYPE

/* This is where we will dump our messages */
#define LOGFILE "/tmp/xxx"

static GtkObjectClass *myserver_parent_class;

/*
 * Implemented GtkObject::destroy
 * The code here is supposed to destroy an instance of my (your)
 * object.  In this case, it just frees the string we allocated 
 * for this instance.
 */
static void
myserver_object_destroy (GtkObject *object)
{
    MyServer *myserver = MYSERVER (object);
    g_free (myserver->my_instance_data);
    myserver_parent_class->destroy (object);
}


/*
 * CORBA Demo::MyServer::sendmsg method implementation
 * Note the correspondance between the arguments and how
 * the 'sendmsg' method is defined in the IDL.  Note that
 * here, in the server, we are 'receiving' the message: 
 * it was the client that sent it.
 */
static void
impl_demo_myserver_getmesg (PortableServer_Servant servant,
                        const CORBA_char *string,
                        CORBA_Environment *ev)
{
    MyServer *myserver = MYSERVER (bonobo_object_from_servant (servant));
    FILE *fh = fopen (LOGFILE, "a");
    fprintf (fh, "MyServer received this message: %s\n"
                 "\t Hey, the instance data is: %s)\n",
             string, 
             myserver->my_instance_data);
     fclose (fh);
}


static void
myserver_class_init (MyServerClass *klass)
{
    GtkObjectClass *object_class = (GtkObjectClass *) klass;
    POA_Bonobo_Dasample_MyServer__epv *epv = &klass->epv;

    FILE *fh = fopen (LOGFILE, "a");
    fprintf (fh, "duude, man, initing the class!! \n");
    fclose (fh);

    myserver_parent_class = gtk_type_class (PARENT_TYPE);
    object_class->destroy = myserver_object_destroy;

    /* 'sendmsg' is the same name as defined in the IDL */
    epv->sendmsg = impl_demo_myserver_getmesg;
}


static void
myserver_init (MyServer *myserver)
{
    static int i = 0;
    FILE *fh = fopen (LOGFILE, "a");
    fprintf (fh, "Like, yeah, wow, initializzzzing instance %d \n", i);
    fclose (fh);
    myserver->my_instance_data = g_strdup_printf (
               "I am myserver instance %d!", i++);
}

GtkType
myserver_get_type (void)
{
    static GtkType type = 0;

    FILE *fh = fopen (LOGFILE, "a");
    fprintf (fh, "getting myserver type=%p!\n", type);
    fclose (fh);

    if (!type) 
    {
        GtkTypeInfo info = {
            "MyServer",
             sizeof (MyServer),
             sizeof (MyServerClass),
             (GtkClassInitFunc) myserver_class_init,
             (GtkObjectInitFunc) myserver_init,
             NULL, /* reserved 1 */
             NULL, /* reserved 2 */
             (GtkClassInitFunc) NULL
        };
        /*
         *   Here we use bonobo_x_type_unique instead of
         * gtk_type_unique, this auto-generates a load of
         * CORBA structures for us. All derived types must
         * use bonobo_x_type_unique.
         */
        type = bonobo_x_type_unique (
             PARENT_TYPE,
             POA_Bonobo_Dasample_MyServer__init, NULL,
             GTK_STRUCT_OFFSET (MyServerClass, epv),
             &info);
    }
    return type;
}

MyServer *
myserver_new (void)
{
     return gtk_type_new (myserver_get_type ());
}

/* ============================ END OF FILE ===================== */

