summaryrefslogtreecommitdiffstats
path: root/dnssd/servicebase.cpp
blob: ef2943b95385076276e212934be1e8d1722150bc (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
/* This file is part of the KDE project
 *
 * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "servicebase.h"
#include <qregexp.h>

namespace DNSSD
{

ServiceBase::ServiceBase(const QString& name, const QString& type, const QString& domain,
			 const QString& host, unsigned short port) : 
    		m_serviceName(name), m_type(type), m_domain(domain), m_hostName(host), m_port(port)
{}

ServiceBase::~ServiceBase()
{}

QString ServiceBase::encode()
{
	return  m_serviceName.replace("\\","\\\\").replace(".","\\.") + QString(".") + m_type +
			 QString(".") + m_domain;
}

// example: 3rd\.\032Floor\032Copy\032Room._ipp._tcp.dns-sd.org.  - normal service
// 3rd\.\032Floor\032Copy\032Room.dns-sd.org  - domain
// 	_ipp._tcp.dns-sd.org	- metaquery

void ServiceBase::decode(const QString& name)
{
	QString rest;
	if (name[0]=='_') { 		// metaquery
		m_serviceName="";
		rest=name;
	} else {		// normal service or domain
		QString decoded_name=name;
		decoded_name=decoded_name.replace("\\\\","\\");
		int i = decoded_name.find(QRegExp("[^\\\\]\\."));
		if (i==-1) return;            // first find service name
		rest = decoded_name.mid(i+2);
		m_serviceName=decoded_name.left(i+1).replace("\\.",".");
	}
	m_type = rest.section('.',0,1);
	// does it really have a type?
	if (m_type[0]=='_' && m_type[m_type.find('.')+1]=='_')
		m_domain = rest.section('.',2,-1,QString::SectionIncludeTrailingSep);
	else {
		m_type="";
		m_domain=rest;
	}
}

const QString& ServiceBase::serviceName() const
{
	return m_serviceName;
}

const QString& ServiceBase::type() const
{
	return m_type;
}

const QString& ServiceBase::domain() const
{
	return m_domain;
}

const QString& ServiceBase::hostName() const
{
	return m_hostName;
}

unsigned short ServiceBase::port() const
{
	return m_port;
}
const QMap<QString,QString>& ServiceBase::textData() const
{
	return m_textData;
}

void ServiceBase::virtual_hook(int, void*)
{}

QDataStream & operator<< (QDataStream & s, const ServiceBase & a)
{
	s << a.m_serviceName << a.m_type << a.m_domain << a.m_hostName << Q_INT16(a.m_port) << a.m_textData;
	return s;
}

QDataStream & operator>> (QDataStream & s, ServiceBase & a)
{
	Q_INT16 port;
	s >> a.m_serviceName >> a.m_type >> a.m_domain >> a.m_hostName >> port >> a.m_textData;
	a.m_port = port;	
	return s;
}

}