|
1 /* |
|
2 * Copyright (c) 2006 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: |
|
13 * |
|
14 * Description: Socket Interaction Thread implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include <e32math.h> |
|
21 #include "sit.h" |
|
22 #include "sitdeathobserver.h" |
|
23 #include "taskarrivalobserver.h" |
|
24 #include "taskhandlercreator.h" |
|
25 #include "log.h" |
|
26 |
|
27 // Exports |
|
28 |
|
29 EXPORT_C CSit::CSit(MSitDeathListener* aSitDeathListener) |
|
30 : iTaskThread(NULL), iSitDeathListener(aSitDeathListener) |
|
31 { |
|
32 LOG(Log::Printf(_L("CSit::CSit\n"))); |
|
33 } |
|
34 |
|
35 EXPORT_C CSit::~CSit() |
|
36 { |
|
37 LOG(Log::Printf(_L("CSit::~CSit\n"))); |
|
38 delete iSitDeathObserver; |
|
39 |
|
40 if (iTaskThread != NULL) |
|
41 { |
|
42 LOG(Log::Printf(_L("CSit::~CSit - closing task thread\n"))); |
|
43 iTaskThread->Close(); |
|
44 } |
|
45 delete iTaskThread; |
|
46 } |
|
47 |
|
48 EXPORT_C void CSit::StartL() |
|
49 { |
|
50 LOG(Log::Printf(_L("CSit::StartL\n"))); |
|
51 |
|
52 // Only start the thread if it is not already running |
|
53 if (!iTaskThread) |
|
54 { |
|
55 LOG(Log::Printf(_L("CSit::StartL - thread not running, starting it\n"))); |
|
56 StartThreadL(); |
|
57 } |
|
58 } |
|
59 |
|
60 EXPORT_C TThreadId CSit::ThreadId() |
|
61 { |
|
62 if (iTaskThread) |
|
63 { |
|
64 return iTaskThread->Id(); |
|
65 } |
|
66 else |
|
67 { |
|
68 return 0; |
|
69 } |
|
70 } |
|
71 |
|
72 EXPORT_C TBool CSit::EventRequiresSit(TEventType aEventType) |
|
73 { |
|
74 return TaskHandlerCreator::EventRequiresSit(aEventType); |
|
75 } |
|
76 |
|
77 EXPORT_C TEventType CSit::FindTaskRequestEventType(TEventType aCancelEventType) |
|
78 { |
|
79 return TaskHandlerCreator::FindTaskRequestEventType(aCancelEventType); |
|
80 } |
|
81 |
|
82 EXPORT_C TEventType CSit::FindCancelEventType(TEventType aTaskRequestEventType) |
|
83 { |
|
84 return TaskHandlerCreator::FindCancelEventType(aTaskRequestEventType); |
|
85 } |
|
86 |
|
87 EXPORT_C TBool CSit::IsTaskCancellationObservationRequest(TEventType aEventType) |
|
88 { |
|
89 return TaskHandlerCreator::IsTaskCancellationObservationRequest(aEventType); |
|
90 } |
|
91 |
|
92 // Internals |
|
93 |
|
94 |
|
95 void CSit::StartThreadL() |
|
96 { |
|
97 LOG(Log::Printf(_L("CSit::StartThreadL\n"))); |
|
98 TName threadName(KSitName); |
|
99 |
|
100 iTaskThread = new (ELeave) RThread; |
|
101 |
|
102 TInt ret = iTaskThread->Create(threadName, |
|
103 ThreadFunction, |
|
104 KDefaultStackSize, |
|
105 KMinHeapSize, |
|
106 KSitMaxHeapSize, |
|
107 this, |
|
108 EOwnerProcess); |
|
109 |
|
110 LOG(Log::Printf(_L("CSit::StartThreadL - iTaskThread->Create returned %d\n"), ret)); |
|
111 User::LeaveIfError(ret); |
|
112 |
|
113 iSitDeathObserver = new (ELeave) CSitDeathObserver(iTaskThread->Id(), this); |
|
114 iSitDeathObserver->StartObservingL(); |
|
115 |
|
116 iTaskThread->Resume(); |
|
117 } |
|
118 |
|
119 TInt CSit::ThreadFunction(TAny* aParameters) |
|
120 { |
|
121 LOG(Log::Printf(_L("CSit::ThreadFunction - begin\n"))); |
|
122 __UHEAP_MARK; |
|
123 |
|
124 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
125 |
|
126 TInt ret = KErrNoMemory; |
|
127 |
|
128 if (cleanup) |
|
129 { |
|
130 CSit* const sit = STATIC_CAST(CSit*, aParameters); |
|
131 TRAP(ret, sit->StartWorkingL()); |
|
132 delete cleanup; |
|
133 } |
|
134 |
|
135 __UHEAP_MARKEND; |
|
136 |
|
137 LOG(Log::Printf(_L("CSit::ThreadFunction - end\n"))); |
|
138 return ret; |
|
139 } |
|
140 |
|
141 void CSit::StartWorkingL() |
|
142 { |
|
143 LOG(Log::Printf(_L("CSit::StartWorkingL - begin\n"))); |
|
144 // Create and install the active scheduler we need |
|
145 CActiveScheduler* scheduler = new(ELeave) CActiveScheduler; |
|
146 CleanupStack::PushL(scheduler); |
|
147 CActiveScheduler::Install(scheduler); |
|
148 |
|
149 // Create the task arrival observer active object |
|
150 CTaskArrivalObserver* taskArrivalObserver = CTaskArrivalObserver::NewL(); |
|
151 CleanupStack::PushL(taskArrivalObserver); |
|
152 |
|
153 // Start observing task arrivals |
|
154 taskArrivalObserver->Start(); |
|
155 |
|
156 // Start running active objects |
|
157 CActiveScheduler::Start(); |
|
158 |
|
159 // Cleanup the task handler and scheduler |
|
160 CleanupStack::PopAndDestroy(2); // taskArrivalObserver, scheduler |
|
161 |
|
162 LOG(Log::Printf(_L("CSit::StartWorkingL - end\n"))); |
|
163 } |
|
164 |
|
165 void CSit::SitDied() |
|
166 { |
|
167 LOG(Log::Printf(_L("CSit::SitDied\n"))); |
|
168 |
|
169 if (iTaskThread != NULL) |
|
170 { |
|
171 iTaskThread->Close(); |
|
172 delete iTaskThread; |
|
173 iTaskThread = NULL; |
|
174 |
|
175 iSitDeathListener->SitDied(); |
|
176 |
|
177 // We must delete the observer object as well. |
|
178 // With this, the StartThreadL method works |
|
179 // correctly as it creates a new observer. |
|
180 delete iSitDeathObserver; |
|
181 iSitDeathObserver = NULL; |
|
182 } |
|
183 } |
|
184 |
|
185 HBufC16* CSit::To16BitL(const TDesC8& aDes) |
|
186 { |
|
187 HBufC16* desCopy; |
|
188 |
|
189 TInt desLength = aDes.Length(); |
|
190 |
|
191 if (desLength > 0) |
|
192 { |
|
193 desCopy = HBufC16::NewL(desLength); |
|
194 desCopy->Des().Copy(aDes); |
|
195 } |
|
196 else |
|
197 { |
|
198 desCopy = HBufC16::NewL(1); |
|
199 } |
|
200 |
|
201 return desCopy; |
|
202 } |