Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include "randcliserv.h"
26 #include "randsvrimpl.h"
29 //const TInt KFastTickTimer=1000000; // These are testing numbers!
30 //const TInt KSlowTickTimer=30000000;
31 const TInt KThreshold=1024;
33 const TInt KFastTickTimer=30000000; // These are the real numbers!
34 const TInt KSlowTickTimer=0x7fffffff;
39 // Signal the owning thread that the server has started successfully
40 // This may itself fail
43 RProcess::Rendezvous(KErrNone);
47 EXPORT_C TInt RunRandomServer(TAny* /*aUnused*/)
49 CTrapCleanup* cleanup=CTrapCleanup::New();
55 TInt ret = User::RenameThread(KRandomServerName);
57 __ASSERT_ALWAYS(ret==KErrNone,User::Panic(KRandomServerName,KErrServerTerminated));
59 if (CRandomScheduler::New())
61 ret = CRandomServer::New();
64 // Initialisation complete, now signal the client
67 CRandomScheduler::Start();
72 TBool CRandomScheduler::New(void)
75 rs=new CRandomScheduler;
76 CRandomScheduler::Install(rs);
80 void CRandomScheduler::Error(TInt /*aError*/) const
82 User::Panic(KRandomServerName, 3);
85 CRandomServer::CRandomServer(void) : CServer2(EPriorityLow)
89 CRandomServer::~CRandomServer(void)
91 // This should never happen....but in case it does:
97 TInt CRandomServer::New(void)
100 self=new CRandomServer;
105 TRAPD(ret,self->ConstructL());
111 return self->Start(KRandomServerName);
114 void CRandomServer::ConstructL(void)
116 iPool=new (ELeave) TUint8[KRandomPoolSize];
120 iTicker=CPeriodic::NewL(EPriorityLow);
121 TCallBack callback(Tick,this);
122 iTicker->Start(KFastTickTimer,KFastTickTimer,callback); // **** these figures might need tweaking!
127 TInt CRandomServer::Tick(TAny* aServer)
129 CRandomServer* svr=(CRandomServer*)aServer;
134 if (svr->iQuality>KThreshold)
136 TCallBack callback(Tick,svr);
137 svr->iTicker->Cancel();
138 svr->iTicker->Start(KSlowTickTimer,KSlowTickTimer,callback); // **** these figures might need tweaking!
142 if (svr->iQuality>(KRandomPoolSize<<3))
144 svr->iQuality=(KRandomPoolSize<<3);
149 CSession2* CRandomServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const
151 return CRandomSession::NewL(const_cast<CRandomServer*>(this));
152 //CRandomSession::NewL(CONST_CAST(CRandomServer*,this),Message().Client());
155 TPtrC8 CRandomServer::GetRandom(void)
157 TPtr8 res(&iPool[iPoolOut],iHash->HashSize(),iHash->HashSize());
158 iPoolOut+=iHash->HashSize();
159 if ((iPoolOut+iHash->HashSize())>KRandomPoolSize)
163 return iHash->Hash(res);
166 CMessageDigest* CRandomServer::Hash(void) const
171 void CRandomServer::Stir(void)
175 TPtrC8 r((TUint8*)&rnd,sizeof(TInt));
177 TPtr8 dest(&iPool[iPoolIn],iHash->HashSize());
178 dest.Copy(iHash->Hash(dest));
179 iPoolIn+=iHash->HashSize();
180 if ((iPoolIn+iHash->HashSize())>KRandomPoolSize)
186 CRandomSession* CRandomSession::NewL(CRandomServer* aServer)
188 CRandomSession* self;
189 self=new (ELeave) CRandomSession(aServer);
193 CRandomSession::CRandomSession(CRandomServer* aServer) : CSession2(), iServer(aServer)
197 CRandomSession::~CRandomSession(void)
201 void CRandomSession::ServiceL(const RMessage2& aMessage)
203 switch (aMessage.Function())
207 TInt ret = FillBuffer(aMessage);
208 aMessage.Complete(ret);
212 aMessage.Complete(KErrNotSupported);
217 TInt CRandomSession::FillBuffer(const RMessage2& aMessage)
219 TInt length = aMessage.Int1();
220 if (length < 0 || length > KRandomBlockSize)
222 iServer->iQuality -= length;
223 if (iServer->iQuality<0)
229 if (iServer->iQuality<KThreshold)
231 TCallBack callback(CRandomServer::Tick,iServer);
232 iServer->iTicker->Cancel();
233 iServer->iTicker->Start(KFastTickTimer,KFastTickTimer,callback); // **** these figures might need tweaking!
234 iServer->iFast=ETrue;
237 TBuf8<KRandomBlockSize> buf(0);
240 TInt hashsize=iServer->Hash()->HashSize();
241 for (i=0; i+hashsize < length; i+=hashsize)
243 buf.Append(iServer->GetRandom());
246 TPtrC8 ptr(iServer->GetRandom().Ptr(), length-i);
249 TRAPD(ret, aMessage.WriteL(0, buf));
253 GLDEF_C TInt E32Main(void)
255 return RunRandomServer(NULL);