aboutsummaryrefslogtreecommitdiff
path: root/xbmc/platform/posix/filesystem/SMBWSDiscovery.cpp
blob: c2f247da10aaa7dff3cbdd1daa759444ae050c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 *  Copyright (C) 2021- Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "SMBWSDiscovery.h"

#include "FileItem.h"
#include "FileItemList.h"
#include "ServiceBroker.h"
#include "network/IWSDiscovery.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

#include "platform/posix/filesystem/SMBWSDiscoveryListener.h"

#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>

using namespace std::chrono;
using namespace WSDiscovery;

namespace WSDiscovery
{
std::unique_ptr<IWSDiscovery> IWSDiscovery::GetInstance()
{
  return std::make_unique<WSDiscovery::CWSDiscoveryPosix>();
}

std::atomic<bool> CWSDiscoveryPosix::m_isInitialized{false};

CWSDiscoveryPosix::CWSDiscoveryPosix()
{
  // Set our wsd_instance ID to seconds since epoch
  auto epochduration = system_clock::now().time_since_epoch();
  wsd_instance_id = epochduration.count() * system_clock::period::num / system_clock::period::den;

  m_WSDListenerUDP = std::make_unique<CWSDiscoveryListenerUDP>();
  m_isInitialized = true;
}

CWSDiscoveryPosix::~CWSDiscoveryPosix()
{
  StopServices();
}

bool CWSDiscoveryPosix::StopServices()
{
  m_WSDListenerUDP->Stop();

  return true;
}

bool CWSDiscoveryPosix::StartServices()
{
  m_WSDListenerUDP->Start();

  return true;
}

bool CWSDiscoveryPosix::IsRunning()
{
  return m_WSDListenerUDP->IsRunning();
}

bool CWSDiscoveryPosix::GetServerList(CFileItemList& items)
{
  {
    std::unique_lock<CCriticalSection> lock(m_critWSD);

    for (const auto& item : m_vecWSDInfo)
    {
      auto found = item.computer.find('/');
      std::string host;
      if (found != std::string::npos)
        host = item.computer.substr(0, found);
      else
      {
        if (item.xaddrs_host.empty())
          continue;
        host = item.xaddrs_host;
      }

      CFileItemPtr pItem = std::make_shared<CFileItem>(host);
      pItem->SetPath("smb://" + host + '/');
      pItem->m_bIsFolder = true;
      items.Add(pItem);
    }
  }
  return true;
}

bool CWSDiscoveryPosix::GetCached(const std::string& strHostName, std::string& strIpAddress)
{
  const std::string match = strHostName + "/";

  std::unique_lock<CCriticalSection> lock(m_critWSD);
  for (const auto& item : m_vecWSDInfo)
  {
    if (!item.computer.empty() && StringUtils::StartsWithNoCase(item.computer, match))
    {
      strIpAddress = item.xaddrs_host;
      CLog::Log(LOGDEBUG, LOGWSDISCOVERY, "CWSDiscoveryPosix::Lookup - {} -> {}", strHostName,
                strIpAddress);
      return true;
    }
  }

  return false;
}

void CWSDiscoveryPosix::SetItems(std::vector<wsd_req_info> entries)
{
  {
    std::unique_lock<CCriticalSection> lock(m_critWSD);
    m_vecWSDInfo = std::move(entries);
  }
}
} // namespace WSDiscovery