LibMISB/Examples/Example Application: Difference between revisions

mNo edit summary
Line 55: Line 55:
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <iostream>
#include <string>
#include <libmisb/libmisb.hpp>
#include "libmisb/formatter/jsonformatter.hpp"


#include <arpa/inet.h>
#include <arpa/inet.h>
Line 91: Line 95:
}
}


char *converter_caller(const char *json_path){
std::string converter(const char *json_path){
  int pipefd[2];
  char buffer[4096];
    pid_t child_pid;


    if (pipe(pipefd) == -1) {
  /* Initialize MISB object*/
        perror("pipe");
  std::string input_file;
        exit(EXIT_FAILURE);
  libmisb::LibMisb libmisb;
    }
  libmisb.SetDebuggerLever(LIBMISB_DEBUG);


     child_pid = fork();
  std::shared_ptr<libmisb::formatter::iFormatter> json_formatter =
     std::make_shared<libmisb::formatter::JsonFormatter>();
  libmisb.SetFormatter(json_formatter);


    if (child_pid == -1) {
  input_file = json_path;
        perror("fork");
 
        exit(EXIT_FAILURE);
  std::ifstream file(input_file);
    }
  std::string data;
  std::stringstream buffer;


    if (access(json_path, F_OK) != 0) {
  if (file.is_open()) {
        printf("JSON File '%s' does not exist.\n", json_path);
    buffer << file.rdbuf();
        exit(EXIT_FAILURE);
    data = buffer.str();
    }  
    file.close();
  }


    if (child_pid == 0) { // Child process
  std::vector<unsigned char> packet_encoded = libmisb.Encode(data);
        close(pipefd[0]); // Close the read end of the pipe
  if (packet_encoded.empty()) {
    LOGGER.Log("Encode result packet is empty", LIBMISB_ERROR);
  }
  std::string string_packet = "";
  std::string string_byte;


        // Redirect stdout to the write end of the pipe
    for (uint i = 0; i < packet_encoded.size(); i++) {
        dup2(pipefd[1], STDOUT_FILENO);
      string_byte = std::to_string(packet_encoded[i]);
 
      string_packet += string_byte;
        // Execute the command
      string_packet += " ";
        execlp("./misb-converter", "./misb-converter", "--verbose", "--encode", "-i", json_path, "-o", "klv.bin", NULL);
 
        // If execlp fails
        perror("execlp");
        exit(EXIT_FAILURE);
    } else { // Parent process
        close(pipefd[1]); // Close the write end of the pipe
 
        // Read the output from the read end of the pipe
        ssize_t bytesRead = read(pipefd[0], buffer, sizeof(buffer));
 
        if (bytesRead == -1) {
            perror("read");
            exit(EXIT_FAILURE);
        }
 
        // Null-terminate the string
        buffer[bytesRead] = '\0';
 
        wait(NULL);
    }
 
    char* result = strdup(buffer);
    if (result == NULL) {
        perror("strdup");
        exit(EXIT_FAILURE);
     }
     }
 
    LOGGER.Log(string_packet, LIBMISB_INFO);
    return result;
  return string_packet;
}
}


int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
Line 161: Line 143:
   printf("JSON Path: %s\n", json_path);
   printf("JSON Path: %s\n", json_path);


 
   GstMetaDemo *metademo = static_cast<GstMetaDemo*>(g_malloc(sizeof(GstMetaDemo)));
   GstMetaDemo *metademo = g_malloc(sizeof(GstMetaDemo));
   if(!metademo){
   if(!metademo){
     g_print("Could not create demo\n");
     g_print("Could not create demo\n");
Line 182: Line 163:
     g_print("Usage: %s <IP address> <Port number> <JSON file path>\n", argv[0]);
     g_print("Usage: %s <IP address> <Port number> <JSON file path>\n", argv[0]);
   }
   }
   char *output_converter = converter_caller(json_path);
 
  std::string string_converter = converter(json_path);
  const int length = string_converter.length();
   char* output_converter = new char[length + 1];
 
  strcpy(output_converter, string_converter.c_str());  
   
   
   if (!create_pipeline(metademo, ip, port, output_converter)) {
   if (!create_pipeline(metademo, ip, port, output_converter)) {
Line 260: Line 246:


   // Use snprintf to format the string with the new values
   // Use snprintf to format the string with the new values
   memmove(output_converter, output_converter + 14, strlen(output_converter) - 14 + 1);
   output_converter[strlen(output_converter) - 1] = '\0';
  output_converter[strlen(output_converter) - 2] = '\0';
   snprintf(modified_pipeline, sizeof(modified_pipeline), actual_pipeline, output_converter, ip, port);
   snprintf(modified_pipeline, sizeof(modified_pipeline), actual_pipeline, output_converter, ip, port);


Line 277: Line 262:
   /*We need to copy the array since the GByteArray will be the new owner and free it for us*/
   /*We need to copy the array since the GByteArray will be the new owner and free it for us*/
   metalen = sizeof(klv_metadata);
   metalen = sizeof(klv_metadata);
   array_copy = g_malloc (metalen);
   array_copy = static_cast<guint8*>(g_malloc(metalen));
   memcpy (array_copy, klv_metadata, metalen);
   memcpy (array_copy, klv_metadata, metalen);