2 @defgroup GRAS_ex GRAS Examples
3 @ingroup GRAS_User_Guide
9 \section GRAS_examples Examples
11 There is for now rather few examples of GRAS, but it's better than
19 The initiatic tour of the tutorial also contains several examples. The
20 most proeminent one is:
22 - \ref GRAS_tut_tour_explicitwait_use
24 \section GRAS_tut_presentation Tutorial
26 We even have a tutorial for the GRAS framework. It details in a
27 hopefully pedagogic order all the points of the API, along with example
28 of use for each of them. Unfortunately, it is not finished yet (the main
29 part missing is the one on how to describe data). Here is the table of
33 - \ref GRAS_tut_intro_what
34 - \ref GRAS_tut_intro_model
36 - \ref GRAS_tut_tour_install
37 - \ref GRAS_tut_tour_setup
38 - \ref GRAS_tut_tour_simpleexchange
39 - \ref GRAS_tut_tour_args
40 - \ref GRAS_tut_tour_callbacks
41 - \ref GRAS_tut_tour_globals
42 - \ref GRAS_tut_tour_logs
43 - \ref GRAS_tut_tour_timers
44 - \ref GRAS_tut_tour_exceptions
45 - \ref GRAS_tut_tour_rpc
46 - \ref GRAS_tut_tour_explicitwait
47 - \ref GRAS_tut_tour_message_recaping
49 \section GRAS_howto_presentation HOWTOsbis
51 The tutorial and the API documentation present the framework little
52 piece by little piece and provide a lot of information on each of them.
53 Quite orthogonally to this, the HOWTOs try to present transversal
54 aspects of the framework to give you some broader point of view on it.
55 How infortunate it is that only one such HOWTO exist for now...
58 - \ref GRAS_howto_design
61 /** @defgroup GRAS_ex Examples */
62 /** @defgroup GRAS_tut Tutorial */
66 #####################################################################
67 /** @addtogroup GRAS_ex
69 There is for now rather few examples of GRAS, but it's better than
77 The initiatic tour of the tutorial also contains several examples. The
78 most proeminent one is:
80 - \ref GRAS_tut_tour_explicitwait_use
82 There is some more examples in the distribution, under the directory
83 <tt>examples/gras</tt>.
86 #####################################################################
87 ######################### EXAMPLES #################################
88 #####################################################################
89 ---------------------------------------------------------------------
90 ------------------------- Ping Pong ---------------------------------
91 ---------------------------------------------------------------------
92 /** @defgroup GRAS_ex_ping Ping-Pong
95 This example implements the very classical ping-pong in GRAS. It
96 involves a client (initiating the ping-pong) and a server (answering to
99 It works the following way:
100 - Both the client and the server register all needed messages
101 - The server registers a callback to the ping message, which sends pong
103 - The client sends the ping message to the server, and waits for the
104 pong message as an answer.
106 This example resides in the <b>examples/gras/ping/ping.c</b> file. Yes, both
107 the code of the client and of the server is placed in the same file. See
108 the \ref GRAS_tut_tour_setup of the tutorial if wondering.
110 \section GRAS_ex_ping_toc Table of contents of the ping example
111 - \ref GRAS_ex_ping_common
112 - \ref GRAS_ex_ping_initial
113 - \ref GRAS_ex_ping_register
114 - \ref GRAS_ex_ping_server
115 - \ref GRAS_ex_ping_serdata
116 - \ref GRAS_ex_ping_sercb
117 - \ref GRAS_ex_ping_sermain
118 - \ref GRAS_ex_ping_client
119 - \ref GRAS_ex_ping_climain
123 \dontinclude gras/ping/ping_common.c
125 \section GRAS_ex_ping_common 1) Common code to the client and the server
127 \subsection GRAS_ex_ping_initial 1.a) Initial settings
129 Let's first load the module header and declare a logging category (see
130 \ref XBT_log for more info on logging).
135 The module header <tt>ping.h</tt> reads:
137 \dontinclude gras/ping/ping.h
142 \subsection GRAS_ex_ping_register 1.b) Register the messages
144 This function, called by both the client and the server is in charge of
145 declaring the existing messages to GRAS. Since the payload does not
146 involve any newly created types but only int, this is quite easy.
147 (to exchange more complicated types, see \ref GRAS_dd or
148 \ref GRAS_ex_mmrpc for an example).
150 \dontinclude gras/ping/ping_common.c
151 \skip register_messages
154 [Back to \ref GRAS_ex_ping_toc]
156 \section GRAS_ex_ping_server 2) Server's code
158 \subsection GRAS_ex_ping_serdata 2.a) The server's globals
160 In order to ensure the communication between the "main" and the callback
161 of the server, we need to declare some globals. We have to put them in a
162 struct definition so that they can be handled properly in GRAS (see the
163 \ref GRAS_tut_tour_globals for more info).
165 \dontinclude gras/ping/ping_server.c
169 \subsection GRAS_ex_ping_sercb 2.b) The callback to the ping message
171 Here is the callback run when the server receives any ping message (this
172 will be registered later by the server).
174 \skip server_cb_ping_handler
175 \until end_of_server_cb_ping_handler
177 \subsection GRAS_ex_ping_sermain 2.c) The "main" of the server
179 This is the "main" of the server. As explained in the tutorial, \ref
180 GRAS_tut_tour_setup, you must not write any main()
181 function yourself. Instead, you just have to write a regular function
182 like this one which will act as a main.
187 [Back to \ref GRAS_ex_ping_toc]
189 \section GRAS_ex_ping_client 3) Client's code
191 \subsection GRAS_ex_ping_climain 3.a) Client's "main" function
193 This function is quite straightforward, and the inlined comments should
194 be enough to understand it.
196 \dontinclude gras/ping/ping_client.c
200 [Back to \ref GRAS_ex_ping_toc]
203 ---------------------------------------------------------------------
204 --------------------- Simple Token Ring -----------------------------
205 ---------------------------------------------------------------------
207 /** @defgroup GRAS_ex_token Token Ring example
210 This example implements the token ring algorithm. It involves several
211 nodes arranged in a ring (each of them have a left and a right neighbour)
212 and exchanging a "token". This algorithm is one of the solution to ensure
213 the mutual exclusion between distributed processes. There is only one
214 token at any time, so the process in its possession is ensured to be the
215 only one having it. So, if there is an action you want all processes to
216 do alternativly, but you cannot afford to have two processes doing it at
217 the same time, let the process having the token doing it.
219 Actually, there is a lot of different token ring algorithms in the
220 litterature, so this example implements one of them: the simplest one.
221 The ring is static (no new node can join it, and you'll get trouble if
222 one node dies or leaves), and nothing is done for the case in which the
225 - \ref GRAS_ex_stoken_deploy
226 - \ref GRAS_ex_stoken_global
227 - \ref GRAS_ex_stoken_callback
228 - \ref GRAS_ex_stoken_main
230 \section GRAS_ex_stoken_deploy 1) Deployment file
232 Here is the deployment file:
233 \include examples/gras/mutual_exclusion/simple_token/simple_token.xml
235 The neighbour of each node is given at startup as command line argument.
236 Moreover, one of the nodes is instructed by a specific argument (the one
237 on Tremblay here) to create the token at the begining of the algorithm.
239 \section GRAS_ex_stoken_global 2) Global definition
241 The token is incarned by a specific message, which circulates from node
242 to node (the payload is an integer incremented at each hop). So, the most
243 important part of the code is the message callback, which forwards the
244 message to the next node. That is why we have to store all variable in a
245 global, as explained in the \ref GRAS_globals section.
247 \dontinclude examples/gras/mutual_exclusion/simple_token/simple_token.c
251 \section GRAS_ex_stoken_callback 3) The callback
253 Even if this is the core of this algorithm, this function is quite
256 \skip node_cb_stoken_handler
257 \until end_of_node_cb_stoken_handler
259 \section GRAS_ex_stoken_main 4) The main function
261 This function is splited in two parts: The first one performs all the
262 needed initialisations (points 1-7) while the end (point 8. below) calls
263 gras_msg_handle() as long as the planned amount of ring loops are not
271 ---------------------------------------------------------------------
272 -------------------------- MM RPC -----------------------------------
273 ---------------------------------------------------------------------
275 /** @defgroup GRAS_ex_mmrpc A simple RPC for matrix multiplication
278 This example implements a remote matrix multiplication. It involves a client
279 (creating the matrices and sending the multiplications requests) and a server
280 (computing the multiplication on client's behalf).
282 This example also constitutes a more advanced example of data description
283 mechanisms, since the message payload type is a bit more complicated than in
284 other examples such as the ping one (\ref GRAS_ex_ping).
286 It works the following way (not very different from the ping example):
287 - Both the client and the server register all needed messages and datatypes
288 - The server registers a callback to the "request" message, which computes
289 what needs to be and returns the result to the expeditor.
290 - The client creates two matrices, ask for their multiplication and check
293 This example resides in the <b>examples/gras/mmrpc/mmrpc.c</b> file. (See
294 the \ref GRAS_tut_tour_setup of the tutorial if wondering why both the server
295 and the client live in the same source file)
297 \section GRAS_ex_mmrpc_toc Table of contents of the mmrpc example
298 - \ref GRAS_ex_mmrpc_common
299 - \ref GRAS_ex_mmrpc_header
300 - \ref GRAS_ex_mmrpc_dataregister
301 - \ref GRAS_ex_mmrpc_logdef
302 - \ref GRAS_ex_mmrpc_msgregister
303 - \ref GRAS_ex_mmrpc_server
304 - \ref GRAS_ex_mmrpc_serinc
305 - \ref GRAS_ex_mmrpc_sercb
306 - \ref GRAS_ex_mmrpc_sermain
307 - \ref GRAS_ex_mmrpc_client
308 - \ref GRAS_ex_mmrpc_cliinc
309 - \ref GRAS_ex_mmrpc_climain
314 \section GRAS_ex_mmrpc_common 1) Common code to the client and the server (mmrpc_common.c and mmrpc.h)
317 \subsection GRAS_ex_mmrpc_header 1.a) Module header (mmrpc.h)
319 This loads the gras header and declare the function's prototypes as well
322 \dontinclude gras/mmrpc/mmrpc.h
327 \subsection GRAS_ex_mmrpc_dataregister 1.b) Register the data types (mmrpc.h)
329 The messages involved in a matrix of double. This type is automatically
330 known by the GRAS mecanism, using the gras_datadesc_matrix() function of the
333 \subsection GRAS_ex_mmrpc_logdef 1.c) Logging category definition (mmrpc_common.c)
335 Let's first load the module header and declare a logging category (see
336 \ref XBT_log for more info on logging). This logging category does live
337 in this file (ie the required symbols are defined here and declared as
338 "extern" in any other file using them). That is why we use
339 \ref XBT_LOG_NEW_DEFAULT_CATEGORY here and
340 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY in mmrpc_client.c and mmrpc_server.c.
342 \dontinclude gras/mmrpc/mmrpc_common.c
346 \subsection GRAS_ex_mmrpc_msgregister 1.d) Register the messages (mmrpc_common.c)
348 This function, called by both the client and the server is in charge of
349 declaring the existing messages to GRAS.
351 The datatype description builded that way can then be used to build an array datatype or
354 \skip register_messages
357 [Back to \ref GRAS_ex_mmrpc_toc]
359 \section GRAS_ex_mmrpc_server 2) Server's code (mmrpc_server.c)
361 \subsection GRAS_ex_mmrpc_serinc 2.a) Server intial settings
363 All module symbols live in the mmrpc_common.c file. We thus have to
364 define \ref XBT_DEFINE_TYPE_EXTERN to the preprocessor so that the
365 \ref XBT_DEFINE_TYPE symbols don't get included here. Likewise, we use
366 \ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY to get the log category in here.
368 \dontinclude gras/mmrpc/mmrpc_server.c
372 \subsection GRAS_ex_mmrpc_sercb 2.b) The callback to the mmrpc message
374 Here is the callback run when the server receives any mmrpc message (this
375 will be registered later by the server). Note the way we get the message
376 payload. In the ping example, there was one additional level of pointer
377 indirection (see \ref GRAS_ex_ping_sercb). This is because the payload is
378 an array here (ie a pointer) whereas it is a scalar in the ping example.
380 \skip server_cb_request_handler
381 \until end_of_server_cb_request_handler
383 \subsection GRAS_ex_mmrpc_sermain 2.c) The "main" of the server
385 This is the "main" of the server. As explained in the tutorial, \ref
386 GRAS_tut_tour_setup, you must not write any main()
387 function yourself. Instead, you just have to write a regular function
388 like this one which will act as a main.
393 [Back to \ref GRAS_ex_mmrpc_toc]
395 \section GRAS_ex_mmrpc_client 3) Client's code (mmrpc_client.c)
397 \subsection GRAS_ex_mmrpc_cliinc 2.a) Server intial settings
399 As for the server, some extra love is needed to make sure that automatic
400 datatype parsing and log categories do work even if we are using several
403 \dontinclude gras/mmrpc/mmrpc_client.c
407 \subsection GRAS_ex_mmrpc_climain 3.b) Client's "main" function
409 This function is quite straightforward, and the inlined comments should
410 be enough to understand it.
412 \dontinclude gras/mmrpc/mmrpc_client.c
416 [Back to \ref GRAS_ex_mmrpc_toc]
419 ---------------------------------------------------------------------
420 ---------------------------- Timers ---------------------------------
421 ---------------------------------------------------------------------
423 /** @defgroup GRAS_ex_timer Some timer games
426 This example fools around with the GRAS timers (\ref GRAS_timer). It is
427 mainly a regression test, since it uses almost all timer features.
429 The main program registers a repetititive task and a delayed one, and
430 then loops until the <tt>still_to_do</tt> variables of its globals reach
431 0. The delayed task set it to 5, and the repetititive one decrease it
432 each time. Here is an example of output:
433 \verbatim Initialize GRAS
435 [1108335471] Programming the repetitive_action with a frequency of 1.000000 sec
436 [1108335471] Programming the delayed_action for after 2.000000 sec
437 [1108335471] Have a rest
438 [1108335472] Canceling the delayed_action.
439 [1108335472] Re-programming the delayed_action for after 2.000000 sec
440 [1108335472] Repetitive_action has nothing to do yet
441 [1108335473] Repetitive_action has nothing to do yet
442 [1108335473] delayed_action setting globals->still_to_do to 5
443 [1108335474] repetitive_action decrementing globals->still_to_do. New value: 4
444 [1108335475] repetitive_action decrementing globals->still_to_do. New value: 3
445 [1108335476] repetitive_action decrementing globals->still_to_do. New value: 2
446 [1108335477] repetitive_action decrementing globals->still_to_do. New value: 1
447 [1108335478] repetitive_action decrementing globals->still_to_do. New value: 0
448 Exiting GRAS\endverbatim
451 - \ref GRAS_ex_timer_decl
452 - \ref GRAS_ex_timer_delay
453 - \ref GRAS_ex_timer_repeat
454 - \ref GRAS_ex_timer_main
458 \section GRAS_ex_timer_decl 1. Declarations and headers
462 \section GRAS_ex_timer_delay 2. Source code of the delayed action
463 \skip repetitive_action
464 \until end_of_repetitive_action
466 \section GRAS_ex_timer_repeat 3. Source code of the repetitive action
468 \until end_of_delayed_action
470 \section GRAS_ex_timer_main 4. Source code of main function