dde00ae495785ceaef2fe9c5c1f2040ab168221a
[ric-plt/lib/rmr.git] / src / nanomsg / include / rmr_private.h
1 // : vi ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       rmr_private.h
23         Abstract:       Private header information for the uta library functions.
24                                 This should contain only things which are specific to nanomsg;
25                                 anything else is defined in the common/rmr_agnostic.h header.
26
27         Author:         E. Scott Daniels
28         Date:           27 November 2018
29
30         Mods:           28 February 2019 - moved most of the crap here to agnosic.
31 */
32
33 #ifndef _rmr_private_h
34 #define _rmr_private_h
35
36 /*
37         Manages an endpoint. Typedef for this is defined in agnostic.h
38 */
39 struct endpoint {
40         char*   name;                   // end point name (symtab reference)
41         char*   proto;                  // connection proto (should only be TCP, but future might bring others)
42         char*   addr;                   // address used for connection
43         int             nn_sock;                // the nano-msg socket to write to for this entry
44         int             open;                   // true if we've established the connection
45 };
46
47 /*
48         Context describing our world. Should be returned to user programme on
49         call to initialise, and passed as first parm on all calls to other
50         visible functions.
51
52         The typedef for ctx is in the agnostic header
53 */
54 struct uta_ctx {
55         char*   my_name;                        // dns name of this host to set in sender field of a message
56         int             shutdown;                       // threads should exit if this is set
57         int max_mlen;                           // max message length payload+header
58         int     max_plen;                               // max payload length
59         int     flags;                                  // CTXFL_ constants
60         int nrtele;                                     // number of elements in the routing table
61         int     nn_sock;                                // our general listen socket
62         int     trace_data_len;                 // len of tracing data that sits just past header (0 means none)
63         int     d1_len;                                 // lengths for additional post header, pre payload data areas
64         int d2_len;
65         int last_rto;                           // last receive timeout set so that we don't bash in on every call
66         route_table_t* rtable;          // the active route table
67         route_table_t* old_rtable;      // the previously used rt, sits here to allow for draining
68         route_table_t* new_rtable;      // route table under construction
69         if_addrs_t*     ip_list;                // list manager of the IP addresses that are on our known interfaces
70         void*   mring;                          // ring where msgs are queued while waiting for a call response msg
71
72         char*   rtg_addr;                       // addr/port of the route table generation publisher
73         int             rtg_port;                       // the port that the rtg listens on
74
75         wh_mgt_t*       wormholes;              // wormhole management
76         pthread_t       rtc_th;                 // thread info for the rtc listener
77 };
78
79
80 /*
81         Prototypes of the functions which are defined in our static modules (nothing
82         from common should be here).
83 */
84
85 // ---- housekeeping and initialisation ----------
86 static void* init( char* usr_port, int max_mlen, int flags );
87 static void free_ctx( uta_ctx_t* ctx );
88
89 // --- message and context management --------
90 static rmr_mbuf_t* send_msg( uta_ctx_t* ctx, rmr_mbuf_t* msg, int nn_sock );
91 static void* rcv_payload( uta_ctx_t* ctx, rmr_mbuf_t* old_msg );
92
93
94 // ---- route table and connection management ---------------
95
96 static int uta_link2( char* target );
97 static int rt_link2_ep( endpoint_t* ep );
98 static endpoint_t*  uta_add_ep( route_table_t* rt, rtable_ent_t* rte, char* ep_name, int group  );
99 static int uta_epsock_byname( route_table_t* rt, char* ep_name );
100 static int uta_epsock_rr( route_table_t *rt, int mtype, int group, int* more );
101
102 // ------ msg ------------------------------------------------
103 static rmr_mbuf_t* alloc_zcmsg( uta_ctx_t* ctx, rmr_mbuf_t* msg, int size, int state, int tr_size );
104 static inline rmr_mbuf_t* clone_msg( rmr_mbuf_t* old_msg  );
105 static rmr_mbuf_t* rcv_msg( uta_ctx_t* ctx, rmr_mbuf_t* old_msg );
106 static void* rcv_payload( uta_ctx_t* ctx, rmr_mbuf_t* old_msg );
107 static rmr_mbuf_t* send_msg( uta_ctx_t* ctx, rmr_mbuf_t* msg, int nn_sock );
108 static rmr_mbuf_t* send2ep( uta_ctx_t* ctx, endpoint_t* ep, rmr_mbuf_t* msg );
109
110
111
112 /*
113 // --- message ring --------------------------
114 static void* uta_mk_ring( int size );
115 static void uta_ring_free( void* vr );
116 static inline void* uta_ring_extract( void* vr );
117 static inline int uta_ring_insert( void* vr, void* new_data );
118
119 // --- message and context management --------
120 static int ie_test( void* r, int i_factor, long inserts );
121 static void free_ctx( uta_ctx_t* ctx );
122 static rmr_mbuf_t* alloc_zcmsg( uta_ctx_t* ctx, rmr_mbuf_t* msg, int size, int state );
123 static inline rmr_mbuf_t* clone_msg( rmr_mbuf_t* old_msg  );
124 static rmr_mbuf_t* rcv_msg( uta_ctx_t* ctx, rmr_mbuf_t* old_msg );
125
126 // ----- route table static things ---------
127 static void collect_things( void* st, void* entry, char const* name, void* thing, void* vthing_list );
128 static void del_rte( void* st, void* entry, char const* name, void* thing, void* data );
129 static char* uta_fib( char* fname );
130 static route_table_t* uta_rt_init( );
131 static route_table_t* uta_rt_clone( route_table_t* srt );
132 static void uta_rt_drop( route_table_t* rt );
133 static endpoint_t*  uta_add_ep( route_table_t* rt, rtable_ent_t* rte, char* ep_name, int group  );
134 static rtable_ent_t* uta_add_rte( route_table_t* rt, int mtype, int nrrgroups );
135 static endpoint_t* uta_get_ep( route_table_t* rt, char const* ep_name );
136 static int uta_epsock_byname( route_table_t* rt, char* ep_name );
137 static int uta_epsock_rr( route_table_t *rt, int mtype, int group, int* more );
138 static void read_static_rt( uta_ctx_t* ctx, int vlevel );
139 static void parse_rt_rec( uta_ctx_t* ctx, char* buf, int vlevel );
140 static void* rtc( void* vctx );
141 static endpoint_t* rt_ensure_ep( route_table_t* rt, char const* ep_name );
142
143
144 // --- tools_static protos ------------------
145 static int uta_tokenise( char* buf, char** tokens, int max, char sep );
146 static char* uta_h2ip( char const* hname );
147 static int uta_link2( char* target );
148 static int uta_lookup_rtg( uta_ctx_t* ctx );
149 static int uta_has_str( char const* buf, char const* str, char sep, int max );
150 */
151
152 static int rt_link2_ep( endpoint_t* ep );
153 static rmr_mbuf_t* send2ep( uta_ctx_t* ctx, endpoint_t* ep, rmr_mbuf_t* msg );
154
155 #endif