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 #ifdef Q_OS_ANDROID
103  strlist << "ERROR";
104  strlist << "getloadavg() not supported in Android";
105 #elif !defined(_WIN32)
106  double loads[3];
107  if (getloadavg(loads,3) == -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 #else
119  strlist << "ERROR";
120  strlist << "getloadavg() not supported on Windows";
121 #endif
122 
123  sock->WriteStringList(strlist);
124  return true;
125 }
126 
133 {
134  QStringList strlist;
135  time_t uptime = 0;
136 
137  if (getUptime(uptime))
138  strlist << QString::number(uptime);
139  else
140  {
141  strlist << "ERROR";
142  strlist << "Could not determine uptime.";
143  }
144 
145  sock->WriteStringList(strlist);
146  return true;
147 }
148 
155 {
156  QStringList strlist;
157 
158  strlist << gCoreContext->GetHostName();
159 
160  sock->WriteStringList(strlist);
161  return true;
162 }
163 
170 {
171  QStringList strlist;
172  int totalMB = 0;
173  int freeMB = 0;
174  int totalVM = 0;
175  int freeVM = 0;
176 
177  if (getMemStats(totalMB, freeMB, totalVM, freeVM))
178  {
179  strlist << QString::number(totalMB) << QString::number(freeMB)
180  << QString::number(totalVM) << QString::number(freeVM);
181  }
182  else
183  {
184  strlist << "ERROR"
185  << "Could not determine memory stats.";
186  }
187 
188  sock->WriteStringList(strlist);
189  return true;
190 }
191 
198 {
199  QStringList strlist;
200  strlist << MythTZ::getTimeZoneID()
201  << QString::number(MythTZ::calc_utc_offset())
203 
204  sock->WriteStringList(strlist);
205  return true;
206 }
207 
bool WriteStringList(const QStringList &strlist)
static bool HandleQueryUptime(SocketHandler *sock)
void SetAnnounce(const QStringList &new_announce)
Definition: mythsocket.cpp:502
static bool HandleQueryHostname(SocketHandler *sock)
QString current_iso_string(bool stripped)
Returns current Date and Time in UTC as a string.
Definition: mythdate.cpp:18
bool HandleAnnounce(MythSocket *socket, QStringList &commands, QStringList &slist) override
Definition: basehandler.cpp:16
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
void AddSocketHandler(SocketHandler *socket)
int calc_utc_offset(void)
static bool HandleQueryLoad(SocketHandler *sock)
Definition: basehandler.cpp:98
static bool HandleQueryTimeZone(SocketHandler *sock)
string hostname
Definition: caa.py:17
static bool HandleQueryMemStats(SocketHandler *sock)
#define getloadavg(x, y)
Definition: compat.h:322
bool HandleQuery(SocketHandler *sock, QStringList &commands, QStringList &slist) override
Dispatch query messages received from a client.
Definition: basehandler.cpp:71
MythSocketManager * m_parent
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
QString getTimeZoneID(void)
Returns the zoneinfo time zone ID or as much time zone information as possible.
QString GetHostName(void)
void SendSystemEvent(const QString &msg)
bool getUptime(time_t &uptime)
Returns uptime statistics.