No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commonitor.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2018 dimercur
  3. *
  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. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __COMMONITOR_H__
  18. #define __COMMONITOR_H__
  19. #include "messages.h"
  20. #include <string>
  21. using namespace std;
  22. /**
  23. * Class used for generating a server and communicating through it with monitor
  24. *
  25. * @brief Communication class with monitor (server)
  26. *
  27. */
  28. class ComMonitor {
  29. public:
  30. /**
  31. * Constructor
  32. */
  33. ComMonitor() {}
  34. /**
  35. * Destructor
  36. */
  37. virtual ~ComMonitor() {}
  38. /**
  39. * Create a server and open a socket over TCP
  40. *
  41. * @param port Port used for communication
  42. * @return Socket number
  43. * @throw std::runtime_error if it fails
  44. */
  45. int Open(int port);
  46. /**
  47. * Close socket and server
  48. */
  49. void Close();
  50. /**
  51. * Wait for a client to connect
  52. * @return Client number
  53. * @throw std::runtime_error if it fails
  54. */
  55. int AcceptClient();
  56. /**
  57. * Send a message to monitor
  58. *
  59. * @param msg Message to send to monitor
  60. * @attention Message given in parameter will be destroyed (delete) after being sent. No need for user to delete message after that.
  61. * @warning Write is not thread safe : check that multiple tasks can't access this method simultaneously
  62. */
  63. void Write(Message &msg);
  64. /**
  65. * Function called at beginning of Write method
  66. * Use it to do some synchronization (call of mutex, for example)
  67. */
  68. virtual void Write_Pre() {}
  69. /**
  70. * Function called at end of Write method
  71. * Use it to do some synchronization (release of mutex, for example)
  72. */
  73. virtual void Write_Post() {}
  74. /**
  75. * Receive a message from monitor
  76. *
  77. * @return Message received from monitor
  78. * @attention Message provided is produced by the method. You must delete it when you are done using it
  79. * @warning Read is not thread safe : check that multiple tasks can't access this method simultaneously
  80. */
  81. Message *Read();
  82. /**
  83. * Function called at beginning of Read method
  84. * Use it to do some synchronization (call of mutex, for example)
  85. */
  86. virtual void Read_Pre() {}
  87. /**
  88. * Function called at end of Read method
  89. * Use it to do some synchronization (release of mutex, for example)
  90. */
  91. virtual void Read_Post() {}
  92. protected:
  93. /**
  94. * Socket descriptor
  95. */
  96. int socketFD = -1;
  97. /**
  98. * Socket descriptor
  99. */
  100. int clientID = -1;
  101. /**
  102. * Method used internally to convert a message content to a string that can be sent over TCP
  103. * @param msg Message to be converted
  104. * @return A string, image of the message
  105. */
  106. string MessageToString(Message &msg);
  107. /**
  108. * Method used internally to convert a string received over TCP to a message
  109. * @param s String containing message
  110. * @return A message, image of the string
  111. */
  112. Message *StringToMessage(string &s);
  113. };
  114. #endif /* __COMMONITOR_H__ */