libflute
Classes | Functions | Variables
flute-receiver.cpp File Reference
#include <cstdio>
#include <iostream>
#include <argp.h>
#include <cstdlib>
#include <fstream>
#include <string>
#include <filesystem>
#include <libconfig.h++>
#include <boost/asio.hpp>
#include "spdlog/async.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/syslog_sink.h"
#include "Version.h"
#include "Receiver.h"
#include "File.h"
Include dependency graph for flute-receiver.cpp:

Go to the source code of this file.

Classes

struct  ft_arguments
 Holds all options passed on the command line. More...
 

Functions

static void print_version (FILE *stream, struct argp_state *)
 Print the program version in MAJOR.MINOR.PATCH format. More...
 
static auto parse_opt (int key, char *arg, struct argp_state *state) -> error_t
 Parses the command line options into the arguments struct. More...
 
auto main (int argc, char **argv) -> int
 Main entry point for the program. More...
 

Variables

void(* argp_program_version_hook )(FILE *, struct argp_state *) = print_version
 
const char * argp_program_bug_address = "Austrian Broadcasting Services <obeca@ors.at>"
 
static char doc [] = "FLUTE/ALC receiver demo"
 
static struct argp_option options []
 
static struct argp argp
 

Function Documentation

◆ main()

auto main ( int  argc,
char **  argv 
) -> int

Main entry point for the program.

Parameters
argcCommand line agument count
argvCommand line arguments
Returns
0 on clean exit, -1 on failure

Definition at line 129 of file flute-receiver.cpp.

129  {
130  struct ft_arguments arguments;
131  /* Default values */
132  arguments.mcast_target = "238.1.1.95";
133  arguments.flute_interface= "0.0.0.0";
134 
135  // Parse the arguments
136  argp_parse(&argp, argc, argv, 0, nullptr, &arguments);
137 
138  // Set up logging
139  std::string ident = "flute-receiver";
140  auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID | LOG_PERROR | LOG_CONS );
141 
142  spdlog::set_level(
143  static_cast<spdlog::level::level_enum>(arguments.log_level));
144  spdlog::set_pattern("[%H:%M:%S.%f %z] [%^%l%$] [thr %t] %v");
145 
146  spdlog::set_default_logger(syslog_logger);
147  spdlog::info("FLUTE receiver demo starting up");
148 
149  try {
150  // Create a Boost io_context
151  boost::asio::io_context io;
152 
153  // Create the receiver
154  LibFlute::Receiver receiver(
155  arguments.flute_interface,
156  arguments.mcast_target,
157  (short)arguments.mcast_port,
158  arguments.tsi,
159  io);
160 
161  // Configure IPSEC, if enabled
162  if (arguments.enable_ipsec)
163  {
164  receiver.enable_ipsec(1, arguments.aes_key);
165  }
166 
167  receiver.register_completion_callback(
168  [output_path = arguments.output_path](std::shared_ptr<LibFlute::File> file) { //NOLINT
169  std::string out_file = file->meta().content_location;
170  if (output_path && std::strlen(output_path) > 0) {
171  out_file = (std::filesystem::path(output_path) / std::filesystem::path(out_file).filename()).string();
172  }
173 
174  spdlog::info("{} (TOI {}) has been received",
175  out_file, file->meta().toi);
176  FILE *fd = fopen(out_file.c_str(), "wb");
177  fwrite(file->buffer(), 1, file->length(), fd);
178  fclose(fd);
179  });
180 
181  // Start the IO service
182  io.run();
183  } catch (std::exception ex ) {
184  spdlog::error("Exiting on unhandled exception: %s", ex.what());
185  }
186 
187 exit:
188  return 0;
189 }
FLUTE receiver class.
Definition: Receiver.h:29
static struct argp argp
Holds all options passed on the command line.
const char * mcast_target
const char * output_path

◆ parse_opt()

static auto parse_opt ( int  key,
char *  arg,
struct argp_state *  state 
) -> error_t
static

Parses the command line options into the arguments struct.

Definition at line 77 of file flute-receiver.cpp.

77  {
78  auto arguments = static_cast<struct ft_arguments *>(state->input);
79  switch (key) {
80  case 'm':
81  arguments->mcast_target = arg;
82  break;
83  case 'i':
84  arguments->flute_interface = arg;
85  break;
86  case 'k':
87  arguments->aes_key = arg;
88  arguments->enable_ipsec = true;
89  break;
90  case 'p':
91  arguments->mcast_port = static_cast<unsigned short>(strtoul(arg, nullptr, 10));
92  break;
93  case 'l':
94  arguments->log_level = static_cast<unsigned>(strtoul(arg, nullptr, 10));
95  break;
96  case 'T':
97  arguments->tsi = static_cast<uint64_t>(strtoul(arg, nullptr, 10));
98  break;
99  case 'o':
100  arguments->output_path = arg;
101  break;
102  default:
103  return ARGP_ERR_UNKNOWN;
104  }
105  return 0;
106 }

◆ print_version()

void print_version ( FILE *  stream,
struct argp_state *  state 
)
static

Print the program version in MAJOR.MINOR.PATCH format.

Definition at line 114 of file flute-receiver.cpp.

114  {
115  fprintf(stream, "%s.%s.%s\n", std::to_string(VERSION_MAJOR).c_str(),
116  std::to_string(VERSION_MINOR).c_str(),
117  std::to_string(VERSION_PATCH).c_str());
118 }

Variable Documentation

◆ argp

struct argp argp
static
Initial value:
= {options, parse_opt, nullptr, doc,
nullptr, nullptr, nullptr}
static struct argp_option options[]
static auto parse_opt(int key, char *arg, struct argp_state *state) -> error_t
Parses the command line options into the arguments struct.
static char doc[]

Definition at line 77 of file flute-receiver.cpp.

◆ argp_program_bug_address

const char* argp_program_bug_address = "Austrian Broadcasting Services <obeca@ors.at>"

Definition at line 43 of file flute-receiver.cpp.

◆ argp_program_version_hook

void(* argp_program_version_hook) (FILE *, struct argp_state *) ( FILE *  ,
struct argp_state *   
) = print_version

Definition at line 42 of file flute-receiver.cpp.

◆ doc

char doc[] = "FLUTE/ALC receiver demo"
static

Definition at line 44 of file flute-receiver.cpp.

◆ options

struct argp_option options[]
static
Initial value:
= {
{"interface", 'i', "IF", 0, "IP address of the interface to bind flute receivers to (default: 0.0.0.0)", 0},
{"target", 'm', "IP", 0, "Multicast address to receive on (default: 238.1.1.95)", 0},
{"port", 'p', "PORT", 0, "Multicast port (default: 40085)", 0},
{"ipsec-key", 'k', "KEY", 0, "To enable IPSec/ESP decryption of packets, provide a hex-encoded AES key here", 0},
{"log-level", 'l', "LEVEL", 0,
"Log verbosity: 0 = trace, 1 = debug, 2 = info, 3 = warn, 4 = error, 5 = "
"critical, 6 = none. Default: 2.",
0},
{"tsi", 'T', "ID", 0, "The TSI to use for the FLUTE session (default: 16)", 0},
{"output-path", 'o', "PATH", 0, "Directory to save received files", 0},
{nullptr, 0, nullptr, 0, nullptr, 0}}

Definition at line 44 of file flute-receiver.cpp.