Skip to content

Commit

Permalink
Merge pull request #822 from fridrich/fridrich
Browse files Browse the repository at this point in the history
Another approach to the index.html and index.js
  • Loading branch information
mpbraendli authored Nov 27, 2024
2 parents e483c52 + b249554 commit 6fcccb5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 82 deletions.
66 changes: 37 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.2)

project(Welle.Io LANGUAGES C CXX)

Expand All @@ -13,7 +13,7 @@ if(NOT WELLE-IO_VERSION)
endif()

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 14)

option(BUILD_WELLE_IO "Build Welle.io" ON )
option(BUILD_WELLE_CLI "Build welle-cli" ON )
Expand Down Expand Up @@ -77,7 +77,7 @@ endif()
if(BUILD_WELLE_CLI AND NOT ANDROID)
find_package(Lame REQUIRED)
if (FLAC)
find_package(FLACPP REQUIRED) # test if FLAC is installed on the system
find_package(FLACPP REQUIRED) # test if FLAC is installed on the system
add_definitions(-DHAVE_FLAC)
endif()
endif()
Expand Down Expand Up @@ -200,11 +200,12 @@ endif()

if (SOAPYSDR)
find_package(SoapySDR NO_MODULE REQUIRED)
# Note: SoapySDRConfig.cmake sets C++11 standard so it needs to be reset to C++17
set(CMAKE_CXX_STANDARD 17)
# Note: SoapySDRConfig.cmake sets C++11 standard so it needs to be reset to C++14
set(CMAKE_CXX_STANDARD 14)
endif()

include_directories(
${CMAKE_CURRENT_BINARY_DIR}
src
src/backend
src/output
Expand Down Expand Up @@ -458,7 +459,14 @@ endif()

if(BUILD_WELLE_CLI AND NOT ANDROID)
set(cliExecutableName welle-cli)
add_executable (${cliExecutableName} ${welle_cli_sources} ${backend_sources} ${input_sources} ${fft_sources})
add_executable (${cliExecutableName}
${welle_cli_sources}
${backend_sources}
${input_sources}
${fft_sources}
index.html.h
index.js.h
favicon.ico.h)

if(CMAKE_BUILD_TYPE MATCHES Debug)
SET_TARGET_PROPERTIES(${cliExecutableName} PROPERTIES COMPILE_FLAGS "-O2 -fno-omit-frame-pointer -fsanitize=address")
Expand All @@ -478,34 +486,34 @@ if(BUILD_WELLE_CLI AND NOT ANDROID)
Threads::Threads
)

find_program(RESOURCE_COMPILER xxd)
add_custom_command(
TARGET ${cliExecutableName} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/src/welle-cli/index.html
${CMAKE_CURRENT_BINARY_DIR}/index.html)
OUTPUT index.html.h
COMMAND ${RESOURCE_COMPILER} -i -n index_html
${CMAKE_SOURCE_DIR}/src/welle-cli/index.html
index.html.h
DEPENDS ${CMAKE_SOURCE_DIR}/src/welle-cli/index.html)
set_property(SOURCE index.html.h PROPERTY SKIP_AUTOMOC ON)

add_custom_command(
TARGET ${cliExecutableName} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/src/welle-cli/index.js
${CMAKE_CURRENT_BINARY_DIR}/index.js)
OUTPUT index.js.h
COMMAND ${RESOURCE_COMPILER} -i -n index_js
${CMAKE_SOURCE_DIR}/src/welle-cli/index.js
index.js.h
DEPENDS ${CMAKE_SOURCE_DIR}/src/welle-cli/index.js)
set_property(SOURCE index.js.h PROPERTY SKIP_AUTOMOC ON)

