//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: testing/testadapter/uni/uniserver.cpp
//
//Version: $Revision: 1.7 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/11/04 08:16:50 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Jussi Turunen
//
//Description:
//      Constructs a conduit stack for server used in the UNI testing.
//      See unicodingstate.h for ASCII art picture of the stack.
//
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 

#include <OB/CORBA.h>
#include <OB/Util.h>

#include <typeinfo>
#include <string>

#include <stdio.h>    // for getopt, printf, strcpy and FILE
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

#ifdef HAVE_FSTREAM
#   include <fstream>
#else
#   include <fstream.h>
#endif
#include "pf/system.h"
#include "pf/conduit.h"
#include "pf/debug.h"
#include "protocol/saal/saalunilink.h"

#include "unicodingprotocol.h"
#include "../adapter/tastate.h"
#include "../adapter/msg_impl.h"


int main(int argc, char* argv[], char*[])
{

    if (argc == 1)
    {
        cout << "too few argumets" << endl;
        cout << "USAGE:" << endl;
        cout << "./server -v" << endl;
        cout << "./server -z port -C VCI -i reference_file -P PCO " 
             << "-o basename_for_own_ref_file <BOA options>" << endl;
        cout << endl;
        cout << "-i specifies the file which the tester IOR is read from"
             << " (must be the whole filename)." << endl;
        cout << "-o specifies the name of the file to which the adapter IOR " 
             << endl << "reference is written to. (Just the basename, no "
             << ".ref extension)" << endl;
        cout << "NOTE! there is a  difference between -o and -i "
             << "(basename / filename)." << endl;
        cout << "-P PCO is a letter defining the PCO used in testing (R or T)" 
             << " in UNI" << endl;
        cout << "Example:" << endl;
        cout << argv[0] << " -z 0 -C 301 -i forwarder.ref -o "
             << "testerAdapterr -P R <ORB/BOA options>" << endl;
        exit(0);
    }

    pfUlong port;
    pfUlong vci;
    char testerFileRef[300];
    char myAdapterFileRef[300];
    string PCO;

    //strcpy(myAdapterFileRef, "testerAdapter"); 

    pfSystem::init(argc, argv);

    try
    {

        // The rest of the argument handling is done here so that
        // BOA options are correctly read by the BOA.
        // port, vpi, version
        const char *options = "z:C:i:P:o:v";
        int opcode;
        opterr=0; // no "invalid option" messages from getopt
        char pco;
        
        while((opcode=getopt(argc, argv, options)) != -1)
        {
            switch (opcode)
            {
                case 'z':
                    port = atoi(optarg);
                    cout << "port was " << port << endl;
                    break;
                case 'v':
                    cout << "version: UNItest x.y" << endl;
                    exit(0);
                    break;
                case 'C':
                    vci = atoi(optarg);
                    cout << "VCI was " << vci << endl;
                    break;
                case 'i':
                    strcpy(testerFileRef, optarg);
                    cout << "Filename was " << testerFileRef << endl;
                    break;
                case 'P':
                    PCO = string(optarg);
                    cout << "PCO was " << PCO << endl;
                    //pco = tolower(PCO[0]);
                    //cout << "pco was " << pco << endl;
                    break;
                case 'o':
                    strcpy(myAdapterFileRef, optarg);
                    cout << "Object ref will be written to "
                         << myAdapterFileRef << ".ref" << endl;
                    break;
                default:
                    break;
            }
        }

        string file1(myAdapterFileRef);
        pfConduit testerAdapter;

        taState *activeState = new taState();
        // IOR *MUST* be specified with the -f switch
        testerAdapter = pfTestAdapter::
            createTestAdapter(pfSystem::instance()->getORB(), 
			      file1, activeState, PCO, testerFileRef);

        debugOutputCout();

        pfConduit uniCodingProxy = uniCodingProtocol :: create();
        // createATMUNIlink forms the following protocols
        // USSCF <-> SSCOP <-> CPCS
        saalUNIlink *lowerStack = saalUNIlink::createATMUNIlink(0, port,
                                                                0, vci);
        pfConduit usscfProxy = lowerStack->getConduit();

        // Created conduits are connected here.
        testerAdapter.connectToA(uniCodingProxy);
        uniCodingProxy.connectToB(testerAdapter);
        uniCodingProxy.connectToA(usscfProxy);
        usscfProxy.connectToB(uniCodingProxy);

        pfSystem::instance()->run();
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
    catch(CORBA_SystemException& ex)
#endif
    {
        OBPrintException(ex);
        return 1;
    }
    return 0;
}

