# RequesterSession.java

    /*
    ** Module   : RequesterSession.java
    ** Abstract : Requester side of a multiplexed session
    **
    ** Copyright (c) 2004-2017, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ----------------Description-----------------
    ** 001 ECF 20071101   @35864 Created initial version. Requester side of a
    **                           multiplexed session.
    ** 002 GES 20090723   @43363 Reduced logging of normal session ends to FINE
    **                           from INFO.
    ** 003 CA  20090825   @43757 In callTerminateRemote, if the exception is a
    **                           InterruptedException, will not be logged as a
    **                           WARNING.
    ** 004 CA  20090825   @43769 Fixed a race issue which can lead to main
    **                           conversation thread interruption: in
    **                           callTerminateRemote, if the remote R/W threads
    **                           end after the transaction is finished, the
    **                           main conversation thread may remain set as
    **                           interrupted, leading into client being reset to
    **                           login screen.
    ** 005 CA  20090826   @43771 One more deadlock fix, this time introduced by
    **                           H004 (@43769) - do not wait for the remote R/W
    **                           threads to finish if the main session is closed;
    **                           else, it will result in a deadlock, where the
    **                           conversation thread waits for R/W threads to
    **                           finish (as the main session is ending) and the
    **                           R thread tries to end this session too.
    ** 006 CA  20090901   @43814 In preprocess, build a control for this virtual
    **                           session, if one doesn't exist. The control  for
    **                           the context under which the virtual session is
    **                           created is set as the virtual control's
    **                           leftSideControl, to chain the calls. Also, now
    **                           the virtual sessions save the ID for the local
    **                           context under which they were created, to fix
    **                           a deregistration leak.
    ** 007 CA  20090921   @43949 SilentUnwindException's are intercepted and
    **                           not logged. Rolledback H004 and H005 (@43769 and
    **                           43771) as these tried to solve some side-effects
    **                           of the  incorrect Control mechanism when
    **                           server-to-server  sessions were used (solved by
    **                           H006).
    ** 008 CA  20100329   @44781 The session termination code was moved to
    **                           RouterSessionManager, to allow proper
    **                           synchronization for each server-to-server queue.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.net;

    /**
     * An extension of a multiplexed session, used on the requesting side of a
     * virtualized connection.
     * <p>
     * This class overrides the parent's {@link #terminate()} method to make a
     * remote call to trigger cleanup of the virtual session on the other side of
     * the network connection.  It also overrides {@link #cleanupContext()} to do
     * nothing, to avoid terminating the current user context.
     */
    final class RequesterSession
    extends VirtualSession
    {
       /**
        * Constructor.  The current thread's effective context will be used as the
        * session's security context.
        *
        * @param   queue
        *          Message transport.
        * @param   context
        *          Security context.  Must not be <code>null</code>.
        * @param   peerID
        *          Unique identifier for this session's peer security context on
        *          the other side of the network connection.
        * @param   contextID
        *          Unique identifier for this session's local security context on
        *          the this side of the network connection.
        */
       RequesterSession(Queue queue, Object context, int peerID, int contextID)
       {
          super(queue, context, peerID, contextID);
       }

       /**
        * Terminate the virtual network session on the remote side of the
        * connection, then perform termination processing for the session on this
        * side.
        */
       public void terminate()
       {
          SessionManager.get().terminateVirtual(this);
       }

       /**
        * Override the parent's implementation to do nothing.  The security
        * context must not be terminated by this session, because it was not
        * created for this session.  Rather, the session was "piggy-backed" on the
        * existing security context for the authenticated subject which requested
        * the virtual session.
        */
       protected final void cleanupContext()
       {
       }

       /**
        * Update the security context ID of the given message to this session's
        * (peer) context ID, before it is sent.  This will the other side of the
        * connection to demultiplex the inbound message to provide the appropriate
        * security context.
        *
        * @param   message
        *          Message to process.
        */
       protected void preprocess(Message message)
       {
          super.preprocess(message);

          // make sure this session has a valid Control
          Control ct = Control.locate(getContextID());
          if (!ct.isReady())
          {
             ct.initControl(this);

             ct.setLeftSideControl(Control.locate(0));
          }
       }
    }