02cea30cb007545cf1aeb6f3a11f630068a7599a
[ric-plt/xapp-frame-cpp.git] / src / xapp / xapp.cpp
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         Mnemonic:       xapp.cpp
23         Abstract:       The main xapp class.
24                                 This class actually extends the messenger class as most of what
25                                 would be done here is just passing through to the messenger.
26
27         Date:           13 March 2020
28         Author:         E. Scott Daniels
29 */
30
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <rmr/rmr.h>
35 #include <rmr/RIC_message_types.h>
36
37
38 #include <iostream>
39 #include <thread>
40
41 #include <messenger.hpp>
42 #include "xapp.hpp"
43
44 // --------------- private -----------------------------------------------------
45
46
47
48 // --------------- builders -----------------------------------------------
49
50 /*
51         If wait4table is true, then the construction of the object does not
52         complete until the underlying transport has a new copy of the route
53         table.
54
55         If port is nil, then the default port is used (4560).
56 */
57 Xapp::Xapp( char* port, bool wait4table ) : Messenger( port, wait4table ) {
58         // what's left to do?
59 }
60
61 /*
62         Destroyer.
63 */
64 Xapp::~Xapp() {
65         // nothing to destroy; superclass destruction is magically invoked.
66 }
67
68 /*
69         Start n threads, each running a listener which will drive callbacks.
70
71         The final listener is started in the main thread; in otherwords this
72         function won't return unless that listener crashes.
73 */
74 void Xapp::Run( int nthreads ) {
75         int joined;                                             // at end, number of threads joined back
76         int i;
77         std::thread** tinfo;                            // each thread we'll start
78
79         tinfo = new std::thread* [nthreads-1];
80
81         for( i = 0; i < nthreads - 1; i++ ) {                           // thread for each n-1; last runs here
82                 tinfo[i] = new std::thread( &Xapp::Listen, this );
83         }
84
85         this->Listen();                                         // will return only when halted
86
87         for( i = 0; i < nthreads - 1; i++ ) {                           // wait for others to stop
88                 tinfo[i]->join();
89         }
90 }
91
92 /*
93         Halt the xapp. This will drive the messenger's stop function to prevent any
94         active listeners from running, and will shut things down.
95 */
96 void Xapp::Halt() {
97         this->Stop();                   // messenger shut off
98 }
99