/***********************************************************************
 * 
 *  Copyright (C) 2005-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.
 * 
 ***********************************************************************/

using System;
using System.Collections;
using System.Text;
using System.Threading;
using sscs.verbs;
using sscs.common;

/*
 * After the bytes format is finalized for Windows this class might need to 
 * be modified..
 * Depending upon impact either a new method can be invoked after platfrom 
 * check or a Factory pattern can be used to redesign this class.
 */
       
/* 
 * Namespaces used inside SSCS(main) will be of the following format "sscs.*"
 * The NameSpaces available are.... 
 * sscs.common [session, RequestParser]
 * sscs.authentication
 * sscs.verbs;[ssVerb, .......]
 * sscs.cache [secretstore, keychain, secret]
 * sscs.lss;
 * sscs.synchronization;
 * sscs.crypto
 */
 
namespace sscs.common 
{
    /* This class will be used to convert raw bytes to one of the SecretStore 
     * Verbs. Although there is no need to have many instances,
     * this class will NOT be singleton class since we 
     * do not want a hit on performance (synchronization, obtaining locks,
     * releasing locks...etc )
     * Making it singleton is not giving us any advantage versus performance.
     */
    internal class RequestParser
    {
        Hashtable msgIdMap = new Hashtable();

        internal RequestParser()
        {
            msgIdMap.Add(1,"sscs.verbs.OpenSecretStore");
            msgIdMap.Add(2,"sscs.verbs.CloseSecretStore");
            msgIdMap.Add(3,"sscs.verbs.RemoveSecretStore");
            msgIdMap.Add(4,"sscs.verbs.EnumerateKeyChainIds");
            msgIdMap.Add(5,"sscs.verbs.AddKeyChain");
            msgIdMap.Add(6,"sscs.verbs.RemoveKeyChain");
            msgIdMap.Add(7,"sscs.verbs.EnumerateSecretIds");
            msgIdMap.Add(8,"sscs.verbs.ReadSecret");
            msgIdMap.Add(9,"sscs.verbs.WriteSecret");
            msgIdMap.Add(10,"sscs.verbs.RemoveSecret");
            msgIdMap.Add(11,"sscs.verbs.GetSecretStoreInfo");
            msgIdMap.Add(12,"sscs.verbs.GetKeyChainInfo");
            msgIdMap.Add(13,"sscs.verbs.LockCache");
            msgIdMap.Add(14,"sscs.verbs.UnLockCache");
            msgIdMap.Add(15,"sscs.verbs.SetMasterPasscode");
            msgIdMap.Add(16,"sscs.verbs.ReadKey");
            msgIdMap.Add(17,"sscs.verbs.WriteKey");
            msgIdMap.Add(18,"sscs.verbs.SetMasterPassword");
            msgIdMap.Add(19,"sscs.verbs.IsSecretPersistent");
			msgIdMap.Add(20,"sscs.verbs.ObjectSerialization");
			msgIdMap.Add(21,"sscs.verbs.WriteBinaryKey");
			msgIdMap.Add(22,"sscs.verbs.ReadBinaryKey");
        }
	
	    		
        /* Processes the request and returns the corrrect SSverb.
         * This interface works on the class member rawbytes and 
         * returns the result.  		  
         */
        internal SSVerb ParseRequest(byte[] rawbytes)
        {
            if (rawbytes == null)
 	        throw new FormatException("Message format incorrect");
 		       
            String className = GetClassName(rawbytes); 

            SSVerb theVerb = (SSVerb)Activator.CreateInstance(null, className ).Unwrap();
            theVerb.SetMessageContent(rawbytes); 
            
            /* 
             * TBD: We can send the activation params in the same call. 
             */
            //SSVerb theVerb = (SSVerb)Activator.CreateInstance(Type.GetType(className)).Unwrap();
           
            return theVerb;
        }

        private string GetClassName(byte[] ipcbytes)
        {
           /* 
            * Read first two bytes and get ushort
            * Look up table and send class name  
            */
            ushort msgId = BitConverter.ToUInt16(ipcbytes,0);
            return ((String)(msgIdMap[(int)msgId]));
        }
    }
}