//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: printobject.cpp
//
//Version: $Revision: 1.3 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/09 11:41:47 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Jussi Turunen
//
//Description:
//      class printobject implementation. See corresponding header
//      file for description.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 


#include "pf/debug.h"

#include "printobject.h"
#include "../adapter/taprimitives.h"

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_FSTREAM
#   include <fstream>
#else
#   include <fstream.h>
#endif


printObject :: printObject(void)
    : otMessage_skel()
{
    return;
}

printObject :: ~printObject(void)
{
}

void printObject :: send(const char* identifier_,
                        const otMessage::SerializedMessage& message_)
{
    string identifier = identifier_;

    debugString("PDU RECEIVED FOR PRINTING", identifier);
    for (pfUlong i = 0; i < message_.length(); ++i)
    {
        otMessage::ElementKind kind = message_[i].kind;
        string id = message_[i].identifier.in();
        string value = message_[i].value.in();
        pfUlong number = message_[i].number;
        debugString("id", id);
        debugPfUlong("kind", kind);
        if (kind == otMessage::Integer ||
            kind == otMessage::Boolean)
        {
            debugPfUlong("number", number);
        }
        else
        {
            debugString("value", value);
        }
    }
    return;
}

otMessage::SerializedMessage*
printObject :: sendTwoWay(const char *, const ParameterList&)
{
    throw NotImplemented();
    return 0;
}

void printObject :: saveIOR(CORBA_ORB_var orb_, string filename_)
{
    try
    {
	CORBA_String_var s = orb_->object_to_string(this);

        //
        // NOTE: use C FILEs rather than C++ file streams, because the
        // latter don't work with g++ when RTTI is in use
        //
        FILE *fp = fopen(filename_.c_str(), "w");
        if(fp == 0)
	{
	    extern int errno;
	    cerr << "printObject: can't open `" << filename_ << endl;
	    exit(0);
	}
	
        fprintf(fp, "%s\n", s.in());
        fclose(fp);
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
    catch(CORBA_SystemException& ex)
#endif
    {
	OBPrintException(ex);
	return;
    }
    return;
}

