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