SshClient.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <QString>
  19. class QProcess;
  20. class QDateTime;
  21. class QStringList;
  22. class App;
  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);
  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. private:
  93. App* myApp;
  94. QProcess* myProcess;
  95. QRegExp* myCommandRegex;
  96. bool myConnectedFlag;
  97. int myConnectionTimerID;
  98. int myPingTimerID;
  99. int myPongTimerID;
  100. QStringList* myClients;
  101. QString myUsername;
  102. QString myHostname;
  103. /**
  104. Parses all commands coming from the server and execute the appropriate
  105. command.
  106. @param time Time the message arrived
  107. @param command The command that just arrived
  108. @param commandData The command data
  109. */
  110. void parse(const QDateTime& time, const QString& command, const QString& commandData);
  111. /**
  112. Ping and pong functions used to keep the connection alive.
  113. */
  114. void ping();
  115. void pong();
  116. /**
  117. Reconnects to the server.
  118. */
  119. void reconnect();
  120. };
  121. #endif // SSHCLIENT_H