libflute
Classes | Functions | Variables
flute-transmitter.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 "Transmitter.h"
Include dependency graph for flute-transmitter.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 transmitter demo"
 
static struct argp_option options []
 
static char args_doc [] = "[FILE...]"
 
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 117 of file flute-transmitter.cpp.

117  {
118  struct ft_arguments arguments;
119  /* Default values */
120  arguments.mcast_target = "238.1.1.95";
121 
122  argp_parse(&argp, argc, argv, 0, nullptr, &arguments);
123 
124  // Set up logging
125  std::string ident = "flute-transmitter";
126  auto syslog_logger = spdlog::syslog_logger_mt("syslog", ident, LOG_PID | LOG_PERROR | LOG_CONS );
127 
128  spdlog::set_level(
129  static_cast<spdlog::level::level_enum>(arguments.log_level));
130  spdlog::set_pattern("[%H:%M:%S.%f %z] [%^%l%$] [thr %t] %v");
131 
132  spdlog::set_default_logger(syslog_logger);
133  spdlog::info("FLUTE transmitter demo starting up");
134 
135 
136  // We're responsible for buffer management, so create a vector of structs that
137  // are going to hold the data buffers
138  struct FsFile {
139  std::string location;
140  char* buffer;
141  size_t len;
142  uint32_t toi;
143  };
144  std::vector<FsFile> files;
145 
146  // read the file contents into the buffers
147  for (int j = 0; arguments.files[j]; j++) {
148  std::string location = arguments.files[j];
149  std::ifstream file(arguments.files[j], std::ios::binary | std::ios::ate);
150  std::streamsize size = file.tellg();
151  file.seekg(0, std::ios::beg);
152 
153  char* buffer = (char*)malloc(size);
154  file.read(buffer, size);
155  files.push_back(FsFile{ arguments.files[j], buffer, (size_t)size});
156  }
157 
158  // Create a Boost io_service
159  boost::asio::io_service io;
160 
161  // Construct the transmitter class
162  LibFlute::Transmitter transmitter(
163  arguments.mcast_target,
164  arguments.mcast_port,
165  0,
166  arguments.mtu,
167  arguments.rate_limit,
168  io);
169 
170  // Configure IPSEC ESP, if enabled
171  if (arguments.enable_ipsec)
172  {
173  transmitter.enable_ipsec(1, arguments.aes_key);
174  }
175 
176  // Register a completion callback
177  transmitter.register_completion_callback(
178  [&files](uint32_t toi) {
179  for (auto& file : files) {
180  if (file.toi == toi) {
181  spdlog::info("{} (TOI {}) has been transmitted",
182  file.location, file.toi);
183  // could free() the buffer here
184  }
185  }
186  });
187 
188  // Queue all the files
189  for (auto& file : files) {
190  file.toi = transmitter.send( file.location,
191  "application/octet-stream",
192  transmitter.seconds_since_epoch() + 60, // 1 minute from now
193  file.buffer,
194  file.len
195  );
196  spdlog::info("Queued {} ({} bytes) for transmission, TOI is {}",
197  file.location, file.len, file.toi);
198  }
199 
200  // Start the io_service, and thus sending data
201  io.run();
202 
203 exit:
204  return 0;
205 }
FLUTE transmitter class.
Definition: Transmitter.h:34
static struct argp argp
Holds all options passed on the command line.
const char * mcast_target

◆ 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 63 of file flute-transmitter.cpp.

63  {
64  auto arguments = static_cast<struct ft_arguments *>(state->input);
65  switch (key) {
66  case 'm':
67  arguments->mcast_target = arg;
68  break;
69  case 'k':
70  arguments->aes_key = arg;
71  arguments->enable_ipsec = true;
72  break;
73  case 'p':
74  arguments->mcast_port = static_cast<unsigned short>(strtoul(arg, nullptr, 10));
75  break;
76  case 't':
77  arguments->mtu = static_cast<unsigned short>(strtoul(arg, nullptr, 10));
78  break;
79  case 'r':
80  arguments->rate_limit = static_cast<uint32_t>(strtoul(arg, nullptr, 10));
81  break;
82  case 'l':
83  arguments->log_level = static_cast<unsigned>(strtoul(arg, nullptr, 10));
84  break;
85  case ARGP_KEY_NO_ARGS:
86  argp_usage (state);
87  case ARGP_KEY_ARG:
88  arguments->files = &state->argv[state->next-1];
89  state->next = state->argc;
90  break;
91  default:
92  return ARGP_ERR_UNKNOWN;
93  }
94  return 0;
95 }

◆ print_version()

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

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

Definition at line 104 of file flute-transmitter.cpp.

104  {
105  fprintf(stream, "1.0.0\n");
106 }

Variable Documentation

◆ argp

struct argp argp
static
Initial value:
nullptr, nullptr, nullptr}
static char args_doc[]
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 97 of file flute-transmitter.cpp.

◆ argp_program_bug_address

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

Definition at line 31 of file flute-transmitter.cpp.

◆ argp_program_version_hook

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

Definition at line 30 of file flute-transmitter.cpp.

◆ args_doc

char args_doc[] = "[FILE...]"
static

Definition at line 97 of file flute-transmitter.cpp.

◆ doc

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

Definition at line 32 of file flute-transmitter.cpp.

◆ options

struct argp_option options[]
static
Initial value:
= {
{"target", 'm', "IP", 0, "Target multicast address (default: 238.1.1.95)", 0},
{"port", 'p', "PORT", 0, "Target port (default: 40085)", 0},
{"mtu", 't', "BYTES", 0, "Path MTU to size ALC packets for (default: 1500)", 0},
{"rate-limit", 'r', "KBPS", 0, "Transmit rate limit (kbps), 0 = no limit, default: 1000 (1 Mbps)", 0},
{"ipsec-key", 'k', "KEY", 0, "To enable IPSec/ESP encryption 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},
{nullptr, 0, nullptr, 0, nullptr, 0}}

Definition at line 32 of file flute-transmitter.cpp.