00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "TBonjourRegistrar.h"
00034 #include "TSysEvtHandler.h"
00035 #include "TError.h"
00036 #include "TSystem.h"
00037
00038 #include <arpa/inet.h>
00039
00040
00041 ClassImp(TBonjourRegistrar)
00042
00043
00044 TBonjourRegistrar::TBonjourRegistrar() : fDNSRef(0), fBonjourSocketHandler(0)
00045 {
00046
00047
00048
00049 gSystem->Setenv("AVAHI_COMPAT_NOWARN", "1");
00050 }
00051
00052
00053 TBonjourRegistrar::~TBonjourRegistrar()
00054 {
00055
00056
00057 delete fBonjourSocketHandler;
00058
00059 if (fDNSRef) {
00060 DNSServiceRefDeallocate(fDNSRef);
00061 fDNSRef = 0;
00062 }
00063 }
00064
00065
00066 Int_t TBonjourRegistrar::RegisterService(const TBonjourRecord &record, UShort_t servicePort)
00067 {
00068
00069
00070
00071 if (fDNSRef) {
00072 Warning("RegisterService", "already registered a service");
00073 return 0;
00074 }
00075
00076 UShort_t sport = htons(servicePort);
00077
00078
00079 DNSServiceErrorType err = DNSServiceRegister(&fDNSRef, 0, kDNSServiceInterfaceIndexAny,
00080 !strlen(record.GetServiceName()) ? 0
00081 : record.GetServiceName(),
00082 record.GetRegisteredType(),
00083 !strlen(record.GetReplyDomain()) ? 0
00084 : record.GetReplyDomain(),
00085 0, sport,
00086 record.GetTXTRecordsLength(),
00087 !strlen(record.GetTXTRecords()) ? 0
00088 : record.GetTXTRecords(),
00089 (DNSServiceRegisterReply)BonjourRegisterService,
00090 this);
00091 if (err != kDNSServiceErr_NoError) {
00092 Error("RegisterService", "error in DNSServiceRegister (%d)", err);
00093 return -1;
00094 }
00095
00096 Int_t sockfd = DNSServiceRefSockFD(fDNSRef);
00097 if (sockfd == -1) {
00098 Error("RegisterService", "invalid sockfd");
00099 return -1;
00100 }
00101
00102 fBonjourSocketHandler = new TFileHandler(sockfd, TFileHandler::kRead);
00103 fBonjourSocketHandler->Connect("Notified()", "TBonjourRegistrar", this, "BonjourSocketReadyRead()");
00104 fBonjourSocketHandler->Add();
00105
00106 return 0;
00107 }
00108
00109
00110 void TBonjourRegistrar::ServiceRegistered(TBonjourRecord *record)
00111 {
00112
00113
00114 Emit("ServiceRegistered(TBonjourRecord*)", (Long_t)record);
00115 }
00116
00117
00118 void TBonjourRegistrar::BonjourSocketReadyRead()
00119 {
00120
00121
00122
00123
00124 DNSServiceErrorType err = DNSServiceProcessResult(fDNSRef);
00125 if (err != kDNSServiceErr_NoError)
00126 Error("BonjourSocketReadyRead", "error in DNSServiceProcessResult");
00127 }
00128
00129
00130 void TBonjourRegistrar::BonjourRegisterService(DNSServiceRef, DNSServiceFlags,
00131 DNSServiceErrorType errCode,
00132 const char *name, const char *regType,
00133 const char *domain, void *context)
00134 {
00135
00136
00137 TBonjourRegistrar *registrar = static_cast<TBonjourRegistrar*>(context);
00138 if (errCode != kDNSServiceErr_NoError) {
00139 ::Error("TBonjourRegistrar::BonjourRegisterService", "error in BonjourRegisterService");
00140
00141 } else {
00142 registrar->fFinalRecord = TBonjourRecord(name, regType, domain);
00143 registrar->ServiceRegistered(®istrar->fFinalRecord);
00144 }
00145 }