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.

openvibeStimulationConnection.h 2.0KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <sys/timeb.h>
  3. #include <boost/interprocess/ipc/message_queue.hpp>
  4. #include <iostream> // log
  5. #include <utility>
  6. namespace OpenViBE {
  7. class CStimulationConnection
  8. {
  9. public:
  10. /**
  11. * Create a new OpenvibeStimulationConnection object.
  12. *
  13. * Initiates the message queue.
  14. *
  15. * @param queueName The name of the queue.
  16. * @throw boost::interprocess::interprocess_exception Throws an
  17. * interprocess exception in the queue fails to be created.
  18. */
  19. explicit CStimulationConnection(std::string queueName = "openvibeExternalStimulations")
  20. : m_messageQueueName(std::move(queueName))
  21. {
  22. boost::interprocess::message_queue::remove(m_messageQueueName.c_str());
  23. try
  24. {
  25. m_messageQueue = new boost::interprocess::message_queue(boost::interprocess::create_only, m_messageQueueName.c_str(), m_maxMessages,
  26. m_chunkLength * sizeof(uint64_t));
  27. }
  28. catch (boost::interprocess::interprocess_exception& exception)
  29. {
  30. std::cout << exception.what() << std::endl; // log
  31. throw;
  32. }
  33. }
  34. /**
  35. * Send a stimulation to the OpenViBE Acquisition server on the initiated
  36. * queue.
  37. *
  38. * @throw boost::interprocess::interprocess_exception Throws an exception
  39. * if the message sending failed.
  40. */
  41. void sendStimulation(const uint64_t stimulationID) const
  42. {
  43. struct timeb currentTime;
  44. ftime(&currentTime);
  45. const uint64_t stimulationTime = currentTime.time * 1000 + currentTime.millitm;
  46. uint64_t message[3];
  47. message[0] = 0; // unused at the moment
  48. message[1] = stimulationID;
  49. message[2] = stimulationTime;
  50. try { m_messageQueue->send(&message, sizeof(message), 0); }
  51. catch (boost::interprocess::interprocess_exception& exception)
  52. {
  53. std::cout << exception.what() << std::endl; // log
  54. throw;
  55. }
  56. }
  57. protected:
  58. std::string m_messageQueueName;
  59. int m_chunkLength = 3;
  60. int m_maxMessages = 5000;
  61. // openvibe currently uses messages of length of 3
  62. boost::interprocess::message_queue* m_messageQueue = nullptr;
  63. };
  64. } // namespace OpenViBE