DNSSD
servicebase.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "servicebase.h"
00022 #include <qregexp.h>
00023
00024 namespace DNSSD
00025 {
00026
00027 ServiceBase::ServiceBase(const QString& name, const QString& type, const QString& domain,
00028 const QString& host, unsigned short port) :
00029 m_serviceName(name), m_type(type), m_domain(domain), m_hostName(host), m_port(port)
00030 {}
00031
00032 ServiceBase::~ServiceBase()
00033 {}
00034
00035 QString ServiceBase::encode()
00036 {
00037 return m_serviceName.replace("\\","\\\\").replace(".","\\.") + QString(".") + m_type +
00038 QString(".") + m_domain;
00039 }
00040
00041
00042
00043
00044
00045 void ServiceBase::decode(const QString& name)
00046 {
00047 QString rest;
00048 if (name[0]=='_') {
00049 m_serviceName="";
00050 rest=name;
00051 } else {
00052 QString decoded_name=name;
00053 decoded_name=decoded_name.replace("\\\\","\\");
00054 int i = decoded_name.find(QRegExp("[^\\\\]\\."));
00055 if (i==-1) return;
00056 rest = decoded_name.mid(i+2);
00057 m_serviceName=decoded_name.left(i+1).replace("\\.",".");
00058 }
00059 m_type = rest.section('.',0,1);
00060
00061 if (m_type[0]=='_' && m_type[m_type.find('.')+1]=='_')
00062 m_domain = rest.section('.',2,-1,QString::SectionIncludeTrailingSep);
00063 else {
00064 m_type="";
00065 m_domain=rest;
00066 }
00067 }
00068
00069 const QString& ServiceBase::serviceName() const
00070 {
00071 return m_serviceName;
00072 }
00073
00074 const QString& ServiceBase::type() const
00075 {
00076 return m_type;
00077 }
00078
00079 const QString& ServiceBase::domain() const
00080 {
00081 return m_domain;
00082 }
00083
00084 const QString& ServiceBase::hostName() const
00085 {
00086 return m_hostName;
00087 }
00088
00089 unsigned short ServiceBase::port() const
00090 {
00091 return m_port;
00092 }
00093 const QMap<QString,QString>& ServiceBase::textData() const
00094 {
00095 return m_textData;
00096 }
00097
00098 void ServiceBase::virtual_hook(int, void*)
00099 {}
00100
00101 QDataStream & operator<< (QDataStream & s, const ServiceBase & a)
00102 {
00103 s << a.m_serviceName << a.m_type << a.m_domain << a.m_hostName << Q_INT16(a.m_port) << a.m_textData;
00104 return s;
00105 }
00106
00107 QDataStream & operator>> (QDataStream & s, ServiceBase & a)
00108 {
00109 Q_INT16 port;
00110 s >> a.m_serviceName >> a.m_type >> a.m_domain >> a.m_hostName >> port >> a.m_textData;
00111 a.m_port = port;
00112 return s;
00113 }
00114
00115 }