Add API allowing xAPPs to send alarm messages
[ric-plt/xapp-frame-cpp.git] / src / messaging / messenger.hpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2020 Nokia
5         Copyright (c) 2020 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 /*
23         Mnemonic:       messenger.hpp
24         Abstract:       Headers for the messenger class
25
26         Date:           10 March 2020
27         Author:         E. Scott Daniels
28 */
29
30 #ifndef _messenger_hpp
31 #define _messenger_hpp
32
33
34 #include <iostream>
35 #include <string>
36 #include <map>
37 #include <memory>
38 #include <mutex>
39
40 #include <rmr/rmr.h>
41
42 #include "message.hpp"
43 #include "alarm.hpp"
44
45 #ifndef RMR_FALSE
46         #define RMR_FALSE       0
47         #define RMR_TRUE        1
48 #endif
49
50 namespace xapp {
51
52
53 class Messenger {
54
55         private:
56                 std::map<int,Callback*> cb_hash;        // callback functions associated with message types
57                 std::mutex*     gate;                                   // overall mutex should we need searialisation
58                 bool            ok_2_run;
59                 bool            callbacks = false;              // true if callbacks are defined
60                 void*           mrc;                                    // message router context
61                 char*           listen_port;                    // port we ask msg router to listen on
62
63                 // copy and assignment are PRIVATE so that they fail if xapp tries; messenger cannot be copied!
64                 Messenger( const Messenger& soi );
65                 Messenger& operator=( const Messenger& soi );
66
67         public:
68                 // -- constants which need an instance; that happens as a global in the .cpp file (wtf C++)
69                 static const int MAX_PAYLOAD;                   // max message size we'll handle
70                 static const int DEFAULT_CALLBACK;              // parm for add callback to set default
71
72                 Messenger( const char* port, bool wait4table ); // builder
73                 Messenger( Messenger&& soi );                           // move construction
74                 Messenger& operator=( Messenger&& soi );        // move operator
75                 ~Messenger();                                                           // destroyer
76
77                 void Add_msg_cb( int mtype, user_callback fun_name, void* data );
78
79                 std::unique_ptr<Message> Alloc_msg( int payload_size );                 // message allocation
80
81                 std::unique_ptr<xapp::Alarm> Alloc_alarm( );                                    // alarm allocation
82                 std::unique_ptr<xapp::Alarm> Alloc_alarm( std::string meid );
83                 std::unique_ptr<xapp::Alarm> Alloc_alarm( int prob_id, std::string meid );
84
85                 void Listen( );                                                                                                 // lisen driver
86                 std::unique_ptr<Message> Receive( int timeout );                                // receive 1 message
87                 void Stop( );                                                                                                   // force to stop
88                 bool Wait_for_cts( int max_wait );
89
90                 int     Wormhole_open( std::string endpoint );
91 };
92
93
94 } // namespace
95 #endif