MythTV  master
basehandler.cpp
Go to the documentation of this file.
1 #include <QString>
2 #include <QStringList>
3 
4 #include "mythsystemevent.h"
5 #include "mythsocket.h"
6 #include "mythsocketmanager.h"
7 #include "socketrequesthandler.h"
8 #include "sockethandler.h"
9 #include "mythmiscutil.h"
10 #include "mythlogging.h"
11 #include "mythcorecontext.h"
12 #include "mythtimezone.h"
13 
15 
17  QStringList &commands, QStringList &slist)
18 {
19  if (commands.size() != 4)
20  return false;
21 
22  bool blockShutdown = true;
23  if (commands[1] == "Playback")
24  blockShutdown = true;
25  else if (commands[1] == "Monitor")
26  blockShutdown = false;
27  else
28  return false;
29 
30  QString hostname = commands[2];
31 
32  int eventlevel = commands[3].toInt();
33  bool systemevents = ( (eventlevel == 1) || (eventlevel == 3));
34  bool normalevents = ( (eventlevel == 1) || (eventlevel == 2));
35 
36  auto *handler = new SocketHandler(socket, m_parent, hostname);
37  socket->SetAnnounce(slist);
38 
39  handler->BlockShutdown(blockShutdown);
40  handler->AllowStandardEvents(normalevents);
41  handler->AllowSystemEvents(systemevents);
42 
43  m_parent->AddSocketHandler(handler);
44 
45  handler->WriteStringList(QStringList("OK"));
46  handler->DecrRef();
47  handler = nullptr;
48 
49  LOG(VB_GENERAL, LOG_DEBUG, QString("MainServer::ANN %1")
50  .arg(commands[1]));
51  LOG(VB_GENERAL, LOG_NOTICE, QString("adding: %1 as a client (events: %2)")
52  .arg(commands[2]).arg(eventlevel));
53  gCoreContext->SendSystemEvent(QString("CLIENT_CONNECTED HOSTNAME %1")
54  .arg(commands[2]));
55 
56  return true;
57 }
58 
72  QStringList &commands,
73  QStringList &/*slist*/)
74 {
75  QString command = commands[0];
76  bool res = false;
77 
78  if (command == "QUERY_LOAD")
79  res = HandleQueryLoad(sock);
80  else if (command == "QUERY_UPTIME")
81  res = HandleQueryUptime(sock);
82  else if (command == "QUERY_HOSTNAME")
83  res = HandleQueryHostname(sock);
84  else if (command == "QUERY_MEMSTATS")
85  res = HandleQueryMemStats(sock);
86  else if (command == "QUERY_TIME_ZONE")
87  res = HandleQueryTimeZone(sock);
88 
89  return res;
90 }
91 
99 {
100  QStringList strlist;
101 
102 #if defined(_WIN32) || defined(Q_OS_ANDROID)
103  strlist << "ERROR";
104  strlist << "getloadavg() not supported";
105 #else
106  loadArray loads = getLoadAvgs();
107  if (loads[0] == -1)
108  {
109  strlist << "ERROR";
110  strlist << "getloadavg() failed";
111  }
112  else
113  {
114  strlist << QString::number(loads[0])
115  << QString::number(loads[1])
116  << QString::number(loads[2]);
117  }
118 #endif
119 
120  sock->WriteStringList(strlist);
121  return true;
122 }
123 
130 {
131  QStringList strlist;
132  time_t uptime = 0;
133 
134  if (getUptime(uptime))
135  strlist << QString::number(uptime);
136  else
137  {
138  strlist << "ERROR";
139  strlist << "Could not determine uptime.";
140  }
141 
142  sock->WriteStringList(strlist);
143  return true;
144 }
145 
152 {
153  QStringList strlist;
154 
155  strlist << gCoreContext->GetHostName();
156 
157  sock->WriteStringList(strlist);
158  return true;
159 }
160 
167 {
168  QStringList strlist;
169  int totalMB = 0;
170  int freeMB = 0;
171  int totalVM = 0;
172  int freeVM = 0;
173 
174  if (getMemStats(totalMB, freeMB, totalVM, freeVM))
175  {
176  strlist << QString::number(totalMB) << QString::number(freeMB)
177  << QString::number(totalVM) << QString::number(freeVM);
178  }
179  else
180  {
181  strlist << "ERROR"
182  << "Could not determine memory stats.";
183  }
184 
185  sock->WriteStringList(strlist);
186  return true;
187 }
188 
195 {
196  QStringList strlist;
197  strlist << MythTZ::getTimeZoneID()
198  << QString::number(MythTZ::calc_utc_offset())
200 
201  sock->WriteStringList(strlist);
202  return true;
203 }
204 
BaseRequestHandler::HandleQuery
bool HandleQuery(SocketHandler *sock, QStringList &commands, QStringList &slist) override
Dispatch query messages received from a client.
Definition: basehandler.cpp:71
MythTZ::getTimeZoneID
QString getTimeZoneID(void)
Returns the zoneinfo time zone ID or as much time zone information as possible.
Definition: mythtimezone.cpp:56
mythsocketmanager.h
getLoadAvgs
loadArray getLoadAvgs(void)
Returns the system load averages.
Definition: mythmiscutil.cpp:178
SocketRequestHandler::m_parent
MythSocketManager * m_parent
Definition: socketrequesthandler.h:33
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythsystemevent.h
socketrequesthandler.h
BaseRequestHandler::HandleQueryHostname
static bool HandleQueryHostname(SocketHandler *sock)
Definition: basehandler.cpp:151
MythSocket
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:27
mythlogging.h
MythCoreContext::SendSystemEvent
void SendSystemEvent(const QString &msg)
Definition: mythcorecontext.cpp:1554
BaseRequestHandler::HandleQueryMemStats
static bool HandleQueryMemStats(SocketHandler *sock)
Definition: basehandler.cpp:166
MythSocketManager::AddSocketHandler
void AddSocketHandler(SocketHandler *socket)
Definition: mythsocketmanager.cpp:135
sockethandler.h
BaseRequestHandler::HandleQueryUptime
static bool HandleQueryUptime(SocketHandler *sock)
Definition: basehandler.cpp:129
SocketHandler::WriteStringList
bool WriteStringList(const QStringList &strlist)
Definition: sockethandler.cpp:27
loadArray
std::array< double, 3 > loadArray
Definition: mythmiscutil.h:36
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
getUptime
bool getUptime(time_t &uptime)
Returns uptime statistics.
Definition: mythmiscutil.cpp:68
BaseRequestHandler::HandleQueryTimeZone
static bool HandleQueryTimeZone(SocketHandler *sock)
Definition: basehandler.cpp:194
MythTZ::calc_utc_offset
int calc_utc_offset(void)
Definition: mythtimezone.cpp:24
basehandler.h
mythmiscutil.h
mythcorecontext.h
MythSocket::SetAnnounce
void SetAnnounce(const QStringList &new_announce)
Definition: mythsocket.cpp:508
MythDate::current_iso_string
QString current_iso_string(bool stripped)
Returns current Date and Time in UTC as a string.
Definition: mythdate.cpp:18
BaseRequestHandler::HandleQueryLoad
static bool HandleQueryLoad(SocketHandler *sock)
Definition: basehandler.cpp:98
SocketHandler
Definition: sockethandler.h:17
MythCoreContext::GetHostName
QString GetHostName(void)
Definition: mythcorecontext.cpp:859
getMemStats
bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
Definition: mythmiscutil.cpp:111
musicbrainzngs.caa.hostname
string hostname
Definition: caa.py:17
BaseRequestHandler::HandleAnnounce
bool HandleAnnounce(MythSocket *socket, QStringList &commands, QStringList &slist) override
Definition: basehandler.cpp:16
mythsocket.h
mythtimezone.h