Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/_smp_example_8cpp_source.html
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 13 Aug 2010 16:47:46 +0100
changeset 14 578be2adaf3e
parent 6 43e37759235e
permissions -rw-r--r--
Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TB9.2 Example Applications: examples/Base/SmpExample/src/SmpExample.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/Base/SmpExample/src/SmpExample.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00004"></a>00004 <span class="comment">// under the terms of &quot;Eclipse Public License v1.0&quot;</span>
<a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00006"></a>00006 <span class="comment">// at the URL &quot;http://www.eclipse.org/legal/epl-v10.html&quot;.</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
<a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 
<a name="l00014"></a>00014 <span class="preprocessor">#include &lt;d32dbms.h&gt;</span>  <span class="comment">//Used for RDbDatabase class. </span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;SmpExample.h&quot;</span>
<a name="l00016"></a>00016 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KDatabase1, <span class="stringliteral">&quot;C:\\DBforSMP1.db&quot;</span>);
<a name="l00017"></a>00017 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KDatabase2, <span class="stringliteral">&quot;C:\\DBforSMP2.db&quot;</span>);
<a name="l00018"></a>00018 
<a name="l00022"></a><a class="code" href="class_c_smp_example.html#a4f902855860346e1529c8aad989100f0">00022</a> <a class="code" href="class_c_smp_example.html#a4f902855860346e1529c8aad989100f0">CSmpExample::~CSmpExample</a>()
<a name="l00023"></a>00023     {
<a name="l00024"></a>00024     iReadThread.Close(); <span class="comment">//Reader thread closed. </span>
<a name="l00025"></a>00025     iWriteThread1.Close(); <span class="comment">//Writer thread closed. </span>
<a name="l00026"></a>00026     iWriteThread2.Close(); <span class="comment">//Writer thread closed.</span>
<a name="l00027"></a>00027     <span class="keyword">delete</span> iConsole;
<a name="l00028"></a>00028     }
<a name="l00029"></a>00029 
<a name="l00033"></a><a class="code" href="class_c_smp_example.html#aaf2dab332a98167a1dde6309732a2460">00033</a> <a class="code" href="class_c_smp_example.html">CSmpExample</a>* <a class="code" href="class_c_smp_example.html#aaf2dab332a98167a1dde6309732a2460">CSmpExample::NewL</a>()
<a name="l00034"></a>00034     {
<a name="l00035"></a>00035     <a class="code" href="class_c_smp_example.html">CSmpExample</a>* <span class="keyword">self</span> = <span class="keyword">new</span>(ELeave)<a class="code" href="class_c_smp_example.html">CSmpExample</a>();
<a name="l00036"></a>00036     CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00037"></a>00037     <span class="keyword">self</span>-&gt;ConstructL();
<a name="l00038"></a>00038     CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00039"></a>00039     <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00040"></a>00040     }
<a name="l00041"></a>00041 
<a name="l00045"></a><a class="code" href="class_c_smp_example.html#aa9bbdb0f3c1cb00236cf6eedc2fe0840">00045</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#aa9bbdb0f3c1cb00236cf6eedc2fe0840">CSmpExample::ConstructL</a>()
<a name="l00046"></a>00046     { 
<a name="l00047"></a>00047     <a class="code" href="class_c_smp_example.html#a6ed7d09453967159b3cb9cb8c271aeab">CreateDatabaseL</a>(KDatabase1);
<a name="l00048"></a>00048     <a class="code" href="class_c_smp_example.html#a6ed7d09453967159b3cb9cb8c271aeab">CreateDatabaseL</a>(KDatabase2);
<a name="l00049"></a>00049     
<a name="l00050"></a>00050     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextConsoleTitle, <span class="stringliteral">&quot;SmpExample&quot;</span>);
<a name="l00051"></a>00051     iConsole = <a class="code" href="class_c_smp_example.html#aaf2dab332a98167a1dde6309732a2460">Console::NewL</a>(KTextConsoleTitle, TSize(KConsFullScreen, KConsFullScreen));  
<a name="l00052"></a>00052 
<a name="l00053"></a>00053     <span class="comment">//Creates a reader thread to read the databases.</span>
<a name="l00054"></a>00054     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KReader, <span class="stringliteral">&quot;ReaderThread&quot;</span>);
<a name="l00055"></a>00055     User::LeaveIfError(iReadThread.Create(KReader, <a class="code" href="class_c_smp_example.html#ac56a561b32603f3d9fccb186b90ee28b">ReadThreadFuncL</a>, KDefaultStackSize, KMinHeapSize, 256*KMinHeapSize, <span class="keyword">this</span>, EOwnerProcess));   
<a name="l00056"></a>00056 
<a name="l00057"></a>00057     <span class="comment">//Creates a writer thread to write to the DBforSMP1.db database. </span>
<a name="l00058"></a>00058     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KWriter1, <span class="stringliteral">&quot;WriterThread1&quot;</span>);
<a name="l00059"></a>00059     User::LeaveIfError(iWriteThread1.Create(KWriter1, <a class="code" href="class_c_smp_example.html#a3a99e9be00e57c1d8489de6f62d00a2b">WriteThread1FuncL</a>, KDefaultStackSize, KMinHeapSize, 256*KMinHeapSize, <span class="keyword">this</span>, EOwnerProcess));    
<a name="l00060"></a>00060     
<a name="l00061"></a>00061     <span class="comment">//Creates a writer thread to write to the DBforSMP2.db database. </span>
<a name="l00062"></a>00062     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KWriter2, <span class="stringliteral">&quot;WriterThread2&quot;</span>);
<a name="l00063"></a>00063     User::LeaveIfError(iWriteThread2.Create(KWriter2, <a class="code" href="class_c_smp_example.html#a41ccf5be445f2ad57cee4fa93b8830c6">WriteThread2FuncL</a>, KDefaultStackSize, KMinHeapSize, 256*KMinHeapSize, <span class="keyword">this</span>, EOwnerProcess));           
<a name="l00064"></a>00064 
<a name="l00065"></a>00065 
<a name="l00066"></a>00066     <span class="comment">//Sets priority to the threads. </span>
<a name="l00067"></a>00067     iWriteThread1.SetPriority(EPriorityMuchLess);
<a name="l00068"></a>00068     iWriteThread2.SetPriority(EPriorityMore);
<a name="l00069"></a>00069     iReadThread.SetPriority(EPriorityNormal);
<a name="l00070"></a>00070     }
<a name="l00071"></a>00071 
<a name="l00075"></a><a class="code" href="class_c_smp_example.html#afdb87a66a6b0a41cbea465aa2495aa1c">00075</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#afdb87a66a6b0a41cbea465aa2495aa1c">CSmpExample::StartThreads</a>()
<a name="l00076"></a>00076     {   
<a name="l00077"></a>00077     TRequestStatus writerThread1Status; 
<a name="l00078"></a>00078     TRequestStatus readerThreadStatus; 
<a name="l00079"></a>00079     TRequestStatus writerThread2Status; 
<a name="l00080"></a>00080     
<a name="l00081"></a>00081     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPressKey, <span class="stringliteral">&quot;\nPress any key to start writing to and reading from the database\n&quot;</span>);
<a name="l00082"></a>00082     iConsole-&gt;Printf(KTextPressKey);
<a name="l00083"></a>00083     iConsole-&gt;Getch();
<a name="l00084"></a>00084   
<a name="l00085"></a>00085     <span class="comment">//Requests a notification for the ReaderThread to terminate. </span>
<a name="l00086"></a>00086     iReadThread.Logon(readerThreadStatus);
<a name="l00087"></a>00087     iReadThread.Resume();
<a name="l00088"></a>00088     
<a name="l00089"></a>00089     <span class="comment">//Requests a notification for the WriterThread1 to terminate. </span>
<a name="l00090"></a>00090     iWriteThread1.Logon(writerThread1Status);   
<a name="l00091"></a>00091     iWriteThread1.Resume();
<a name="l00092"></a>00092     
<a name="l00093"></a>00093     <span class="comment">//Requests a notification for the WriterThread2 to terminate. </span>
<a name="l00094"></a>00094     iWriteThread2.Logon(writerThread2Status);   
<a name="l00095"></a>00095     iWriteThread2.Resume();
<a name="l00096"></a>00096     
<a name="l00097"></a>00097     <span class="comment">//Control returns to the main thread when the all the threads terminate. </span>
<a name="l00098"></a>00098     User::WaitForRequest(writerThread1Status);
<a name="l00099"></a>00099     User::WaitForRequest(readerThreadStatus);
<a name="l00100"></a>00100     User::WaitForRequest(writerThread2Status);
<a name="l00101"></a>00101     }
<a name="l00102"></a>00102 
<a name="l00106"></a><a class="code" href="class_c_smp_example.html#a8eaf4cbbf9c4b69d0854140963a22e51">00106</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#a8eaf4cbbf9c4b69d0854140963a22e51">CSmpExample::PrintMessage</a>()
<a name="l00107"></a>00107     {
<a name="l00108"></a>00108     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextWelcome, <span class="stringliteral">&quot;Welcome to the SmpExample.\n&quot;</span>);
<a name="l00109"></a>00109     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel1, <span class="stringliteral">&quot;There are three threads running in the example: WriterThread1 has the lowest priority,\n&quot;</span>);
<a name="l00110"></a>00110     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel2, <span class="stringliteral">&quot;ReaderThread has normal priority and WriterThread2 has maximum priority.\n&quot;</span>);  
<a name="l00111"></a>00111     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel3, <span class="stringliteral">&quot;The two WriterThreads open two different databases and write some integers to them.\n&quot;</span>);
<a name="l00112"></a>00112     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel4, <span class="stringliteral">&quot;ReaderThread reads the two databases and prints the output to the console.\n&quot;</span>);
<a name="l00113"></a>00113     
<a name="l00114"></a>00114     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel5, <span class="stringliteral">&quot;In a unicore environment, WriterThread1 will be scheduled to run last and would not have\n&quot;</span>);
<a name="l00115"></a>00115     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel6, <span class="stringliteral">&quot;written to the database when ReaderThread starts reading the databases.\n&quot;</span>);
<a name="l00116"></a>00116     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextPurposel7, <span class="stringliteral">&quot;But in an SMP environment, both the WriterThreads write to their respective databases simultaneously.\n&quot;</span>);
<a name="l00117"></a>00117     
<a name="l00118"></a>00118     iConsole-&gt;Printf(KTextWelcome);
<a name="l00119"></a>00119     iConsole-&gt;Printf(KTextPurposel1);
<a name="l00120"></a>00120     iConsole-&gt;Printf(KTextPurposel2);
<a name="l00121"></a>00121     iConsole-&gt;Printf(KTextPurposel3);
<a name="l00122"></a>00122     iConsole-&gt;Printf(KTextPurposel4);
<a name="l00123"></a>00123     iConsole-&gt;Printf(KTextPurposel5);
<a name="l00124"></a>00124     iConsole-&gt;Printf(KTextPurposel6);
<a name="l00125"></a>00125     iConsole-&gt;Printf(KTextPurposel7);
<a name="l00126"></a>00126     }
<a name="l00127"></a>00127 
<a name="l00131"></a><a class="code" href="class_c_smp_example.html#a3b589411b99d71c460442290516e04ec">00131</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#a3b589411b99d71c460442290516e04ec">CSmpExample::ReadDatabaseL</a>(<span class="keyword">const</span> TDesC&amp; aDbName, CConsoleBase&amp; console)
<a name="l00132"></a>00132     { 
<a name="l00133"></a>00133     <span class="comment">//Creates a file server session object before any file system manipulation. </span>
<a name="l00134"></a>00134     RFs fsSession;
<a name="l00135"></a>00135     CleanupClosePushL(fsSession);
<a name="l00136"></a>00136     fsSession.Connect();
<a name="l00137"></a>00137     
<a name="l00138"></a>00138     <span class="comment">//Creates Rdbs object</span>
<a name="l00139"></a>00139     RDbs dbs;
<a name="l00140"></a>00140     CleanupClosePushL(dbs); 
<a name="l00141"></a>00141     dbs.Connect();
<a name="l00142"></a>00142     
<a name="l00143"></a>00143     <span class="comment">//Opens the named database using the RDbs object</span>
<a name="l00144"></a>00144     RDbNamedDatabase database;
<a name="l00145"></a>00145     CleanupClosePushL(database);
<a name="l00146"></a>00146     User::LeaveIfError(database.Open(dbs, aDbName)); 
<a name="l00147"></a>00147     
<a name="l00148"></a>00148     <span class="comment">//Locks the database.</span>
<a name="l00149"></a>00149     database.Begin();
<a name="l00150"></a>00150     <span class="comment">//Prepare an SQL query to read one row of numbers from the database. </span>
<a name="l00151"></a>00151     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSQLStatement, <span class="stringliteral">&quot;Select Number1, Number2, Number3  from Numbers order by Number1, Number2, Number3&quot;</span>);
<a name="l00152"></a>00152     
<a name="l00153"></a>00153     <span class="comment">//Creates a view on the database to read it.</span>
<a name="l00154"></a>00154     RDbView view; 
<a name="l00155"></a>00155     CleanupClosePushL(view);
<a name="l00156"></a>00156     
<a name="l00157"></a>00157     User::LeaveIfError(view.Prepare(database, TDbQuery(KSQLStatement, EDbCompareNormal)));
<a name="l00158"></a>00158     User::LeaveIfError(view.EvaluateAll());
<a name="l00159"></a>00159     
<a name="l00160"></a>00160     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextReading, <span class="stringliteral">&quot;Reading the database\t %S.\n&quot;</span>);
<a name="l00161"></a>00161     console.Printf(KTextReading, &amp;aDbName);
<a name="l00162"></a>00162     
<a name="l00163"></a>00163     <span class="comment">//Boolean variable to check whether the database is empty.</span>
<a name="l00164"></a>00164     TBool isDbEmpty= EFalse;
<a name="l00165"></a>00165     <span class="comment">//Iterates through the database till the last row is read. </span>
<a name="l00166"></a>00166     <span class="keywordflow">for</span> (view.FirstL(); view.AtRow(); view.NextL())
<a name="l00167"></a>00167         {       
<a name="l00168"></a>00168         view.GetL();
<a name="l00169"></a>00169         TInt number1 = view.ColInt(1);
<a name="l00170"></a>00170         TInt number2 = view.ColInt(2);
<a name="l00171"></a>00171         TInt number3 = view.ColInt(3);
<a name="l00172"></a>00172         <span class="comment">//Prepare a row formatter to print numbers in the console. </span>
<a name="l00173"></a>00173         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KRowFormatter, <span class="stringliteral">&quot;Reading  %d \t%d\t%d\n&quot;</span>);  
<a name="l00174"></a>00174         <span class="comment">//Reads the integers from the view and display them in the console.</span>
<a name="l00175"></a>00175         console.Printf(KRowFormatter, number1, number2, number3);
<a name="l00176"></a>00176         isDbEmpty= ETrue;
<a name="l00177"></a>00177         }
<a name="l00178"></a>00178     
<a name="l00179"></a>00179     <span class="keywordflow">if</span>(isDbEmpty== EFalse)
<a name="l00180"></a>00180         {
<a name="l00181"></a>00181         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextDbEmpty, <span class="stringliteral">&quot;Database is empty.\n\n&quot;</span>);
<a name="l00182"></a>00182         console.Printf(KTextDbEmpty);
<a name="l00183"></a>00183         }
<a name="l00184"></a>00184     <span class="keywordflow">else</span>
<a name="l00185"></a>00185         {
<a name="l00186"></a>00186         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextAllRead, <span class="stringliteral">&quot;All the numbers in the database have been read.\n\n&quot;</span>);
<a name="l00187"></a>00187         console.Printf(KTextAllRead);
<a name="l00188"></a>00188         }            
<a name="l00189"></a>00189     
<a name="l00190"></a>00190     CleanupStack::PopAndDestroy(&amp;view); 
<a name="l00191"></a>00191     CleanupStack::PopAndDestroy(&amp;database);  
<a name="l00192"></a>00192     CleanupStack::PopAndDestroy(&amp;dbs);    
<a name="l00193"></a>00193     CleanupStack::PopAndDestroy(&amp;fsSession);
<a name="l00194"></a>00194     }
<a name="l00195"></a>00195 
<a name="l00199"></a><a class="code" href="class_c_smp_example.html#a70e2ac4866c0bea841d38203ae2f6194">00199</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#a70e2ac4866c0bea841d38203ae2f6194">CSmpExample::ReadBothDatabasesL</a>()
<a name="l00200"></a>00200     {
<a name="l00201"></a>00201     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTextConsoleTitle, <span class="stringliteral">&quot;ReaderThread&quot;</span>);
<a name="l00202"></a>00202     CConsoleBase* console= <a class="code" href="class_c_smp_example.html#aaf2dab332a98167a1dde6309732a2460">Console::NewL</a>(KTextConsoleTitle, TSize(KConsFullScreen, KConsFullScreen)); 
<a name="l00203"></a>00203     CleanupStack::PushL(console);
<a name="l00204"></a>00204 
<a name="l00205"></a>00205     <span class="comment">//Reads DBforSMP1.db and prints the output to the console. </span>
<a name="l00206"></a>00206     <a class="code" href="class_c_smp_example.html#a3b589411b99d71c460442290516e04ec">ReadDatabaseL</a>(KDatabase1, *console);
<a name="l00207"></a>00207     <span class="comment">//Reads DBforSMP2.db and prints the output to the console.</span>
<a name="l00208"></a>00208     <a class="code" href="class_c_smp_example.html#a3b589411b99d71c460442290516e04ec">ReadDatabaseL</a>(KDatabase2, *console);
<a name="l00209"></a>00209     
<a name="l00210"></a>00210     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KExit, <span class="stringliteral">&quot;Press any key to exit\n&quot;</span>);
<a name="l00211"></a>00211     console-&gt;Printf(KExit);
<a name="l00212"></a>00212     console-&gt;Getch();
<a name="l00213"></a>00213     CleanupStack::PopAndDestroy(console);    
<a name="l00214"></a>00214     }
<a name="l00215"></a>00215 
<a name="l00220"></a><a class="code" href="class_c_smp_example.html#ac56a561b32603f3d9fccb186b90ee28b">00220</a> TInt <a class="code" href="class_c_smp_example.html#ac56a561b32603f3d9fccb186b90ee28b">CSmpExample::ReadThreadFuncL</a>(TAny* <span class="comment">/*aPtr*/</span>)
<a name="l00221"></a>00221     {
<a name="l00222"></a>00222     __UHEAP_MARK;
<a name="l00223"></a>00223     <span class="comment">//Creates cleanup stack.</span>
<a name="l00224"></a>00224     CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00225"></a>00225     <span class="keywordflow">if</span>(!cleanup)
<a name="l00226"></a>00226         {
<a name="l00227"></a>00227         <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00228"></a>00228         }
<a name="l00229"></a>00229      
<a name="l00230"></a>00230     TRAPD(error, <a class="code" href="class_c_smp_example.html#a70e2ac4866c0bea841d38203ae2f6194">ReadBothDatabasesL</a>()); 
<a name="l00231"></a>00231     <span class="keywordflow">if</span>(error != KErrNone)
<a name="l00232"></a>00232         {
<a name="l00233"></a>00233         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic, <span class="stringliteral">&quot;DB Read Failed&quot;</span>);  
<a name="l00234"></a>00234         User::Panic(KUserPanic, error);
<a name="l00235"></a>00235         }
<a name="l00236"></a>00236     <span class="keyword">delete</span> cleanup;
<a name="l00237"></a>00237     __UHEAP_MARKEND;
<a name="l00238"></a>00238     <span class="keywordflow">return</span> KErrNone;
<a name="l00239"></a>00239     }
<a name="l00240"></a>00240 
<a name="l00245"></a><a class="code" href="class_c_smp_example.html#a26abdb02599626bb596e73e90a145037">00245</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#a26abdb02599626bb596e73e90a145037">CSmpExample::WriteDbFuncL</a>(<span class="keyword">const</span> TDesC&amp; aDbName, TInt aNum)
<a name="l00246"></a>00246     {
<a name="l00247"></a>00247     <span class="comment">//Creates a file server session object before any file system manipulation. </span>
<a name="l00248"></a>00248     RFs fsSession;
<a name="l00249"></a>00249     CleanupClosePushL(fsSession);
<a name="l00250"></a>00250     fsSession.Connect();
<a name="l00251"></a>00251 
<a name="l00252"></a>00252 
<a name="l00253"></a>00253     <span class="comment">//Creates Rdbs object   </span>
<a name="l00254"></a>00254     RDbs dbs;
<a name="l00255"></a>00255     CleanupClosePushL(dbs);
<a name="l00256"></a>00256     dbs.Connect();
<a name="l00257"></a>00257     
<a name="l00258"></a>00258     <span class="comment">//Opens the database using the RDbs object</span>
<a name="l00259"></a>00259     RDbNamedDatabase database;
<a name="l00260"></a>00260     CleanupClosePushL(database);
<a name="l00261"></a>00261     User::LeaveIfError(database.Open(dbs, aDbName)); 
<a name="l00262"></a>00262     
<a name="l00263"></a>00263     <span class="comment">//Locks the database.</span>
<a name="l00264"></a>00264     database.Begin();
<a name="l00265"></a>00265     
<a name="l00266"></a>00266     <span class="comment">//Creates a view on the database. </span>
<a name="l00267"></a>00267     RDbView view;
<a name="l00268"></a>00268     CleanupClosePushL(view);
<a name="l00269"></a>00269     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSQLStatement, <span class="stringliteral">&quot;Select Number1, Number2, Number3 from Numbers order by Number1, Number2, Number3&quot;</span>);
<a name="l00270"></a>00270     User::LeaveIfError(view.Prepare(database, TDbQuery(KSQLStatement, EDbCompareNormal)));
<a name="l00271"></a>00271     User::LeaveIfError(view.EvaluateAll());
<a name="l00272"></a>00272 
<a name="l00273"></a>00273     <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0; i&lt;5; i++)
<a name="l00274"></a>00274         {
<a name="l00275"></a>00275         <span class="comment">//Inserts a new row at the end of the database. </span>
<a name="l00276"></a>00276         view.InsertL();
<a name="l00277"></a>00277         <span class="comment">//Fills three columns with numbers.</span>
<a name="l00278"></a>00278         view.SetColL(1, aNum++);
<a name="l00279"></a>00279         view.SetColL(2, aNum++);
<a name="l00280"></a>00280         view.SetColL(3, aNum++);
<a name="l00281"></a>00281         view.PutL();
<a name="l00282"></a>00282         }
<a name="l00283"></a>00283 
<a name="l00284"></a>00284     CleanupStack::PopAndDestroy(&amp;view);       
<a name="l00285"></a>00285     <span class="comment">//Commits the database and unlocks it. </span>
<a name="l00286"></a>00286     database.Commit();
<a name="l00287"></a>00287     CleanupStack::PopAndDestroy(&amp;database);  
<a name="l00288"></a>00288     CleanupStack::PopAndDestroy(&amp;dbs);   
<a name="l00289"></a>00289     CleanupStack::PopAndDestroy(&amp;fsSession);
<a name="l00290"></a>00290     }
<a name="l00291"></a>00291 
<a name="l00292"></a>00292 
<a name="l00297"></a><a class="code" href="class_c_smp_example.html#a41ccf5be445f2ad57cee4fa93b8830c6">00297</a> TInt <a class="code" href="class_c_smp_example.html#a41ccf5be445f2ad57cee4fa93b8830c6">CSmpExample::WriteThread2FuncL</a>(TAny* <span class="comment">/*aPtr*/</span>)
<a name="l00298"></a>00298     {
<a name="l00299"></a>00299     __UHEAP_MARK;
<a name="l00300"></a>00300     <span class="comment">//Creates cleanup stack.</span>
<a name="l00301"></a>00301     CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00302"></a>00302     <span class="keywordflow">if</span>(!cleanup)
<a name="l00303"></a>00303         {
<a name="l00304"></a>00304         <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00305"></a>00305         }
<a name="l00306"></a>00306     
<a name="l00307"></a>00307     <span class="comment">//First number to be written to the database. </span>
<a name="l00308"></a>00308     <span class="keyword">const</span> <span class="keywordtype">int</span> KFirstNumforDb2 = 500; 
<a name="l00309"></a>00309     TRAPD(err, <a class="code" href="class_c_smp_example.html#a26abdb02599626bb596e73e90a145037">WriteDbFuncL</a>(KDatabase2, KFirstNumforDb2));
<a name="l00310"></a>00310     <span class="keywordflow">if</span>(err != KErrNone)
<a name="l00311"></a>00311         {
<a name="l00312"></a>00312         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic, <span class="stringliteral">&quot;DB Write Failed&quot;</span>);  
<a name="l00313"></a>00313         User::Panic(KUserPanic, err);
<a name="l00314"></a>00314         }
<a name="l00315"></a>00315     <span class="keyword">delete</span> cleanup;
<a name="l00316"></a>00316     __UHEAP_MARKEND;
<a name="l00317"></a>00317     <span class="keywordflow">return</span> KErrNone;
<a name="l00318"></a>00318     }
<a name="l00319"></a>00319 
<a name="l00324"></a><a class="code" href="class_c_smp_example.html#a3a99e9be00e57c1d8489de6f62d00a2b">00324</a> TInt <a class="code" href="class_c_smp_example.html#a3a99e9be00e57c1d8489de6f62d00a2b">CSmpExample::WriteThread1FuncL</a>(TAny* <span class="comment">/*aPtr*/</span>)
<a name="l00325"></a>00325     {
<a name="l00326"></a>00326     __UHEAP_MARK;
<a name="l00327"></a>00327     <span class="comment">//Creates cleanup stack.</span>
<a name="l00328"></a>00328     CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00329"></a>00329     <span class="keywordflow">if</span>(!cleanup)
<a name="l00330"></a>00330         {
<a name="l00331"></a>00331         <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00332"></a>00332         }
<a name="l00333"></a>00333 
<a name="l00334"></a>00334     <span class="comment">//First number to be written to the database. </span>
<a name="l00335"></a>00335     <span class="keyword">const</span> <span class="keywordtype">int</span> KFirstNumforDb1 = 0; 
<a name="l00336"></a>00336     TRAPD(err, <a class="code" href="class_c_smp_example.html#a26abdb02599626bb596e73e90a145037">WriteDbFuncL</a>(KDatabase1, KFirstNumforDb1));
<a name="l00337"></a>00337     <span class="keywordflow">if</span>(err != KErrNone)
<a name="l00338"></a>00338         {
<a name="l00339"></a>00339         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic, <span class="stringliteral">&quot;DB Write Failed&quot;</span>);  
<a name="l00340"></a>00340         User::Panic(KUserPanic, err);
<a name="l00341"></a>00341         }
<a name="l00342"></a>00342     <span class="keyword">delete</span> cleanup;
<a name="l00343"></a>00343     __UHEAP_MARKEND;
<a name="l00344"></a>00344     <span class="keywordflow">return</span> KErrNone;
<a name="l00345"></a>00345     }
<a name="l00346"></a>00346 
<a name="l00351"></a><a class="code" href="class_c_smp_example.html#a6ed7d09453967159b3cb9cb8c271aeab">00351</a> <span class="keywordtype">void</span> <a class="code" href="class_c_smp_example.html#a6ed7d09453967159b3cb9cb8c271aeab">CSmpExample::CreateDatabaseL</a>(<span class="keyword">const</span> TDesC&amp; aDbName)
<a name="l00352"></a>00352     {
<a name="l00353"></a>00353     <span class="comment">//Creates a file server session object before any file system manipulation. </span>
<a name="l00354"></a>00354     RFs fsSession;
<a name="l00355"></a>00355     CleanupClosePushL(fsSession);
<a name="l00356"></a>00356     fsSession.Connect();
<a name="l00357"></a>00357 
<a name="l00358"></a>00358     <span class="comment">//Creates Rdbs object</span>
<a name="l00359"></a>00359     RDbs dbs;
<a name="l00360"></a>00360     CleanupClosePushL(dbs); 
<a name="l00361"></a>00361     dbs.Connect();
<a name="l00362"></a>00362     
<a name="l00363"></a>00363     <span class="comment">//Creates a new database with the name specified in the parameter aDbName, </span>
<a name="l00364"></a>00364     <span class="comment">//if database with same name is present, it will be replaced.    </span>
<a name="l00365"></a>00365     RDbNamedDatabase database;
<a name="l00366"></a>00366     CleanupClosePushL(database);  
<a name="l00367"></a>00367     User::LeaveIfError(database.Replace(fsSession, aDbName)); 
<a name="l00368"></a>00368     database.Close(); 
<a name="l00369"></a>00369     
<a name="l00370"></a>00370     <span class="comment">//Opens the database using the RDbs object</span>
<a name="l00371"></a>00371     User::LeaveIfError(database.Open(dbs, aDbName));       
<a name="l00372"></a>00372     <span class="comment">//Creates a table definition.</span>
<a name="l00373"></a>00373     CDbColSet* columns=CDbColSet::NewLC();
<a name="l00374"></a>00374 
<a name="l00375"></a>00375     <span class="comment">//Adds three columns each containing Int32 values. </span>
<a name="l00376"></a>00376     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KCol1, <span class="stringliteral">&quot;Number1&quot;</span>);
<a name="l00377"></a>00377     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KCol2, <span class="stringliteral">&quot;Number2&quot;</span>);
<a name="l00378"></a>00378     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KCol3, <span class="stringliteral">&quot;Number3&quot;</span>);   
<a name="l00379"></a>00379     columns-&gt;AddL(TDbCol(KCol1, EDbColInt32));
<a name="l00380"></a>00380     columns-&gt;AddL(TDbCol(KCol2, EDbColInt32));
<a name="l00381"></a>00381     columns-&gt;AddL(TDbCol(KCol3, EDbColInt32));
<a name="l00382"></a>00382  
<a name="l00383"></a>00383 
<a name="l00384"></a>00384      <span class="comment">//Creates the table, table name is &quot;Numbers&quot; and add the columns to it.</span>
<a name="l00385"></a>00385     <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTable, <span class="stringliteral">&quot;Numbers&quot;</span>);
<a name="l00386"></a>00386     User::LeaveIfError(database.CreateTable(KTable, *columns));
<a name="l00387"></a>00387     
<a name="l00388"></a>00388     CDbKey* key=CDbKey::NewLC();
<a name="l00389"></a>00389 
<a name="l00390"></a>00390      <span class="comment">//Add the key columns.</span>
<a name="l00391"></a>00391      TDbKeyCol number1(KCol1);
<a name="l00392"></a>00392      key-&gt;AddL(number1);
<a name="l00393"></a>00393      TDbKeyCol number2(KCol2);
<a name="l00394"></a>00394      key-&gt;AddL(number2);
<a name="l00395"></a>00395      TDbKeyCol number3(KCol3);
<a name="l00396"></a>00396      key-&gt;AddL(number3);
<a name="l00397"></a>00397     User::LeaveIfError(database.CreateIndex(KTable, KTable, *key));
<a name="l00398"></a>00398     
<a name="l00399"></a>00399     <span class="comment">//Cleans up the column set.  </span>
<a name="l00400"></a>00400     CleanupStack::PopAndDestroy(key);
<a name="l00401"></a>00401     CleanupStack::PopAndDestroy(columns);  
<a name="l00402"></a>00402     CleanupStack::PopAndDestroy(&amp;database);  
<a name="l00403"></a>00403     CleanupStack::PopAndDestroy(&amp;dbs);   
<a name="l00404"></a>00404     CleanupStack::PopAndDestroy(&amp;fsSession);
<a name="l00405"></a>00405     }
<a name="l00406"></a>00406 
<a name="l00411"></a>00411 <span class="keyword">static</span> <span class="keywordtype">void</span> MainL()
<a name="l00412"></a>00412     {
<a name="l00413"></a>00413     <a class="code" href="class_c_smp_example.html">CSmpExample</a>* smpExample = <a class="code" href="class_c_smp_example.html#aaf2dab332a98167a1dde6309732a2460">CSmpExample::NewL</a>();
<a name="l00414"></a>00414     CleanupStack::PushL(smpExample);    
<a name="l00415"></a>00415     smpExample-&gt;<a class="code" href="class_c_smp_example.html#a8eaf4cbbf9c4b69d0854140963a22e51">PrintMessage</a>();
<a name="l00416"></a>00416     smpExample-&gt;<a class="code" href="class_c_smp_example.html#afdb87a66a6b0a41cbea465aa2495aa1c">StartThreads</a>();
<a name="l00417"></a>00417     CleanupStack::PopAndDestroy(smpExample);
<a name="l00418"></a>00418     }
<a name="l00419"></a>00419 
<a name="l00423"></a>00423 <span class="keyword">extern</span> TInt E32Main()
<a name="l00424"></a>00424     {
<a name="l00425"></a>00425     <span class="comment">//Creates cleanup stack.</span>
<a name="l00426"></a>00426     __UHEAP_MARK;
<a name="l00427"></a>00427     CTrapCleanup* cleanup = CTrapCleanup::New();
<a name="l00428"></a>00428     <span class="keywordflow">if</span>(!cleanup)
<a name="l00429"></a>00429         {
<a name="l00430"></a>00430         <span class="keywordflow">return</span> KErrNoMemory;
<a name="l00431"></a>00431         }
<a name="l00432"></a>00432     <span class="comment">//Run application code inside a TRAP harness.</span>
<a name="l00433"></a>00433     TRAPD(mainError, MainL());
<a name="l00434"></a>00434     <span class="keywordflow">if</span>(mainError != KErrNone)
<a name="l00435"></a>00435         {
<a name="l00436"></a>00436         <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KUserPanic, <span class="stringliteral">&quot;Main Failed&quot;</span>);  
<a name="l00437"></a>00437         User::Panic(KUserPanic, mainError);
<a name="l00438"></a>00438         }
<a name="l00439"></a>00439     <span class="keyword">delete</span> cleanup;
<a name="l00440"></a>00440     __UHEAP_MARKEND;
<a name="l00441"></a>00441     <span class="keywordflow">return</span> KErrNone;
<a name="l00442"></a>00442     }
<a name="l00443"></a>00443 
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>