Generated C files for E2AP v2.0
[sim/e2-interface.git] / e2sim / previous / test / Pendulum / e2sim_test_client.c
diff --git a/e2sim/previous/test/Pendulum/e2sim_test_client.c b/e2sim/previous/test/Pendulum/e2sim_test_client.c
deleted file mode 100644 (file)
index b178ff7..0000000
+++ /dev/null
@@ -1,378 +0,0 @@
-/*\r
- *\r
- * Copyright 2019 AT&T Intellectual Property\r
- * Copyright 2019 Nokia\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *\r
- */\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <time.h>\r
-#include <unistd.h>            //for close()\r
-#include <stdlib.h>\r
-#include <sys/socket.h>\r
-#include <netinet/in.h>\r
-#include <netinet/sctp.h>\r
-#include <arpa/inet.h> //for inet_ntop()\r
-#include <assert.h>\r
-\r
-#include "e2sim_defs.h"\r
-#include "e2sim_sctp.h"\r
-#include "x2ap_message_handler.h"\r
-\r
-#include "x2ap_generate_messages.h"\r
-\r
-//OSN 2019\r
-#include "Pendulum_asn_codec.h"\r
-\r
-//rmr\r
-#include <errno.h>\r
-#include <sys/epoll.h>\r
-#include <rmr/rmr.h>\r
-#include "rmr_wrapper.h"\r
-\r
-//time\r
-#include <sys/time.h>\r
-\r
-//these are the metrics being sent to the a1 mediator\r
-int ave_ric_rtt_last_epoch=0;\r
-int ave_msg_rate_last_epoch=0;\r
-int ave_pendulum_msg_rate_last_epoch=0;\r
-\r
-int total_rtt_current_epoch=0;\r
-int total_messages_current_epoch=0;\r
-int total_pendulum_messages_current_epoch=0;\r
-\r
-int total_rtt_entries_current_epoch=0;\r
-\r
-int epoch_duration = 1;//in seconds\r
-\r
-long rtt_epoch_start_time =0;\r
-\r
-long msg_rate_epoch_start_time =0;\r
-long pendulum_msg_rate_epoch_start_time = 0;\r
-\r
-long current_timestamp_in_us(){\r
-       struct timeval currentTime;\r
-       gettimeofday(&currentTime, NULL);\r
-       return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec;\r
-}\r
-void update_rtt_metrics(long rtt){//called every time there is a new rtt measurement\r
-       if(rtt_epoch_start_time == 0)\r
-               rtt_epoch_start_time = current_timestamp_in_us(); //start of a new epoch\r
-\r
-       total_rtt_current_epoch = total_rtt_current_epoch+rtt;\r
-       total_rtt_entries_current_epoch++;\r
-\r
-       if((current_timestamp_in_us() - rtt_epoch_start_time) > (epoch_duration*1000000)){//an epoch has passed\r
-               ave_ric_rtt_last_epoch = total_rtt_current_epoch/total_rtt_entries_current_epoch;\r
-               total_rtt_current_epoch =0;\r
-               rtt_epoch_start_time = 0;\r
-       }\r
-}\r
-\r
-void update_msg_rate_metrics(){\r
-       if(msg_rate_epoch_start_time == 0)\r
-               msg_rate_epoch_start_time= current_timestamp_in_us(); //start of a new epoch\r
-       total_messages_current_epoch++;\r
-       if((current_timestamp_in_us() - msg_rate_epoch_start_time) > (epoch_duration*1000000)){//an epoch has passed\r
-               ave_msg_rate_last_epoch = total_messages_current_epoch;\r
-               total_messages_current_epoch =0;\r
-               msg_rate_epoch_start_time =0;\r
-       }\r
-}\r
-\r
-void update_pendulum_control_rate()\r
-{\r
-       if(pendulum_msg_rate_epoch_start_time == 0)\r
-               pendulum_msg_rate_epoch_start_time = current_timestamp_in_us(); //start of a new epoch\r
-       total_pendulum_messages_current_epoch++;\r
-\r
-       if((current_timestamp_in_us() - pendulum_msg_rate_epoch_start_time) > (epoch_duration*1000000)){//an epoch has passed\r
-               ave_pendulum_msg_rate_last_epoch = total_pendulum_messages_current_epoch;\r
-               total_pendulum_messages_current_epoch = 0;\r
-               pendulum_msg_rate_epoch_start_time = 0;\r
-       }\r
-\r
-}\r
-\r
-void send_metrics_to_a1_med(struct rmr_context *rmr_c){\r
-        int mtype=103;\r
-        char* metrics= malloc(1024);\r
-        int time_int = current_timestamp_in_us()/1000000;\r
-        //int ave_msg_rate_last_epoch=500;\r
-        snprintf(metrics, 1024, "{%s:%d, %s:%d, %s:%d, %s:%d}", \\r
-                                                                               "\"latency\"", ave_ric_rtt_last_epoch/1000, \\r
-                                                                               "\"ricload\"",ave_msg_rate_last_epoch, \\r
-                                                                               "\"load\"",ave_pendulum_msg_rate_last_epoch, \\r
-                                                                               "\"time\"",time_int);\r
-        rmr_send_wrapper(rmr_c, mtype, metrics);\r
-        printf("Sent message of type:%d to a1_med with content:%s\n",mtype,metrics);\r
-}\r
-\r
-void forward_to_load_consumer(struct rmr_context *rmr_c){//the content does not matter\r
-        int mtype=105;\r
-        char* load_message="dummy load";\r
-        rmr_send_wrapper(rmr_c, mtype, load_message);\r
-        printf("Sent message of type:%d to load consumer with content:%s\n",mtype,load_message);\r
-}\r
-\r
-static void pendulum_control_E2_Termination(int client_fd)\r
-{\r
-  printf("--------------------------------------\n");\r
-  printf("E2 TERMINATION - START PENDULUM CONTROL\n");\r
-  printf("--------------------------------------\n");\r
-\r
-  uint8_t *send_buffer;\r
-  uint8_t recv_buffer[1024];\r
-  uint32_t send_len;\r
-  uint32_t recv_len;\r
-\r
-  clock_t begin = clock();\r
-  double rtt;\r
-  double rtt_stats[100000];\r
-  long   recv_count = 0;\r
-  long   fail_count = -1; //ignore the first message (see adruino code)\r
-\r
-  long   sqn;\r
-  int    count = 0;\r
-\r
-  //=================================\r
-\r
-  //Setup context\r
-  struct rmr_context *rmr_c; //obtain our enhanced rmr_context\r
-  int  mtype = 0;                                              // we can loop through several message types\r
-  char*        lport = "43086";                                // default listen port\r
-  long rcount = 0;                                             // number of acks received\r
-\r
-  if( (eparm = getenv( "E2TERM_RMR_RCV_PORT" )) != NULL ) {\r
-                lport = strdup( eparm );\r
-  }\r
-\r
-  rmr_c = rmr_init_wrapper(lport);\r
-\r
-  while( ! rmr_ready( rmr_c->mrc ) ) {\r
-    fprintf( stderr, "<TEST> waiting for RMR to indicate ready\n" );\r
-    sleep( 1 );\r
-  }\r
-  fprintf( stderr, "[OK]   initialisation complete\n" );\r
-\r
-\r
-  //==================================\r
-  long loop_start_time = 0;\r
-  while(1){\r
-    printf("----------------\n");\r
-    count += 1;\r
-    loop_start_time = current_timestamp_in_us();\r
-    //0. Receiving ASN message from E2 Agent\r
-    memset(recv_buffer, 0, sizeof(recv_buffer));\r
-    printf("Time to receive asn message after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-    recv_len = 0;\r
-\r
-    printf(" 1Time to receive asn message after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-    //long time_of_message_from_e2agent = current_timestamp_in_us();\r
-\r
-    if((recv_len = recv(client_fd, &recv_buffer, sizeof(recv_buffer), 0)) == -1) {\r
-        perror("recv");\r
-        return;\r
-    }\r
-\r
-    long time_of_message_from_e2agent = current_timestamp_in_us();\r
-\r
-    printf(" 2Time to receive asn message after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-    if(recv_len == 0) {\r
-        rmr_close_wrapper(rmr_c);\r
-\r
-        printf("Connection from closed by remote peer.\n");\r
-        if(close(client_fd) == -1) {\r
-            perror("close");\r
-        }\r
-        return;\r
-    }\r
-\r
-    printf(" 3Time to receive asn message after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-    // begin = clock() - begin;\r
-    // rtt = 1000*((double)begin)/CLOCKS_PER_SEC; // in ms\r
-    //printf("E2Term-Adruino-E2Term = %f ms\n", rtt);\r
-\r
-    //2. Decode ASN message and Extract pendulum angle\r
-    char *recv_str;\r
-    recv_str = pendulum_get_strval(recv_buffer, recv_len);\r
-    // if( (strcmp(recv_str, "-1") == 0) || (strcmp(recv_str, "") == 0) )\r
-\r
-    if(strcmp(recv_str, "\n") == 0)\r
-    {\r
-      printf("RECEIVED EOL\n");\r
-    }\r
-\r
-    printf(" 4Time to receive asn message after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-    // if(atof(recv_str) <= 0)\r
-    // {\r
-    //   printf("FAILLLLLL\n");\r
-    //   fail_count += 1;\r
-    // }\r
-    // else {\r
-    //   rtt_stats[recv_count] = atof(recv_str);\r
-    //   recv_count++;\r
-    // }\r
-\r
-    printf("Time to receive angle message from arduino after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-    printf("Received message #%d from Adruino: %s\n", count, recv_str);\r
-    //printf("Last reported RTT (Adruino-RIC-Adruino): %f ms, fail_count = %ld\n",\r
-    // atof(recv_str)/1000, fail_count);\r
-\r
-    // 3. [BHARATH] send_to_xApp(&pendulum_state, &response)\r
-    // while(1) {\r
-    // usleep( 10 );\r
-    // char* message = "foo 111";\r
-    char reply[1024];\r
-    int got_pend_control=0;\r
-    rmr_send_wrapper(rmr_c, mtype, recv_str );\r
-    printf("Sent message of type:%d to pendulum xApp with content:%s\n",mtype,recv_str);\r
-    long angle_receive_time =0;\r
-    while (got_pend_control == 0){\r
-           if(rmr_poll_for_message(rmr_c) == 1) {\r
-                   update_msg_rate_metrics();\r
-                   switch(rmr_c->rbuf->mtype) {\r
-                           case 33 :\r
-                                   angle_receive_time = current_timestamp_in_us();\r
-                                   got_pend_control=1;\r
-                                   update_pendulum_control_rate(); //add this\r
-                                   strcpy(reply,rmr_c->rbuf->payload);\r
-                                   printf("Received control message from pendulum xapp with message type: %d and content %s\n",rmr_c->rbuf->mtype, reply);\r
-                                   break;\r
-                           case 102 :\r
-                                   printf("Received METRIC request from A1 mediator with message type: %d and content %s\n",rmr_c->rbuf->mtype,rmr_c->rbuf->payload);\r
-                                   send_metrics_to_a1_med(rmr_c);\r
-                                   break;\r
-                           case 104 :\r
-                                   printf("***************************Received load from load generator****************************");\r
-                                   forward_to_load_consumer(rmr_c);\r
-                                   break;\r
-                           default :\r
-                                   continue;\r
-                   }\r
-           }\r
-\r
-    }\r
-    printf("Time to receive control message from xapp after starting main loop: %ld microseconds \n",current_timestamp_in_us() - loop_start_time);\r
-//    snprintf(reply, 1024, "$%d#\n", (int)ave_ric_rtt_last_epoch/1000);\r
-    send_len = pendulum_create_asn_msg(&send_buffer, 0, 0, 0, reply);\r
-    printf("Time to create asn message after receiving angle: %ld microseconds\n",current_timestamp_in_us() - angle_receive_time);\r
-    begin = clock();\r
-\r
-    //6. Send ASN1 message to socket\r
-    if(sctp_send_to_socket(client_fd, send_buffer, (size_t)send_len) > 0){\r
-      printf("Sent ASN1 response to E2 Agent\n");\r
-    }\r
-    long time_of_reply_to_e2agent = current_timestamp_in_us();\r
-    printf("Time to send asn message after receiving angle: %ld microseconds \n",current_timestamp_in_us() - angle_receive_time);\r
-    long rtt = (time_of_reply_to_e2agent - time_of_message_from_e2agent);\r
-    ave_ric_rtt_last_epoch = rtt;\r
-    printf("RIC RTT is %lf milliseconds\n", rtt/1000.0);\r
-    //update_rtt_metrics(rtt);\r
-  }\r
-\r
-  rmr_close_wrapper(rmr_c);\r
-\r
-}\r
-\r
-int main(int argc, char* argv[])\r
-{\r
-  // test_rmr(); return 0;\r
-\r
-  const char* server_ip   = DEFAULT_SCTP_IP;\r
-  int server_port         = X2AP_SCTP_PORT;\r
-\r
-  //read input\r
-  if(argc == 3) //user provided IP and PORT\r
-  {\r
-    server_ip = argv[1];\r
-    server_port = atoi(argv[2]);\r
-    if(server_port < 1 || server_port > 65535) {\r
-      printf("Invalid port number (%d). Valid values are between 1 and 65535.\n", server_port);\r
-      return 1;\r
-    }\r
-  }\r
-  else if(argc == 2) //user provided only IP\r
-  {\r
-    server_ip = argv[1];\r
-  }\r
-  else if(argc == 1)\r
-  {\r
-    server_ip = DEFAULT_SCTP_IP;\r
-  }\r
-  else\r
-  {\r
-    printf("Unrecognized option.\n");\r
-    printf("Usage: %s [SERVER IP ADDRESS] [SERVER PORT]\n", argv[0]);\r
-    return 0;\r
-  }\r
-\r
-  int client_fd;\r
-  client_fd = sctp_start_client(server_ip, server_port);\r
-\r
-  uint8_t *buffer;\r
-  uint32_t  len;\r
-\r
-  //Note: put a while(1) loop here if want client to stay\r
-  // for(int i = 0; i < 3; i++)\r
-  // {\r
-    buffer = NULL;\r
-    len = 0;\r
-\r
-    printf("------------------------\n");\r
-    clock_t begin;\r
-    begin = clock();\r
-\r
-    //Create pdu for x2 message and send to socket\r
-    len = x2ap_generate_x2_setup_request(&buffer);\r
-    if(sctp_send_to_socket(client_fd, buffer, (size_t)len) > 0){\r
-      printf("Sent X2 SETUP REQUEST\n");\r
-    }\r
-\r
-    //=======================================================================\r
-    //printf("waiting for server response\n");\r
-    uint8_t recv_buf[MAX_SCTP_BUFFER];\r
-    int recv_len = 0;\r
-\r
-    //sctp_recv_from_socket(client_fd, recv_buf, sizeof(recv_buf));\r
-    memset(recv_buf, 0, sizeof(recv_buf));\r
-    recv_len = recv(client_fd, &recv_buf, sizeof(recv_buf), 0);\r
-    if(recv_len == -1)\r
-    {\r
-      perror("recv()");\r
-      return -1;\r
-    }\r
-\r
-    //printf("Received a message of size %d\n", recv_len);\r
-    x2ap_eNB_handle_message(recv_buf, recv_len, NULL);\r
-\r
-    begin = clock() - begin;\r
-    double time_taken = 1000*((double)begin)/CLOCKS_PER_SEC; // in ms\r
-    printf("Close-loop time: %f ms \n", time_taken);\r
-    printf("X2 Setup Completed \n");\r
-\r
-  // } //end iteration\r
-\r
-  //=========================================================================\r
-  // Pendulum interaction\r
-  // Receive pendulum state from E2 Agent and send response\r
-  pendulum_control_E2_Termination(client_fd);\r
-\r
-  close(client_fd);\r
-\r
-  return 0;\r
-}\r