IEBgps/src/IEBgpServer.cpp
changeset 3 93fff7023be8
equal deleted inserted replaced
2:e1e28b0273b0 3:93fff7023be8
       
     1 /*
       
     2 * Copyright (c) 2009 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: Juha Kauppinen, Mika Hokkanen
       
    13 * 
       
    14 * Description: Photo Browser
       
    15 *
       
    16 */
       
    17 
       
    18 // Include files
       
    19 #include <e32cons.h>
       
    20 
       
    21 #include <IEBgpsInfo.h>
       
    22 //#include <IEBGPSTrace.h>
       
    23 
       
    24 #include "IEBgpServer.h"
       
    25 #include "IEBgpServerSession.h"
       
    26 
       
    27 
       
    28 // --------------------------- MEMBER FUNCTIONS ---------------------------- //
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 //
       
    33 // ----------------------------------------------------------------------------
       
    34 CIEBgpServer* CIEBgpServer::NewL()
       
    35 {
       
    36 	DP0_IMAGIC((_L("CIEBgpServer::NewL ++")));
       
    37 	CIEBgpServer* self = new (ELeave) CIEBgpServer();
       
    38 	CleanupStack::PushL(self);
       
    39 	self->ConstructL();
       
    40 	CleanupStack::Pop();
       
    41 	DP0_IMAGIC((_L("CIEBgpServer::NewL --")));
       
    42 	return self;
       
    43 }
       
    44 
       
    45 CIEBgpServer::~CIEBgpServer()
       
    46 {
       
    47 DP0_IMAGIC((_L("CIEBgpServer::~CIEBgpServer")));
       
    48 
       
    49 iFileServer.Close();
       
    50 RFbsSession::Disconnect();
       
    51 
       
    52 DP0_IMAGIC((_L("CIEBgpServer::~CIEBgpServer")));	
       
    53 }
       
    54 
       
    55 //EPriorityIdle, EPriorityLow, EPriorityStandard, EPriorityUserInput, EPriorityHigh
       
    56 CIEBgpServer::CIEBgpServer()
       
    57 :CServer2(EPriorityIdle)
       
    58 {	
       
    59 }
       
    60 
       
    61 void CIEBgpServer::ConstructL()
       
    62 {
       
    63 	DP0_IMAGIC((_L("CIEBgpServer::ConstructL ++")));
       
    64 	StartL(KIEBgpServerName);
       
    65 	
       
    66 	User::LeaveIfError(iFileServer.Connect());
       
    67     User::LeaveIfError( FbsStartup() );
       
    68     User::LeaveIfError(RFbsSession::Connect());
       
    69 	
       
    70 	DP0_IMAGIC((_L("CIEBgpServer::ConstructL --")));
       
    71 }
       
    72 
       
    73 CSession2* CIEBgpServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
       
    74 {
       
    75 	DP0_IMAGIC((_L("CIEBgpServer::NewSessionL ++")));
       
    76 	CIEBgpServerSession* session = CIEBgpServerSession::NewL(const_cast<RFs*>(&iFileServer));
       
    77 	DP0_IMAGIC((_L("CIEBgpServer::NewSessionL --")));
       
    78 	return session;
       
    79 }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // Thread function to start the server
       
    83 //
       
    84 // ----------------------------------------------------------------------------
       
    85 GLDEF_C TInt CIEBgpServer::ThreadFunction(TAny* /*aParam*/)
       
    86 {
       
    87 	DP0_IMAGIC((_L("CIEBgpServer::ThreadFunction ++")));
       
    88 	// First get the cleanup stack
       
    89 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
    90 	
       
    91 	TRAPD(error, StartServerL());
       
    92 	
       
    93 	delete cleanupStack;
       
    94 	
       
    95 	DP0_IMAGIC((_L("CIEBgpServer::ThreadFunction --")));
       
    96 	
       
    97 	return error;
       
    98 }
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // Starts the server
       
   102 //
       
   103 // ----------------------------------------------------------------------------
       
   104 void CIEBgpServer::StartServerL()
       
   105 {
       
   106 	DP0_IMAGIC((_L("CIEBgpServer::StartServerL ++")));
       
   107 	// Create an active scheduler before server is created
       
   108 	CActiveScheduler* as = new (ELeave) CActiveScheduler();
       
   109 	CleanupStack::PushL(as);
       
   110 	CActiveScheduler::Install(as);
       
   111 	
       
   112 	// Create server
       
   113 	CIEBgpServer* server = CIEBgpServer::NewL();
       
   114 	CleanupStack::PushL(server);
       
   115 	
       
   116 	RThread::Rendezvous(KErrNone);
       
   117 	
       
   118 	// Start active scheduler
       
   119 	CActiveScheduler::Start();
       
   120 	
       
   121 	// Clean up
       
   122 	CleanupStack::PopAndDestroy(2, as);
       
   123 	
       
   124 	DP0_IMAGIC((_L("CIEBgpServer::StartServerL --")));
       
   125 }
       
   126 
       
   127 // ----------------------------------------------------------------------------
       
   128 // Creates the server thread
       
   129 //
       
   130 // ----------------------------------------------------------------------------
       
   131 EXPORT_C TInt CreateServerThread(RThread& aThread)
       
   132 {
       
   133 	DP0_IMAGIC((_L("CIEBgpServer::CreateServerThread ++")));
       
   134 	TInt error = KErrNone;
       
   135 	
       
   136 	// Check if the server already exists
       
   137 	TFindServer findServer(KIEBgpServerName);
       
   138 	TFileName matchingServer;
       
   139 	
       
   140 	if(findServer.Next(matchingServer) != KErrNone)
       
   141 	{
       
   142 		error = aThread.Create(KIEBgpServerName, // Server name
       
   143 							CIEBgpServer::ThreadFunction, // thread function to call when thread is created and resumed
       
   144 							KDefaultStackSize, // stack size
       
   145 							KIEHeapSizeMin, // min heap size
       
   146 							KIEHeapSizeMax, // max heap size
       
   147 							NULL); // data ptr needed (if)
       
   148 
       
   149 		if(error == KErrNone)
       
   150 		{
       
   151 			// Thread created successfully, resume it
       
   152 			TRequestStatus rendezvousStatus;
       
   153 			
       
   154 			//Keep priority low to enable smooth UI drawing
       
   155 			//aThread.SetPriority(EPriorityNormal);
       
   156 			aThread.SetPriority(EPriorityLess);//EPriorityNormal, EPriorityLess, EPriorityMuchLess, EPriorityNull
       
   157 			aThread.Rendezvous(rendezvousStatus);
       
   158 			aThread.Resume();
       
   159 			User::WaitForRequest(rendezvousStatus);
       
   160 		}
       
   161 		else
       
   162 		{
       
   163 			// error in thread creation
       
   164 			aThread.Close();
       
   165 		}
       
   166 	}
       
   167 	else
       
   168 	{
       
   169 		error = KErrAlreadyExists;
       
   170 	}
       
   171 	DP0_IMAGIC((_L("CIEBgpServer::CreateServerThread --")));
       
   172 	return error;
       
   173 }
       
   174 
       
   175 // EOF