modchannels.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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] =
  72. std::unique_ptr<ModChannel>(new ModChannel(channel));
  73. }
  74. bool ModChannelMgr::setChannelState(const std::string &channel, ModChannelState state)
  75. {
  76. if (!channelRegistered(channel))
  77. return false;
  78. auto channel_it = m_registered_channels.find(channel);
  79. channel_it->second->setState(state);
  80. return true;
  81. }
  82. bool ModChannelMgr::removeChannel(const std::string &channel)
  83. {
  84. if (!channelRegistered(channel))
  85. return false;
  86. m_registered_channels.erase(channel);
  87. return true;
  88. }
  89. bool ModChannelMgr::joinChannel(const std::string &channel, session_t peer_id)
  90. {
  91. if (!channelRegistered(channel))
  92. registerChannel(channel);
  93. return m_registered_channels[channel]->registerConsumer(peer_id);
  94. }
  95. bool ModChannelMgr::leaveChannel(const std::string &channel, session_t peer_id)
  96. {
  97. if (!channelRegistered(channel))
  98. return false;
  99. // Remove consumer from channel
  100. bool consumerRemoved = m_registered_channels[channel]->removeConsumer(peer_id);
  101. // If channel is empty, remove it
  102. if (m_registered_channels[channel]->getChannelPeers().empty()) {
  103. removeChannel(channel);
  104. }
  105. return consumerRemoved;
  106. }
  107. void ModChannelMgr::leaveAllChannels(session_t peer_id)
  108. {
  109. for (auto &channel_it : m_registered_channels)
  110. channel_it.second->removeConsumer(peer_id);
  111. }
  112. static std::vector<u16> empty_channel_list;
  113. const std::vector<u16> &ModChannelMgr::getChannelPeers(const std::string &channel) const
  114. {
  115. const auto &channel_it = m_registered_channels.find(channel);
  116. if (channel_it == m_registered_channels.end())
  117. return empty_channel_list;
  118. return channel_it->second->getChannelPeers();
  119. }