MulticastTopology.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <algorithm>
  28. #include "Constants.hpp"
  29. #include "MulticastTopology.hpp"
  30. #include "Topology.hpp"
  31. namespace ZeroTier {
  32. MulticastTopology::MulticastTopology()
  33. {
  34. }
  35. MulticastTopology::~MulticastTopology()
  36. {
  37. }
  38. void MulticastTopology::clean(const Topology &topology)
  39. {
  40. uint64_t now = Utils::now();
  41. for(std::map< MulticastGroup,std::vector<MulticastGroupMember> >::iterator mm(_members.begin());mm!=_members.end();) {
  42. std::vector<MulticastGroupMember>::iterator reader(mm->second.begin());
  43. std::vector<MulticastGroupMember>::iterator writer(mm->second.begin());
  44. unsigned long count = 0;
  45. while (reader != mm->second.end()) {
  46. if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
  47. *writer = *reader;
  48. /* We sort in ascending order of most recent relevant activity. For peers we've learned
  49. * about by direct LIKEs, we do this in order of their own activity. For indirectly
  50. * acquired peers we do this minus a constant to place these categorically below directly
  51. * learned peers. For peers with no active Peer record, we use the time we last learned
  52. * about them minus one day (a large constant) to put these at the bottom of the list.
  53. * List is sorted in ascending order of rank and multicasts are sent last-to-first. */
  54. if (writer->learnedFrom) {
  55. SharedPtr<Peer> p(topology.getPeer(writer->learnedFrom));
  56. if (p)
  57. writer->rank = p->lastUnicastFrame() - ZT_MULTICAST_LIKE_EXPIRE;
  58. else writer->rank = writer->timestamp - 86400000;
  59. } else {
  60. SharedPtr<Peer> p(topology.getPeer(writer->address));
  61. if (p)
  62. writer->rank = p->lastUnicastFrame();
  63. else writer->rank = writer->timestamp - 86400000;
  64. }
  65. ++writer;
  66. ++count;
  67. }
  68. ++reader;
  69. }
  70. if (count) {
  71. mm->second.resize(count);
  72. std::sort(mm->second.begin(),mm->second.end()); // sorts in ascending order of rank
  73. ++mm;
  74. } else _members.erase(mm++);
  75. }
  76. }
  77. } // namespace ZeroTier