|
1 // SessionWriter.cpp |
|
2 // |
|
3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "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 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include "SessionWriter.h" |
|
14 #include "cliserv.h" |
|
15 #include "CloggerServer.h" |
|
16 |
|
17 CSession2* CSessionWriterServer::NewSessionL(const TVersion& /*aVersion*/, const RMessage2& /*aMessage*/) const |
|
18 { |
|
19 return new(ELeave) CSessionWriterSession(); |
|
20 } |
|
21 |
|
22 CSessionWriterServer::CSessionWriterServer(CCloggerServer& aCloggerServer) |
|
23 : CServer2(CActive::EPriorityHigh, ESharableSessions), iCloggerServer(aCloggerServer) |
|
24 { |
|
25 } |
|
26 |
|
27 void CSessionWriterServer::ConstructL() |
|
28 { |
|
29 StartL(KSessionLogServer); |
|
30 } |
|
31 |
|
32 CSessionWriterServer& CSessionWriterSession::Server() |
|
33 { |
|
34 return *static_cast<CSessionWriterServer*>(const_cast<CServer2*>(CSession2::Server())); |
|
35 } |
|
36 |
|
37 void CSessionWriterSession::ServiceL(const RMessage2 &aMessage) |
|
38 { |
|
39 TInt res = KErrNone; |
|
40 TBool handled = ETrue; |
|
41 |
|
42 switch (aMessage.Function()) |
|
43 { |
|
44 case ERegisterForLogMessages: |
|
45 { |
|
46 if (iSessionWriter) |
|
47 { |
|
48 // We're already registered |
|
49 res = KErrAlreadyExists; |
|
50 break; |
|
51 } |
|
52 CSessionWriter* writer = Server().CloggerServer().RegisterSessionWithSessionWriter(this); |
|
53 if (!writer) |
|
54 { |
|
55 // Only one session can be registered at once, and some other session is |
|
56 res = KErrInUse; |
|
57 break; |
|
58 } |
|
59 iSessionWriter = writer; |
|
60 RChunk& bufChunk = Server().CloggerServer().GetBufChunk(); |
|
61 aMessage.Complete(bufChunk); |
|
62 return; // As we've handled it ourself |
|
63 } |
|
64 case EGetNextLog: |
|
65 { |
|
66 if (!iSessionWriterMessage.IsNull()) |
|
67 { |
|
68 res = KErrAlreadyExists; |
|
69 break; |
|
70 } |
|
71 if (!iSessionWriter) |
|
72 { |
|
73 PanicClient(aMessage, ENotRegisteredForLogs); |
|
74 return; |
|
75 } |
|
76 iClientSharedChunkBase = (TAny*)aMessage.Ptr1(); |
|
77 iSessionWriter->SetEnabled(ETrue); |
|
78 iSessionWriterMessage = aMessage; |
|
79 if (iWaitingForClient) |
|
80 { |
|
81 iWaitingForClient = EFalse; |
|
82 iSessionWriter->Completed(); |
|
83 } |
|
84 return; // Don't want to complete this yet! |
|
85 } |
|
86 case ECancelGetNextLog: |
|
87 if (iSessionWriter) |
|
88 { |
|
89 iSessionWriter->SetEnabled(EFalse); |
|
90 } |
|
91 if (!iSessionWriterMessage.IsNull()) |
|
92 { |
|
93 iSessionWriterMessage.Complete(KErrCancel); |
|
94 } |
|
95 break; |
|
96 default: |
|
97 handled = EFalse; |
|
98 } |
|
99 |
|
100 if (handled) |
|
101 { |
|
102 aMessage.Complete(res); |
|
103 } |
|
104 else |
|
105 { |
|
106 PanicClient(aMessage, EPanicIllegalFunction); |
|
107 } |
|
108 } |
|
109 |
|
110 void CSessionWriterSession::WriteBuffer(const TDesC8& aBuf) |
|
111 { |
|
112 const TUint8* ptr = aBuf.Ptr(); |
|
113 const TUint8* base = Server().CloggerServer().GetBufChunk().Base(); |
|
114 const TUint8* clientDataPtr = (const TUint8*)((TInt)iClientSharedChunkBase + (TInt)(ptr - base)); |
|
115 TPtrC8 clientPtr(clientDataPtr, aBuf.Length()); |
|
116 TPckg<TPtrC8> ptrBuf(clientPtr); |
|
117 |
|
118 iWaitingForClient = ETrue; |
|
119 TInt err = iSessionWriterMessage.Write(0, ptrBuf); |
|
120 iSessionWriterMessage.Complete(err); |
|
121 } |
|
122 |
|
123 TBool CSessionWriterSession::WaitingForClient() |
|
124 { |
|
125 return iWaitingForClient; |
|
126 } |
|
127 |
|
128 CSessionWriterSession::~CSessionWriterSession() |
|
129 { |
|
130 if (iSessionWriter) |
|
131 { |
|
132 Server().CloggerServer().RegisterSessionWithSessionWriter(NULL); |
|
133 } |
|
134 } |
|
135 |
|
136 ////////////////////////////// |
|
137 |
|
138 CSessionWriter::CSessionWriter(CCloggerServer& aServer) |
|
139 : iServer(aServer) |
|
140 { |
|
141 } |
|
142 |
|
143 void CSessionWriter::WriteBuf(TInt aBuf) |
|
144 { |
|
145 ASSERT(!IsBusyWriting() && iEnabled && iSession); |
|
146 { |
|
147 iBuf = aBuf; |
|
148 const TDesC8& buf = iServer.GetBuf(aBuf); |
|
149 iSession->WriteBuffer(buf); |
|
150 } |
|
151 } |
|
152 |
|
153 TBool CSessionWriter::IsBusyWriting() |
|
154 { |
|
155 return iSession && iSession->WaitingForClient(); |
|
156 } |
|
157 |
|
158 CSessionWriter::~CSessionWriter() |
|
159 { |
|
160 } |
|
161 |
|
162 void CSessionWriter::CloseWriter() |
|
163 { |
|
164 delete this; |
|
165 } |
|
166 |
|
167 void CSessionWriter::SetEnabled(TBool aEnabled) |
|
168 { |
|
169 if (!iSession) |
|
170 { |
|
171 // If there's no session, we can't enable ourselves even if someone asks us to |
|
172 aEnabled = EFalse; |
|
173 } |
|
174 iEnabled = aEnabled; |
|
175 if (IsBusyWriting() && !aEnabled) |
|
176 { |
|
177 // Need to say we've completed this buffer |
|
178 iServer.CompletedWritingBuf(this, iBuf); |
|
179 } |
|
180 } |
|
181 |
|
182 void CSessionWriter::Completed() |
|
183 { |
|
184 iServer.CompletedWritingBuf(this, iBuf); |
|
185 } |