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_t2.cpp |
| 23 | Abstract: This is a simple demo xapp which controls it's own listen |
| 24 | loop (does not register callbacks and does not invoke the |
| 25 | run function in the xapp instance. |
| 26 | |
| 27 | Date: 18 March 2020 |
| 28 | Author: E. Scott Daniels |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 29 | |
| 30 | Caution: This example code is pulled directly into one or more of the documents |
| 31 | (starting from the "start-example" tag below. Use caution with |
| 32 | line lengths and contents because of the requirement that this |
| 33 | be documentation as well as working code. |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 34 | */ |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 35 | // start-example |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | |
| 41 | #include <iostream> |
| 42 | #include <memory> |
| 43 | |
| 44 | #include "ricxfcpp/xapp.hpp" |
| 45 | |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 46 | extern int main( int argc, char** argv ) { |
| 47 | std::unique_ptr<Xapp> xfw; |
E. Scott Daniels | 6ef23e1 | 2020-07-15 08:03:22 -0400 | [diff] [blame] | 48 | std::unique_ptr<xapp::Message> msg; |
| 49 | xapp::Msg_component payload; // special type of unique pointer to the payload |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 50 | |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 51 | int sz; |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 52 | int len; |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 53 | int i; |
| 54 | int ai; |
| 55 | int response_to = 0; // max timeout wating for a response |
| 56 | char* port = (char *) "4555"; |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 57 | int mtype = 0; |
| 58 | int rmtype; // received message type |
| 59 | int delay = 1000000; // mu-sec delay; default 1s |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 60 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 61 | |
| 62 | // very simple flag processing (no bounds/error checking) |
| 63 | while( ai < argc ) { |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 64 | if( argv[ai][0] != '-' ) { |
| 65 | break; |
| 66 | } |
| 67 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 68 | // we only support -x so -xy must be -x -y |
| 69 | switch( argv[ai][1] ) { |
| 70 | // delay between messages (mu-sec) |
| 71 | case 'd': |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 72 | delay = atoi( argv[ai+1] ); |
| 73 | ai++; |
| 74 | break; |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 75 | |
| 76 | case 'p': |
| 77 | port = argv[ai+1]; |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 78 | ai++; |
| 79 | break; |
| 80 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 81 | // timeout in seconds; we need to convert to ms for rmr calls |
| 82 | case 't': |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 83 | response_to = atoi( argv[ai+1] ) * 1000; |
| 84 | ai++; |
| 85 | break; |
| 86 | } |
| 87 | ai++; |
| 88 | } |
| 89 | |
| 90 | fprintf( stderr, "<XAPP> response timeout set to: %d\n", response_to ); |
| 91 | fprintf( stderr, "<XAPP> listening on port: %s\n", port ); |
| 92 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 93 | // get an instance and wait for a route table to be loaded |
| 94 | xfw = std::unique_ptr<Xapp>( new Xapp( port, true ) ); |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 95 | msg = xfw->Alloc_msg( 2048 ); |
| 96 | |
| 97 | for( i = 0; i < 100; i++ ) { |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 98 | mtype++; |
| 99 | if( mtype > 10 ) { |
| 100 | mtype = 0; |
| 101 | } |
| 102 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 103 | // we'll reuse a received message; get max size |
| 104 | sz = msg->Get_available_size(); |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 105 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 106 | // direct access to payload; add something silly |
| 107 | payload = msg->Get_payload(); |
| 108 | len = snprintf( (char *) payload.get(), sz, "This is message %d\n", i ); |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 109 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 110 | // payload updated in place, prevent copy by passing nil |
E. Scott Daniels | 6ef23e1 | 2020-07-15 08:03:22 -0400 | [diff] [blame] | 111 | if ( ! msg->Send_msg( mtype, xapp::Message::NO_SUBID, len, NULL )) { |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 112 | fprintf( stderr, "<SNDR> send failed: %d\n", i ); |
| 113 | } |
| 114 | |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 115 | // receive anything that might come back |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 116 | msg = xfw->Receive( response_to ); |
| 117 | if( msg != NULL ) { |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 118 | rmtype = msg->Get_mtype(); |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 119 | payload = msg->Get_payload(); |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 120 | fprintf( stderr, "got: mtype=%d payload=(%s)\n", |
| 121 | rmtype, (char *) payload.get() ); |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 122 | } else { |
E. Scott Daniels | 3a2533f | 2020-04-22 12:40:27 -0400 | [diff] [blame] | 123 | msg = xfw->Alloc_msg( 2048 ); |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 124 | } |
| 125 | |
E. Scott Daniels | 0b08d9d | 2020-03-27 10:18:37 -0400 | [diff] [blame] | 126 | if( delay > 0 ) { |
| 127 | usleep( delay ); |
| 128 | } |
E. Scott Daniels | 8cb3c6f | 2020-03-19 11:36:37 -0400 | [diff] [blame] | 129 | } |
| 130 | } |