MythTV  master
networkcontrol.h
Go to the documentation of this file.
1 #ifndef NETWORKCONTROL_H_
2 #define NETWORKCONTROL_H_
3 
4 #include <deque>
5 using namespace std;
6 
7 #include <QWaitCondition>
8 #include <QStringList>
9 #include <QTcpSocket>
10 #include <QRunnable>
11 #include <QMutex>
12 #include <QEvent>
13 
14 #include "mthread.h"
15 #include "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 *);
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;
42  QTextStream *m_textStream;
43 };
44 
45 class NetworkCommand : public QObject
46 {
47  Q_OBJECT
48  public:
50  {
51  m_command = c.trimmed();
52  m_client = cli;
53  m_args = m_command.simplified().split(" ");
54  }
55 
57  {
58  m_command = nc.m_command;
59  m_client = nc.m_client;
60  m_args = m_command.simplified().split(" ");
61  return *this;
62  }
63 
64  QString getCommand() { return m_command; }
65  NetworkControlClient *getClient() { return m_client; }
66  QString getArg(int arg) { return m_args[arg]; }
67  int getArgCount() { return m_args.size(); }
68  QString getFrom(int arg);
69 
70  private:
71  QString m_command;
73  QStringList m_args;
74 };
75 
76 class NetworkControlCloseEvent : public QEvent
77 {
78  public:
80  QEvent(kEventType), m_networkControlClient(ncc) {}
81 
82  NetworkControlClient *getClient() { return m_networkControlClient; }
83 
84  static Type kEventType;
85 
86  private:
88 };
89 
90 class NetworkControl;
91 class MythUIType;
92 
93 class NetworkControl : public ServerPool, public QRunnable
94 {
95  Q_OBJECT
96 
97  public:
99  ~NetworkControl();
100 
101  private slots:
102  void newConnection(QTcpSocket *socket);
103  void receiveCommand(QString &command);
104  void deleteClient(void);
105 
106  protected:
107  void run(void) override; // QRunnable
108 
109  private:
110  QString processJump(NetworkCommand *nc);
111  QString processKey(NetworkCommand *nc);
112  QString processLiveTV(NetworkCommand *nc);
113  QString processPlay(NetworkCommand *nc, int clientID);
114  QString processQuery(NetworkCommand *nc);
115  QString processSet(NetworkCommand *nc);
116  QString processMessage(NetworkCommand *nc);
117  QString processNotification(NetworkCommand *nc);
118  QString processTheme(NetworkCommand *nc);
119  QString processHelp(NetworkCommand *nc);
120 
121  void notifyDataAvailable(void);
122  void sendReplyToClient(NetworkControlClient *ncc, QString &reply);
123  void customEvent(QEvent *e) override; // QObject
124 
125  QString listRecordings(QString chanid = "", QString starttime = "");
126  QString listSchedule(const QString& chanID = "") const;
127  QString listChannels(const uint start, const uint limit) const;
128  QString saveScreenshot(NetworkCommand *nc);
129 
130  void processNetworkControlCommand(NetworkCommand *nc);
131 
132  void deleteClient(NetworkControlClient *ncc);
133 
134  QString getWidgetType(MythUIType *type);
135 
136  QString prompt;
137  bool gotAnswer;
138  QString answer;
139  QMap <QString, QString> jumpMap;
140  QMap <QString, int> keyMap;
141  QMap <int, QString> keyTextMap;
142 
143  mutable QMutex clientLock;
144  QList<NetworkControlClient*> clients;
145 
146  QList<NetworkCommand*> networkControlCommands; // protected by ncLock
147  QMutex ncLock;
148  QWaitCondition ncCond; // protected by ncLock
149 
150  QList<NetworkCommand*> networkControlReplies;
151  QMutex nrLock;
152 
154  bool stopCommandThread; // protected by ncLock
155 };
156 
157 #endif
158 
159 /* vim: set expandtab tabstop=4 shiftwidth=4: */
160 
QTcpSocket * m_socket
NetworkControlClient * m_client
QList< NetworkCommand * > networkControlCommands
NetworkControlClient * getClient()
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
unsigned int slots[4]
Definition: element.c:38
QMap< QString, QString > jumpMap
NetworkCommand(NetworkControlClient *cli, QString c)
QMap< int, QString > keyTextMap
QTcpSocket * getSocket()
NetworkControlClient * getClient()
unsigned int uint
Definition: compat.h:140
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
QList< NetworkCommand * > networkControlReplies
NetworkControlCloseEvent(NetworkControlClient *ncc)
QWaitCondition ncCond
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
MThread * commandThread
QString getCommand()
QMap< QString, int > keyMap
QString getArg(int arg)
NetworkCommand & operator=(NetworkCommand const &nc)
QTextStream * m_textStream
QStringList m_args
QTextStream * getTextStream()
NetworkControlClient * m_networkControlClient
QList< NetworkControlClient * > clients