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