SshClient.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #ifndef SSHCLIENT_H
  16. #define SSHCLIENT_H
  17. #include <QObject>
  18. #include <QStringList>
  19. class QProcess;
  20. class QDateTime;
  21. class App;
  22. class QHostAddress;
  23. /**
  24. SSH Client exclusively for connecting to QWNET's central.
  25. It depends on openssh installed on the system and opens a process
  26. with the ssh binary to make all the communication.
  27. @author Mihawk <luiz@netdome.biz>
  28. @file SshClient.h
  29. */
  30. class SshClient : public QObject
  31. {
  32. Q_OBJECT
  33. public:
  34. // Error codes
  35. enum Error { NoError, ConnectionTimedOutError };
  36. /**
  37. Constructor
  38. @param app The main application
  39. @param parent The parent object if possible
  40. */
  41. explicit SshClient(App* app, QObject *parent = 0);
  42. /**
  43. Destructor
  44. */
  45. ~SshClient();
  46. /**
  47. Connects to a given ssh server.
  48. @param user Username
  49. @param host Hostname
  50. */
  51. bool connectToHost(const QString& user, const QString &host, quint16 port);
  52. /**
  53. Returns whether we are connected right now.
  54. @return True if connected, false otherwise
  55. */
  56. bool isConnected() const;
  57. /**
  58. Disconnects from current server.
  59. */
  60. void disconnectFromHost();
  61. /**
  62. Writes data to the server.
  63. @param data Data to write
  64. */
  65. void write(const QString& data);
  66. signals:
  67. /**
  68. Emitted when an error occurs.
  69. @param errorCode The error code
  70. */
  71. void error(Error errorCode);
  72. /**
  73. Emitted when we just connected to a server.
  74. */
  75. void connected();
  76. protected:
  77. /**
  78. Handles events that occur periodically like PING PONG and connection timeout.
  79. @param e Pointer to the event that happened
  80. */
  81. void timerEvent(QTimerEvent *e);
  82. private slots:
  83. /**
  84. Called when there is data to be read coming from the server.
  85. */
  86. void read();
  87. /**
  88. Called when the ssh binary process has exited.
  89. @param exitCode The exit code
  90. */
  91. void exited(int exitCode);
  92. /**
  93. Called when a simple ping request to an QW server has finished
  94. @param host The server's address
  95. @param port The server's port
  96. @param ms The server's ping
  97. */
  98. void serverPong(const QHostAddress& host, quint16 port, int ms);
  99. /**
  100. Called when a simple ping request to an QW server has finished.
  101. This function reply to the REQ_ASSIGNMENTS request from central.
  102. The reason this is being done here is because central requests the ping
  103. in the answer.
  104. @param host The server's address
  105. @param port The server's port
  106. @param ms The server's ping
  107. */
  108. void assignmentsReply(const QHostAddress& host, quint16 port, int ms);
  109. private:
  110. App* myApp; // The app
  111. QProcess* myProcess; // The ssh process
  112. QRegExp* myCommandRegex; // Regex used on every command, so it was made persistent
  113. bool myConnectedFlag; // Are we connected?
  114. int myConnectionTimerID; // Times out connection process
  115. int myPingTimerID; // PING? PONG! Checks if the connection is still alive
  116. int myPongTimerID; // PING? PONG! Checks if the connection is still alive
  117. QStringList myClients; // List of other clients connected to central
  118. QString myUsername; // My username on central
  119. QString myHostname; // Central's hostname
  120. quint16 myPort; // Central's port number
  121. QStringList myRoles; // My roles on central
  122. QStringList myCommands; // The list of commands that I can issue
  123. /**
  124. Parses all commands coming from the server and execute the appropriate
  125. command.
  126. @param time Time the message arrived
  127. @param command The command that just arrived
  128. @param commandData The command data
  129. */
  130. void parse(const QDateTime& time, const QString& command, const QString& commandData);
  131. /**
  132. Ping and pong functions used to keep the connection alive.
  133. */
  134. void ping();
  135. void pong();
  136. /**
  137. Reconnects to the server.
  138. */
  139. void reconnect();
  140. };
  141. #endif // SSHCLIENT_H