Extend doc of RMR python binding methods
[ric-plt/xapp-frame-py.git] / docs / rmr_api.rst
1 RMR Python Bindings
2 ===================
3
4 Overview
5 --------
6
7 The xapp python framework repository includes a python submodule
8 called `rmr`.  This package (`ricxappframe.rmr`) is a CTYPES wrapper
9 around the RMR shared library.  Most Xapp users will never use this
10 package natively; however python apps that need access to the low
11 level RMR API can use this package.  Usage of this python package
12 requires that you have the RMR shared-object library installed.
13
14
15 RMR API
16 -------
17
18 ..
19   Sphinx can generate API documentation by running Python to pull doc strings
20   from the binding code using these Sphinx directives that are commented out:
21          .. automodule:: ricxappframe.rmr.rmr
22              :members:
23   But that approach requires the RMR library to be installed, which is difficult
24   to achieve at ReadTheDocs.io.  Instead, the RST below was generated and captured
25   according to the method shown at
26   https://stackoverflow.com/questions/2668187/make-sphinx-generate-rst-class-documentation-from-pydoc
27
28
29
30 .. py:module:: ricxappframe.rmr.rmr
31
32
33 ..
34     !! processed by numpydoc !!
35
36 .. py:data:: RMR_MAX_RCV_BYTES
37    :module: ricxappframe.rmr.rmr
38    :value: 65536
39
40
41    Maximum size message to receive
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58    ..
59        !! processed by numpydoc !!
60
61 .. py:data:: RMRFL_MTCALL
62    :module: ricxappframe.rmr.rmr
63    :value: 2
64
65
66    Multi-threaded initialization flag
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83    ..
84        !! processed by numpydoc !!
85
86 .. py:data:: RMRFL_NONE
87    :module: ricxappframe.rmr.rmr
88    :value: 0
89
90
91    Empty flag
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108    ..
109        !! processed by numpydoc !!
110
111 .. py:data:: RMR_OK
112    :module: ricxappframe.rmr.rmr
113    :value: 0
114
115
116    State constant for OK
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133    ..
134        !! processed by numpydoc !!
135
136 .. py:data:: RMR_ERR_TIMEOUT
137    :module: ricxappframe.rmr.rmr
138    :value: 12
139
140
141    State constant for timeout
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158    ..
159        !! processed by numpydoc !!
160
161 .. py:data:: RMR_ERR_RETRY
162    :module: ricxappframe.rmr.rmr
163    :value: 10
164
165
166    State constant for retry
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183    ..
184        !! processed by numpydoc !!
185
186 .. py:class:: rmr_mbuf_t
187    :module: ricxappframe.rmr.rmr
188
189
190    Mirrors public members of type rmr_mbuf_t from RMR header file src/common/include/rmr.h
191
192    | typedef struct {
193    |    int     state;          // state of processing
194    |    int     mtype;          // message type
195    |    int     len;            // length of data in the payload (send or received)
196    |    unsigned char* payload; // transported data
197    |    unsigned char* xaction; // pointer to fixed length transaction id bytes
198    |    int sub_id;             // subscription id
199    |    int      tp_state;      // transport state (a.k.a errno)
200    |
201    | these things are off limits to the user application
202    |
203    |    void*   tp_buf;         // underlying transport allocated pointer (e.g. nng message)
204    |    void*   header;         // internal message header (whole buffer: header+payload)
205    |    unsigned char* id;      // if we need an ID in the message separate from the xaction id
206    |    int flags;              // various MFL (private) flags as needed
207    |    int alloc_len;          // the length of the allocated space (hdr+payload)
208    | } rmr_mbuf_t;
209
210    RE PAYLOADs type below, see the documentation for c_char_p:
211       class ctypes.c_char_p
212            Represents the C char * datatype when it points to a zero-terminated string.
213            For a general character pointer that may also point to binary data, POINTER(c_char)
214            must be used. The constructor accepts an integer address, or a bytes object.
215
216
217
218
219
220
221
222
223
224
225
226
227
228    :Attributes:
229
230        **len**
231            Structure/Union member
232
233        **mtype**
234            Structure/Union member
235
236        **payload**
237            Structure/Union member
238
239        **state**
240            Structure/Union member
241
242        **sub_id**
243            Structure/Union member
244
245        **tp_state**
246            Structure/Union member
247
248        **xaction**
249            Structure/Union member
250
251
252    ..
253        !! processed by numpydoc !!
254
255 .. py:function:: rmr_init(uproto_port: ctypes.c_char_p, max_msg_size: int, flags: int) -> ctypes.c_void_p
256    :module: ricxappframe.rmr.rmr
257
258
259    Prepares the environment for sending and receiving messages.
260    Refer to RMR C documentation for method::
261
262        extern void* rmr_init(char* uproto_port, int max_msg_size, int flags)
263
264    This function raises an exception if the returned context is None.
265
266    :Parameters:
267
268        **uproto_port: c_char_p**
269            Pointer to a buffer with the port number as a string; e.g., "4550"
270
271        **max_msg_size: integer**
272            Maximum message size to receive
273
274        **flags: integer**
275            RMR option flags
276
277    :Returns:
278
279        c_void_p:
280            Pointer to RMR context
281
282
283
284
285
286
287
288
289
290
291
292
293
294    ..
295        !! processed by numpydoc !!
296
297 .. py:function:: rmr_ready(vctx: ctypes.c_void_p) -> int
298    :module: ricxappframe.rmr.rmr
299
300
301    Checks if a routing table has been received and installed.
302    Refer to RMR C documentation for method::
303
304        extern int rmr_ready(void* vctx)
305
306    :Parameters:
307
308        **vctx: ctypes c_void_p**
309            Pointer to RMR context
310
311    :Returns:
312
313        1 for yes, 0 for no
314            ..
315
316
317
318
319
320
321
322
323
324
325
326
327
328    ..
329        !! processed by numpydoc !!
330
331 .. py:function:: rmr_close(vctx: ctypes.c_void_p)
332    :module: ricxappframe.rmr.rmr
333
334
335    Closes the listen socket.
336    Refer to RMR C documentation for method::
337
338        extern void rmr_close(void* vctx)
339
340    :Parameters:
341
342        **vctx: ctypes c_void_p**
343            Pointer to RMR context
344
345    :Returns:
346
347        None
348            ..
349
350
351
352
353
354
355
356
357
358
359
360
361
362    ..
363        !! processed by numpydoc !!
364
365 .. py:function:: rmr_set_stimeout(vctx: ctypes.c_void_p, rloops: int) -> int
366    :module: ricxappframe.rmr.rmr
367
368
369    Sets the configuration for how RMR will retry message send operations.
370    Refer to RMR C documentation for method::
371
372        extern int rmr_set_stimeout(void* vctx, int rloops)
373
374    :Parameters:
375
376        **vctx: ctypes c_void_p**
377            Pointer to RMR context
378
379        **rloops: int**
380            Number of retry loops
381
382    :Returns:
383
384        0 on success, -1 on failure
385            ..
386
387
388
389
390
391
392
393
394
395
396
397
398
399    ..
400        !! processed by numpydoc !!
401
402 .. py:function:: rmr_alloc_msg(vctx: ctypes.c_void_p, size: int, payload=None, gen_transaction_id: bool = False, mtype=None, meid=None, sub_id=None, fixed_transaction_id=None)
403    :module: ricxappframe.rmr.rmr
404
405
406    Allocates and returns a buffer to write and send through the RMR library.
407    Refer to RMR C documentation for method::
408
409        extern rmr_mbuf_t* rmr_alloc_msg(void* vctx, int size)
410
411    Optionally populates the message from the remaining arguments.
412
413    TODO: on next API break, clean up transaction_id ugliness. Kept for now to preserve API.
414
415    :Parameters:
416
417        **vctx: ctypes c_void_p**
418            Pointer to RMR context
419
420        **size: int**
421            How much space to allocate
422
423        **payload: bytes**
424            if not None, attempts to set the payload
425
426        **gen_transaction_id: bool**
427            if True, generates and sets a transaction ID.
428            Note, option fixed_transaction_id overrides this option
429
430        **mtype: bytes**
431            if not None, sets the sbuf's message type
432
433        **meid: bytes**
434            if not None, sets the sbuf's meid
435
436        **sub_id: bytes**
437            if not None, sets the sbuf's subscription id
438
439        **fixed_transaction_id: bytes**
440            if not None, used as the transaction ID.
441            Note, this overrides the option gen_transaction_id
442
443    :Returns:
444
445        c_void_p:
446            Pointer to rmr_mbuf structure
447
448
449
450
451
452
453
454
455
456
457
458
459
460    ..
461        !! processed by numpydoc !!
462
463 .. py:function:: rmr_realloc_payload(ptr_mbuf: ctypes.c_void_p, new_len: int, copy: bool = False, clone: bool = False)
464    :module: ricxappframe.rmr.rmr
465
466
467    Allocates and returns a message buffer large enough for the new length.
468    Refer to RMR C documentation for method::
469
470        extern rmr_mbuf_t* rmr_realloc_payload(rmr_mbuf_t*, int, int, int)
471
472    :Parameters:
473
474        **ptr_mbuf: c_void_p**
475            Pointer to rmr_mbuf structure
476
477        **new_len: int**
478            Length
479
480        **copy: bool**
481            Whether to copy the original paylod
482
483        **clone: bool**
484            Whether to clone the original buffer
485
486    :Returns:
487
488        c_void_p:
489            Pointer to rmr_mbuf structure
490
491
492
493
494
495
496
497
498
499
500
501
502
503    ..
504        !! processed by numpydoc !!
505
506 .. py:function:: rmr_free_msg(ptr_mbuf: ctypes.c_void_p)
507    :module: ricxappframe.rmr.rmr
508
509
510    Releases the message buffer.
511    Refer to RMR C documentation for method::
512
513        extern void rmr_free_msg(rmr_mbuf_t* mbuf )
514
515    :Parameters:
516
517        **ptr_mbuf: c_void_p**
518            Pointer to rmr_mbuf structure
519
520    :Returns:
521
522        None
523            ..
524
525
526
527
528
529
530
531
532
533
534
535
536
537    ..
538        !! processed by numpydoc !!
539
540 .. py:function:: rmr_payload_size(ptr_mbuf: ctypes.c_void_p) -> int
541    :module: ricxappframe.rmr.rmr
542
543
544    Gets the number of bytes available in the payload.
545    Refer to RMR C documentation for method::
546
547        extern int rmr_payload_size(rmr_mbuf_t* msg)
548
549    :Parameters:
550
551        **ptr_mbuf: c_void_p**
552            Pointer to rmr_mbuf structure
553
554    :Returns:
555
556        int:
557            Number of bytes available
558
559
560
561
562
563
564
565
566
567
568
569
570
571    ..
572        !! processed by numpydoc !!
573
574 .. py:function:: rmr_send_msg(vctx: ctypes.c_void_p, ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t) -> ricxappframe.rmr.rmr.LP_rmr_mbuf_t
575    :module: ricxappframe.rmr.rmr
576
577
578    Sends the message according to the routing table and returns an empty buffer.
579    Refer to RMR C documentation for method::
580
581        extern rmr_mbuf_t* rmr_send_msg(void* vctx, rmr_mbuf_t* msg)
582
583    :Parameters:
584
585        **vctx: ctypes c_void_p**
586            Pointer to RMR context
587
588        **ptr_mbuf: c_void_p**
589            Pointer to rmr_mbuf structure
590
591    :Returns:
592
593        c_void_p:
594            Pointer to rmr_mbuf structure
595
596
597
598
599
600
601
602
603
604
605
606
607
608    ..
609        !! processed by numpydoc !!
610
611 .. py:function:: rmr_rcv_msg(vctx: ctypes.c_void_p, ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t) -> ricxappframe.rmr.rmr.LP_rmr_mbuf_t
612    :module: ricxappframe.rmr.rmr
613
614
615    Waits for a message to arrive, and returns it.
616    Refer to RMR C documentation for method::
617
618        extern rmr_mbuf_t* rmr_rcv_msg(void* vctx, rmr_mbuf_t* old_msg)
619
620    :Parameters:
621
622        **vctx: ctypes c_void_p**
623            Pointer to RMR context
624
625        **ptr_mbuf: c_void_p**
626            Pointer to rmr_mbuf structure
627
628    :Returns:
629
630        c_void_p:
631            Pointer to rmr_mbuf structure
632
633
634
635
636
637
638
639
640
641
642
643
644
645    ..
646        !! processed by numpydoc !!
647
648 .. py:function:: rmr_torcv_msg(vctx: ctypes.c_void_p, ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t, ms_to: int) -> ricxappframe.rmr.rmr.LP_rmr_mbuf_t
649    :module: ricxappframe.rmr.rmr
650
651
652    Waits up to the timeout value for a message to arrive, and returns it.
653    Refer to RMR C documentation for method::
654
655        extern rmr_mbuf_t* rmr_torcv_msg(void* vctx, rmr_mbuf_t* old_msg, int ms_to)
656
657    :Parameters:
658
659        **vctx: ctypes c_void_p**
660            Pointer to RMR context
661
662        **ptr_mbuf: c_void_p**
663            Pointer to rmr_mbuf structure
664
665        **ms_to: int**
666            Time out value in milliseconds
667
668    :Returns:
669
670        c_void_p:
671            Pointer to rmr_mbuf structure
672
673
674
675
676
677
678
679
680
681
682
683
684
685    ..
686        !! processed by numpydoc !!
687
688 .. py:function:: rmr_rts_msg(vctx: ctypes.c_void_p, ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t, payload=None, mtype=None) -> ricxappframe.rmr.rmr.LP_rmr_mbuf_t
689    :module: ricxappframe.rmr.rmr
690
691
692    Sends a message to the originating endpoint and returns an empty buffer.
693    Refer to RMR C documentation for method::
694
695        extern rmr_mbuf_t* rmr_rts_msg(void* vctx, rmr_mbuf_t* msg)
696
697    additional features beyond c-rmr:
698        if payload is not None, attempts to set the payload
699        if mtype is not None, sets the sbuf's message type
700
701    :Parameters:
702
703        **vctx: ctypes c_void_p**
704            Pointer to an RMR context
705
706        **ptr_mbuf: ctypes c_void_p**
707            Pointer to an RMR message buffer
708
709        **payload: bytes**
710            Payload
711
712        **mtype: bytes**
713            Message type
714
715    :Returns:
716
717        c_void_p:
718            Pointer to rmr_mbuf structure
719
720
721
722
723
724
725
726
727
728
729
730
731
732    ..
733        !! processed by numpydoc !!
734
735 .. py:function:: rmr_call(vctx: ctypes.c_void_p, ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t) -> ricxappframe.rmr.rmr.LP_rmr_mbuf_t
736    :module: ricxappframe.rmr.rmr
737
738
739    Sends a message, waits for a response and returns it.
740    Refer to RMR C documentation for method::
741
742        extern rmr_mbuf_t* rmr_call(void* vctx, rmr_mbuf_t* msg)
743
744    :Parameters:
745
746        **ptr_mbuf: ctypes c_void_p**
747            Pointer to an RMR message buffer
748
749    :Returns:
750
751        c_void_p:
752            Pointer to rmr_mbuf structure
753
754
755
756
757
758
759
760
761
762
763
764
765
766    ..
767        !! processed by numpydoc !!
768
769 .. py:function:: rmr_set_meid(ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t, byte_str: bytes) -> int
770    :module: ricxappframe.rmr.rmr
771
772
773    Sets the managed entity field in the message and returns the number of bytes copied.
774    Refer to RMR C documentation for method::
775
776        extern int rmr_bytes2meid(rmr_mbuf_t* mbuf, unsigned char const* src, int len);
777
778    Caution:  the meid length supported in an RMR message is 32 bytes, but C applications
779    expect this to be a nil terminated string and thus only 31 bytes are actually available.
780
781    Raises: exceptions.MeidSizeOutOfRang
782
783    :Parameters:
784
785        **ptr_mbuf: ctypes c_void_p**
786            Pointer to an RMR message buffer
787
788        **byte_tr: bytes**
789            Managed entity ID value
790
791    :Returns:
792
793        int:
794            number of bytes copied
795
796
797
798
799
800
801
802
803
804
805
806
807
808    ..
809        !! processed by numpydoc !!
810
811 .. py:function:: rmr_get_meid(ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t) -> bytes
812    :module: ricxappframe.rmr.rmr
813
814
815    Gets the managed entity ID (meid) from the message header.
816    This is a python-friendly version of RMR C method::
817
818        extern unsigned char* rmr_get_meid(rmr_mbuf_t* mbuf, unsigned char* dest);
819
820    :Parameters:
821
822        **ptr_mbuf: ctypes c_void_p**
823            Pointer to an RMR message buffer
824
825    :Returns:
826
827        bytes:
828            Managed entity ID
829
830
831
832
833
834
835
836
837
838
839
840
841
842    ..
843        !! processed by numpydoc !!
844
845 .. py:function:: rmr_get_src(ptr_mbuf: ricxappframe.rmr.rmr.LP_rmr_mbuf_t, dest: ctypes.c_char_p) -> ctypes.c_char_p
846    :module: ricxappframe.rmr.rmr
847
848
849    Copies the message-source information to the buffer.
850    Refer to RMR C documentation for method::
851
852        extern unsigned char* rmr_get_src(rmr_mbuf_t* mbuf, unsigned char* dest);
853
854    :Parameters:
855
856        **ptr_mbuf: ctypes POINTER(rmr_mbuf_t)**
857            Pointer to an RMR message buffer
858
859        **dest: ctypes c_char_p**
860            Pointer to a buffer to receive the message source
861
862    :Returns:
863
864        string:
865            message-source information
866
867
868
869
870
871
872
873
874
875
876
877
878
879    ..
880        !! processed by numpydoc !!
881
882 .. py:function:: get_payload(ptr_mbuf: ctypes.c_void_p) -> bytes
883    :module: ricxappframe.rmr.rmr
884
885
886    Gets the binary payload from the rmr_buf_t*.
887
888
889    :Parameters:
890
891        **ptr_mbuf: ctypes c_void_p**
892            Pointer to an rmr message buffer
893
894    :Returns:
895
896        bytes:
897            the message payload
898
899
900
901
902
903
904
905
906
907
908
909
910
911    ..
912        !! processed by numpydoc !!
913
914 .. py:function:: get_xaction(ptr_mbuf: ctypes.c_void_p) -> bytes
915    :module: ricxappframe.rmr.rmr
916
917
918    Gets the transaction ID from the rmr_buf_t*.
919
920
921    :Parameters:
922
923        **ptr_mbuf: ctypes c_void_p**
924            Pointer to an rmr message buffer
925
926    :Returns:
927
928        bytes:
929            the transaction id
930
931
932
933
934
935
936
937
938
939
940
941
942
943    ..
944        !! processed by numpydoc !!
945
946 .. py:function:: message_summary(ptr_mbuf: ctypes.c_void_p) -> dict
947    :module: ricxappframe.rmr.rmr
948
949
950    Returns a dict with the fields of an RMR message.
951
952
953    :Parameters:
954
955        **ptr_mbuf: ctypes c_void_p**
956            Pointer to an rmr message buffer
957
958    :Returns:
959
960        dict:
961            dict message summary
962
963
964
965
966
967
968
969
970
971
972
973
974
975    ..
976        !! processed by numpydoc !!
977
978 .. py:function:: set_payload_and_length(byte_str: bytes, ptr_mbuf: ctypes.c_void_p)
979    :module: ricxappframe.rmr.rmr
980
981
982    Sets an rmr payload and content length.
983    In place method, no return.
984
985
986    :Parameters:
987
988        **byte_str: bytes**
989            the bytes to set the payload to
990
991        **ptr_mbuf: ctypes c_void_p**
992            Pointer to an rmr message buffer
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007    ..
1008        !! processed by numpydoc !!
1009
1010 .. py:function:: generate_and_set_transaction_id(ptr_mbuf: ctypes.c_void_p)
1011    :module: ricxappframe.rmr.rmr
1012
1013
1014    Generates a UUID and sets the RMR transaction id to it
1015
1016
1017    :Parameters:
1018
1019        **ptr_mbuf: ctypes c_void_p**
1020            Pointer to an rmr message buffer
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035    ..
1036        !! processed by numpydoc !!
1037
1038 .. py:function:: set_transaction_id(ptr_mbuf: ctypes.c_void_p, tid_bytes: bytes)
1039    :module: ricxappframe.rmr.rmr
1040
1041
1042    Sets an RMR transaction id
1043    TODO: on next API break, merge these two functions. Not done now to preserve API.
1044
1045
1046    :Parameters:
1047
1048        **ptr_mbuf: ctypes c_void_p**
1049            Pointer to an rmr message buffer
1050
1051        **tid_bytes: bytes**
1052            bytes of the desired transaction id
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067    ..
1068        !! processed by numpydoc !!
1069
1070 .. py:function:: get_src(ptr_mbuf: ctypes.c_void_p) -> str
1071    :module: ricxappframe.rmr.rmr
1072
1073
1074    Gets the message source (likely host:port)
1075
1076
1077    :Parameters:
1078
1079        **ptr_mbuf: ctypes c_void_p**
1080            Pointer to an rmr message buffer
1081
1082    :Returns:
1083
1084        string:
1085            message source
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099    ..
1100        !! processed by numpydoc !!
1101