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 *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  {
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;
72  NetworkControlClient *m_client {nullptr};
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:
87  NetworkControlClient *m_networkControlClient {nullptr};
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() override;
100 
101  private slots:
102  void newConnection(QTcpSocket *client);
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  static QString processSet(NetworkCommand *nc);
116  static QString processMessage(NetworkCommand *nc);
117  static 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  static QString listRecordings(const QString& chanid = "", const QString& starttime = "");
126  static QString listSchedule(const QString& chanID = "") ;
127  static QString listChannels(uint start, uint limit) ;
128  static QString saveScreenshot(NetworkCommand *nc);
129 
130  void processNetworkControlCommand(NetworkCommand *nc);
131 
132  void deleteClient(NetworkControlClient *ncc);
133 
134  static QString getWidgetType(MythUIType *type);
135 
136  QString m_prompt {"# "};
137  bool m_gotAnswer {false};
138  QString m_answer {""};
139  QMap <QString, QString> m_jumpMap;
140  QMap <QString, int> m_keyMap;
141  QMap <int, QString> m_keyTextMap;
142 
143  mutable QMutex m_clientLock {QMutex::Recursive};
144  QList<NetworkControlClient*> m_clients;
145 
146  QList<NetworkCommand*> m_networkControlCommands; // protected by ncLock
147  QMutex m_ncLock;
148  QWaitCondition m_ncCond; // protected by ncLock
149 
150  QList<NetworkCommand*> m_networkControlReplies;
151  QMutex m_nrLock;
152 
153  MThread *m_commandThread {nullptr};
154  bool m_stopCommandThread {false}; // protected by ncLock
155 };
156 
157 #endif
158 
159 /* vim: set expandtab tabstop=4 shiftwidth=4: */
160 
QList< NetworkCommand * > m_networkControlCommands
NetworkControlClient * m_client
NetworkControlClient * getClient()
QMap< int, QString > m_keyTextMap
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
unsigned int slots[4]
Definition: element.c:38
QWaitCondition m_ncCond
QTcpSocket * getSocket()
NetworkControlClient * getClient()
QList< NetworkCommand * > m_networkControlReplies
QMap< QString, int > m_keyMap
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
QList< NetworkControlClient * > m_clients
NetworkControlCloseEvent(NetworkControlClient *ncc)
unsigned int uint
Definition: compat.h:140
QMap< QString, QString > m_jumpMap
Manages a collection of sockets listening on different ports.
Definition: serverpool.h:59
QString getCommand()
QString getArg(int arg)
NetworkCommand & operator=(NetworkCommand const &nc)
NetworkCommand(NetworkControlClient *cli, const QString &c)
QStringList m_args
QTextStream * getTextStream()