MythTV  master
networkcontrol.h
Go to the documentation of this file.
1 #ifndef NETWORKCONTROL_H_
2 #define NETWORKCONTROL_H_
3 
4 #include <deque>
5 
6 #include <QWaitCondition>
7 #include <QStringList>
8 #include <QTcpSocket>
9 #include <QRunnable>
10 #include <QMutex>
11 #include <QEvent>
12 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
13 #include <QRecursiveMutex>
14 #endif
15 
16 #include "mthread.h"
17 #include "serverpool.h"
18 
19 class MainServer;
20 class QTextStream;
21 
22 // Locking order
23 // clientLock -> ncLock
24 // -> nrLock
25 
26 class NetworkControlClient : public QObject
27 {
28  Q_OBJECT
29  public:
30  explicit NetworkControlClient(QTcpSocket *s);
31  ~NetworkControlClient() override;
32 
33  QTcpSocket *getSocket() { return m_socket; }
34  QTextStream *getTextStream() { return m_textStream; }
35 
36  signals:
37  void commandReceived(QString&);
38 
39  public slots:
40  void readClient();
41 
42  private:
43  QTcpSocket *m_socket {nullptr};
44  QTextStream *m_textStream {nullptr};
45 };
46 
47 class NetworkCommand : public QObject
48 {
49  Q_OBJECT
50  public:
51  NetworkCommand(NetworkControlClient *cli, const QString& c)
52  : m_command(c.trimmed()),
53  m_client(cli)
54  {
55  m_args = m_command.simplified().split(" ");
56  }
57 
59  {
60  if (this == &nc)
61  return *this;
62  m_command = nc.m_command;
63  m_client = nc.m_client;
64  m_args = m_command.simplified().split(" ");
65  return *this;
66  }
67 
68  QString getCommand() { return m_command; }
70  QString getArg(int arg) { return m_args[arg]; }
71  int getArgCount() { return m_args.size(); }
72  QString getFrom(int arg);
73 
74  private:
75  QString m_command;
77  QStringList m_args;
78 };
79 
80 class NetworkControlCloseEvent : public QEvent
81 {
82  public:
84  QEvent(kEventType), m_networkControlClient(ncc) {}
85 
87 
88  static Type kEventType;
89 
90  private:
92 };
93 
94 class NetworkControl;
95 class MythUIType;
96 
97 class NetworkControl : public ServerPool, public QRunnable
98 {
99  Q_OBJECT
100 
101  public:
102  NetworkControl();
103  ~NetworkControl() override;
104 
105  private slots:
106  void newControlConnection(QTcpSocket *client);
107  void receiveCommand(QString &command);
108  void deleteClient(void);
109 
110  protected:
111  void run(void) override; // QRunnable
112 
113  private:
114  QString processJump(NetworkCommand *nc);
115  QString processKey(NetworkCommand *nc);
116  QString processLiveTV(NetworkCommand *nc);
117  QString processPlay(NetworkCommand *nc, int clientID);
118  QString processQuery(NetworkCommand *nc);
119  static QString processSet(NetworkCommand *nc);
120  static QString processMessage(NetworkCommand *nc);
121  static QString processNotification(NetworkCommand *nc);
122  QString processTheme(NetworkCommand *nc);
123  QString processHelp(NetworkCommand *nc);
124 
125  void notifyDataAvailable(void);
126  void sendReplyToClient(NetworkControlClient *ncc, QString &reply);
127  void customEvent(QEvent *e) override; // QObject
128 
129  static QString listRecordings(const QString& chanid = "", const QString& starttime = "");
130  static QString listSchedule(const QString& chanID = "") ;
131  static QString listChannels(uint start, uint limit) ;
132  static QString saveScreenshot(NetworkCommand *nc);
133 
135 
137 
138  static QString getWidgetType(MythUIType *type);
139 
140  QString m_prompt {"# "};
141  bool m_gotAnswer {false};
142  QString m_answer {""};
143  QMap <QString, QString> m_jumpMap;
144  QMap <QString, int> m_keyMap;
145  QMap <int, QString> m_keyTextMap;
146 
147 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
148  mutable QMutex m_clientLock {QMutex::Recursive};
149 #else
150  mutable QRecursiveMutex m_clientLock;
151 #endif
152  QList<NetworkControlClient*> m_clients;
153 
154  QList<NetworkCommand*> m_networkControlCommands; // protected by ncLock
155  QMutex m_ncLock;
156  QWaitCondition m_ncCond; // protected by ncLock
157 
158  QList<NetworkCommand*> m_networkControlReplies;
159  QMutex m_nrLock;
160 
162  bool m_stopCommandThread {false}; // protected by ncLock
163 };
164 
165 #endif
166 
167 /* vim: set expandtab tabstop=4 shiftwidth=4: */
168 
NetworkCommand
Definition: networkcontrol.h:47
NetworkControl::newControlConnection
void newControlConnection(QTcpSocket *client)
Definition: networkcontrol.cpp:368
NetworkCommand::m_args
QStringList m_args
Definition: networkcontrol.h:77
NetworkControl::m_networkControlCommands
QList< NetworkCommand * > m_networkControlCommands
Definition: networkcontrol.h:154
NetworkControl::saveScreenshot
static QString saveScreenshot(NetworkCommand *nc)
Definition: networkcontrol.cpp:1815
NetworkControl::notifyDataAvailable
void notifyDataAvailable(void)
Definition: networkcontrol.cpp:1532
NetworkControlCloseEvent::m_networkControlClient
NetworkControlClient * m_networkControlClient
Definition: networkcontrol.h:91
NetworkControl::processLiveTV
QString processLiveTV(NetworkCommand *nc)
NetworkControl::m_ncLock
QMutex m_ncLock
Definition: networkcontrol.h:155
NetworkControl::m_keyMap
QMap< QString, int > m_keyMap
Definition: networkcontrol.h:144
NetworkControl::sendReplyToClient
void sendReplyToClient(NetworkControlClient *ncc, QString &reply)
Definition: networkcontrol.cpp:1538
NetworkControlClient::m_textStream
QTextStream * m_textStream
Definition: networkcontrol.h:44
ServerPool
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
NetworkControlClient::readClient
void readClient()
Definition: networkcontrol.cpp:416
NetworkControl::receiveCommand
void receiveCommand(QString &command)
Definition: networkcontrol.cpp:441
NetworkControl
Definition: networkcontrol.h:97
NetworkControl::getWidgetType
static QString getWidgetType(MythUIType *type)
Definition: networkcontrol.cpp:1115
NetworkCommand::getFrom
QString getFrom(int arg)
Definition: networkcontrol.cpp:1843
NetworkControlClient::commandReceived
void commandReceived(QString &)
NetworkControl::m_gotAnswer
bool m_gotAnswer
Definition: networkcontrol.h:141
MainServer
Definition: mainserver.h:113
NetworkControlClient
Definition: networkcontrol.h:26
NetworkControl::processSet
static QString processSet(NetworkCommand *nc)
Definition: networkcontrol.cpp:1074
NetworkControl::processHelp
QString processHelp(NetworkCommand *nc)
Definition: networkcontrol.cpp:1322
NetworkControl::m_stopCommandThread
bool m_stopCommandThread
Definition: networkcontrol.h:162
NetworkControlCloseEvent
Definition: networkcontrol.h:80
NetworkControl::deleteClient
void deleteClient(void)
Definition: networkcontrol.cpp:337
NetworkControl::listChannels
static QString listChannels(uint start, uint limit)
Definition: networkcontrol.cpp:1765
NetworkControlClient::m_socket
QTcpSocket * m_socket
Definition: networkcontrol.h:43
NetworkControl::processNetworkControlCommand
void processNetworkControlCommand(NetworkCommand *nc)
Definition: networkcontrol.cpp:292
NetworkControl::~NetworkControl
~NetworkControl() override
Definition: networkcontrol.cpp:246
NetworkCommand::NetworkCommand
NetworkCommand(NetworkControlClient *cli, const QString &c)
Definition: networkcontrol.h:51
NetworkControl::processQuery
QString processQuery(NetworkCommand *nc)
Definition: networkcontrol.cpp:917
uint
unsigned int uint
Definition: compat.h:140
NetworkCommand::operator=
NetworkCommand & operator=(NetworkCommand const &nc)
Definition: networkcontrol.h:58
NetworkControl::m_keyTextMap
QMap< int, QString > m_keyTextMap
Definition: networkcontrol.h:145
NetworkControl::m_nrLock
QMutex m_nrLock
Definition: networkcontrol.h:159
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:84
NetworkCommand::getArgCount
int getArgCount()
Definition: networkcontrol.h:71
NetworkControl::listRecordings
static QString listRecordings(const QString &chanid="", const QString &starttime="")
Definition: networkcontrol.cpp:1709
NetworkControlClient::~NetworkControlClient
~NetworkControlClient() override
Definition: networkcontrol.cpp:408
NetworkCommand::m_command
QString m_command
Definition: networkcontrol.h:75
NetworkControl::m_clientLock
QRecursiveMutex m_clientLock
Definition: networkcontrol.h:150
NetworkControl::m_commandThread
MThread * m_commandThread
Definition: networkcontrol.h:161
NetworkControl::m_prompt
QString m_prompt
Definition: networkcontrol.h:140
NetworkControlClient::NetworkControlClient
NetworkControlClient(QTcpSocket *s)
Definition: networkcontrol.cpp:396
NetworkControlClient::getTextStream
QTextStream * getTextStream()
Definition: networkcontrol.h:34
NetworkCommand::m_client
NetworkControlClient * m_client
Definition: networkcontrol.h:76
NetworkControlCloseEvent::NetworkControlCloseEvent
NetworkControlCloseEvent(NetworkControlClient *ncc)
Definition: networkcontrol.h:83
NetworkControl::processNotification
static QString processNotification(NetworkCommand *nc)
Definition: networkcontrol.cpp:1520
serverpool.h
NetworkControl::m_jumpMap
QMap< QString, QString > m_jumpMap
Definition: networkcontrol.h:143
NetworkControl::NetworkControl
NetworkControl()
Definition: networkcontrol.cpp:79
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
NetworkControl::processKey
QString processKey(NetworkCommand *nc)
Definition: networkcontrol.cpp:476
NetworkControl::processTheme
QString processTheme(NetworkCommand *nc)
Definition: networkcontrol.cpp:1159
NetworkControl::run
void run(void) override
Definition: networkcontrol.cpp:274
NetworkControl::m_ncCond
QWaitCondition m_ncCond
Definition: networkcontrol.h:156
NetworkControl::processMessage
static QString processMessage(NetworkCommand *nc)
Definition: networkcontrol.cpp:1507
NetworkControl::m_answer
QString m_answer
Definition: networkcontrol.h:142
NetworkControl::listSchedule
static QString listSchedule(const QString &chanID="")
Definition: networkcontrol.cpp:1654
NetworkCommand::getClient
NetworkControlClient * getClient()
Definition: networkcontrol.h:69
NetworkControl::processJump
QString processJump(NetworkCommand *nc)
Definition: networkcontrol.cpp:455
NetworkControlCloseEvent::kEventType
static Type kEventType
Definition: networkcontrol.h:88
NetworkControl::m_clients
QList< NetworkControlClient * > m_clients
Definition: networkcontrol.h:152
NetworkControl::processPlay
QString processPlay(NetworkCommand *nc, int clientID)
Definition: networkcontrol.cpp:576
NetworkCommand::getCommand
QString getCommand()
Definition: networkcontrol.h:68
NetworkControlCloseEvent::getClient
NetworkControlClient * getClient()
Definition: networkcontrol.h:86
NetworkControl::customEvent
void customEvent(QEvent *e) override
Definition: networkcontrol.cpp:1567
NetworkCommand::getArg
QString getArg(int arg)
Definition: networkcontrol.h:70
NetworkControlClient::getSocket
QTcpSocket * getSocket()
Definition: networkcontrol.h:33
NetworkControl::m_networkControlReplies
QList< NetworkCommand * > m_networkControlReplies
Definition: networkcontrol.h:158