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.

140 lines
3.4 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. //
  2. // ccallsignlistitem.cpp
  3. // m17ref
  4. //
  5. // Created by Jean-Luc Deltombe (LX3JL) on 31/01/2016.
  6. // Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
  7. //
  8. // ----------------------------------------------------------------------------
  9. // This file is part of m17ref.
  10. //
  11. // m17ref is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // m17ref is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // with this software. If not, see <http://www.gnu.org/licenses/>.
  23. // ----------------------------------------------------------------------------
  24. #include <string.h>
  25. #include "main.h"
  26. #include "callsignlistitem.h"
  27. ////////////////////////////////////////////////////////////////////////////////////////
  28. // constructor
  29. CCallsignListItem::CCallsignListItem() {}
  30. CCallsignListItem::CCallsignListItem(const CCallsign &callsign, const CIp &ip, const char *modules)
  31. {
  32. m_Callsign = callsign;
  33. m_Ip = ip;
  34. m_Ip.SetPort(M17_PORT);
  35. m_Mods.clear();
  36. if ( modules != nullptr )
  37. {
  38. if ( modules[0] == '*' )
  39. {
  40. for ( char i = 0; i < NB_OF_MODULES; i++ )
  41. {
  42. m_Mods.append(1, 'A' + i);
  43. }
  44. }
  45. else
  46. {
  47. for (const char *p=modules; *p; p++)
  48. {
  49. // duplicates not allowed!
  50. if (m_Mods.npos == (m_Mods.find(*p)))
  51. {
  52. int i = *p - 'A';
  53. // don't add mods that aren't configured
  54. if (i >= 0 && i < NB_OF_MODULES)
  55. {
  56. m_Mods.append(1, *p);
  57. }
  58. else
  59. {
  60. std::cerr << "Peer module " << *p << " is not configured!" << std::endl;
  61. }
  62. }
  63. else
  64. {
  65. std::cout << "Warning: Module " << *p << " is listed multiple times!" << std::endl;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. CCallsignListItem::CCallsignListItem(const CCallsign &callsign, const char *url, const char *modules)
  72. {
  73. m_Callsign = callsign;
  74. m_Ip = CIp(strchr(url, ':') ? AF_INET6 : AF_INET, M17_PORT, url);
  75. m_Mods.assign(modules);
  76. }
  77. CCallsignListItem::CCallsignListItem(const CCallsignListItem &item)
  78. {
  79. m_Callsign = item.m_Callsign;
  80. m_Ip = item.m_Ip;
  81. m_Mods.assign(item.m_Mods);
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////////////
  84. // compare
  85. bool CCallsignListItem::HasSameCallsign(const CCallsign &callsign) const
  86. {
  87. return m_Callsign.HasSameCallsign(callsign);
  88. }
  89. bool CCallsignListItem::HasSameCallsignWithWildcard(const CCallsign &callsign) const
  90. {
  91. return m_Callsign.HasSameCallsignWithWildcard(callsign);
  92. }
  93. bool CCallsignListItem::HasModuleListed(char module) const
  94. {
  95. return m_Mods.npos != m_Mods.find(module);
  96. }
  97. bool CCallsignListItem::HasSameIp(const CIp &ip)
  98. {
  99. return ip == m_Ip;
  100. }
  101. bool CCallsignListItem::CheckListedModules(const char *mods) const
  102. {
  103. if (mods == nullptr)
  104. return false;
  105. // make sure every mods character is matched in m_Mods
  106. const auto count = m_Mods.size();
  107. bool found[count];
  108. for (unsigned i=0; i<count; i++)
  109. found[i] = false;
  110. for (auto p=mods; *p; p++)
  111. {
  112. auto pos = m_Mods.find(*p);
  113. if (pos == m_Mods.npos)
  114. return false;
  115. else
  116. found[pos] = true;
  117. }
  118. for (unsigned i=0; i<count; i++)
  119. {
  120. if (! found[i])
  121. return false;
  122. }
  123. return true;
  124. }