modchannels.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Minetest
  3. Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "modchannels.h"
  17. #include <algorithm>
  18. #include <cassert>
  19. #include "util/basic_macros.h"
  20. bool ModChannel::registerConsumer(session_t peer_id)
  21. {
  22. // ignore if peer_id already joined
  23. if (CONTAINS(m_client_consumers, peer_id))
  24. return false;
  25. m_client_consumers.push_back(peer_id);
  26. return true;
  27. }
  28. bool ModChannel::removeConsumer(session_t peer_id)
  29. {
  30. bool found = false;
  31. auto peer_removal_fct = [peer_id, &found](u16 p) {
  32. if (p == peer_id)
  33. found = true;
  34. return p == peer_id;
  35. };
  36. m_client_consumers.erase(
  37. std::remove_if(m_client_consumers.begin(),
  38. m_client_consumers.end(), peer_removal_fct),
  39. m_client_consumers.end());
  40. return found;
  41. }
  42. bool ModChannel::canWrite() const
  43. {
  44. return m_state == MODCHANNEL_STATE_READ_WRITE;
  45. }
  46. void ModChannel::setState(ModChannelState state)
  47. {
  48. assert(state != MODCHANNEL_STATE_INIT);
  49. m_state = state;
  50. }
  51. bool ModChannelMgr::channelRegistered(const std::string &channel) const
  52. {
  53. return m_registered_channels.find(channel) != m_registered_channels.end();
  54. }
  55. ModChannel *ModChannelMgr::getModChannel(const std::string &channel)
  56. {
  57. if (!channelRegistered(channel))
  58. return nullptr;
  59. return m_registered_channels[channel].get();
  60. }
  61. bool ModChannelMgr::canWriteOnChannel(const std::string &channel) const
  62. {
  63. const auto channel_it = m_registered_channels.find(channel);
  64. if (channel_it == m_registered_channels.end()) {
  65. return false;
  66. }
  67. return channel_it->second->canWrite();
  68. }
  69. void ModChannelMgr::registerChannel(const std::string &channel)
  70. {
  71. m_registered_channels[channel] = std::make_unique<ModChannel>(channel);
  72. }
  73. bool ModChannelMgr::setChannelState(const std::string &channel, ModChannelState state)
  74. {
  75. if (!channelRegistered(channel))
  76. return false;
  77. auto channel_it = m_registered_channels.find(channel);
  78. channel_it->second->setState(state);
  79. return true;
  80. }
  81. bool ModChannelMgr::removeChannel(const std::string &channel)
  82. {
  83. if (!channelRegistered(channel))
  84. return false;
  85. m_registered_channels.erase(channel);
  86. return true;
  87. }
  88. bool ModChannelMgr::joinChannel(const std::string &channel, session_t peer_id)
  89. {
  90. if (!channelRegistered(channel))
  91. registerChannel(channel);
  92. return m_registered_channels[channel]->registerConsumer(peer_id);
  93. }
  94. bool ModChannelMgr::leaveChannel(const std::string &channel, session_t peer_id)
  95. {
  96. if (!channelRegistered(channel))
  97. return false;
  98. // Remove consumer from channel
  99. bool consumerRemoved = m_registered_channels[channel]->removeConsumer(peer_id);
  100. // If channel is empty, remove it
  101. if (m_registered_channels[channel]->getChannelPeers().empty()) {
  102. removeChannel(channel);
  103. }
  104. return consumerRemoved;
  105. }
  106. void ModChannelMgr::leaveAllChannels(session_t peer_id)
  107. {
  108. for (auto &channel_it : m_registered_channels)
  109. channel_it.second->removeConsumer(peer_id);
  110. }
  111. static std::vector<u16> empty_channel_list;
  112. const std::vector<u16> &ModChannelMgr::getChannelPeers(const std::string &channel) const
  113. {
  114. const auto &channel_it = m_registered_channels.find(channel);
  115. if (channel_it == m_registered_channels.end())
  116. return empty_channel_list;
  117. return channel_it->second->getChannelPeers();
  118. }