Add initial codes
[it/test.git] / ons_2019_demo / a1_med / dummy_a1_med.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // :vim ts=4 sw=4 noet:
21
22 /*
23         Mnemonic:       rmr_sender2.c
24         Abstract:       Very simple test sender that polls and deals with responses
25                                 in between sends (from a single process).
26
27         Date:           18 February 2018
28         Author:         E. Scott Daniels
29
30         Modified:       18 Mar 2019 - changes to support demo
31 */
32
33 #include <unistd.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <sys/epoll.h>
39 #include <time.h>
40 #include <rmr/rmr.h>
41 #include "dummy_a1_rmr_wrapper.h"
42
43
44 void usage( char* argv0 ) {
45         fprintf( stderr, "usage: %s [mtype-max]\n", argv0 );
46         fprintf( stderr, "Sender will send messages with rotating msg types from 0 through mtype-max (if supplied)\n" );
47         fprintf( stderr, "if not supplied, only mtype 0 is sent\n" );
48         fprintf( stderr, "The default listen port for return messages is 43086; this can be changed by setting DUMMY_SENDER_RMR_RCV_PORT  in the environment.\n" );
49         fprintf( stderr, "The sender will send forever unless DEMO_SENDER_MAX is set in the environment which causes termination after max messages.\n" );
50         fprintf( stderr, "The sender will poll for received messages after each send. The amount of time waited is controlled with DEMO_SENDER_PTO (ms) in the env. Use 0 for non-blocking poll.\n" );
51 }
52
53 void send_delay_message(struct rmr_context *rmr_c, char* delay_file_path){
54         FILE *fp;
55         char delay[255];
56
57         fp = fopen(delay_file_path, "r");
58         fscanf(fp, "%s", delay);
59         int mtype = 100;
60         rmr_pure_send(rmr_c, mtype, delay);
61         fprintf( stderr, "Sent delay insert message of type:%d (delay insert) to pendulum control xApp, with content:%s\n",mtype,delay);
62         fclose(fp);
63 }
64
65 void send_and_rec_metrics(struct rmr_context *rmr_c, char* metrics_file_path){
66         int mtype=102;
67         char* message="--give metrics--";
68         rmr_pure_send(rmr_c, mtype, message );
69         printf("Sent message of type:%d to E2 terminator with content:%s\n",mtype,message);
70         int got_metrics=0;
71         while (got_metrics == 0){
72                 if(rmr_poll_for_message(rmr_c) == 1) {
73                         if(rmr_c->rbuf->mtype == 103)
74                                 got_metrics=1;
75                 }
76         }
77         printf("Recieved metrics from E2 terminator with content:%s\n",rmr_c->rbuf->payload);
78
79         FILE *fp;
80         fp = fopen(metrics_file_path, "w+");
81         fprintf(fp,"%s",rmr_c->rbuf->payload);
82         fclose(fp);
83
84 }
85 int main( int argc, char** argv ) {
86         struct rmr_context *rmr_c; //obtain our enhanced rmr_context
87         char*   lport = "43086";                                // default listen port
88         char* delay_file_path ="";
89         char* metrics_file_path ="";
90         if( (eparm = getenv( "DUMMY_SENDER_RMR_RCV_PORT" )) != NULL ) {
91                 lport = strdup( eparm );
92         }
93
94         if( (eparm = getenv( "DELAY_FILE_PATH" )) != NULL ) {
95                 delay_file_path = eparm ;
96         }
97
98         if( (eparm = getenv( "METRICS_FILE_PATH" )) != NULL ) {
99                 metrics_file_path = eparm ;
100         }
101
102         rmr_c = rmr_init_wrapper(lport);
103
104         while( ! rmr_ready( rmr_c->mrc ) ) {
105                 fprintf( stderr, "<TEST> waiting for RMr to indicate ready\n" );
106                 sleep( 1 );
107         }
108         fprintf( stderr, "[OK]   initialisation complete\n" );
109         fprintf( stderr, "======================================\n[OK]   A1 mediator is up and running!\n==================================\n" );
110
111         while( 1 ) {
112                 sleep (2);
113
114                 send_delay_message(rmr_c, delay_file_path);
115
116                 send_and_rec_metrics(rmr_c, metrics_file_path);
117                 fprintf( stderr, "-------------------------------------------\n");
118         }
119
120
121
122         fprintf( stderr, "[INFO] sender is terminating\n");
123         rmr_close_wrapper(rmr_c);
124
125         return 0;
126 }