/*
 *  OrbPco.java v0.10 20-DEC-1999
 *  Copyright (c) TKK/TLM/Calypso
 *  Author: Alexey Mednonogov
 */

package codec.pco;

import java.io.*;
import java.util.*;

import codec.*;
import codec.adapt.*;
import codec.convert.*;
import codec.debug.*;
import codec.dyntree.*;
import codec.export.*;
import codec.orb.*;
import codec.pco.*;
import codec.server.*;
import codec.client.*;
import codec.visit.*;
import codec.build.*;

/** Calypso Gateway PCO instance. */
final public class OrbPco extends com.t3.ot.pco.PCO { 

    private boolean isTestCase = false;
    private String testCaseName;
    private String pcoName;
    private String pcoRefFile;

    public OrbPco (String pcoName_, String pcoRefFile_) {

        super(pcoName_);

        pcoName = pcoName_;
        pcoRefFile = pcoRefFile_;

        org.omg.CORBA.ORB orb = 
            CorbaServer.getCodecObject().getOrbGeneric().getORB();

        com.t3.ot.misc.IOR.save(orb, this, pcoRefFile_);
    }

    public String getPcoName() {
        return pcoName;
    }

    public void receiveDisconnect() {

        System.out.println("OrbPco::receiveDisconnect(): " + 
            "Ending testcase " + testCaseName + ".");

        isTestCase = false;

        CorbaServer.getCodecObject().
			getCorbaObjectPool().deregister(pcoName);
    }

    public void receiveConnect(String testCaseName_) {

        System.out.println("OrbPco::receiveConnect(): " + 
            "Starting testcase " + testCaseName_ + ".");

        testCaseName = testCaseName_;

        CorbaServer.getCodecObject().
			getCorbaObjectPool().deregister(pcoName);

        isTestCase = true;
    }

    /** Method is invoked in the background every time Tester sends an ASP
     *  to Gateway. */
    public void receiveASP (com.t3.ot.pco.ASP asp)
		throws org.omg.CORBA.SystemException {

		try {

        if (isTestCase == false) return;
        System.gc(); // Fantastically improves JDK 1.2 reliability.
        String aspName = asp.getName();
        
        // Server-related activities:

        if (aspName.equals("pSREG_IOR")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": registering by IOR.");
            ServerPool.registerIOR(pcoName, asp);
            return;
        }
        if (aspName.equals("pSREG_RFILE")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": registering by RFILE.");
            ServerPool.registerRefFile(pcoName, asp);
            return;
        }
        if (aspName.equals("pSREG_NSERV")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": registering by NSERV.");
            ServerPool.registerNaming(pcoName, asp);
            return;
        }
        if (aspName.startsWith("pCALL_i")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": calling operation.");
            ServerPool.callOperation(pcoName, asp);
            return;
        }

        // Client-related activities:

        if (aspName.equals("pCREG_RFILE")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": advertising RFILE.");
            ClientPool.registerRefFile(pcoName, asp);
            return;
        }
        if (aspName.equals("pCREG_NSERV")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": advertising NSERV.");
            ClientPool.registerNaming(pcoName, asp);
            return;
        }
        if (aspName.startsWith("pREPLY_i")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": sending response.");
            ClientPool.operResponse(pcoName, asp);
            return;
        }
        if (aspName.equals("pRAISE")) {

            System.out.println("OrbPco::receiveASP(): " + pcoName +
                               ": throwing exception.");
            ClientPool.operResponse(pcoName, asp);
            return;
        }

        // If we are here, then PDU received from Tester is unrecognized.
        // This deserves issuing fatal Gateway Exception PDU:

        System.err.println("OrbPco::receiveASP(): PDU name \"" +
            aspName + "\" has not been recognized by encoder.");
        AspConverter.throwFatalException(pcoName);
        return;       

		}
		catch (org.omg.CORBA.SystemException ex) {

			// System.out.println(ex.getMessage());
            // ex.printStackTrace();
			// throw ex;
		}
    }
            
    /** Method is invoked by Gateway every time it wishes to send ASP to
     *  Tester. */
    public void sendASP(com.t3.ot.pco.ASP asp) {

        if (isTestCase == false) return;
        System.gc(); // Fantastically improves JDK 1.2 reliability.
        System.out.println("OrbPco::sendASP(): " + 
		    "Sending ASP through " + pcoName + ".");
        super.sendASP(asp);
    }
}
