kerneltest/e32test/property/t_broadcast.cpp
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "t_property.h"
       
    17 
       
    18 class CPropBroadcastSlave : public CBase
       
    19 	{
       
    20 public:
       
    21 	static TInt LoopThreadEntry(TAny*);
       
    22 
       
    23 	CPropBroadcastSlave(CPropBroadcast* aProg, TUid aCategory, TUint aMasterKey, TUint aSlaveKey);
       
    24 	void Create(TThreadPriority);
       
    25 	void Delete();
       
    26 	void Loop();
       
    27 
       
    28 	CPropBroadcast*	iProg;
       
    29 	TUid			iCategory;
       
    30 	TUint			iMasterKey;
       
    31 	TUint			iSlaveKey;
       
    32 
       
    33 	RProperty				iProp;
       
    34 	RThread					iThread;
       
    35 	TRequestStatus			iStatus;
       
    36 
       
    37 	CPropBroadcastSlave*	iNext;
       
    38 	};
       
    39 
       
    40 
       
    41 
       
    42 CPropBroadcastSlave::CPropBroadcastSlave(CPropBroadcast* aProg, TUid aCategory, TUint aMasterKey, TUint aSlaveKey) : 
       
    43 		iProg(aProg), iCategory(aCategory), iMasterKey(aMasterKey), iSlaveKey(aSlaveKey)
       
    44 	{
       
    45 	}
       
    46 
       
    47 void CPropBroadcastSlave::Create(TThreadPriority aPrio)
       
    48 	{
       
    49 	TInt r = iProp.Define(iCategory, iSlaveKey, RProperty::EInt, KPassPolicy, KPassPolicy);
       
    50 	TF_ERROR_PROG(iProg, r, r == KErrNone);
       
    51 	r = iProp.Attach(iCategory, iSlaveKey);
       
    52 	TF_ERROR_PROG(iProg, r, r == KErrNone);
       
    53 
       
    54 	r = iThread.Create(KNullDesC, LoopThreadEntry, 0x2000, NULL, this);
       
    55 	TF_ERROR_PROG(iProg, r, r == KErrNone);
       
    56 	iThread.Logon(iStatus);
       
    57 	iThread.SetPriority(aPrio);
       
    58 	iThread.Resume();
       
    59 	}
       
    60 
       
    61 void CPropBroadcastSlave::Delete()
       
    62 	{
       
    63 	iThread.Kill(EExitKill);
       
    64 	iThread.Close();
       
    65 	User::WaitForRequest(iStatus);
       
    66 	TF_ERROR_PROG(iProg, iStatus.Int(), iStatus.Int() == EExitKill);
       
    67 	
       
    68 	TInt r = iProp.Delete(iCategory, iSlaveKey);
       
    69 	TF_ERROR_PROG(iProg, r, r == KErrNone);
       
    70 	iProp.Close();
       
    71 
       
    72 	delete this;
       
    73 	}
       
    74 
       
    75 TInt CPropBroadcastSlave::LoopThreadEntry(TAny* ptr)
       
    76 	{
       
    77 	CPropBroadcastSlave* self = (CPropBroadcastSlave*) ptr;
       
    78 	self->Loop();
       
    79 	return KErrNone;
       
    80 	}
       
    81 
       
    82 void CPropBroadcastSlave::Loop()
       
    83 	{
       
    84 	RProperty mProp;
       
    85 	TInt r = mProp.Attach(iCategory, iMasterKey, EOwnerThread);
       
    86 	TF_ERROR_PROG(iProg, r, r == KErrNone);
       
    87 	
       
    88 	RProperty sProp;
       
    89 	r = sProp.Attach(iCategory, iSlaveKey, EOwnerThread);
       
    90 	TF_ERROR_PROG(iProg, r, r == KErrNone);
       
    91 
       
    92 	TInt value = 1;
       
    93 	for(;;)
       
    94 		{
       
    95 		TRequestStatus status;
       
    96 		mProp.Subscribe(status);
       
    97 
       
    98 		r = sProp.Set(value);
       
    99 		TF_ERROR_PROG(iProg, r, r == KErrNone);
       
   100 
       
   101 		User::WaitForRequest(status);
       
   102 
       
   103 		r = mProp.Get(value);
       
   104 		TF_ERROR_PROG(iProg, r, r == KErrNone);
       
   105 		}
       
   106 	}
       
   107 
       
   108 _LIT(KPropBroadcastName, "RProperty Broadcast");
       
   109  
       
   110 CPropBroadcast::CPropBroadcast(TUid aCategory, TUint aMasterKey, 
       
   111 								TUint aSlaveKeySlot, TUint aSlaveCount, TUint aFirstHighPriority) : 
       
   112 	  CTestProgram(KPropBroadcastName), iCategory(aCategory), iMasterKey(aMasterKey), 
       
   113 		  iSlaveKeySlot(aSlaveKeySlot), iSlaveCount(aSlaveCount), iFirstHighPriority(aFirstHighPriority)
       
   114 	{
       
   115 	}
       
   116 
       
   117 
       
   118 void CPropBroadcast::Run(TUint aCount)
       
   119 	{
       
   120 	RProperty mProp;
       
   121 	TInt r = mProp.Define(iCategory, iMasterKey, RProperty::EInt, KPassPolicy, KPassPolicy);
       
   122 	TF_ERROR(r, r == KErrNone);
       
   123 	r = mProp.Attach(iCategory, iMasterKey);
       
   124 	TF_ERROR(r, r == KErrNone);
       
   125 
       
   126 	for (TUint j = 0; j < iSlaveCount; ++j)
       
   127 		{
       
   128 		CPropBroadcastSlave* slave = new CPropBroadcastSlave(this, iCategory, iMasterKey, iSlaveKeySlot + j);
       
   129 		slave->Create((j < iFirstHighPriority) ? EPriorityLess : EPriorityMore);
       
   130 
       
   131 		slave->iNext = iSlaveList;
       
   132 		iSlaveList = slave;
       
   133 		}
       
   134 
       
   135 	for (TUint i = 1; i < aCount; ++i)
       
   136 		{
       
   137 		CPropBroadcastSlave* slave = iSlaveList;
       
   138 		while (slave)
       
   139 			{
       
   140 			TRequestStatus status;
       
   141 			slave->iProp.Subscribe(status);
       
   142 			TInt value;
       
   143 			r = slave->iProp.Get(value);
       
   144 			TF_ERROR(r, r == KErrNone);
       
   145 			if (value == (TInt) i)
       
   146 				{
       
   147 				slave->iProp.Cancel();
       
   148 				}
       
   149 			User::WaitForRequest(status);
       
   150 			TF_ERROR(status.Int(), (status.Int() == KErrCancel) || (status.Int() == KErrNone));
       
   151 			r = slave->iProp.Get(value);
       
   152 			TF_ERROR(r, r == KErrNone);
       
   153 			TF_ERROR(value, value == (TInt) i);
       
   154 			slave = slave->iNext;
       
   155 			}
       
   156 		mProp.Set(i + 1);
       
   157 		}
       
   158 
       
   159 	for (;;)
       
   160 		{
       
   161 		CPropBroadcastSlave* slave = iSlaveList;
       
   162 		if (slave == NULL) break;
       
   163 		iSlaveList = slave->iNext;
       
   164 		slave->Delete();
       
   165 		}
       
   166 
       
   167 	mProp.Delete(iCategory, iMasterKey);
       
   168 	mProp.Close();
       
   169 	}