if(APPLE AND WITH_APP_BUNDLE)
INSTALL (TARGETS ${cliExecutableName} RUNTIME DESTINATION ${GUI_INSTALL_DIR}/${executableName}.app/Contents/MacOS)
INSTALL (
FILES
${PROJECT_SOURCE_DIR}/src/welle-cli/index.html
${PROJECT_SOURCE_DIR}/src/welle-cli/index.js
DESTINATION ${GUI_INSTALL_DIR}/${executableName}.app/Contents/Resources/welle-cli
)
elseif(UNIX)
add_custom_command(
OUTPUT favicon.ico.h
COMMAND ${RESOURCE_COMPILER} -i -n favicon_ico
${CMAKE_SOURCE_DIR}/src/welle-cli/favicon.ico
favicon.ico.h
DEPENDS ${CMAKE_SOURCE_DIR}/src/welle-cli/favicon.ico)
set_property(SOURCE favicon.ico.h PROPERTY SKIP_AUTOMOC ON)

if(UNIX AND NOT APPLE OR NOT WITH_APP_BUNDLE)
INSTALL (TARGETS ${cliExecutableName} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
INSTALL (FILES src/welle-cli/doc/man/welle-cli.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1/)
INSTALL (
FILES
${PROJECT_SOURCE_DIR}/src/welle-cli/index.html
${PROJECT_SOURCE_DIR}/src/welle-cli/index.js
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/welle-io/html/
)
endif()
endif()

Expand Down
Binary file added src/welle-cli/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions src/welle-cli/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<title>welle-cli webfrontend</title>
<meta charset="UTF-8">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<style>
body {
background-color: linen;
Expand Down
66 changes: 24 additions & 42 deletions src/welle-cli/webradiointerface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/

#include "welle-cli/webradiointerface.h"
#include <filesystem>
#include <algorithm>
#include <cmath>
#include <complex>
Expand Down Expand Up @@ -57,6 +56,10 @@
#include "welle-cli/jsonconvert.h"
#include "welle-cli/webprogrammehandler.h"

#include "index.html.h"
#include "index.js.h"
#include "favicon.ico.h"

#ifdef __unix__
# include <unistd.h>
# if _POSIX_VERSION >= 200809L
Expand Down Expand Up @@ -105,6 +108,9 @@ static const char* http_contenttype_js =
static const char* http_contenttype_html =
"Content-Type: text/html; charset=utf-8\r\n";

static const char* http_contenttype_ico =
"Content-Type: image/x-icon\r\n";

static const char* http_nocache = "Cache-Control: no-cache\r\n";

static string to_hex(uint32_t value, int width)
Expand Down Expand Up @@ -132,15 +138,13 @@ static bool send_http_response(Socket& s, const string& statuscode,

WebRadioInterface::WebRadioInterface(CVirtualInput& in,
int port,
const string &base_dir,
DecodeSettings ds,
RadioReceiverOptions rro) :
dabparams(1),
input(in),
spectrum_fft_handler(dabparams.T_u),
rro(rro),
decode_settings(ds),
base_dir(base_dir)
decode_settings(ds)
{
{
// Ensure that rx always exists when rx_mut is free!
Expand Down Expand Up @@ -466,10 +470,13 @@ bool WebRadioInterface::dispatch_client(Socket&& client)
else {
if (req.is_get) {
if (req.url == "/") {
success = send_file(s, "index.html", http_contenttype_html);
success = send_file(s, index_html, index_html_len, http_contenttype_html);
}
else if (req.url == "/index.js") {
success = send_file(s, "index.js", http_contenttype_js);
success = send_file(s, index_js, index_js_len, http_contenttype_js);
}
else if (req.url == "/favicon.ico") {
success = send_file(s, favicon_ico, favicon_ico_len, http_contenttype_ico);
}
else if (req.url == "/mux.json") {
success = send_mux_json(s);
Expand Down Expand Up @@ -568,47 +575,22 @@ bool WebRadioInterface::dispatch_client(Socket&& client)
}

bool WebRadioInterface::send_file(Socket& s,
const string& filename,
const unsigned char *file,
const unsigned int file_length,
const string& content_type)
{
filesystem::path path_name;
if (!base_dir.empty())
{
path_name = base_dir;
path_name /= filename;
}
else
{
path_name = filename;
if (not send_http_response(s, http_ok, "", content_type)) {
cerr << "Failed to send file headers" << endl;
return false;
}
FILE *fd = fopen(path_name.c_str(), "r");
if (fd) {
if (not send_http_response(s, http_ok, "", content_type)) {
cerr << "Failed to send file headers" << endl;
fclose(fd);
return false;
}

vector<char> data(1024);
ssize_t ret = 0;
do {
ret = fread(data.data(), 1, data.size(), fd);
ret = s.send(data.data(), ret, MSG_NOSIGNAL);
if (ret == -1) {
cerr << "Failed to send file data" << endl;
fclose(fd);
return false;
}

} while (ret > 0);

fclose(fd);
return true;
}
else {
return send_http_response(s, http_500, "file '" + filename + "' is missing!");
ssize_t ret = 0;
ret = s.send((void*)file, file_length, MSG_NOSIGNAL);
if (ret == -1) {
cerr << "Failed to send file data" << endl;
return false;
}
return false;
return true;
}

static vector<PeakJson> calculate_cir_peaks(const vector<float>& cir_linear)
Expand Down
6 changes: 2 additions & 4 deletions src/welle-cli/webradiointerface.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class WebRadioInterface : public RadioControllerInterface {
WebRadioInterface(
CVirtualInput& in,
int port,
const std::string& base_dir,
DecodeSettings cs,
RadioReceiverOptions rro);
virtual ~WebRadioInterface();
Expand Down Expand Up @@ -107,7 +106,8 @@ class WebRadioInterface : public RadioControllerInterface {
bool dispatch_client(Socket&& client);
// Send a file
bool send_file(Socket& s,
const std::string& filename,
const unsigned char *file,
const unsigned int file_length,
const std::string& content_type);

// Generate and send the mux.json
Expand Down Expand Up @@ -216,6 +216,4 @@ class WebRadioInterface : public RadioControllerInterface {
std::chrono::time_point<std::chrono::steady_clock> time_change;
};
std::list<ActiveCarouselService> carousel_services_active;

std::string base_dir;
};
9 changes: 2 additions & 7 deletions src/welle-cli/welle-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ class RadioInterface : public RadioControllerInterface {
struct options_t {
string soapySDRDriverArgs = "";
string antenna = "";
string base_dir = "";
int gain = -1;
string channel = "10B";
string iqsource = "";
Expand Down Expand Up @@ -315,7 +314,6 @@ static void usage()
endl <<
"Web server mode:" << endl <<
" -w port Enable web server on port <port>." << endl <<
" -b path Base directory of index.html and index.js files." << endl <<
" -C number Number of programmes to decode in a carousel" << endl <<
" (to be used with -w, cannot be used with -D)." << endl <<
" This is useful if your machine cannot decode all programmes" << endl <<
Expand Down Expand Up @@ -411,14 +409,11 @@ options_t parse_cmdline(int argc, char **argv)
options.rro.decodeTII = true;

int opt;
while ((opt = getopt(argc, argv, "A:b:c:C:dDf:F:g:hp:O:Ps:Tt:uvw:")) != -1) {
while ((opt = getopt(argc, argv, "A:c:C:dDf:F:g:hp:O:Ps:Tt:uvw:")) != -1) {
switch (opt) {
case 'A':
options.antenna = optarg;
break;
case 'b':
options.base_dir = optarg;
break;
case 'c':
options.channel = optarg;
break;
Expand Down Expand Up @@ -610,7 +605,7 @@ int main(int argc, char **argv)
return 1;
}

WebRadioInterface wri(*in, options.web_port, options.base_dir, ds, options.rro);
WebRadioInterface wri(*in, options.web_port, ds, options.rro);
wri.serve();
}
else {
Expand Down

0 comments on commit 6fcccb5

Please sign in to comment.