examples/Base/IPC/secureserver/secureserverccountsubsession.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Contains the implementation of functions described in the CSecureServerSubSession class.  
00028 */
00029 
00030 
00031 
00035 #include "secureclientandserver.h"
00036 #include "secureserver.h"
00037 
00043 CSecureServerSubSession::CSecureServerSubSession(CSecureServerSession* aSession) : iSession(aSession)
00044  {
00045  }
00046 
00051 void CSecureServerSubSession::SetFromStringL(const RMessage2& aMessage)
00052         {
00053         TInt deslen = aMessage.GetDesLength(0);
00054         RBuf buffer;
00055 
00056         buffer.CreateL(deslen);
00057         buffer.CleanupClosePushL();
00058         // The descriptor is encapsulated in the 0th offset of aMessage.
00059         // Hence, read the 0th offset of the message to the buffer.
00060         aMessage.ReadL(0,buffer,0);
00061         // Check for empty string.
00062         if (buffer.Length() == 0)
00063                 {
00064                 User::Leave(ENonNumericString);
00065                 }
00066         TLex16 lexer;
00067         lexer.Assign(buffer);
00068         while (!lexer.Eos())
00069                 {
00070                 TChar thechar;
00071                 thechar = lexer.Peek();
00072                 // Check for non numeric character.
00073                 if (!thechar.IsDigit())
00074                         {
00075                         User::Leave(ENonNumericString);
00076                         }
00077                 lexer.Inc();
00078                 }
00079         lexer.Assign(buffer);
00080         if (lexer.Val(iCount))
00081                 {
00082                 User::Leave(ENonNumericString);
00083                 }
00084         CleanupStack::PopAndDestroy();
00085         }
00086 
00090 void CSecureServerSubSession::Increase()
00091         {
00092         iCount ++;
00093         }
00094 
00100 void CSecureServerSubSession::IncreaseBy(const RMessage2& aMessage)
00101         {
00102         iCount += aMessage.Int0();
00103         }
00104 
00108 void CSecureServerSubSession::Decrease()
00109         {
00110         iCount --;
00111         }
00112 
00118 void CSecureServerSubSession::DecreaseBy(const RMessage2& aMessage)
00119         {
00120         iCount -= aMessage.Int0();
00121         }
00122 
00127 void CSecureServerSubSession::Reset()
00128         {
00129         iCount = 0;
00130         }
00131 
00136 void CSecureServerSubSession::CounterValueL(const RMessage2& aMessage)
00137         {
00138         // Write the value of counter to the 0th offset of aMessage by converting it to a package.
00139         TPckgBuf<TInt> p(iCount);
00140         aMessage.WriteL(0,p);
00141         }
00142 
00155 void CSecureServerSubSession::SaveCounterValueL()
00156         {
00157         RFs fs;
00158         User::LeaveIfError(fs.Connect());
00159         _LIT(KFileName,"counter.dat");
00160         User::LeaveIfError(fs.CreatePrivatePath(RFs::GetSystemDrive()));
00161         User::LeaveIfError(fs.SetSessionToPrivate(RFs::GetSystemDrive()));
00162         TFileName thePath;
00163         User::LeaveIfError(fs.PrivatePath(thePath));
00164         TInt err=fs.MkDir(thePath);
00165         if (err!=KErrAlreadyExists)
00166                 {
00167                 User::LeaveIfError(err);
00168                 }
00169         thePath.Append(KFileName);
00170         RFile file;
00171 
00172         TBuf8<20> counterVal;
00173         counterVal.FillZ();
00174         // Append the counter value to a buffer and write to the file.
00175         counterVal.AppendNum(iCount);
00176         User::LeaveIfError(file.Replace(fs,thePath,EFileWrite));
00177         User::LeaveIfError(file.Write(counterVal));
00178         User::LeaveIfError(file.Flush());
00179 
00180         file.Close();
00181         fs.Close();
00182         }
00183 
00188 void CSecureServerSubSession::SetCounterValueFromFileL()
00189         {
00190         RFs fs;
00191         User::LeaveIfError(fs.Connect());
00192         _LIT(KFileName,"counter.dat");
00193         User::LeaveIfError(fs.CreatePrivatePath(RFs::GetSystemDrive()));
00194         User::LeaveIfError(fs.SetSessionToPrivate(RFs::GetSystemDrive()));
00195         TFileName thePath;
00196         User::LeaveIfError(fs.PrivatePath(thePath));
00197         TInt err=fs.MkDir(thePath);
00198         if (err!=KErrAlreadyExists)
00199                 {
00200                 User::LeaveIfError(err);
00201                 }
00202         thePath.Append(KFileName);
00203         RFile file;
00204 
00205         TBuf8<20> readVal;
00206         User::LeaveIfError(file.Open(fs,thePath,EFileRead));
00207         // Read counter value to the readVal buffer.
00208         User::LeaveIfError(file.Read(readVal));
00209         file.Close();
00210         // Convert value in readVal to TInt.
00211         TLex8 lex(readVal);
00212         lex.Val(iCount);
00213 
00214         fs.Close();
00215         }

Generated by  doxygen 1.6.2