examples/S60CppExamples/ClientServerAsync/doc/index.html

00001 "-//W3C//DTD HTML 4.0 Transitional//EN">
00002 
00003 
00004 
00005 "Content-Type" content="text/html;charset=iso-8859-1">
00006 Asynchronous Client/Server Example
00007 "style.css" rel="stylesheet" type="text/css">
00008 
00009 
00010 "0" width="100%" height="8" bgcolor="#eeeeee">
00011 
"100%" height="1">"2" color="#000000" face="Arial, Helvetica, sans-serif"> 00012 S60 5th Edition SDK
Example Applications Guide
00013 00014
class="tabs"> 00015
00020

Asynchronous Client/Server Example

00021

00022 class="el" href="index.html#Intro_sec">1. About this Example
00023 class="el" href="index.html#Arch_sec">2. Architecture
00024 class="el" href="index.html#Design_sec">3. Design and Implementation

00025


00026

class="anchor" name="Intro_sec"> 00027 1. About this Example

00028 This example demonstrates the architecture of a simple client server application utilising asynchronous calls to the server. The server supplies the current time to its clients.

00029


00030

class="anchor" name="Sub11"> 00031 1.1 APIs demonstrated

00032
    00033
  • CActive
  • CServer2
  • CSession2
  • RMessagePtr2
  • RMessage2
  • RSessionBase
00034

00035


00036

class="anchor" name="Sub12"> 00037 1.2 Prerequisites

00038 This example makes use of the standard Symbian OS application framework, comprising the Application, Document, UI, and View classes. The reader should be aware of this architecture before attempting to understand this example. The example makes use of several other Symbian OS concepts which the reader should be aware of before attempting to understand this example. These are:

00039

    00040
  • Asynchronous programming, in particular the following topics:
      00041
    • Inter-process communication overview.
    • Client/server overview.
    • Using client/server.
    • CActive
    00042
00043

00044

    00045
  • Thread and process management, in particular the following topics:
      00046
    • Thread and process management overview.
    • Semaphores overview.
    • Threads and processes overview.
    • Using semaphores.
    00047
00048

00049


00050

class="anchor" name="Sub13"> 00051 1.3 Running this example

00052 The application initially presents a default time to the user, as shown in the following screenshot.

00053

"center"> 00054 "initial_screen.png" alt="initial_screen.png"> 00055
00056

00057 The Options menu presents the following choices:

00058

"center"> 00059 "options_menu.png" alt="options_menu.png"> 00060
00061

00062

    00063
  • Select Start Clock to start updating the displayed time.
  • Select Exit at any time, to exit the application.
00064

00065 On selecting Start Clock, the display will be updated periodically with the current time. While the clock is running, the Options menu presents the following choices:

00066

"center"> 00067 "options_menu_running.png" alt="options_menu_running.png"> 00068
00069

00070

    00071
  • Select Stop Clock to stop updating the displayed time.
  • Select Exit at any time, to exit the application.
00072

00073


00074

class="anchor" name="Arch_sec"> 00075 2. Architecture

00076 This example exists as a complete application and has the standard Symbian OS application architecture employing the Application, Document, UI, and View classes. The reader should be familiar with this architecture before proceeding.

00077


00078

class="anchor" name="Design_sec"> 00079 3. Design and Implementation

00080 The following component diagram illustrates the split of classes over the client and server components, and their inter-relationships.

00081

"center"> 00082 "component.jpg" alt="component.jpg"> 00083
00084

00085 The client is a standard Symbian OS application, utilising an RSessionBase-derived object to communicate with the server. The server is implemented as an EXE.

00086 In the following description, an overview of this example's design is presented in the Design overview section. This is followed by a description of three use case scenarios, where the user:

00087

    00088
  • Invokes the application - here, the application establishes communication with the server, creating the server if necessary. This is achieved using the RTimeServerSession class, and is described in the Creating a session section.
  • Makes a server request - the application uses the established session to request data from the server. This is described in the Making a server request section.
  • Cancels a server request - the user cancels an outstanding request. This is described in the Canceling a server request section.
00089

00090


00091

00092 3.1 Design overview

00093 The Symbian OS client/server architecture uses four key concepts: server (CServer2), session (CSession2 and RSessionBase), sub-session (RSubSessionBase), and message (RMessage2, and RMessagePtr2).

