|
1 // Copyright (c) 2006-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 "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 // Name : stdioserver.cpp |
|
15 // Part of : stdio server |
|
16 // To implement the server |
|
17 // |
|
18 // |
|
19 |
|
20 |
|
21 #include "StdioServer.h" //CStdioServer |
|
22 #include "StdioSession.h" //CStdioSession |
|
23 #include <e32debug.h> //RDebug |
|
24 |
|
25 // Initializing the static variable which is the RFs and RCommServ |
|
26 RFs CStdioServer::iFs; |
|
27 RCommServ CStdioServer::iCs; |
|
28 |
|
29 //------------------------------------------------------------------------------- |
|
30 // Function Name : RunTheServerL() |
|
31 // Description : Static Function called from the TRAP. It Creates the scheduler |
|
32 // installs and signals the starting client process once the |
|
33 // server is up. |
|
34 //------------------------------------------------------------------------------- |
|
35 |
|
36 static void RunTheServerL() |
|
37 { |
|
38 User::LeaveIfError(RThread::RenameMe(KServerName)); |
|
39 |
|
40 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler; |
|
41 CleanupStack::PushL(scheduler); |
|
42 CActiveScheduler::Install(scheduler); |
|
43 |
|
44 CStdioServer::NewL(); |
|
45 |
|
46 RProcess::Rendezvous(KErrNone); |
|
47 |
|
48 CActiveScheduler::Start(); |
|
49 CleanupStack::PopAndDestroy(); |
|
50 } |
|
51 |
|
52 //------------------------------------------------------------------------------- |
|
53 // Function Name : RunServer() |
|
54 // Description : Static function called from the E32Main function.This function |
|
55 // starts the TRAP and calls the static function RunTheServerL() |
|
56 // from the TRAP. |
|
57 //------------------------------------------------------------------------------- |
|
58 |
|
59 static TInt RunServer() |
|
60 { |
|
61 TRAPD(ret, RunTheServerL()); |
|
62 return ret; |
|
63 } |
|
64 |
|
65 |
|
66 //------------------------------------------------------------------------------- |
|
67 // Function Name : E32Main() |
|
68 // Description : Entry point for the server process. |
|
69 // |
|
70 //------------------------------------------------------------------------------- |
|
71 TInt E32Main() |
|
72 { |
|
73 TInt ret = KErrNoMemory; |
|
74 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
75 if (cleanup) |
|
76 { |
|
77 ret = RunServer(); |
|
78 delete cleanup; |
|
79 } |
|
80 return ret; |
|
81 } |
|
82 |
|
83 //------------------------------------------------------------------------------- |
|
84 // Function Name : CStdioServer::NewL() |
|
85 // Description : Static class function to create the server. This function will |
|
86 // be called Static function RunServerL(). This function leaves |
|
87 // the object on the stack and returns the handle to created |
|
88 // CstdioServer(). The server is created as an active object with |
|
89 // EPriorityStandard. |
|
90 //------------------------------------------------------------------------------- |
|
91 |
|
92 CServer2* CStdioServer::NewL() |
|
93 { |
|
94 CStdioServer *tptr = new (ELeave) CStdioServer(EPriorityStandard); |
|
95 CleanupStack::PushL(tptr); |
|
96 tptr->ConstructL(); |
|
97 CleanupStack::Pop(); |
|
98 return tptr; |
|
99 } |
|
100 |
|
101 |
|
102 RFs& CStdioServer::FsSession() |
|
103 { |
|
104 return iFs; |
|
105 } |
|
106 |
|
107 RCommServ& CStdioServer::CsSession() |
|
108 { |
|
109 return iCs; |
|
110 } |
|
111 |
|
112 //------------------------------------------------------------------------------- |
|
113 // Function Name : CStdioServer::CStdioServer() |
|
114 // Description : Default Constructor. Sets the proiority to EPriorityStandard of |
|
115 // the active object by passing it to the base class.Initialize the |
|
116 // Session Count to 0. |
|
117 //------------------------------------------------------------------------------- |
|
118 |
|
119 |
|
120 CStdioServer::CStdioServer(CActive::TPriority aPriority) : CServer2(aPriority, ESharableSessions) |
|
121 { |
|
122 iSessionCount = 0; |
|
123 } |
|
124 |
|
125 //------------------------------------------------------------------------------- |
|
126 // Function Name : CStdioServer::ConstructL() |
|
127 // Description : This function starts the Server and is called from |
|
128 // CStdioServer::NewLC It calls the StartL function of Cserver2 |
|
129 // to add the server to the active scheduler. Also constructs a |
|
130 // new asynchronous timer.then ShutDown::Start is called to request |
|
131 // an event after the SHUTDOWNDELAY which is 2 secs, this is in |
|
132 // case the client session fails to connect. |
|
133 //-------------------------------------------------------------------------------- |
|
134 |
|
135 |
|
136 void CStdioServer::ConstructL() |
|
137 { |
|
138 StartL(KServerName); |
|
139 iFs.Connect(); |
|
140 iCs.Connect(); |
|
141 iShutDown = CShutDown::NewL(); |
|
142 iShutDown->Start(); |
|
143 } |
|
144 |
|
145 |
|
146 //------------------------------------------------------------------------------- |
|
147 // Function Name : CStdioServer::AddSession() |
|
148 // Description : This function will be called by the CStdioSession::CreateL |
|
149 // function when there is a request from the client to create a |
|
150 // new session. The class will keep a count of the number of the |
|
151 // session it has with the client.Anytime there is request for a |
|
152 // new the session the count is increased and CShutDown :: Cancel |
|
153 // is called to cancels the shutdown timer. |
|
154 //-------------------------------------------------------------------------------- |
|
155 |
|
156 |
|
157 void CStdioServer::AddSession() |
|
158 { |
|
159 iSessionCount++; |
|
160 iShutDown->Cancel(); |
|
161 } |
|
162 |
|
163 |
|
164 //------------------------------------------------------------------------------- |
|
165 // Function Name : CStdioServer::RemoveSession() |
|
166 // Description : This function will be called from the destructor on the |
|
167 // CStdioSession when there is request from the client to close |
|
168 // the session or the client process dies suddenly. As the |
|
169 // CStdioServer is designed to be transient,if the count of the |
|
170 // Session reaches zero then ShutDown :: Start is called to |
|
171 // request an event after the SHUTDOWNDELAY which is 2 secs. |
|
172 //-------------------------------------------------------------------------------- |
|
173 |
|
174 void CStdioServer::RemoveSession() |
|
175 { |
|
176 iSessionCount--; |
|
177 if( iSessionCount == 0 ) |
|
178 { |
|
179 iShutDown->Start(); |
|
180 } |
|
181 } |
|
182 |
|
183 //------------------------------------------------------------------------------- |
|
184 // Function Name : CStdioServer::~CStdioServer() |
|
185 // Description : Desctructor. Closes the Session opened with File and Comm |
|
186 // Server |
|
187 //-------------------------------------------------------------------------------- |
|
188 |
|
189 CStdioServer::~CStdioServer() |
|
190 { |
|
191 iFs.Close(); |
|
192 iCs.Close(); |
|
193 } |
|
194 |
|
195 |
|
196 //------------------------------------------------------------------------------- |
|
197 // Function Name : CStdioServer::NewSessionL() |
|
198 // Description : This function is called when ever there is a request from the |
|
199 // client to create a session. This function will create a new |
|
200 // object of CStdioSession type. |
|
201 //-------------------------------------------------------------------------------- |
|
202 |
|
203 |
|
204 CSession2* CStdioServer::NewSessionL(const TVersion& aVersion,const RMessage2& /*aMessage2*/) const |
|
205 { |
|
206 TVersion v(KStdioServMajorVersionNumber, KStdioServMinorVersionNumber, KStdioServBuildVersionNumber); |
|
207 if (!User::QueryVersionSupported(v, aVersion)) |
|
208 { |
|
209 User::Leave(KErrNotSupported); |
|
210 } |
|
211 CSession2 *temp = NULL; |
|
212 TRAPD(ret , temp = CStdioSession::NewL()); |
|
213 User::LeaveIfError(ret); |
|
214 return temp; |
|
215 } |
|
216 |