E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 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 | */ |
E. Scott Daniels | 0ac0fb4 | 2020-06-26 09:01:52 -0400 | [diff] [blame] | 57 | Xapp::Xapp( const char* port, bool wait4table ) : Messenger( port, wait4table ) { |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 58 | // nothing to do; all handled in Messenger constructor |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 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 ) { |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 75 | int i; |
| 76 | std::thread** tinfo; // each thread we'll start |
| 77 | |
| 78 | tinfo = new std::thread* [nthreads-1]; |
| 79 | |
| 80 | for( i = 0; i < nthreads - 1; i++ ) { // thread for each n-1; last runs here |
| 81 | tinfo[i] = new std::thread( &Xapp::Listen, this ); |
| 82 | } |
| 83 | |
| 84 | this->Listen(); // will return only when halted |
| 85 | |
| 86 | for( i = 0; i < nthreads - 1; i++ ) { // wait for others to stop |
| 87 | tinfo[i]->join(); |
| 88 | } |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 89 | |
E. Scott Daniels | b0c88ed | 2020-08-13 15:12:35 -0400 | [diff] [blame] | 90 | delete[] tinfo; |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
| 94 | Halt the xapp. This will drive the messenger's stop function to prevent any |
| 95 | active listeners from running, and will shut things down. |
| 96 | */ |
| 97 | void Xapp::Halt() { |
| 98 | this->Stop(); // messenger shut off |
| 99 | } |
| 100 | |