blob: e815bea19ea802688d4fce335cd27619fdb711bc [file] [log] [blame]
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001.. This work is licensed under a Creative Commons Attribution 4.0 International License.
2.. SPDX-License-Identifier: CC-BY-4.0
3.. CAUTION: this document is generated from source in doc/src/rtd.
4.. To make changes edit the source and recompile the document.
5.. Do NOT make changes directly to .rst or .md files.
6
7============================================================================================
8User's Guide
9============================================================================================
10--------------------------------------------------------------------------------------------
11RIC Message Router -- RMR
12--------------------------------------------------------------------------------------------
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040013
14
15Overview
16========
17
E. Scott Danielsece5bbe2020-07-21 13:39:18 -040018The RIC Message Router (RMR) is a library for peer-to-peer
19communication. Applications use the library to send and
20receive messages where the message routing and endpoint
21selection is based on the message type rather than DNS host
22name-IP port combinations. The library provides the following
23major features:
24
25
26* Routing and endpoint selection is based on *message type.*
27
28* Application is insulated from the underlying transport
29 mechanism and/or protocols.
30
31* Message distribution (round robin or fanout) is selectable
32 by message type.
33
34* Route management updates are received and processed
35 asynchronously and without overt application involvement.
36
37
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040038
39
40Purpose
41-------
42
E. Scott Danielsece5bbe2020-07-21 13:39:18 -040043RMR's main purpose is to provide an application with the
44ability to send and receive messages to/from other peer
45applications with minimal effort on the application's part.
46To achieve this, RMR manages all endpoint information,
47connections, and routing information necessary to establish
48and maintain communication. From the application's point of
49view, all that is required to send a message is to allocate
50(via RMR) a message buffer, add the payload data, and set the
51message type. To receive a message, the application needs
52only to invoke the receive function; when a message arrives a
53message buffer will be returned as the function result.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040054
55
56Message Routing
57---------------
58
E. Scott Danielsece5bbe2020-07-21 13:39:18 -040059Applications are required to place a message type into a
60message before sending, and may optionally add a subscription
61ID when appropriate. The combination of message type, and
62subscription ID are refered to as the *message key,* and is
63used to match an entry in a routing table which provides the
64possible endpoints expecting to receive messages with the
65matching key.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040066
67
68Round Robin Delivery
69--------------------
70
E. Scott Danielsece5bbe2020-07-21 13:39:18 -040071An endpoint from RMR's perspective is an application to which
72RMR may establish a connection, and expect to send messages
73with one or more defined message keys. Each entry in the
74route table consists of one or more endpoint groups, called
75round robin groups. When a message matches a specific entry,
76the entry's groups are used to select the destination of the
77message. A message is sent once to each group, with messages
78being *balanced* across the endpoints of a group via round
79robin selection. Care should be taken when defining multiple
80groups for a message type as there is extra overhead required
81and thus the overall message latency is somewhat increased.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040082
83
84Routing Table Updates
85---------------------
86
E. Scott Danielsece5bbe2020-07-21 13:39:18 -040087Route table information is made available to RMR a static
88file (loaded once), or by updates sent from a separate route
89manager application. If a static table is provided, it is
90loaded during RMR initialization and will remain in use until
91an external process connects and delivers a route table
92update (often referred to as a dynamic update). Dynamic
93updates are listened for in a separate process thread and
94applied automatically; the application does not need to allow
95for, or trigger, updates.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040096
97
98Latency And Throughput
99----------------------
100
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400101While providing insulation from the underlying message
102transport mechanics, RMR must also do so in such a manner
103that message latency and throughput are not impacted. In
104general, the RMR induced overhead, incurred due to the
105process of selecting an endpoint for each message, is minimal
106and should not impact the overall latency or throughput of
107the application. This impact has been measured with test
108applications running on the same physical host and the
109average latency through RMR for a message was on the order of
1100.02 milliseconds.
111
112As an application's throughput increases, it becomes easy for
113the application to overrun the underlying transport mechanism
114(e.g. NNG), consume all available TCP transmit buffers, or
115otherwise find itself in a situation where a send might not
116immediately complete. RMR offers different *modes* which
117allow the application to manage these states based on the
118overall needs of the application. These modes are discussed
119in the *Configuration* section of this document.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400120
121
122General Use
123===========
124
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400125To use, the RMR based application simply needs to initialise
126the RMR environment, wait for RMR to have received a routing
127table (become ready), and then invoke either the send or
128receive functions. These steps, and some behind the scenes
129details, are described in the following paragraphs.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400130
131
132Initialisation
133--------------
134
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400135The RMR function ``rmr_init()`` is used to set up the RMR
136environment and must be called before messages can be sent or
137received. One of the few parameters that the application must
138communicate to RMR is the port number that will be used as
139the listen port for new connections. The port number is
140passed on the initialisation function call and a TCP listen
141socket will be opened with this port. If the port is already
142in use RMR will report a failure; the application will need
143to reinitialise with a different port number, abort, or take
144some other action appropriate for the application.
145
146In addition to creating a TCP listen port, RMR will start a
147process thread which will be responsible for receiving
148dynamic updates to the route table. This thread also causes a
149TCP listen port to be opened as it is expected that the
150process which generates route table updates will connect and
151send new information when needed. The route table update port
152is **not** supplied by the application, but is supplied via
153an environment variable as this value is likely determined by
154the mechanism which is starting and configuring the
155application.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400156
157
158The RMR Context
159---------------
160
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400161On successful initialisation, a void pointer, often called a
162*handle* by some programming languages, is returned to the
163application. This is a reference to the RMR control
164information and must be passed as the first parameter on most
165RMR function calls. RMR refers to this as the context, or
166ctx.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400167
168
169Wait For Ready
170--------------
171
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400172An application which is only receiving messages does not need
173to wait for RMR to *become ready* after the call to the
174initialization function. However, before the application can
175successfully send a message, RMR must have loaded a route
176table, and the application must wait for RMR to report that
177it has done so. The RMR function ``rmr_ready()`` will return
178the value *true* (1) when a complete route table has been
179loaded and can be used to determine the endpoint for a send
180request.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400181
182
183Receiving Messages
184------------------
185
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400186The process of receiving is fairly straight forward. The
187application invokes the RMR ``rmr_rcv_msg()`` function which
188will block until a message is received. The function returns
189a pointer to a message block which provides all of the
190details about the message. Specifically, the application has
191access to the following information either directly or
192indirectly:
193
194
195* The payload (actual data)
196
197* The total payload length in bytes
198
199* The number of bytes of the payload which contain valid data
200
201* The message type and subscription ID values
202
203* The hostname and IP address of the source of the message
204 (the sender)
205
206* The transaction ID
207
208* Tracing data (if provided)
209
210
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400211
212
213The Message Payload
214-------------------
215
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400216The message payload contains the *raw* data that was sent by
217the peer application. The format will likely depend on the
218message type, and is expected to be known by the application.
219A direct pointer to the payload is available from the message
220buffer (see appendix B for specific message buffer details).
221
222Two payload-related length values are also directly
223available: the total payload length, and the number of bytes
224actually filled with data. The used length is set by the
225caller, and may or not be an accurate value. The total
226payload length is determined when the buffer is created for
227sending, and is the maximum number of bytes that the
228application may modify should the buffer be used to return a
229response.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400230
231
232Message Type and Subscription ID
233--------------------------------
234
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400235The message type and subscription ID are both directly
236available from the message buffer, and are the values which
237were used to by RMR in the sending application to select the
238endpoint. If the application resends the message, as opposed
239to returning the message buffer as a response, the message
240number and/or the subscription ID might need to be changed to
241avoid potential issues[1].
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400242
243
244Sender Information
245------------------
246
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400247The source, or sender information, is indirectly available to
248the application via the ``rmr_get_src()`` and
249``rmr_get_ip()`` functions. The former returns a string
250containing ``hostname:port,`` while the string
251``ip:port`` is returned by the latter.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400252
253
254Transaction ID
255--------------
256
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400257The message buffer contains a fixed length set of bytes which
258applications can set to track related messages across the
259application concept of a transaction. RMR will use the
260transaction ID for matching a response message when the
261``rmr_call()`` function is used to send a message.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400262
263
264Trace Information
265-----------------
266
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400267RMR supports the addition of an optional trace information to
268any message. The presence and size is controlled by the
269application, and can vary from message to message if desired.
270The actual contents of the trace information is determined by
271the application; RMR provides only the means to set, extract,
272and obtain a direct reference to the trace bytes. The trace
273data field in a message buffer is discussed in greater detail
274in the *Trace Data* section.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400275
276
277Sending Messages
278----------------
279
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400280Sending requires only slightly more work on the part of the
281application than receiving a message. The application must
282allocate an RMR message buffer, populate the message payload
283with data, set the message type and length, and optionally
284set the subscription ID. Information such as the source IP
285address, hostname, and port are automatically added to the
286message buffer by RMR, so there is no need for the
287application to worry about these.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400288
289
290Message Buffer Allocation
291-------------------------
292
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400293The function ``rmr_msg_alloc()`` allocates a *zero copy*
294buffer and returns a pointer to the RMR ``rmr_mbuf_t``
295structure. The message buffer provides direct access to the
296payload, length, message type and subscription ID fields. The
297buffer must be preallocated in order to allow the underlying
298transport mechanism to allocate the payload space from its
299internal memory pool; this eliminates multiple copies as the
300message is sent, and thus is more efficient.
301
302If a message buffer has been received, and the application
303wishes to use the buffer to send a response, or to forward
304the buffer to another application, a new buffer does **not**
305need to be allocated. The application may set the necessary
306information (message type, etc.), and adjust the payload, as
307is necessary and then pass the message buffer to
308``rmr_send_msg()`` or ``rmr_rts_msg()`` to be sent or
309returned to the sender.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400310
311
312Populating the Message Buffer
313-----------------------------
314
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400315The application has direct access to several of the message
316buffer fields, and should set them appropriately.
317
318
319 .. list-table::
320 :widths: 15,80
321 :header-rows: 0
322 :class: borderless
323
324 * - **len**
325 -
326 This is the number of bytes that the application placed into
327 the payload. Setting length to 0 is allowed, and length may
328 be less than the allocated payload size.
329
330 * - **mtype**
331 -
332 The message type that RMR will use to determine the endpoint
333 used as the target of the send.
334
335 * - **sub_id**
336 -
337 The subscription ID if the message is to be routed based on
338 the combination of message type and subscription ID. If no
339 subscription ID is valid for the message, the application
340 should set the field with the RMR constant
341 ``RMR_VOID_SUBID.``
342
343 * - **payload**
344 -
345 The application should obtain the reference (pointer) to the
346 payload from the message buffer and place any data into the
347 payload. The application is responsible for ensuring that the
348 maximum payload size is not exceeded. The application may
349 obtain the maximum size via the ``rmr_payload_size()``
350 function.
351
352 * - **trace data**
353 -
354 Optionally, the application may add trace information to the
355 message buffer.
356
357
358
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400359
360
361Sending a Message Buffer
362------------------------
363
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400364Once the application has populated the necessary bits of a
365message, it may be sent by passing the buffer to the
366``rmr_send_msg()`` function. This function will select an
367endpoint to receive the message, based on message type and
368subscription ID, and will pass the message to the underlying
369transport mechanism for actual transmission on the
370connection. (Depending on the underlying transport mechanism,
371the actual connection to the endpoint may happen at the time
372of the first message sent to the endpoint, and thus the
373latency of the first send might be longer than expected.)
374
375On success, the send function will return a reference to a
376message buffer; the status within that message buffer will
377indicate what the message buffer contains. When the status is
378``RMR_OK`` the reference is to a **new** message buffer for
379the application to use for the next send; the payload size is
380the same as the payload size allocated for the message that
381was just sent. This is a convenience as it eliminates the
382need for the application to call the message allocation
383function at some point in the future, and assumes the
384application will send many messages which will require the
385same payload dimensions.
386
387If the message contains any status other than ``RMR_OK,``
388then the message could **not** be sent, and the reference is
389to the unsent message buffer. The value of the status will
390indicate whether the nature of the failure was transient (
391``RMR_ERR_RETRY``) or not. Transient failures are likely to
392be successful if the application attempts to send the message
393at a later time. Unfortunately, it is impossible for RMR to
394know the exact transient failure (e.g. connection being
395established, or TCP buffer shortage), and thus it is not
396possible to communicate how long the application should wait
397before attempting to resend, if the application wishes to
398resend the message. (More discussion with respect to message
399retries can be found in the *Handling Failures* section.)
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400400
401
402Advanced Usage
403==============
404
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400405Several forms of usage fall into a more advanced category and
406are described in the following sections. These include
407blocking call, return to sender and wormhole functions.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400408
409
410The Call Function
411-----------------
412
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400413The RMR function ``rmr_call()`` sends a message in the exact
414same manner as the ``rmr_send_msg()()`` function, with the
415endpoint selection based on the message key. But unlike the
416send function, ``rmr_call()`` will block and wait for a
417response from the application that is selected to receive the
418message. The matching message is determined by the
419transaction ID which the application must place into the
420message buffer prior to invoking ``rmr_call()``. Similarly,
421the responding application must ensure that the same
422transaction ID is placed into the message buffer before
423returning its response.
424
425The return from the call is a message buffer with the
426response message; there is no difference between a message
427buffer returned by the receive function and one returned by
428the ``rmr_call()`` function. If a response is not received in
429a reasonable amount of time, a nil message buffer is returned
430to the calling application.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400431
432
433Returning a Response
434--------------------
435
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400436Because of the nature of RMR's routing policies, it is
437generally not possible for an application to control exactly
438which endpoint is sent a message. There are cases, such as
439responding to a message delivered via ``rmr_call()`` that the
440application must send a message and guarantee that RMR routes
441it to an exact destination. To enable this, RMR provides the
442``rmr_rts_msg(),`` return to sender, function. Upon receipt
443of any message, an application may alter the payload, and if
444necessary the message type and subscription ID, and pass the
445altered message buffer to the ``rmr_rts_msg()`` function to
446return the altered message to the application which sent it.
447When this function is used, RMR will examine the message
448buffer for the source information and use that to select the
449connection on which to write the response.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400450
451
452Multi-threaded Calls
453--------------------
454
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400455The basic call mechanism described above is **not** thread
456safe, as it is not possible to guarantee that a response
457message is delivered to the correct thread. The RMR function
458``rmr_mt_call()`` accepts an additional parameter which
459identifies the calling thread in order to ensure that the
460response is delivered properly. In addition, the application
461must specifically initialise the multi-threaded call
462environment by passing the ``RMRFL_MTCALL`` flag as an option
463to the ``rmr_init()`` function.
464
465One advantage of the multi-threaded call capability in RMR is
466the fact that only the calling thread is blocked. Messages
467received which are not responses to the call are continued to
468be delivered via normal ``rmr_rcv_msg()`` calls.
469
470While the process is blocked waiting for the response, it is
471entirely possible that asynchronous, non-matching, messages
472will arrive. When this happens, RMR will queues the messages
473and return them to the application over the next calls to
474``rmr_rcv_msg().``
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400475
476
477Wormholes
478---------
479
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400480As was mentioned earlier, the design of RMR is to eliminate
481the need for an application to know a specific endpoint, even
482when a response message is being sent. In some rare cases it
483may be necessary for an application to establish a direct
484connection to an RMR-based application rather than relying on
485message type and subscription ID based routing. The
486*wormhole* functions provide an application with the ability
487to create a direct connection and then to send and receive
488messages across the connection. The following are the RMR
489functions which provide wormhole communications:
490
491
492 .. list-table::
493 :widths: auto
494 :header-rows: 0
495 :class: borderless
496
497 * - **rmr_wh_open**
498 -
499 Open a connection to an endpoint. Name or IP address and port
500 of the endpoint is supplied. Returns a wormhole ID that the
501 application must use when sending a direct message.
502
503 * - **rmr_wh_send_msg**
504 -
505 Sends an RMR message buffer to the connected application. The
506 message type and subscription ID may be set in the message,
507 but RMR will ignore both.
508
509 * - **rmr_wh_close**
510 -
511 Closes the direct connection.
512
513
514
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400515
516
517Handling Failures
518=================
519
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400520The vast majority of states reported by RMR are fatal; if
521encountered during setup or initialization, then it is
522unlikely that any message oriented processing should
523continue, and when encountered on a message operation
524continued operation on that message should be abandoned.
525Specifically with regard to message sending, it is very
526likely that the underlying transport mechanism will report a
527*soft,* or transient, failure which might be successful if
528the operation is retried at a later point in time. The
529paragraphs below discuss the methods that an application
530might deal with these soft failures.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400531
532
533Failure Notification
534--------------------
535
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400536When a soft failure is reported, the returned message buffer
537returned by the RMR function will be ``RMR_ERR_RETRY.`` These
538types of failures can occur for various reasons; one of two
539reasons is typically the underlying cause:
540
541
542* The session to the targeted recipient (endpoint) is not
543 connected.
544
545* The transport mechanism buffer pool is full and cannot
546 accept another buffer.
547
548
549
550Unfortunately, it is not possible for RMR to determine which
551of these two cases is occurring, and equally as unfortunate
552the time to resolve each is different. The first, no
553connection, may require up to a second before a message can
554be accepted, while a rejection because of buffer shortage is
555likely to resolve in less than a millisecond.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400556
557
558Application Response
559--------------------
560
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400561The action which an application takes when a soft failure is
562reported ultimately depends on the nature of the application
563with respect to factors such as tolerance to extended message
564latency, dropped messages, and over all message rate.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400565
566
567RMR Retry Modes
568---------------
569
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400570In an effort to reduce the workload of an application
571developer, RMR has a default retry policy such that RMR will
572attempt to retransmit a message up to 1000 times when a soft
573failure is reported. These retries generally take less than 1
574millisecond (if all 1000 are attempted) and in most cases
575eliminates nearly all reported soft failures to the
576application. When using this mode, it might allow the
577application to simply treat all bad return values from a send
578attempt as permanent failures.
579
580If an application is so sensitive to any delay in RMR, or the
581underlying transport mechanism, it is possible to set RMR to
582return a failure immediately on any kind of error (permanent
583failures are always reported without retry). In this mode,
584RMR will still set the state in the message buffer to
585``RMR_ERR_RETRY,`` but will **not** make any attempts to
586resend the message. This zero-retry policy is enabled by
587invoking the ``rmr_set_stimeout()`` with a value of 0; this
588can be done once immediately after ``rmr_init()`` is invoked.
589
590Regardless of the retry mode which the application sets, it
591will ultimately be up to the application to handle failures
592by queuing the message internally for resend, retrying
593immediately, or dropping the send attempt all together. As
594stated before, only the application can determine how to best
595handle send failures.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400596
597
598Other Failures
599--------------
600
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400601RMR will return the state of processing for message based
602operations (send/receive) as the status in the message
603buffer. For non-message operations, state is returned to the
604caller as the integer return value for all functions which
605are not expected to return a pointer (e.g.
606``rmr_init()``.) The following are the RMR state constants
607and a brief description of their meaning.
608
609
610 .. list-table::
611 :widths: auto
612 :header-rows: 0
613 :class: borderless
614
615 * - **RMR_OK**
616 -
617 state is good; operation finished successfully
618
619 * - **RMR_ERR_BADARG**
620 -
621 argument passed to function was unusable
622
623 * - **RMR_ERR_NOENDPT**
624 -
625 send/call could not find an endpoint based on msg type
626
627 * - **RMR_ERR_EMPTY**
628 -
629 msg received had no payload; attempt to send an empty message
630
631 * - **RMR_ERR_NOHDR**
632 -
633 message didn't contain a valid header
634
635 * - **RMR_ERR_SENDFAILED**
636 -
637 send failed; errno may contain the transport provider reason
638
639 * - **RMR_ERR_CALLFAILED**
640 -
641 unable to send the message for a call function; errno may
642 contain the transport provider reason
643
644 * - **RMR_ERR_NOWHOPEN**
645 -
646 no wormholes are open
647
648 * - **RMR_ERR_WHID**
649 -
650 the wormhole id provided was invalid
651
652 * - **RMR_ERR_OVERFLOW**
653 -
654 operation would have busted through a buffer/field size
655
656 * - **RMR_ERR_RETRY**
657 -
658 request (send/call/rts) failed, but caller should retry
659 (EAGAIN for wrappers)
660
661 * - **RMR_ERR_RCVFAILED**
662 -
663 receive failed (hard error)
664
665 * - **RMR_ERR_TIMEOUT**
666 -
667 response message not received in a reasonable amount of time
668
669 * - **RMR_ERR_UNSET**
670 -
671 the message hasn't been populated with a transport buffer
672
673 * - **RMR_ERR_TRUNC**
674 -
675 length in the received buffer is longer than the size of the
676 allocated payload, received message likely truncated (length
677 set by sender could be wrong, but we can't know that)
678
679 * - **RMR_ERR_INITFAILED**
680 -
681 initialisation of something (probably message) failed
682
683 * - **RMR_ERR_NOTSUPP**
684 -
685 the request is not supported, or RMR was not initialised for
686 the request
687
688
689
690Depending on the underlying transport mechanism, and the
691nature of the call that RMR attempted, the system
692``errno`` value might reflect additional detail about the
693failure. Applications should **not** rely on errno as some
694transport mechanisms do not set it with any consistency.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400695
696
697Configuration and Control
698=========================
699
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400700With the assumption that most RMR based applications will be
701executed in a containerised environment, there are some
702underlying mechanics which the developer may need to know in
703order to properly provide a configuration specification to
704the container management system. The following paragraphs
705briefly discuss these.
706
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400707
708
709TCP Ports
710---------
711
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400712RMR requires two (2) TCP listen ports: one for general
713application-to-application communications and one for
714route-table updates. The general communication port is
715specified by the application at the time RMR is initialised.
716The port used to listen for route table updates is likely to
717be a constant port shared by all applications provided they
718are running in separate containers. To that end, the port
719number defaults to 4561, but can be configured with an
720environment variable (see later paragraph in this section).
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400721
722
723Host Names
724----------
725
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400726RMR is typically host name agnostic. Route table entries may
727contain endpoints defined either by host name or IP address.
728In the container world the concept of a *service name* might
729exist, and likely is different than a host name. RMR's only
730requirement with respect to host names is that a name used on
731a route table entry must be resolvable via the
732``gethostbyname`` system call.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400733
734
735Environment Variables
736---------------------
737
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400738Several environment variables are recognised by RMR which, in
739general, are used to define interfaces and listen ports (e.g.
740the route table update listen port), or debugging
741information. Generally this information is system controlled
742and thus RMR expects this information to be defined in the
743environment rather than provided by the application. The
744following is a list of the environment variables which RMR
745recognises:
746
747
748 .. list-table::
749 :widths: auto
750 :header-rows: 0
751 :class: borderless
752
E. Scott Daniels26864552021-02-22 14:42:21 -0500753 * - **RMR_ASYNC_CONN**
754 -
755 Allows the async connection mode to be turned off (by setting
756 the value to 0). When set to 1, or missing from the
757 environment, RMR will invoke the connection interface in the
758 transport mechanism using the non-blocking (async) mode. This
759 will likely result in many "soft failures" (retry) until the
760 connection is established, but allows the application to
761 continue unimpeded should the connection be slow to set up.
762
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400763 * - **RMR_BIND_IF**
764 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500765 This provides the interface that RMR will bind listen ports
766 to, allowing for a single interface to be used rather than
767 listening across all interfaces. This should be the IP
768 address assigned to the interface that RMR should listen on,
769 and if not defined RMR will listen on all interfaces.
E. Scott Daniels9c923bc2020-08-03 09:22:20 -0400770
771 * - **RMR_CTL_PORT**
772 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500773 This variable defines the port that RMR should open for
774 communications with Route Manager, and other RMR control
775 applications. If not defined, the port 4561 is assumed.
776
777 Previously, the ``RMR_RTG_SVC`` (route table generator
778 service port) was used to define this port. However, a future
779 version of Route Manager will require RMR to connect and
780 request tables, thus that variable is now used to supply the
781 Route Manager's well-known address and port.
782
783 To maintain backwards compatibility with the older Route
784 Manager versions, the presence of this variable in the
785 environment will shift RMR's behaviour with respect to the
786 default value used when ``RMR_RTG_SVC`` is **not** defined.
787
788 When ``RMR_CTL_PORT`` is **defined:** RMR assumes that Route
789 Manager requires RMR to connect and request table updates is
790 made, and the default well-known address for Route manager is
791 used (routemgr:4561).
792
793 When ``RMR_CTL_PORT`` is **undefined:** RMR assumes that
794 Route Manager will connect and push table updates, thus the
795 default listen port (4561) is used.
796
797 To avoid any possible misinterpretation and/or incorrect
798 assumptions on the part of RMR, it is recommended that both
799 the ``RMR_CTL_PORT`` and ``RMR_RTG_SVC`` be defined. In the
800 case where both variables are defined, RMR will behave
801 exactly as is communicated with the variable's values.
E. Scott Daniels9c923bc2020-08-03 09:22:20 -0400802
803 * - **RMR_RTREQ_FREQ**
804 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500805 When RMR needs a new route table it will send a request once
806 every ``n`` seconds. The default value for ``n`` is 5, but
807 can be changed if this variable is set prior to invoking the
808 process. Accepted values are between 1 and 300 inclusive.
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400809
E. Scott Daniels26864552021-02-22 14:42:21 -0500810 * - **RMR_RTG_SVC**
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400811 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500812 The value of this variable depends on the Route Manager in
813 use.
814
815 When the Route Manager is expecting to connect to an xAPP and
816 push route tables, this variable must indicate the
817 ``port`` which RMR should use to listen for these
818 connections.
819
820 When the Route Manager is expecting RMR to connect and
821 request a table update during initialisation, the variable
822 should be the ``host`` of the Route Manager process.
823
824 The ``RMR_CTL_PORT`` variable (added with the support of
825 sending table update requests to Route manager), controls the
826 behaviour if this variable is not set. See the description of
827 that variable for details.
828
829 * - **RMR_HR_LOG**
830 -
831 By default RMR writes messages to standard error (incorrectly
832 referred to as log messages) in human readable format. If
833 this environment variable is set to 0, the format of standard
834 error messages might be written in some format not easily
835 read by humans. If missing, a value of 1 is assumed.
836
837 * - **RMR_LOG_VLEVEL**
838 -
839 This is a numeric value which corresponds to the verbosity
840 level used to limit messages written to standard error. The
841 lower the number the less chatty RMR functions are during
842 execution. The following is the current relationship between
843 the value set on this variable and the messages written:
844
845
846 .. list-table::
847 :widths: auto
848 :header-rows: 0
849 :class: borderless
850
851 * - **0**
852 -
853 Off; no messages of any sort are written.
854
855 * - **1**
856 -
857 Only critical messages are written (default if this variable
858 does not exist)
859
860 * - **2**
861 -
862 Errors and all messages written with a lower value.
863
864 * - **3**
865 -
866 Warnings and all messages written with a lower value.
867
868 * - **4**
869 -
870 Informational and all messages written with a lower value.
871
872 * - **5**
873 -
874 Debugging mode -- all messages written, however this requires
875 RMR to have been compiled with debugging support enabled.
876
877
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400878
879 * - **RMR_RTG_ISRAW**
880 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500881 **Deprecated.** Should be set to 1 if the route table
882 generator is sending "plain" messages (not using RMR to send
883 messages), 0 if the RTG is using RMR to send. The default is
884 1 as we don't expect the RTG to use RMR.
885
886 This variable is only recognised when using the NNG transport
887 library as it is not possible to support NNG "raw"
888 communications with other transport libraries. It is also
889 necessary to match the value of this variable with the
890 capabilities of the Route Manager; at some point in the
891 future RMR will assume that all Route Manager messages will
892 arrive via an RMR connection and will ignore this variable.
893
894 * - **RMR_SEED_RT**
895 -
896 This is used to supply a static route table which can be used
897 for debugging, testing, or if no route table generator
898 process is being used to supply the route table. If not
899 defined, no static table is used and RMR will not report
900 *ready* until a table is received. The static route table may
901 contain both the route table (between newrt start and end
902 records), and the MEID map (between meid_map start and end
903 records).
904
905 * - **RMR_SRC_ID**
906 -
907 This is either the name or IP address which is placed into
908 outbound messages as the message source. This will used when
909 an RMR based application uses the rmr_rts_msg() function to
910 return a response to the sender. If not supplied RMR will use
911 the hostname which in some container environments might not
912 be routable.
913
914 The value of this variable is also used for Route Manager
915 messages which are sent via an RMR connection.
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400916
E. Scott Danielsd07cc972021-04-01 10:05:33 -0400917 * - **RMR_STASH_RT**
918 -
919 Names the file where RMR should write the latest update it
920 receives from the source of route tables (generally Route
921 Manager). This is meant to assist with debugging and/or
922 troubleshooting when it is suspected that route information
923 isn't being sent and/or received correctly. If this variable
924 is not given, RMR will save the last update using the
925 ``RMR_SEED_RT`` variable value and adding a ``.stash`` suffix
926 to the filename so as not to overwrite the static table.
927
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400928 * - **RMR_VCTL_FILE**
929 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500930 This supplies the name of a verbosity control file. The core
931 RMR functions do not produce messages unless there is a
932 critical failure. However, the route table collection thread,
933 not a part of the main message processing component, can
934 write additional messages to standard error. If this variable
935 is set, RMR will extract the verbosity level for these
936 messages (0 is silent) from the first line of the file.
937 Changes to the file are detected and thus the level can be
938 changed dynamically, however RMR will only suss out this
939 variable during initialisation, so it is impossible to enable
940 verbosity after startup.
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400941
E. Scott Daniels26864552021-02-22 14:42:21 -0500942 * - **RMR_WARNINGS**
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400943 -
E. Scott Daniels26864552021-02-22 14:42:21 -0500944 If set to 1, RMR will write some warnings which are
945 non-performance impacting. If the variable is not defined, or
946 set to 0, RMR will not write these additional warnings.
947
948
949
950There are other, non-RMR, variables which may exist and are
951used by RMR. These variable names are not under the control
952of RMR, so they are subject to change without potentiallyb
953being reflected in either RMR's code, or this document. The
954following is a list of these environment variables.
955
956
957 .. list-table::
958 :widths: auto
959 :header-rows: 0
960 :class: borderless
961
962 * - **ALARM_MANAGER_SERVICE_NAME**
963 -
964 This is the DNS name, or IP address, of the process which is
965 listening for RMR alarm messages. If this variable is
966 missing, ``service-ricplt-alarmmanager-rmr`` is assumed.
967
968 * - **ALARM_MANAGER_SERVICE_PORT**
969 -
970 This is the port that the alarm manager is using to accept
971 RMR messages. If the environment variable is missing the
972 value ``4560`` is assumed.
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400973
974
975
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400976
E. Scott Daniels26864552021-02-22 14:42:21 -0500977Logging and Alarms
978------------------
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400979
E. Scott Daniels26864552021-02-22 14:42:21 -0500980As with nearly all UNIX libraries, errors, warnings and
981informational messages are written in plain text to the
982standard error device (stderr). All RMR messages are prefixed
983with the current time (in milliseconds past the standard UNIX
984epoch), the process ID, and a severity indicator. RMR
985messages are written with one of three severity strings:
E. Scott Danielsece5bbe2020-07-21 13:39:18 -0400986
987
988 .. list-table::
989 :widths: auto
990 :header-rows: 0
991 :class: borderless
992
993 * - **[CRI]**
994 -
995 The event is of a critical nature and it is unlikely that RMR
996 will continue to operate correctly if at all. It is almost
997 certain that immediate action will be needed to resolve the
998 issue.
999
1000 * - **[ERR]**
1001 -
1002 The event is not expected and RMR is not able to handle it.
1003 There is a small chance that continued operation will be
1004 negatively impacted. Eventual action to diagnose and correct
1005 the issue will be necessary.
1006
1007 * - **[WRN]**
1008 -
1009 The event was not expected by RMR, but can be worked round.
1010 Normal operation will continue, but it is recommended that
1011 the cause of the problem be investigated.
1012
1013
1014
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001015
1016
E. Scott Daniels26864552021-02-22 14:42:21 -05001017Log message supression
1018----------------------
1019
1020For the most part, the *fast path* code in RMR does no
1021logging; even when messages are squelched, there is a
1022non-zero cosst to check for the setting each time a potential
1023message is to be written. To that end, RMRM will log only
1024severe errors once initialisation has completed. An exception
1025to this policy exists in the route table collection thread.
1026The thread of execution which collects route table updates
1027does not need to be concerned with performance, and as such
1028has the potential to log its actions in a very verbose
1029manner. The environment variable `` RMR_VCTL_FILE `` can be
1030used to define a file where the desired verbosity level (0 to
10314 where 0 is off) can be placed. If the environment variable
1032is not set when the process starts, RMR will assume that the
1033file ``/tmp/rmr.v`` will be used. Beginning with version
10344.6.0 this file does **not** need to exist when the process
1035is started. To change the verbosity level, the desired value
1036is written to the file on the first line.
1037
1038
1039Alarms
1040------
1041
1042The route table colleciton thread is also responsible for
1043watching for situations which need to be reported as alarms
1044to the platform's alarm management service. When a state
1045exists RMR will create and send an alarm (via RMR message) to
1046the alarm service, and will send a *clear* message when the
1047state no longer exists. Currently RMR will alarm only when
1048the application is not removing messages from the receive
1049ring quicklye enough causing RMR to drop messages as they are
1050received.
1051
1052
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001053Notes
1054=====
1055
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001056
1057 [1] It is entirely possible to design a routing table, and
1058 application group, such that the same message type is is
1059 left unchanged and the message is forwarded by an
1060 application after updating the payload. This type of
1061 behaviour is often referred to as service chaining, and can
1062 be done without any "knowledge" by an application with
1063 respect to where the message goes next. Service chaining is
1064 supported by RMR in as much as it allows the message to be
1065 resent, but the actual complexities of designing and
1066 implementing service chaining lie with the route table
1067 generator process.
1068
1069
1070
1071
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001072
1073
1074Appendix A -- Quick Reference
1075=============================
1076
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001077Please refer to the RMR manual pages on the Read the Docs
1078site
1079
1080https://docs.o-ran-sc.org/projects/o-ran-sc-ric-plt-lib-rmr/en/latest/index.html
1081
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001082
1083
1084Appendix B -- Message Buffer Details
1085====================================
1086
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001087The RMR message buffer is a C structure which is exposed in
1088the ``rmr.h`` header file. It is used to manage a message
1089received from a peer endpoint, or a message that is being
1090sent to a peer. Fields include payload length, amount of
1091payload actually used, status, and a reference to the
1092payload. There are also fields which the application should
1093ignore, and could be hidden in the header file, but we chose
1094not to. These fields include a reference to the RMR header
1095information, and to the underlying transport mechanism
1096message struct which may or may not be the same as the RMR
1097header reference.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001098
1099
1100The Structure
1101-------------
1102
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001103The following is the C structure. Readers are cautioned to
1104examine the ``rmr.h`` header file directly; the information
1105here may be out of date (old document in some cache), and
1106thus it may be incorrect.
1107
1108
1109::
1110
1111
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001112 typedef struct {
1113 int state; // state of processing
1114 int mtype; // message type
1115 int len; // length of data in the payload (send or received)
1116 unsigned char* payload; // transported data
1117 unsigned char* xaction; // pointer to fixed length transaction id bytes
1118 int sub_id; // subscription id
1119 int tp_state; // transport state (errno)
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001120
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001121 // these things are off limits to the user application
1122 void* tp_buf; // underlying transport allocated pointer (e.g. nng message)
1123 void* header; // internal message header (whole buffer: header+payload)
1124 unsigned char* id; // if we need an ID in the message separate from the xaction id
1125 int flags; // various MFL_ (private) flags as needed
1126 int alloc_len; // the length of the allocated space (hdr+payload)
1127 void* ring; // ring this buffer should be queued back to
1128 int rts_fd; // SI fd for return to sender
1129 int cookie; // cookie to detect user misuse of free'd msg
1130 } rmr_mbuf_t;
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001131
1132
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001133
1134
1135State vs Transport State
1136------------------------
1137
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001138The state field reflects the state at the time the message
1139buffer is returned to the calling application. For a send
1140operation, if the state is not ``RMR_OK`` then the message
1141buffer references the payload that could not be sent, and
1142when the state is ``RMR_OK`` the buffer references a *fresh*
1143payload that the application may fill in.
1144
1145When the state is not ``RMR_OK,`` C programmes may examine
1146the global ``errno`` value which RMR will have left set, if
1147it was set, by the underlying transport mechanism. In some
1148cases, wrapper modules are not able to directly access the
1149C-library ``errno`` value, and to assist with possible
1150transport error details, the send and receive operations
1151populate ``tp_state`` with the value of ``errno.``
1152
1153Regardless of whether the application makes use of the
1154``tp_state,`` or the ``errno`` value, it should be noted that
1155the underlying transport mechanism may not actually update
1156the errno value; in other words: it might not be accurate. In
1157addition, RMR populates the ``tp_state`` value in the message
1158buffer **only** when the state is not ``RMR_OK.``
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001159
1160
1161Field References
1162----------------
1163
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001164The transaction field was exposed in the first version of
1165RMR, and in hindsight this shouldn't have been done. Rather
1166than break any existing code the reference was left, but
1167additional fields such as trace data, were not directly
1168exposed to the application. The application developer is
1169strongly encouraged to use the functions which get and set
1170the transaction ID rather than using the pointer directly;
1171any data overruns will not be detected if the reference is
1172used directly.
1173
1174In contrast, the payload reference should be used directly by
1175the application in the interest of speed and ease of
1176programming. The same care to prevent writing more bytes to
1177the payload buffer than it can hold must be taken by the
1178application. By the nature of the allocation of the payload
1179in transport space, RMR is unable to add guard bytes and/or
1180test for data overrun.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001181
1182
1183Actual Transmission
1184-------------------
1185
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001186When RMR sends the application's message, the message buffer
1187is **not** transmitted. The transport buffer (tp_buf) which
1188contains the RMR header and application payload is the only
1189set of bytes which are transmitted. While it may seem to the
1190caller like the function ``rmr_send_msg()`` is returning a
1191new message buffer, the same struct is reused and only a new
1192transport buffer is allocated. The intent is to keep the
1193alloc/free cycles to a minimum.
1194
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001195
1196
1197Appendix C -- Glossary
1198======================
1199
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001200Many terms in networking can be interpreted with multiple
1201meanings, and several terms used in various RMR documentation
1202are RMR specific. The following definitions are the meanings
1203of terms used within RMR documentation and should help the
1204reader to understand the intent of meaning.
1205
1206 .. list-table::
1207 :widths: 25,70
1208 :header-rows: 0
1209 :class: borderless
1210
1211 * - **application**
1212 -
1213 A programme which uses RMR to send and/or receive messages
1214 to/from another RMR based application.
1215
1216 * - **Critical error**
1217 -
1218 An error that RMR has encountered which will prevent further
1219 successful processing by RMR. Critical errors usually
1220 indicate that the application should abort.
1221
1222 * - **Endpoint**
1223 -
1224 An RMR based application that is defined as being capable of
1225 receiving one or more types of messages (as defined by a
1226 *routing key.*)
1227
1228 * - **Environment variable**
1229 -
1230 A key/value pair which is set externally to the application,
1231 but which is available to the application (and referenced
1232 libraries) through the ``getenv`` system call. Environment
1233 variables are the main method of communicating information
1234 such as port numbers to RMR.
1235
1236 * - **Error**
1237 -
1238 An abnormal condition that RMR has encountered, but will not
1239 affect the overall processing by RMR, but may impact certain
1240 aspects such as the ability to communicate with a specific
1241 endpoint. Errors generally indicate that something, usually
1242 external to RMR, must be addressed.
1243
1244 * - **Host name**
1245 -
1246 The name of the host as returned by the ``gethostbyname``
1247 system call. In a containerised environment this might be the
1248 container or service name depending on how the container is
1249 started. From RMR's point of view, a host name can be used to
1250 resolve an *endpoint* definition in a *route* table.)
1251
1252 * - **IP**
1253 -
1254 Internet protocol. A low level transmission protocol which
1255 governs the transmission of datagrams across network
1256 boundaries.
1257
1258 * - **Listen socket**
1259 -
1260 A *TCP* socket used to await incoming connection requests.
1261 Listen sockets are defined by an interface and port number
1262 combination where the port number is unique for the
1263 interface.
1264
1265 * - **Message**
1266 -
1267 A series of bytes transmitted from the application to another
1268 RMR based application. A message is comprised of RMR specific
1269 data (a header), and application data (a payload).
1270
1271 * - **Message buffer**
1272 -
1273 A data structure used to describe a message which is to be
1274 sent or has been received. The message buffer includes the
1275 payload length, message type, message source, and other
1276 information.
1277
1278 * - **Message type**
1279 -
1280 A signed integer (0-32000) which identifies the type of
1281 message being transmitted, and is one of the two components
1282 of a *routing key.* See *Subscription ID.*
1283
1284 * - **Payload**
1285 -
1286 The portion of a message which holds the user data to be
1287 transmitted to the remote *endpoint.* The payload contents
1288 are completely application defined.
1289
1290 * - **RMR context**
1291 -
1292 A set of information which defines the current state of the
1293 underlying transport connections that RMR is managing. The
1294 application will be give a context reference (pointer) that
1295 is supplied to most RMR functions as the first parameter.
1296
1297 * - **Round robin**
1298 -
1299 The method of selecting an *endpoint* from a list such that
1300 all *endpoints* are selected before starting at the head of
1301 the list.
1302
1303 * - **Route table**
1304 -
1305 A series of "rules" which define the possible *endpoints* for
1306 each *routing key.*
1307
1308 * - **Route table manager**
1309 -
1310 An application responsible for building a *route table* and
1311 then distributing it to all applicable RMR based
1312 applications.
1313
1314 * - **Routing**
1315 -
1316 The process of selecting an *endpoint* which will be the
1317 recipient of a message.
1318
1319 * - **Routing key**
1320 -
1321 A combination of *message type* and *subscription ID* which
1322 RMR uses to select the destination *endpoint* when sending a
1323 message.
1324
1325 * - **Source**
1326 -
1327 The sender of a message.
1328
1329 * - **Subscription ID**
1330 -
1331 A signed integer value (0-32000) which identifies the
1332 subscription characteristic of a message. It is used in
1333 conjunction with the *message type* to determine the *routing
1334 key.*
1335
1336 * - **Target**
1337 -
1338 The *endpoint* selected to receive a message.
1339
1340 * - **TCP**
1341 -
1342 Transmission Control Protocol. A connection based internet
1343 protocol which provides for lossless packet transportation,
1344 usually over IP.
1345
1346 * - **Thread**
1347 -
1348 Also called a *process thread, or pthread.* This is a
1349 lightweight process which executes in concurrently with the
1350 application and shares the same address space. RMR uses
1351 threads to manage asynchronous functions such as route table
1352 updates.
1353
1354 * - **Trace information**
1355 -
1356 An optional portion of the message buffer that the
1357 application may populate with data that allows for tracing
1358 the progress of the transaction or application activity
1359 across components. RMR makes no use of this data.
1360
1361 * - **Transaction ID**
1362 -
1363 A fixed number of bytes in the *message* buffer) which the
1364 application may populate with information related to the
1365 transaction. RMR makes use of the transaction ID for matching
1366 response messages with the &c function is used to send a
1367 message.
1368
1369 * - **Transient failure**
1370 -
1371 An error state that is believed to be short lived and that
1372 the operation, if retried by the application, might be
1373 successful. C programmers will recognise this as
1374 ``EAGAIN.``
1375
1376 * - **Warning**
1377 -
1378 A warning occurs when RMR has encountered something that it
1379 believes isn't correct, but has a defined work round.
1380
1381 * - **Wormhole**
1382 -
1383 A direct connection managed by RMR between the user
1384 application and a remote, RMR based, application.
1385
1386
1387
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001388
1389
1390Appendix D -- Code Examples
1391===========================
1392
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001393The following snippet of code illustrate some of the basic
1394operation of the RMR library. Please refer to the examples
1395and test directories in the RMR repository for complete RMR
1396based programmes.
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001397
1398
1399Sender Sample
1400-------------
1401
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001402The following code segment shows how a message buffer can be
1403allocated, populated, and sent. The snippet also illustrates
1404how the result from the ``rmr_send_msg()`` function is used
1405to send the next message. It does not illustrate error and/or
1406retry handling.
1407
1408
1409::
1410
1411
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001412 #include <unistd.h>
1413 #include <errno.h>
1414 #include <string.h>
1415 #include <stdio.h>
1416 #include <stdlib.h>
1417 #include <sys/epoll.h>
1418 #include <time.h>
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001419
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001420 #include <rmr/rmr.h>
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001421
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001422 int main( int argc, char** argv ) {
1423 void* mrc; // msg router context
1424 struct epoll_event events[1]; // list of events to give to epoll
1425 struct epoll_event epe; // event definition for event to listen to
1426 int ep_fd = -1; // epoll's file des (given to epoll_wait)
1427 int rcv_fd; // file des for epoll checks
1428 int nready; // number of events ready for receive
1429 rmr_mbuf_t* sbuf; // send buffer
1430 rmr_mbuf_t* rbuf; // received buffer
1431 int count = 0;
1432 int rcvd_count = 0;
1433 char* listen_port = "43086";
1434 int delay = 1000000; // mu-sec delay between messages
1435 int mtype = 0;
1436 int stats_freq = 100;
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001437
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001438 if( argc > 1 ) { // simplistic arg picking
1439 listen_port = argv[1];
1440 }
1441 if( argc > 2 ) {
1442 delay = atoi( argv[2] );
1443 }
1444 if( argc > 3 ) {
1445 mtype = atoi( argv[3] );
1446 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001447
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001448 fprintf( stderr, "<DEMO> listen port: %s; mtype: %d; delay: %d\\n",
1449 listen_port, mtype, delay );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001450
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001451 if( (mrc = rmr_init( listen_port, 1400, RMRFL_NONE )) == NULL ) {
1452 fprintf( stderr, "<DEMO> unable to initialise RMR\\n" );
1453 exit( 1 );
1454 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001455
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001456 rcv_fd = rmr_get_rcvfd( mrc ); // set up epoll things, start by getting the FD from RMR
1457 if( rcv_fd < 0 ) {
1458 fprintf( stderr, "<DEMO> unable to set up polling fd\\n" );
1459 exit( 1 );
1460 }
1461 if( (ep_fd = epoll_create1( 0 )) < 0 ) {
1462 fprintf( stderr, "[FAIL] unable to create epoll fd: %d\\n", errno );
1463 exit( 1 );
1464 }
1465 epe.events = EPOLLIN;
1466 epe.data.fd = rcv_fd;
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001467
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001468 if( epoll_ctl( ep_fd, EPOLL_CTL_ADD, rcv_fd, &epe ) != 0 ) {
1469 fprintf( stderr, "[FAIL] epoll_ctl status not 0 : %s\\n", strerror( errno ) );
1470 exit( 1 );
1471 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001472
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001473 sbuf = rmr_alloc_msg( mrc, 256 ); // alloc 1st send buf; subsequent bufs alloc on send
1474 rbuf = NULL; // don't need to alloc receive buffer
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001475
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001476 while( ! rmr_ready( mrc ) ) { // must have route table
1477 sleep( 1 ); // wait til we get one
1478 }
1479 fprintf( stderr, "<DEMO> rmr is ready\\n" );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001480
1481
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001482 while( 1 ) { // send messages until the cows come home
1483 snprintf( sbuf->payload, 200,
1484 "count=%d received= %d ts=%lld %d stand up and cheer!", // create the payload
1485 count, rcvd_count, (long long) time( NULL ), rand() );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001486
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001487 sbuf->mtype = mtype; // fill in the message bits
1488 sbuf->len = strlen( sbuf->payload ) + 1; // send full ascii-z string
1489 sbuf->state = 0;
1490 sbuf = rmr_send_msg( mrc, sbuf ); // send & get next buf to fill in
1491 while( sbuf->state == RMR_ERR_RETRY ) { // soft failure (device busy?) retry
1492 sbuf = rmr_send_msg( mrc, sbuf ); // w/ simple spin that doesn't give up
1493 }
1494 count++;
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001495
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001496 // check to see if anything was received and pull all messages in
1497 while( (nready = epoll_wait( ep_fd, events, 1, 0 )) > 0 ) { // 0 is non-blocking
1498 if( events[0].data.fd == rcv_fd ) { // waiting on 1 thing, so [0] is ok
1499 errno = 0;
1500 rbuf = rmr_rcv_msg( mrc, rbuf ); // receive and ignore; just count
1501 if( rbuf ) {
1502 rcvd_count++;
1503 }
1504 }
1505 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001506
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001507 if( (count % stats_freq) == 0 ) { // occasional stats out to tty
1508 fprintf( stderr, "<DEMO> sent %d received %d\\n", count, rcvd_count );
1509 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001510
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001511 usleep( delay );
1512 }
1513 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001514
1515
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001516
1517
1518Receiver Sample
1519---------------
1520
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001521The receiver code is even simpler than the sender code as it
1522does not need to wait for a route table to arrive (only
1523senders need to do that), nor does it need to allocate an
1524initial buffer. The example assumes that the sender is
1525transmitting a zero terminated string as the payload.
1526
1527
1528::
1529
1530
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001531 #include <unistd.h>
1532 #include <errno.h>
1533 #include <stdio.h>
1534 #include <stdlib.h>
1535 #include <time.h>
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001536
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001537 #include <rmr/rmr.h>
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001538
1539
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001540 int main( int argc, char** argv ) {
1541 void* mrc; // msg router context
1542 long long total = 0;
1543 rmr_mbuf_t* msg = NULL; // message received
1544 int stat_freq = 10; // write stats after reciving this many messages
1545 int i;
1546 char* listen_port = "4560"; // default to what has become the standard RMR port
1547 long long count = 0;
1548 long long bad = 0;
1549 long long empty = 0;
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001550
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001551 if( argc > 1 ) {
1552 listen_port = argv[1];
1553 }
1554 if( argc > 2 ) {
1555 stat_freq = atoi( argv[2] );
1556 }
1557 fprintf( stderr, "<DEMO> listening on port: %s\\n", listen_port );
1558 fprintf( stderr, "<DEMO> stats will be reported every %d messages\\n", stat_freq );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001559
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001560 mrc = rmr_init( listen_port, RMR_MAX_RCV_BYTES, RMRFL_NONE );
1561 if( mrc == NULL ) {
1562 fprintf( stderr, "<DEMO> ABORT: unable to initialise RMr\\n" );
1563 exit( 1 );
1564 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001565
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001566 while( ! rmr_ready( mrc ) ) { // wait for RMR to get a route table
1567 fprintf( stderr, "<DEMO> waiting for ready\\n" );
1568 sleep( 3 );
1569 }
1570 fprintf( stderr, "<DEMO> rmr now shows ready\\n" );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001571
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001572 while( 1 ) { // receive until killed
1573 msg = rmr_rcv_msg( mrc, msg ); // block until one arrives
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001574
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001575 if( msg ) {
1576 if( msg->state == RMR_OK ) {
1577 count++; // nothing fancy, just count
1578 } else {
1579 bad++;
1580 }
1581 } else {
1582 empty++;
1583 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001584
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001585 if( (count % stat_freq) == 0 ) {
1586 fprintf( stderr, "<DEMO> total received: %lld; errors: %lld; empty: %lld\\n",
1587 count, bad, empty );
1588 }
1589 }
1590 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001591
1592
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001593
1594
1595Receive and Send Sample
1596-----------------------
1597
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001598The following code snippet receives messages and responds to
1599the sender if the message type is odd. The code illustrates
1600how the received message may be used to return a message to
1601the source. Variable type definitions are omitted for clarity
1602and should be obvious.
1603
1604It should also be noted that things like the message type
1605which id returned to the sender (99) is a random value that
1606these applications would have agreed on in advance and is
1607**not** an RMR definition.
1608
1609
1610::
1611
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001612 mrc = rmr_init( listen_port, MAX_BUF_SZ, RMRFL_NOFLAGS );
1613 rmr_set_stimeout( mrc, 1 ); // allow RMR to retry failed sends for ~1ms
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001614
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001615 while( ! rmr_ready( mrc ) ) { // we send, therefore we need a route table
1616 sleep( 1 );
1617 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001618
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001619 mbuf = NULL; // ensure our buffer pointer is nil for 1st call
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001620
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001621 while( TRUE ) {
1622 mbuf = rmr_rcv_msg( mrc, mbuf ); // wait for message
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001623
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001624 if( mbuf == NULL || mbuf->state != RMR_OK ) {
1625 break;
1626 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001627
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001628 if( mbuf->mtype % 2 ) { // respond to odd message types
1629 plen = rmr_payload_size( mbuf ); // max size
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001630
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001631 // reset necessary fields in msg
1632 mbuf->mtype = 99; // response type
1633 mbuf->sub_id = RMR_VOID_SUBID; // we turn subid off
1634 mbuf->len = snprintf( mbuf->payload, plen, "pong: %s", get_info() );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001635
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001636 mbuf = rmr_rts_msg( mrc, mbuf ); // return to sender
1637 if( mbuf == NULL || mbuf->state != RMR_OK ) {
1638 fprintf( stderr, "return to sender failed\\n" );
1639 }
1640 }
1641 }
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001642
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04001643 fprintf( stderr, "abort: receive failure\\n" );
1644 rmr_close( mrc );
E. Scott Danielsece5bbe2020-07-21 13:39:18 -04001645
1646
1647