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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #define SERVER_PORT 1234
  23. /**
  24. * Class used for generating a server and communicating through it with monitor
  25. *
  26. * @brief Communication class with monitor (server)
  27. *
  28. */
  29. class ComMonitor {
  30. public:
  31. /**
  32. * Constructor
  33. */
  34. ComMonitor() {}
  35. /**
  36. * Destructor
  37. */
  38. virtual ~ComMonitor() {}
  39. /**
  40. * Create a server and open a socket over TCP
  41. *
  42. * @param port Port used for communication
  43. * @return Socket number
  44. * @throw std::runtime_error if it fails
  45. */
  46. int Open(int port);
  47. /**
  48. * Close socket and server
  49. */
  50. void Close();
  51. /**
  52. * Wait for a client to connect
  53. * @return Client number
  54. * @throw std::runtime_error if it fails
  55. */
  56. int AcceptClient();
  57. /**
  58. * Send a message to monitor
  59. *
  60. * @param msg Message to send to monitor
  61. * @attention Message given in parameter will be destroyed (delete) after being sent. No need for user to delete message after that.
  62. * @warning Write is not thread safe : check that multiple tasks can't access this method simultaneously
  63. */
  64. void Write(Message &msg);
  65. /**
  66. * Function called at beginning of Write method
  67. * Use it to do some synchronization (call of mutex, for example)
  68. */
  69. virtual void Write_Pre() {}
  70. /**
  71. * Function called at end of Write method
  72. * Use it to do some synchronization (release of mutex, for example)
  73. */
  74. virtual void Write_Post() {}
  75. /**
  76. * Receive a message from monitor
  77. *
  78. * @return Message received from monitor
  79. * @attention Message provided is produced by the method. You must delete it when you are done using it
  80. * @warning Read is not thread safe : check that multiple tasks can't access this method simultaneously
  81. */
  82. Message *Read();
  83. /**
  84. * Function called at beginning of Read method
  85. * Use it to do some synchronization (call of mutex, for example)
  86. */
  87. virtual void Read_Pre() {}
  88. /**
  89. * Function called at end of Read method
  90. * Use it to do some synchronization (release of mutex, for example)
  91. */
  92. virtual void Read_Post() {}
  93. protected:
  94. /**
  95. * Socket descriptor
  96. */
  97. int socketFD = -1;
  98. /**
  99. * Socket descriptor
  100. */
  101. int clientID = -1;
  102. /**
  103. * Method used internally to convert a message content to a string that can be sent over TCP
  104. * @param msg Message to be converted
  105. * @return A string, image of the message
  106. */
  107. string MessageToString(Message &msg);
  108. /**
  109. * Method used internally to convert a string received over TCP to a message
  110. * @param s String containing message
  111. * @return A message, image of the string
  112. */
  113. Message *StringToMessage(string &s);
  114. };
  115. #endif /* __COMMONITOR_H__ */