syncmlfw/dm/hostserver/dmhostserverbase/src/nsmldmhostserver.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Contains a common code for all dedicated dm host servers.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // ------------------------------------------------------------------------------------------------
       
    20 // Includes
       
    21 // ------------------------------------------------------------------------------------------------
       
    22 
       
    23 #include <s32mem.h>
       
    24 #include <data_caging_path_literals.hrh>
       
    25 #include <f32file.h>
       
    26 #include <e32math.h>
       
    27 #include <nsmlconstants.h>
       
    28 #include <nsmldebug.h>
       
    29 #include "nsmldmclientandserver.h"
       
    30 #include "nsmldmhostserver.h"
       
    31 #include "nsmldmhostsession.h"
       
    32 
       
    33 
       
    34 // ------------------------------------------------------------------------------------------------
       
    35 // Constants
       
    36 // ------------------------------------------------------------------------------------------------
       
    37 //
       
    38 const TInt KNSmlMaxSessionCount = 2;  // Allowed clients: Treemodule and another dm host server
       
    39 _LIT ( KNSmlDmThreadAux, "_thread" );
       
    40 _LIT ( KTxtDmHostServerBase,"NSmlDmHostServerBase");
       
    41 const TUint32 KNSmlDMHostServer1ID = 270506498;  // 0x101F9A02
       
    42 const TUint32 KNSmlDMHostServer2ID = 270506499;  // 0x101F9A03
       
    43 const TUint32 KNSmlDMHostServer3ID = 270506500;  // 0x101F9A04
       
    44 const TUint32 KNSmlDMHostServer4ID = 270506501;  // 0x101F9A05
       
    45 
       
    46 
       
    47 // ------------------------------------------------------------------------------------------------
       
    48 // CNSmlDMHostServer::~CNSmlDMHostServer
       
    49 // Destructor.
       
    50 // ------------------------------------------------------------------------------------------------
       
    51 //
       
    52 EXPORT_C CNSmlDMHostServer::~CNSmlDMHostServer()
       
    53 	{
       
    54 	_DBG_FILE("CNSmlDMHostServer::~CNSmlDMHostServer(): begin");
       
    55 	_DBG_FILE("CNSmlDMHostServer::~CNSmlDMHostServer(): end");
       
    56 	}
       
    57 // ------------------------------------------------------------------------------------------------
       
    58 // CNSmlDMHostServer::InitHostServer
       
    59 // Initializes the server, calls aFunction.
       
    60 // ------------------------------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C TInt CNSmlDMHostServer::InitHostServer ( const TDesC& aServerName, TThreadFunction aFunction )
       
    63     {
       
    64     // check server not already started
       
    65 	TFindServer findHostServer ( aServerName );
       
    66 	TFullName name;
       
    67 	if ( findHostServer.Next ( name ) == KErrNone )
       
    68 		{
       
    69 		return KErrAlreadyExists;
       
    70 		}
       
    71 	TInt error( KErrNone );
       
    72     error = ThreadFunction( (TAny*)aFunction );
       
    73 //	_DBG_FILE("CNSmlDMHostServer::InitHostServer(): end");        
       
    74 	return error;
       
    75     }
       
    76 
       
    77 // ------------------------------------------------------------------------------------------------
       
    78 // CNSmlDMHostServer::ThreadFunction
       
    79 // Starting point of a server thread.
       
    80 // ------------------------------------------------------------------------------------------------    
       
    81 //
       
    82 TInt CNSmlDMHostServer::ThreadFunction ( TAny* aStarted )
       
    83     {
       
    84     __UHEAP_MARK;
       
    85     CTrapCleanup* cleanup = CTrapCleanup::New();
       
    86     __ASSERT_ALWAYS ( cleanup, PanicServer ( EMainSchedulerError ) );    
       
    87     CActiveScheduler* scheduler = new CActiveScheduler();
       
    88     __ASSERT_ALWAYS ( scheduler, PanicServer ( EMainSchedulerError ) );
       
    89 	CActiveScheduler::Install ( scheduler );
       
    90 	TThreadFunction function = reinterpret_cast<TThreadFunction>( aStarted );
       
    91 
       
    92 	__UHEAP_MARK;
       
    93 	TInt ret( function( NULL ) );
       
    94 	__UHEAP_MARKEND;
       
    95 
       
    96     CActiveScheduler::Install ( NULL );
       
    97     delete scheduler;
       
    98     delete cleanup;
       
    99     __UHEAP_MARKEND;
       
   100     
       
   101     return ret;
       
   102     }
       
   103 
       
   104 // ------------------------------------------------------------------------------------------------
       
   105 // CNSmlDMHostServer::ServerName
       
   106 // Server exe name is concluded from the current thread name.
       
   107 // ------------------------------------------------------------------------------------------------    
       
   108 //
       
   109 TPtrC CNSmlDMHostServer::ServerName()
       
   110     {
       
   111 	RThread th;    
       
   112 	TName thname = th.Name();
       
   113 	TInt offset = thname.Find ( KNSmlDmThreadAux );
       
   114 	TPtrC serverExeName;
       
   115 	serverExeName.Set ( KNullDesC );		
       
   116 	if ( offset != KErrNotFound )
       
   117 		{
       
   118 		TPtrC serverName = thname.Left ( offset );
       
   119 		if ( !serverName.Compare ( KNSmlDmHostServer1Name ) )
       
   120 			{
       
   121 			serverExeName.Set ( KNSmlDmHostServer1Exe );
       
   122 			}
       
   123 		else if ( !serverName.Compare ( KNSmlDmHostServer2Name ) )
       
   124 			{
       
   125 			serverExeName.Set ( KNSmlDmHostServer2Exe );
       
   126 			} 
       
   127 		else if ( !serverName.Compare ( KNSmlDmHostServer3Name ) )
       
   128 			{
       
   129 			serverExeName.Set ( KNSmlDmHostServer3Exe );
       
   130 			} 
       
   131 		else if ( !serverName.Compare ( KNSmlDmHostServer4Name ) )
       
   132 			{
       
   133 			serverExeName.Set ( KNSmlDmHostServer4Exe );
       
   134 			}
       
   135 		}
       
   136 	return serverExeName;
       
   137     }
       
   138 // ------------------------------------------------------------------------------------------------
       
   139 // CNSmlDMHostServer::NewSessionL
       
   140 // Returns a new session object, when security and version checkings are successfully passed.
       
   141 // ------------------------------------------------------------------------------------------------
       
   142 //
       
   143 CSession2* CNSmlDMHostServer::NewSessionL( const TVersion& aVersion, const RMessage2& aMessage ) const
       
   144 	{
       
   145 	_DBG_FILE("CNSmlDMHostServer::NewSessionL(): begin");
       
   146 	if( iSessionCount >= KNSmlMaxSessionCount )
       
   147 		{
       
   148 		User::Leave(KErrCouldNotConnect);
       
   149 		}
       
   150 
       
   151 	// check we're the right version
       
   152 	TVersion ver ( KNSmlDmHostServerVerMajor, KNSmlDmHostServerVerMinor, KNSmlDmHostServerVerBuild );
       
   153 	if( !User::QueryVersionSupported ( ver, aVersion) )
       
   154 		{
       
   155 		_DBG_FILE("CNSmlDMHostServer::NewSessionL(): wrong version offered -> BAIL OUT!");
       
   156 		User::Leave ( KErrNotSupported );
       
   157 		}
       
   158 
       
   159 	TUint32 idOfCaller = aMessage.SecureId().iId;
       
   160 	if ( idOfCaller != KNSmlDMHostServer1ID && idOfCaller != KNSmlDMHostServer2ID
       
   161 			&& idOfCaller != KNSmlDMHostServer3ID && idOfCaller != KNSmlDMHostServer4ID )
       
   162 		{
       
   163 		_LIT_SECURITY_POLICY_S0 ( mySidPolicy, KNSmlSOSServerSecureID );
       
   164     	TBool status = mySidPolicy().CheckPolicy ( aMessage );
       
   165 		}
       
   166 
       
   167 	_DBG_FILE("CNSmlDMHostServer::NewSessionL(): end");
       
   168 	return CNSmlDMHostSession::NewL ( *CONST_CAST ( CNSmlDMHostServer*, this ) );	
       
   169 	}
       
   170 
       
   171 // ------------------------------------------------------------------------------------------------
       
   172 // CNSmlDMHostServer::CNSmlDMHostServer()
       
   173 // Constructor. Protected to prevent direct instantiation.
       
   174 // ------------------------------------------------------------------------------------------------
       
   175 //
       
   176 EXPORT_C CNSmlDMHostServer::CNSmlDMHostServer() : CServer2 ( EPriorityStandard )
       
   177 	{
       
   178 	_DBG_FILE("CNSmlDMHostServer::CNSmlDMHostServer(): begin");
       
   179 	_DBG_FILE("CNSmlDMHostServer::CNSmlDMHostServer(): end");
       
   180 	}
       
   181 
       
   182 // ------------------------------------------------------------------------------------------------
       
   183 // CNSmlDMHostServer::IncSessionCount
       
   184 // Increments session count.
       
   185 // ------------------------------------------------------------------------------------------------
       
   186 //
       
   187 void CNSmlDMHostServer::IncSessionCount()
       
   188 	{
       
   189 	_DBG_FILE("CNSmlDMHostServer::IncSessionCount(): begin");
       
   190 	iSessionCount++;
       
   191 	_DBG_FILE("CNSmlDMHostServer::IncSessionCount(): end");
       
   192 	}
       
   193 
       
   194 // ------------------------------------------------------------------------------------------------
       
   195 // CNSmlDMHostServer::DecSessionCount
       
   196 // Decrements session count. When count reaches zero, stops active scheduler.
       
   197 // ------------------------------------------------------------------------------------------------
       
   198 //
       
   199 void CNSmlDMHostServer::DecSessionCount()
       
   200 	{
       
   201 	_DBG_FILE("CNSmlDMHostServer::DecSessionCount(): begin");
       
   202 	iSessionCount--;
       
   203     if ( iSessionCount < 1 )
       
   204         {
       
   205         Cancel();
       
   206         CActiveScheduler::Stop();   
       
   207         }
       
   208 	_DBG_FILE("CNSmlDMHostServer::DecSessionCount(): end");
       
   209 	}
       
   210 
       
   211 // ------------------------------------------------------------------------------------------------
       
   212 // CNSmlDMHostServer::CallNewSessionL
       
   213 // Calls NewSessionL.
       
   214 // ------------------------------------------------------------------------------------------------
       
   215 //
       
   216 EXPORT_C CSession2* CNSmlDMHostServer::CallNewSessionL ( const TVersion& aVersion, const RMessage2& aMessage ) const
       
   217     {
       
   218 	_DBG_FILE("CNSmlDMHostServer::CallNewSessionL()");
       
   219     return CNSmlDMHostServer::NewSessionL( aVersion, aMessage );
       
   220     }
       
   221 
       
   222 // ------------------------------------------------------------------------------------------------
       
   223 // CNSmlDMHostServer::PanicServer()
       
   224 // Panics the server. Signals the semaphore in order to prevent dm client halting.
       
   225 // ------------------------------------------------------------------------------------------------
       
   226 //
       
   227 void CNSmlDMHostServer::PanicServer ( TNSmlDmHostServerPanic aPanic )
       
   228     {
       
   229 	_DBG_FILE("CNSmlDMHostServer::PanicServer()");            
       
   230 	RSemaphore s;
       
   231 	TBuf<64> semaphoreName ( ServerName() );
       
   232 	if( s.OpenGlobal ( semaphoreName ) == KErrNone )
       
   233 		{
       
   234 		s.Signal();
       
   235 		s.Close();
       
   236 		}
       
   237 	
       
   238 	User::Panic ( KTxtDmHostServerBase, aPanic );
       
   239     }	
       
   240 
       
   241 // End of file