00094 Servers derive from CServer2, and are responsible for handling any requests from clients to establish connections.

00095 The session represents the channel of communication between client and server. Clients use a derivation of RSessionBase, and servers use a derivation from CSession2.

00096 The following class diagram shows the server-session associations for this example.

00097

00098 classes.png 00099
00100

00101 A client can create multiple sessions with a server. However, it is more resource-efficient to use sub-sessions. This feature is not employed in this example, for simplicity.

00102 The RMessage2 class is the data structure passed between client and server. The client does not manipulate this directly, as it is encapsulated in the client-side interface. Server side sessions read from, and write to, this structure to receive and send data.

00103 To handle asynchronous services, the client requires the use of an active object. In this case, the active object is an instance of the CCSAsyncRequestHandler class. This is responsible for managing the asynchronous nature of this example. It issues requests (through the RTimeServerSession class) for asynchronous services, and receives notification of completion through its RunL method. It uses an observer, MAsyncTimeObserver, to report the completion of requests to interested parties.

00104


00105

00106 3.2 Creating a session

00107 The sequence involved in creating a session is very similar to that described in the Synchronous Client/Server Example, and hence is not described in this document. The only differences in this case are that the session is created by the CCSAsyncRequestHandler class, and the number of asynchronous message slots specified in the RSessionBase::Connect call is no longer zero.

00108


00109

00110 3.3 Making a server request

00111 The RMessage2 and RMessagePtr2 classes are used to transfer information between the client and the server. These classes have several useful methods:

00112

    00113
  • RMessage2 allows client to specify the required server operation.
  • RMessage2 allows four 32-bit words of information to be passed back and forth between the client and server.
  • RMessagePtr2 allows the server to signal when a client's request has completed, using the Complete method.
  • RMessagePtr2 allows the server to panic its client.
00114

00115 An instance of an RMessage2 object is automatically created for the session when the client calls one of the RSessionBase::SendReceive or RSessionBase::Send methods. These methods can take a parameter that is a reference to TIpcArgs object, which can have up to four 32-bit arguments.

00116 The sequence involved in making an asynchronous server request is shown below.

00117

"center"> 00118 "requesttime.jpg" alt="requesttime.jpg"> 00119
00120

00121

    00122
  1. RequestTime is called in response to the user selecting Start Clock. This calls class="el" href="class_c_c_s_async_request_handler.html#481d63e2f4e33b9cb8ed4680a965da25">CCSAsyncRequestHandler::RequestTime.
  2. The class="el" href="class_r_time_server_session.html#0e65b7c45b7af042183f1c5525cfcc1b">RTimeServerSession::RequestTime is called and a TTime reference, and the active object's iStatus flag are passed to it.
  3. A descriptor is now created, representing the supplied TTime object. This descriptor's address is entered into the TIpcArgs object, which is passed to the inherited SendReceive method. SendReceive is an asynchronous method, and returns immediately.
  4. The SetActive method is called, to indicate that the active object has issued a request that is now outstanding. class="el" href="class_c_c_s_async_request_handler.html#98dcd20e4dd117488a6a90dadd4dfac5">CCSAsyncRequestHandler::RunL will be called when the server completes the request.
00123

00124 As a result of the call to SendReceive, the framework calls the class="el" href="class_c_time_server_session.html#75c0a1442736b57144c9ad541d87d370">CTimeServerSession::ServiceL of the associated server-side session, as shown in the following diagram.

00125

"center"> 00126 "servicel.png" alt="servicel.png"> 00127
00128

