/
cheapl_main.cpp
92 lines (81 loc) · 2.44 KB
/
cheapl_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Copyright (C) 2014 Danny Havenith
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "cheaplservice.h"
#include <boost/asio/ip/host_name.hpp>
#include <boost/system/error_code.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
struct config
{
string soundfile_directory {"."};
string usb_device {"Generic USB Audio Device"};
string application_id {"rurandom-cheapl." + truncateto16( boost::asio::ip::host_name())};
string application_version {"0.1"};
private:
static std::string truncateto16( const std::string &input)
{
if (input.size() <= 16) return input;
return input.substr( 0, 16);
}
};
/// Simple configuration retrieval function.
/// For now, let's stick with a _very_ simple command line parser.
config get_config( int argc, char *argv[])
{
config result;
// little known fact: argv ends with a nullptr
++argv; // get past the program name
if (*argv)
{
result.soundfile_directory = *argv++;
}
if (*argv)
{
result.usb_device = *argv++;
}
return result;
}
std::unique_ptr< xpl::cheapl_service> service_ptr;
void exit_handler(void) noexcept
{
try
{
if (service_ptr) service_ptr->signoff();
}
catch (...) // easiest way to guarantee noexcept
{}
}
/// Main function of the cheapl server executable. This will start the cheapl xPL service and execute requests.
/// This program currently takes two optional arguments:
/// # the directory where wav-files can be found that should be played to the RF-connected sound card.
/// # the name of the alsa sound device to which the sequences should be sent.
int main( int argc, char *argv[])
{
int result = 0;
try
{
atexit(exit_handler);
config conf = get_config( argc, argv);
service_ptr.reset( new xpl::cheapl_service{ conf.soundfile_directory, conf.usb_device, conf.application_id, conf.application_version});
service_ptr->run();
}
catch (std::exception &e)
{
std::cerr << "something went wrong: " << e.what() << std::endl;
result = -1;
}
catch (boost::system::error_code &e)
{
std::cerr << "system error: " << e.message() << std::endl;
result = -2;
}
return result;
}