SshClient.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  23. SSH Client exclusively for connecting to QWNET's central.
  24. It depends on openssh installed on the system and opens a process
  25. with the ssh binary to make all the communication.
  26. @author Mihawk <luiz@netdome.biz>
  27. @file SshClient.h
  28. */
  29. class SshClient : public QObject
  30. {
  31. Q_OBJECT
  32. public:
  33. // Error codes
  34. enum Error { NoError, ConnectionTimedOutError };
  35. /**
  36. Constructor
  37. @param app The main application
  38. @param parent The parent object if possible
  39. */
  40. explicit SshClient(App* app, QObject *parent = 0);
  41. /**
  42. Destructor
  43. */
  44. ~SshClient();
  45. /**
  46. Connects to a given ssh server.
  47. @param user Username
  48. @param host Hostname
  49. */
  50. bool connectToHost(const QString& user, const QString &host);
  51. /**
  52. Returns whether we are connected right now.
  53. @return True if connected, false otherwise
  54. */
  55. bool isConnected() const;
  56. /**
  57. Disconnects from current server.
  58. */
  59. void disconnectFromHost();
  60. /**
  61. Writes data to the server.
  62. @param data Data to write
  63. */
  64. void write(const QString& data);
  65. signals:
  66. /**
  67. Emitted when an error occurs.
  68. @param errorCode The error code
  69. */
  70. void error(Error errorCode);
  71. /**
  72. Emitted when we just connected to a server.
  73. */
  74. void connected();
  75. protected:
  76. /**
  77. Handles events that occur periodically like PING PONG and connection timeout.
  78. @param e Pointer to the event that happened
  79. */
  80. void timerEvent(QTimerEvent *e);
  81. private slots:
  82. /**
  83. Called when there is data to be read coming from the server.
  84. */
  85. void read();
  86. /**
  87. Called when the ssh binary process has exited.
  88. @param exitCode The exit code
  89. */
  90. void exited(int exitCode);
  91. private:
  92. App* myApp; // The app
  93. QProcess* myProcess; // The ssh process
  94. QRegExp* myCommandRegex; // Regex used on every command, so it was made persistent
  95. bool myConnectedFlag; // Are we connected?
  96. int myConnectionTimerID; // Times out connection process
  97. int myPingTimerID; // PING? PONG! Checks if the connection is still alive
  98. int myPongTimerID; // PING? PONG! Checks if the connection is still alive
  99. QStringList myClients; // List of other clients connected to central
  100. QString myUsername; // My username on central
  101. QString myHostname; // Central's hostname
  102. QStringList myRoles; // My roles on central
  103. QStringList myCommands; // The list of commands that I can issue
  104. /**
  105. Parses all commands coming from the server and execute the appropriate
  106. command.
  107. @param time Time the message arrived
  108. @param command The command that just arrived
  109. @param commandData The command data
  110. */
  111. void parse(const QDateTime& time, const QString& command, const QString& commandData);
  112. /**
  113. Ping and pong functions used to keep the connection alive.
  114. */
  115. void ping();
  116. void pong();
  117. /**
  118. Reconnects to the server.
  119. */
  120. void reconnect();
  121. };
  122. #endif // SSHCLIENT_H