00129

    00130
  1. class="el" href="class_c_time_server_session.html#75c0a1442736b57144c9ad541d87d370">CTimeServerSession::ServiceL is called by the kernel, and passed an RMessage2 encapsulating the client's request.
  2. RMessage2::Function is called to determine the request type.
  3. If the request is for a time update, RequestTimeL is called.
  4. The received request (RMessage2) is stored. The session now registers its interest in the server's time updates by calling class="el" href="class_c_time_server.html#a8205372b5d379fa4b803e98b0726473">CTimeServer::WaitForTickL.
  5. The server uses an instance of the CHeartbeat class to notify sessions of a time update. New instance is created here if the heartbeat is not already created.
  6. The heartbeat is also started, if it was not already created in previous step.
  7. When the next heartbeat occurs, "el" href="class_c_time_server.html#8bc39d8613772fe04fa5d6a845ede8cd">CTimeServer::Beat is called.
  8. The Beat function calls SendTimeToSessions, which iterates over all established sessions, calling class="el" href="class_c_time_server_session.html#ecff8e289d914777ae0c503db937cf33">CTimeServerSession::SendTimeToClient.
  9. SendTimeToClient is called.
  10. SendTimeToClient calculates the current time and creates a new descriptor representing it (using TPtr8 since it's binary data). RMessage2::WriteL is now called, with the created descriptor as parameter. First parameter of the WriteL is zero identifying the argument index value. The descriptor at the client side was the first argument, so server has to write into the first argument (index 0). If server writes to index 1, in this case, WriteL would return error KErrBadDescriptor. The WriteL function call causes the time to be written to the client's address space.
  11. The server session then signals the client, using the RMessage2::Complete method. This causes the client's CCSAsyncRequestHandler::RunL method to be called, as shown in the following diagram.
00131

00132 Once the server has completed the request, the active object which initiated the request will be signalled, and its RunL method called, as shown in the following diagram.

00133

00134 runl.jpg 00135
00136

00137

    00138
  1. The active scheduler calls the RunL method in response to the server completing the request.
  2. The observer is notified and the time is updated to the screen if the status flag indicates successful completion of the request.
  3. A new request is scheduled, to keep the clock 'running'.
00139

00140


00141

00142 3.4 Canceling a server request

00143 When the user selects Stop Clock, the following sequence is entered.

00144

00145 cancel.jpg 00146
00147

00148

    00149
  1. CancelRequest is called in response to the user selecting Stop Clock from the Options menu.
  2. To handle cancelled requests, active objects should call CActive::Cancel and also implement a DoCancel method. This will be called as a result of calling CActive::Cancel.
  3. DoCancel is called, which instructs the session to cancel the outstanding request by calling RTimeServerSession::CancelRequestTime.
  4. CancelRequestTime() is called.
  5. The synchronous request is now sent to the server, instructing it to cancel the outstanding request by calling SendReceive. The framework will call the corresponding server-side session's ServiceL method as shown in the following sequence.
00150

00151 As a result of the call to SendReceive, the framework calls the CTimeServerSession::ServiceL of the associated server-side session, as shown in the following diagram.

00152

"center"> 00153 "servicel-cancel.png" alt="servicel-cancel.png"> 00154
00155

00156

    00157
  1. ServiceL is called by the framework in response to a request from the client.
  2. The requested method is checked by examining the returned id from RMessage2::Function. In this case, it indicates that the client has requested cancellation of an outstanding request. The servicing of this request should complete in as short a time as possible, since the client waits for completion.
  3. If a previous request is pending, the server completes it by calling Complete on the stored request's associated RMessage2.
  4. The cancellation request is now completed, freeing the client to continue processing.
00158

00159


00160

00161 3.5 Capabilities

00162 The application does not require any capabilities. The program capabilities are defined in both mmp-files as CAPABILITY NONE.
00163 00164 00165 style="x-cell-content-align: top;00166 width: 100%;00167 border-spacing: 0px;00168 border-spacing: 0px;"00169 cellspacing=000170 width=100%>00171 00172 00173 00174 00175 valign=top>00176 00185 00207
00177 padding-right: 10px; 00178 padding-left: 10px; 00179 border-right-style: None; 00180 border-left-style: None; 00181 border-top-style: None; 00182 border-bottom-style: None;" 00183 width=50%> 00184

Nokia 2009

00186 padding-right: 10px; 00187 padding-left: 10px; 00188 border-top-style: None; 00189 border-bottom-style: None; 00190 border-right-style: None;" 00191 width=50%> 00192

00193 align=right> 00194 title="Back to top"> 00195 src="top.gif" 00196 x-maintain-ratio=TRUE 00197 alt="Back to top" 00198 style="border: none; 00199 width: 18px; 00200 height: 15px; 00201 float: none; 00202 border-style: none; 00203 border-style: none;" 00204 width=18 00205 height=15 00206 border=0>

00208 00209 00210

Generated by  doxygen 1.6.2