MythTV master
mythhttpinstance.cpp
Go to the documentation of this file.
1#include <thread>
2
3// MythTV
4#include "mthread.h"
6#include "http/mythhttproot.h"
8
10{
11 static MythHTTPInstance s_instance;
12 return s_instance;
13}
14
16 : m_httpServer(new MythHTTPServer),
17 m_httpServerThread(new MThread("HTTPServer"))
18{
19 // We need to register some types and this should always be hit at least once
20 // before they are needed
21 qRegisterMetaType<HTTPHandler>();
22 qRegisterMetaType<HTTPHandlers>();
23 qRegisterMetaType<HTTPServices>();
24 qRegisterMetaType<DataPayload>();
25 qRegisterMetaType<DataPayloads>();
26 qRegisterMetaType<StringPayload>();
27
28 m_httpServer->moveToThread(m_httpServerThread->qthread());
30 std::this_thread::sleep_for(50us);
31 while (!m_httpServerThread->qthread()->isRunning())
32 {
33 std::this_thread::sleep_for(50us);
34 }
35}
36
38{
40 {
43 }
44 delete m_httpServerThread;
45 delete m_httpServer;
46}
47
54{
56 {
60 Instance().m_httpServerThread = nullptr;
61 }
62
63 delete Instance().m_httpServer;
64 Instance().m_httpServer = nullptr;
65}
66
72{
73 emit Instance().m_httpServer->EnableHTTP(Enable);
74}
75
81void MythHTTPInstance::AddPaths(const QStringList &Paths)
82{
83 emit Instance().m_httpServer->AddPaths(Paths);
84}
85
88void MythHTTPInstance::RemovePaths(const QStringList &Paths)
89{
90 emit Instance().m_httpServer->RemovePaths(Paths);
91}
92
99{
100 emit Instance().m_httpServer->AddHandlers(Handlers);
101}
102
104{
105 emit Instance().m_httpServer->RemoveHandlers(Handlers);
106}
107
109{
110 emit Instance().m_httpServer->AddServices(Services);
111}
112
114{
115 emit Instance().m_httpServer->RemoveServices(Services);
116}
117
119{
121}
122
126{
129}
130
132{
134}
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:267
void quit(void)
calls exit(0)
Definition: mthread.cpp:279
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:284
QThread * qthread(void)
Returns the thread, this will always return the same pointer no matter how often you restart the thre...
Definition: mthread.cpp:217
static void AddErrorPageHandler(const HTTPHandler &Handler)
static void Addservices(const HTTPServices &Services)
static void RemoveHandlers(const HTTPHandlers &Handlers)
static void StopHTTPService()
Stop and delete the MythHTTPServer instance.
static void EnableHTTPService(bool Enable=true)
Signals to the MythHTTPServer instance whether to start or stop listening.
MythHTTPServer * m_httpServer
static void RemovePaths(const QStringList &Paths)
Remove path(s) from the MythHTTPServer instance.
static void AddPaths(const QStringList &Paths)
Add path(s) for the MythHTTPServer instance to handle.
static void AddHandlers(const HTTPHandlers &Handlers)
Add function handlers for specific paths.
static MythHTTPInstance & Instance()
MThread * m_httpServerThread
static void RemoveServices(const HTTPServices &Services)
MythHTTPScopedInstance(const HTTPHandlers &Handlers)
A convenience class to manage the lifetime of a MythHTTPInstance.
void RemoveServices(const HTTPServices &Services)
void RemovePaths(const QStringList &Paths)
void EnableHTTP(bool Enable)
void AddHandlers(const HTTPHandlers &Handlers)
void AddServices(const HTTPServices &Services)
void AddErrorPageHandler(const HTTPHandler &Handler)
void RemoveHandlers(const HTTPHandlers &Handlers)
void AddPaths(const QStringList &Paths)
std::vector< HTTPHandler > HTTPHandlers
Definition: mythhttptypes.h:48
std::vector< HTTPService > HTTPServices
Definition: mythhttptypes.h:55
std::pair< QString, HTTPFunction > HTTPHandler
Definition: mythhttptypes.h:47