MythTV master
mythmediaserver.cpp
Go to the documentation of this file.
1// C/C++
2#include <csignal>
3#include <cstdio>
4#include <cstdlib>
5#include <fcntl.h>
6#include <fstream>
7#include <iostream>
8#include <string>
9#include <unistd.h>
10
11// Qt
12#include <QCoreApplication>
13#include <QDir>
14#include <QString>
15
16// MythTV
17#include "libmyth/mythcontext.h"
20#include "libmythbase/mythconfig.h"
24#include "libmythbase/mythversion.h"
29#include "libmythtv/dbcheck.h"
31
32// MythMediaServer
35
36#if CONFIG_SYSTEMD_NOTIFY
37#include <systemd/sd-daemon.h>
38static inline void ms_sd_notify(const char *str) { sd_notify(0, str); };
39#else
40static inline void ms_sd_notify(const char */*str*/) {};
41#endif
42
43#define LOC QString("MythMediaServer: ")
44#define LOC_WARN QString("MythMediaServer, Warning: ")
45#define LOC_ERR QString("MythMediaServer, Error: ")
46
47int main(int argc, char *argv[])
48{
50 if (!cmdline.Parse(argc, argv))
51 {
54 }
55
56 if (cmdline.toBool("showhelp"))
57 {
59 return GENERIC_EXIT_OK;
60 }
61
62 if (cmdline.toBool("showversion"))
63 {
65 return GENERIC_EXIT_OK;
66 }
67
68 QCoreApplication a(argc, argv);
69 QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHMEDIASERVER);
70
71 int retval = cmdline.Daemonize();
72 if (retval != GENERIC_EXIT_OK)
73 return retval;
74
75 bool daemonize = cmdline.toBool("daemon");
76 QString mask("general");
77 retval = cmdline.ConfigureLogging(mask, daemonize);
78 if (retval != GENERIC_EXIT_OK)
79 return retval;
80
81 ms_sd_notify("STATUS=Connecting to database.");
82 MythContext context {MYTH_BINARY_VERSION};
83 if (!context.Init(false))
84 {
85 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to init MythContext, exiting.");
87 }
88
89 if (!UpgradeTVDatabaseSchema(false, false, true))
90 {
91 LOG(VB_GENERAL, LOG_ERR, "Exiting due to schema mismatch.");
93 }
94
96
97 gCoreContext->SetAsBackend(true); // blocks the event connection
98 ms_sd_notify("STATUS=Connecting to master server.");
100 {
101 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect to master server");
103 }
104
106 if (gCoreContext->GetBackendServerIP().isEmpty())
107 {
108 std::cerr << "No setting found for this machine's BackendServerIP.\n"
109 << "Please run setup on this machine and modify the first page\n"
110 << "of the general settings.\n";
112 }
113
114 ms_sd_notify("STATUS=Creating socket manager");
115 auto *sockmanager = new MythSocketManager();
116 if (!sockmanager->Listen(port))
117 {
118 LOG(VB_GENERAL, LOG_ERR,
119 "Mediaserver exiting, failed to bind to listen port.");
120 delete sockmanager;
122 }
123
124 sockmanager->RegisterHandler(new BaseRequestHandler());
125 sockmanager->RegisterHandler(new FileServerHandler());
126 sockmanager->RegisterHandler(new MessageHandler());
127
128 auto *controlRequestHandler = new ControlRequestHandler();
129 sockmanager->RegisterHandler(controlRequestHandler);
130 controlRequestHandler->ConnectToMaster();
131
132 auto *sysEventHandler = new MythSystemEventHandler();
133
134 // Provide systemd ready notification (for type=notify units)
135 ms_sd_notify("STATUS=");
136 ms_sd_notify("READY=1");
137
138 int exitCode = QCoreApplication::exec();
139
140 ms_sd_notify("STOPPING=1\nSTATUS=Exiting");
141 delete sysEventHandler;
142 delete sockmanager;
143
144 return exitCode ? exitCode : GENERIC_EXIT_OK;
145}
146
147/* vim: set expandtab tabstop=4 shiftwidth=4: */
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
void ApplySettingsOverride(void)
Apply all overrides to the global context.
int Daemonize(void) const
Fork application into background, and detatch from terminal.
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
static void PrintVersion(void)
Print application version information.
void PrintHelp(void) const
Print command line option help.
Startup context for MythTV.
Definition: mythcontext.h:20
void SetAsBackend(bool backend)
int GetBackendServerPort(void)
Returns the locally defined backend control port.
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
QString GetBackendServerIP(void)
Returns the IP address of the locally defined backend IP.
Handles incoming MythSystemEvent messages.
bool UpgradeTVDatabaseSchema(const bool upgradeAllowed, const bool upgradeIfNoUI, const bool informSystemd)
Called from outside dbcheck.cpp to update the schema.
Definition: dbcheck.cpp:362
@ GENERIC_EXIT_CONNECT_ERROR
Can't connect to master backend.
Definition: exitcodes.h:23
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:16
@ GENERIC_EXIT_DB_OUTOFDATE
Database needs upgrade.
Definition: exitcodes.h:19
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
@ GENERIC_EXIT_SETUP_ERROR
Incorrectly setup system.
Definition: exitcodes.h:24
@ GENERIC_EXIT_SOCKET_ERROR
Socket error.
Definition: exitcodes.h:21
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:18
static constexpr const char * MYTH_APPNAME_MYTHMEDIASERVER
Definition: mythappname.h:17
MythCommFlagCommandLineParser cmdline
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
int main(int argc, char *argv[])
#define LOC
static void ms_sd_notify(const char *)