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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/*
* Copyright (C) 2005-2009 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "system.h"
#include "ZeroconfOSX.h"
#include "threads/SingleLock.h"
#include "utils/log.h"
#include <string>
#include <sstream>
CZeroconfOSX::CZeroconfOSX():m_runloop(0)
{
//aquire the main threads event loop
m_runloop = CFRunLoopGetMain();
}
CZeroconfOSX::~CZeroconfOSX()
{
doStop();
}
//methods to implement for concrete implementations
bool CZeroconfOSX::doPublishService(const std::string& fcr_identifier,
const std::string& fcr_type,
const std::string& fcr_name,
unsigned int f_port,
std::map<std::string, std::string> txt)
{
CLog::Log(LOGDEBUG, "CZeroconfOSX::doPublishService identifier: %s type: %s name:%s port:%i", fcr_identifier.c_str(),
fcr_type.c_str(), fcr_name.c_str(), f_port);
CFStringRef name = CFStringCreateWithCString (NULL,
fcr_name.c_str(),
kCFStringEncodingUTF8
);
CFStringRef type = CFStringCreateWithCString (NULL,
fcr_type.c_str(),
kCFStringEncodingUTF8
);
CFNetServiceRef netService = CFNetServiceCreate(NULL, CFSTR(""), type, name, f_port);
CFRelease(name);
CFRelease(type);
//now register it
CFNetServiceClientContext clientContext = { 0, this, NULL, NULL, NULL };
CFStreamError error;
CFNetServiceSetClient(netService, registerCallback, &clientContext);
CFNetServiceScheduleWithRunLoop(netService, m_runloop, kCFRunLoopCommonModes);
//add txt records
if(!txt.empty())
{
//txt map to dictionary
CFDataRef txtData = NULL;
CFMutableDictionaryRef txtDict = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
for(std::map<std::string, std::string>::const_iterator it = txt.begin(); it != txt.end(); ++it)
{
CFStringRef key = CFStringCreateWithCString (NULL,
it->first.c_str(),
kCFStringEncodingUTF8
);
CFDataRef value = CFDataCreate ( NULL,
(UInt8 *)it->second.c_str(),
strlen(it->second.c_str())
);
CFDictionaryAddValue(txtDict,key, value);
}
//add txt records to service
txtData = CFNetServiceCreateTXTDataWithDictionary(NULL, txtDict);
CFNetServiceSetTXTData(netService, txtData);
CFRelease(txtData);
CFRelease(txtDict);
}
Boolean result = CFNetServiceRegisterWithOptions (netService, 0, &error);
if (result == false)
{
// Something went wrong so lets clean up.
CFNetServiceUnscheduleFromRunLoop(netService, m_runloop, kCFRunLoopCommonModes);
CFNetServiceSetClient(netService, NULL, NULL);
CFRelease(netService);
netService = NULL;
CLog::Log(LOGERROR, "CZeroconfOSX::doPublishService CFNetServiceRegister returned "
"(domain = %d, error = %"PRId64")", (int)error.domain, (int64_t)error.error);
} else
{
CSingleLock lock(m_data_guard);
m_services.insert(make_pair(fcr_identifier, netService));
}
return result;
}
bool CZeroconfOSX::doRemoveService(const std::string& fcr_ident)
{
CSingleLock lock(m_data_guard);
tServiceMap::iterator it = m_services.find(fcr_ident);
if(it != m_services.end())
{
cancelRegistration(it->second);
m_services.erase(it);
return true;
} else
return false;
}
void CZeroconfOSX::doStop()
{
CSingleLock lock(m_data_guard);
for(tServiceMap::iterator it = m_services.begin(); it != m_services.end(); ++it)
cancelRegistration(it->second);
m_services.clear();
}
void CZeroconfOSX::registerCallback(CFNetServiceRef theService, CFStreamError* error, void* info)
{
if (error->domain == kCFStreamErrorDomainNetServices)
{
CZeroconfOSX* p_this = reinterpret_cast<CZeroconfOSX*>(info);
switch(error->error) {
case kCFNetServicesErrorCollision:
CLog::Log(LOGERROR, "CZeroconfOSX::registerCallback name collision occured");
break;
default:
CLog::Log(LOGERROR, "CZeroconfOSX::registerCallback returned "
"(domain = %d, error = %"PRId64")", (int)error->domain, (int64_t)error->error);
break;
}
p_this->cancelRegistration(theService);
//remove it
CSingleLock lock(p_this->m_data_guard);
for(tServiceMap::iterator it = p_this->m_services.begin(); it != p_this->m_services.end(); ++it)
{
if(it->second == theService)
p_this->m_services.erase(it);
}
}
}
void CZeroconfOSX::cancelRegistration(CFNetServiceRef theService)
{
assert(theService != NULL);
CFNetServiceUnscheduleFromRunLoop(theService, m_runloop, kCFRunLoopCommonModes);
CFNetServiceSetClient(theService, NULL, NULL);
CFNetServiceCancel(theService);
CFRelease(theService);
}
|