# <center><i class="fa fa-edit"></i> RMR Library Initialization Analysis</center>
###### tags: `Pre-Internship`
:::info
**Goal:**
Learning RMR from inside and outside to further understand how messaging in RIC Works and to help ease the research of RIC managers communication.
:::
## Overview
RMR (RIC Message Router) is a library used in RIC that provides the ability to send and receive message easily to RIC components. RMR manages all endpoint information, connections, and routing information to establish and maintain communication. From the application’s point of view, all that is required to send a message is to allocate (via RMR) a message buffer, add the payload data, and set the message type. To receive a message, the application needs only to invoke the receive function; when a message arrives a message buffer will be returned as the function result. Applications are required to place a message type into a message before sending, and may optionally add a subscription ID when appropriate. The combination of message type, and subscription ID are refered to as the message key, and is used to match an entry in a routing table which provides the possible endpoints expecting to receive messages with the matching key.
## RMR Message Parameters
Each RMR message contains :
| Parameter | Variable Name | Datatype |
| --------------------- | ------------- | -------- |
| Payload (Actual Data) | Payload | Byte |
| Payload Length | PayloadLen | Integer |
| Subscription ID | SubId | Integer |
| Message Type | Mtype | Integer |
| Wormhole ID | Whid | Integer |
| Sender Information | Src | String |
| Fixed Length of Data | Xid | String |
| Message Buffer | MBuf | Pointer |
## General Functions for Initiation


### rmr_init()
The RMR function rmr_init() is used to set up the RMR environment and must be called before messages can be sent or received. Acts as the Wrapper for the init() funcion as it needs to ensure internal flags are masked off before calling the real workhorse .
### init()
## Summary

## Code Workflow
### Function Initiation

The Function has 3 parameters: **User Protocol:Port , Max Message Size, and Flags**. In which the User Protocol:Port can be leave as an empty pointer, and RMR would automatically assign the default one (TCP 4567). RMR then would allocate memory for the general RMR user Parameters called uta_ctx_t (or uta_ctx). The struct is defined in src\rmr\nanomsg\include\ **rmr_private.h**

If Memory Allocation Fails, it would return ENOMEM/ POSIX.1-2001 (RMR Is using errno.h to handle errors), which literally means error allocating memory.
### Message Ring Initiation

* Message ring to hold asynch messages received while waiting for call response is created (and is held at uta_ctx_t -> mring) by calling the function uta_mk_ring(ring size int). The Default Source Code downloaded from OSC contains 128 as the ring size.
### Set Payload Length and Message Length

The Payload Length by default is the RMR_MAX_RCV_BYTES (defined in rmr.h as 2048) + the size of Message Header. If The User defined the max message size when calling the init function, RMR will check whether it is smaller than the default one, if it is indeed smaller, RMR will use the User defined one as the max payload length. Then the length of the message is the maximum payload length + the size of the Message Header.
### Fill Routing Table Generation Information
The function uta_lookup_rtg is called to fill rtg information, which is the address that connects to RoutingManager for Route Table Updates. If not Defined in Source code, the port 5656 will be assigned (to rtg_port) as default and the address for rtg is generated (to rtg_addr). After it is successful
### Initialize Listen Socket, Hostname, Port, and Bind Info

* The Listen Socket is initiated by calling the functionn ctx->nn_sock = nn_socket( AF_SP, NN_PULL );
* If the Number of Listen Socket is 0, that means it is failed to be initialied.
* Work Buffer (wbuf) is a string local variabled declared at the start of the function that is 1024 characters long. The function gethostname(wbuf, sizeof(wbuf)) is called to get the hostname.
* To set the domain portion to zero, we call the function tok = strchr( wbuf, '.' ), and set the address of tok to zero
* The DNS name consisted of the hostname:port. The program will check whether it exceeds the maximum char length (RMR_MAX_SRC, defined in rmr.h, which is 64). By default it is 0.0.0.0:1234
### Flag Management

* All Flags are defined in rmr_agnostic.h