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 
13 #include "mthread.h"
14 #include "serverpool.h"
15 
16 class MainServer;
17 class QTextStream;
18 
19 // Locking order
20 // clientLock -> ncLock
21 // -> nrLock
22 
23 class NetworkControlClient : public QObject
24 {
25  Q_OBJECT
26  public:
27  explicit NetworkControlClient(QTcpSocket *s);
28  ~NetworkControlClient() override;
29 
30  QTcpSocket *getSocket() { return m_socket; }
31  QTextStream *getTextStream() { return m_textStream; }
32 
33  signals:
34  void commandReceived(QString&);
35 
36  public slots:
37  void readClient();
38 
39  private:
40  QTcpSocket *m_socket {nullptr};
41  QTextStream *m_textStream {nullptr};
42 };
43 
44 class NetworkCommand : public QObject
45 {
46  Q_OBJECT
47  public:
48  NetworkCommand(NetworkControlClient *cli, const QString& c)
49  : m_command(c.trimmed()),
50  m_client(cli)
51  {
52  m_args = m_command.simplified().split(" ");
53  }
54 
56  {
57  if (this == &nc)
58  return *this;
59  m_command = nc.m_command;
60  m_client = nc.m_client;
61  m_args = m_command.simplified().split(" ");
62  return *this;
63  }
64 
65  QString getCommand() { return m_command; }
67  QString getArg(int arg) { return m_args[arg]; }
68  int getArgCount() { return m_args.size(); }
69  QString getFrom(int arg);
70 
71  private:
72  QString m_command;
74  QStringList m_args;
75 };
76 
77 class NetworkControlCloseEvent : public QEvent
78 {
79  public:
81  QEvent(kEventType), m_networkControlClient(ncc) {}
82 
84 
85  static Type kEventType;
86 
87  private:
89 };
90 
91 class NetworkControl;
92 class MythUIType;
93 
94 class NetworkControl : public ServerPool, public QRunnable
95 {
96  Q_OBJECT
97 
98  public:
100  ~NetworkControl() override;
101 
102  private slots:
103  void newControlConnection(QTcpSocket *client);
104  void receiveCommand(QString &command);
105  void deleteClient(void);
106 
107  protected:
108  void run(void) override; // QRunnable
109 
110  private:
111  QString processJump(NetworkCommand *nc);
112  QString processKey(NetworkCommand *nc);
113  QString processLiveTV(NetworkCommand *nc);
114  QString processPlay(NetworkCommand *nc, int clientID);
115  QString processQuery(NetworkCommand *nc);
116  static QString processSet(NetworkCommand *nc);
117  static QString processMessage(NetworkCommand *nc);
118  static QString processNotification(NetworkCommand *nc);
119  QString processTheme(NetworkCommand *nc);
120  QString processHelp(NetworkCommand *nc);
121 
122  void notifyDataAvailable(void);
123  void sendReplyToClient(NetworkControlClient *ncc, QString &reply);
124  void customEvent(QEvent *e) override; // QObject
125 
126  static QString listRecordings(const QString& chanid = "", const QString& starttime = "");
127  static QString listSchedule(const QString& chanID = "") ;
128  static QString listChannels(uint start, uint limit) ;
129  static QString saveScreenshot(NetworkCommand *nc);
130 
132 
134 
135  static QString getWidgetType(MythUIType *type);
136 
137  QString m_prompt {"# "};
138  bool m_gotAnswer {false};
139  QString m_answer {""};
140  QMap <QString, QString> m_jumpMap;
141  QMap <QString, int> m_keyMap;
142  QMap <int, QString> m_keyTextMap;
143 
144  mutable QMutex m_clientLock {QMutex::Recursive};
145  QList<NetworkControlClient*> m_clients;
146 
147  QList<NetworkCommand*> m_networkControlCommands; // protected by ncLock
148  QMutex m_ncLock;
149  QWaitCondition m_ncCond; // protected by ncLock
150 
151  QList<NetworkCommand*> m_networkControlReplies;
152  QMutex m_nrLock;
153 
155  bool m_stopCommandThread {false}; // protected by ncLock
156 };
157 
158 #endif
159 
160 /* vim: set expandtab tabstop=4 shiftwidth=4: */
161 
e
QDomElement e
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1420
NetworkCommand
Definition: networkcontrol.h:44
NetworkControl::newControlConnection
void newControlConnection(QTcpSocket *client)
Definition: networkcontrol.cpp:365
NetworkCommand::m_args
QStringList m_args
Definition: networkcontrol.h:74
NetworkControl::m_networkControlCommands
QList< NetworkCommand * > m_networkControlCommands
Definition: networkcontrol.h:147
NetworkControl::saveScreenshot
static QString saveScreenshot(NetworkCommand *nc)
Definition: networkcontrol.cpp:1811
NetworkControl::notifyDataAvailable
void notifyDataAvailable(void)
Definition: networkcontrol.cpp:1528
NetworkControlCloseEvent::m_networkControlClient
NetworkControlClient * m_networkControlClient
Definition: networkcontrol.h:88
NetworkControl::processLiveTV
QString processLiveTV(NetworkCommand *nc)
NetworkControl::m_ncLock
QMutex m_ncLock
Definition: networkcontrol.h:148
NetworkControl::m_keyMap
QMap< QString, int > m_keyMap
Definition: networkcontrol.h:141
NetworkControl::sendReplyToClient
void sendReplyToClient(NetworkControlClient *ncc, QString &reply)
Definition: networkcontrol.cpp:1534
NetworkControlClient::m_textStream
QTextStream * m_textStream
Definition: networkcontrol.h:41
ServerPool
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
NetworkControlClient::readClient
void readClient()
Definition: networkcontrol.cpp:409
NetworkControl::receiveCommand
void receiveCommand(QString &command)
Definition: networkcontrol.cpp:437
NetworkControl
Definition: networkcontrol.h:94
NetworkControl::getWidgetType
static QString getWidgetType(MythUIType *type)
Definition: networkcontrol.cpp:1111
NetworkCommand::getFrom
QString getFrom(int arg)
Definition: networkcontrol.cpp:1839
arg
arg(title).arg(filename).arg(doDelete))
NetworkControlClient::commandReceived
void commandReceived(QString &)
NetworkControl::m_gotAnswer
bool m_gotAnswer
Definition: networkcontrol.h:138
MainServer
Definition: mainserver.h:113
NetworkControlClient
Definition: networkcontrol.h:23
NetworkControl::processSet
static QString processSet(NetworkCommand *nc)
Definition: networkcontrol.cpp:1070
NetworkControl::processHelp
QString processHelp(NetworkCommand *nc)
Definition: networkcontrol.cpp:1318
NetworkControl::m_stopCommandThread
bool m_stopCommandThread
Definition: networkcontrol.h:155
NetworkControlCloseEvent
Definition: networkcontrol.h:77
NetworkControl::deleteClient
void deleteClient(void)
Definition: networkcontrol.cpp:334
NetworkControl::listChannels
static QString listChannels(uint start, uint limit)
Definition: networkcontrol.cpp:1761
NetworkControlClient::m_socket
QTcpSocket * m_socket
Definition: networkcontrol.h:40
NetworkControl::processNetworkControlCommand
void processNetworkControlCommand(NetworkCommand *nc)
Definition: networkcontrol.cpp:289
NetworkControl::~NetworkControl
~NetworkControl() override
Definition: networkcontrol.cpp:243
NetworkCommand::NetworkCommand
NetworkCommand(NetworkControlClient *cli, const QString &c)
Definition: networkcontrol.h:48
NetworkControl::processQuery
QString processQuery(NetworkCommand *nc)
Definition: networkcontrol.cpp:913
uint
unsigned int uint
Definition: compat.h:140
NetworkCommand::operator=
NetworkCommand & operator=(NetworkCommand const &nc)
Definition: networkcontrol.h:55
NetworkControl::m_keyTextMap
QMap< int, QString > m_keyTextMap
Definition: networkcontrol.h:142
NetworkControl::m_nrLock
QMutex m_nrLock
Definition: networkcontrol.h:152
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:84
NetworkCommand::getArgCount
int getArgCount()
Definition: networkcontrol.h:68
NetworkControl::listRecordings
static QString listRecordings(const QString &chanid="", const QString &starttime="")
Definition: networkcontrol.cpp:1705
NetworkControlClient::~NetworkControlClient
~NetworkControlClient() override
Definition: networkcontrol.cpp:401
NetworkCommand::m_command
QString m_command
Definition: networkcontrol.h:72
NetworkControl::m_commandThread
MThread * m_commandThread
Definition: networkcontrol.h:154
NetworkControl::m_prompt
QString m_prompt
Definition: networkcontrol.h:137
NetworkControlClient::NetworkControlClient
NetworkControlClient(QTcpSocket *s)
Definition: networkcontrol.cpp:393
NetworkControlClient::getTextStream
QTextStream * getTextStream()
Definition: networkcontrol.h:31
NetworkCommand::m_client
NetworkControlClient * m_client
Definition: networkcontrol.h:73
NetworkControlCloseEvent::NetworkControlCloseEvent
NetworkControlCloseEvent(NetworkControlClient *ncc)
Definition: networkcontrol.h:80
NetworkControl::processNotification
static QString processNotification(NetworkCommand *nc)
Definition: networkcontrol.cpp:1516
serverpool.h
NetworkControl::m_jumpMap
QMap< QString, QString > m_jumpMap
Definition: networkcontrol.h:140
NetworkControl::NetworkControl
NetworkControl()
Definition: networkcontrol.cpp:76
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
NetworkControl::processKey
QString processKey(NetworkCommand *nc)
Definition: networkcontrol.cpp:472
NetworkControl::processTheme
QString processTheme(NetworkCommand *nc)
Definition: networkcontrol.cpp:1155
NetworkControl::run
void run(void) override
Definition: networkcontrol.cpp:271
NetworkControl::m_ncCond
QWaitCondition m_ncCond
Definition: networkcontrol.h:149
NetworkControl::processMessage
static QString processMessage(NetworkCommand *nc)
Definition: networkcontrol.cpp:1503
NetworkControl::m_answer
QString m_answer
Definition: networkcontrol.h:139
NetworkControl::listSchedule
static QString listSchedule(const QString &chanID="")
Definition: networkcontrol.cpp:1650
NetworkCommand::getClient
NetworkControlClient * getClient()
Definition: networkcontrol.h:66
NetworkControl::processJump
QString processJump(NetworkCommand *nc)
Definition: networkcontrol.cpp:451
NetworkControlCloseEvent::kEventType
static Type kEventType
Definition: networkcontrol.h:85
NetworkControl::m_clients
QList< NetworkControlClient * > m_clients
Definition: networkcontrol.h:145
NetworkControl::processPlay
QString processPlay(NetworkCommand *nc, int clientID)
Definition: networkcontrol.cpp:572
NetworkCommand::getCommand
QString getCommand()
Definition: networkcontrol.h:65
NetworkControlCloseEvent::getClient
NetworkControlClient * getClient()
Definition: networkcontrol.h:83
NetworkControl::customEvent
void customEvent(QEvent *e) override
Definition: networkcontrol.cpp:1563
NetworkCommand::getArg
QString getArg(int arg)
Definition: networkcontrol.h:67
NetworkControlClient::getSocket
QTcpSocket * getSocket()
Definition: networkcontrol.h:30
NetworkControl::m_clientLock
QMutex m_clientLock
Definition: networkcontrol.h:144
NetworkControl::m_networkControlReplies
QList< NetworkCommand * > m_networkControlReplies
Definition: networkcontrol.h:151