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;
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  SocketHandler *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  strlist << QString::number(loads[0])
114  << QString::number(loads[1])
115  << QString::number(loads[2]);
116 #else
117  strlist << "ERROR";
118  strlist << "getloadavg() not supported on Windows";
119 #endif
120 
121  sock->WriteStringList(strlist);
122  return true;
123 }
124 
131 {
132  QStringList strlist;
133  time_t uptime;
134 
135  if (getUptime(uptime))
136  strlist << QString::number(uptime);
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, freeMB, totalVM, freeVM;
171 
172  if (getMemStats(totalMB, freeMB, totalVM, freeVM))
173  {
174  strlist << QString::number(totalMB) << QString::number(freeMB)
175  << QString::number(totalVM) << QString::number(freeVM);
176  }
177  else
178  {
179  strlist << "ERROR"
180  << "Could not determine memory stats.";
181  }
182 
183  sock->WriteStringList(strlist);
184  return true;
185 }
186 
193 {
194  QStringList strlist;
195  strlist << MythTZ::getTimeZoneID()
196  << QString::number(MythTZ::calc_utc_offset())
198 
199  sock->WriteStringList(strlist);
200  return true;
201 }
202 
bool WriteStringList(const QStringList &strlist)
bool HandleQueryUptime(SocketHandler *sock)
void SetAnnounce(const QStringList &new_announce)
Definition: mythsocket.cpp:502
void AllowStandardEvents(bool allow)
Definition: sockethandler.h:36
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 BlockShutdown(bool block)
Definition: sockethandler.h:35
void AddSocketHandler(SocketHandler *socket)
int calc_utc_offset(void)
bool HandleQueryLoad(SocketHandler *sock)
Definition: basehandler.cpp:98
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
bool HandleQueryTimeZone(SocketHandler *sock)
string hostname
Definition: caa.py:17
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 AllowSystemEvents(bool allow)
Definition: sockethandler.h:37
void SendSystemEvent(const QString &msg)
bool getUptime(time_t &uptime)
Returns uptime statistics.