/***********************************************************************
 * 
 *  Copyright (C) 2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library 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
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 *  Author: Juan Carlos Luciani <jluciani@novell.com>
 *
 ***********************************************************************/

#ifndef _SCHANNEL_
#define _SCHANNEL_

//===[ Include files ]=====================================================

//===[ External data ]=====================================================

//===[ External prototypes ]===============================================

//===[ Manifest constants ]================================================

//===[ Type definitions ]==================================================

//===[ Function prototypes ]===============================================

//===[ Global variables ]==================================================

//===[ Type definitions ]==================================================

// Forward reference
class ServerReq;

//
// SChannel Class Definition
//
class SChannel : public ObjRef
{
   // Object State
   enum ChannelStates
   {
      State_FailedInitialization = 1,
      State_Connected,
      State_Disconnected,
      State_Closed
   };
   ChannelStates        m_state;

   // Connection socket
   int                  m_socket;

   // Synchronization variables
   pthread_mutex_t      m_mutex;

   //
   // Server Request Map - This map contains all of the active ServerReq objects.
   //                      The key used to obtain ServerReq object in the map
   //                      is the Request Id.
   //
   typedef map<uint32_t, ServerReq*> RSMap;
   typedef RSMap::iterator RSMapIter;
   typedef pair<RSMapIter, bool> RSIterBoolPair;
   RSMap                m_rsMap;

   //
   // Service connection thread procedure
   //
   // Parameters:
   //    pSmartSChannel (input) -
   //       Pointer to SmartPtr<SChannel> object.
   //
   // Abstract: Thread in charge of servicing channel connection.
   //
   // Returns: Nothing.
   //
   static void* connectionThread(SmartPtr<SChannel> *pSmartSChannel);

public:

   //
   // Constructor
   //
   // Parameters:
   //    connSocket (input) -
   //       Socket for channel connection.
   //
   // Abstract: Constructs SChannel object.
   //
   // Returns: Nothing.
   //
   SChannel(int connSocket);

   //
   // Destructor
   ~SChannel(void);

   //
   // Initialization routine
   //
   // Parameters: None.
   //
   // Abstract: Initializes SChannel object.
   //
   // Returns: 0 if successful.
   //
   int init(void);

   //
   // Close channel routine
   //
   // Parameters: None.
   //
   // Abstract: Closes the channel.
   //
   // Returns: Nothing.
   //
   void closeChannel(void);

   //
   // Send Reply Data routine
   //
   // Parameters:
   //    reqId (input) -
   //       Request Id.
   //       
   //    pServerData (input) -
   //       Pointer to server data that must be sent to
   //       the client. Buffer is NOT released by the
   //       procedure.
   //
   //    serverDataLen (input) -
   //       Length of the server data.
   //
   // Abstract: Sends data to the client for active Request.
   //
   // Returns: 0 if successful.
   //
   int sendReplyData(uint32_t reqId,
                     char *pServerData,
                     int32_t serverDataLen);

   //
   // Send Reply Error routine
   //
   // Parameters:
   //    reqId (input) -
   //       Request Id.
   //       
   // Abstract: Indicates to the client that the request was
   //           not processed successfully..
   //
   // Returns: 0 if successful.
   //
   int sendReplyError(uint32_t reqId);
};
typedef SmartPtr<SChannel> SmartSChannel;


//===[ Function prototypes ]===============================================


#endif // _SCHANNEL_

//=========================================================================
//=========================================================================