Bonjour/Zerconf library

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DNSRequest.cpp Source File

DNSRequest.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "DNSRequest.h"
00025 
00026 DNSRequest::DNSRequest() : m_pNetDnsRequest(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
00027 {
00028 
00029 }
00030 
00031 DNSRequest::~DNSRequest()
00032 {
00033   close();
00034 }
00035   
00036 DNSRequestErr DNSRequest::resolve(const char* hostname)
00037 {
00038   if(!m_pNetDnsRequest)
00039   {
00040     m_pNetDnsRequest = Net::dnsRequest(hostname);
00041     if(!m_pNetDnsRequest)
00042     {
00043       return DNS_IF; //Interface did not return a NetDnsRequest (usually because a default interface does not exist)
00044     }
00045     m_pNetDnsRequest->setOnReply(this, &DNSRequest::onNetDnsReply);
00046     return DNS_OK;
00047   }
00048   else
00049   {
00050     return DNS_INUSE; //The previous req has not completed
00051   }
00052 }
00053 
00054 DNSRequestErr DNSRequest::resolve(Host* pHost)
00055 {
00056   if(!m_pNetDnsRequest)
00057   {
00058     m_pNetDnsRequest = Net::dnsRequest(pHost);
00059     if(!m_pNetDnsRequest)
00060     {
00061       return DNS_IF; //Interface did not return a NetDnsRequest (usually because a default interface does not exist)
00062     }
00063     m_pNetDnsRequest->setOnReply(this, &DNSRequest::onNetDnsReply);
00064     return DNS_OK;
00065   }
00066   else
00067   {
00068     return DNS_INUSE; //The previous req has not completed
00069   }
00070 }
00071 
00072 //Callbacks
00073 void DNSRequest::setOnReply( void (*pMethod)(DNSReply) )
00074 {
00075   m_pCb = pMethod;
00076 }
00077 
00078 #if 0 //For doc only
00079 template<class T> 
00080 void DNSRequest::setOnReply( T* pItem, void (T::*pMethod)(DNSReply) )
00081 {
00082   m_pCbItem = (CDummy*) pItem;
00083   m_pCbMeth = (void (CDummy::*)(DNSReply)) pMethod;
00084 }
00085 #endif
00086 
00087 DNSRequestErr DNSRequest::getResult(IpAddr* pIp)
00088 {
00089   if(!m_pNetDnsRequest)
00090   {
00091     return DNS_SETUP;
00092   }
00093   m_pNetDnsRequest->getResult(pIp);
00094   return DNS_OK;
00095 }
00096   
00097 DNSRequestErr DNSRequest::close()
00098 {
00099   if(!m_pNetDnsRequest)
00100   {
00101     return DNS_SETUP;
00102   }
00103   m_pNetDnsRequest->close();
00104   m_pNetDnsRequest = NULL;
00105   return DNS_OK;
00106 }
00107   
00108 void DNSRequest::onNetDnsReply(NetDnsReply r)
00109 {
00110   if(m_pCbItem && m_pCbMeth)
00111     (m_pCbItem->*m_pCbMeth)((DNSReply) r);
00112   else if(m_pCb)
00113     m_pCb((DNSReply) r);
00114 }