Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

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 #include "if/net/netdnsrequest.h"
00026 
00027 DNSRequest::DNSRequest() : m_pNetDnsRequest(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
00028 {
00029 
00030 }
00031 
00032 DNSRequest::~DNSRequest()
00033 {
00034   close();
00035 }
00036   
00037 DNSRequestErr DNSRequest::resolve(const char* hostname)
00038 {
00039   if(!m_pNetDnsRequest)
00040   {
00041     m_pNetDnsRequest = Net::dnsRequest(hostname);
00042     if(!m_pNetDnsRequest)
00043     {
00044       return DNS_IF; //Interface did not return a NetDnsRequest (usually because a default interface does not exist)
00045     }
00046     m_pNetDnsRequest->setOnReply(this, &DNSRequest::onNetDnsReply);
00047     return DNS_OK;
00048   }
00049   else
00050   {
00051     return DNS_INUSE; //The previous req has not completed
00052   }
00053 }
00054 
00055 DNSRequestErr DNSRequest::resolve(Host* pHost)
00056 {
00057   if(!m_pNetDnsRequest)
00058   {
00059     m_pNetDnsRequest = Net::dnsRequest(pHost);
00060     if(!m_pNetDnsRequest)
00061     {
00062       return DNS_IF; //Interface did not return a NetDnsRequest (usually because a default interface does not exist)
00063     }
00064     m_pNetDnsRequest->setOnReply(this, &DNSRequest::onNetDnsReply);
00065     return DNS_OK;
00066   }
00067   else
00068   {
00069     return DNS_INUSE; //The previous req has not completed
00070   }
00071 }
00072 
00073 //Callbacks
00074 void DNSRequest::setOnReply( void (*pMethod)(DNSReply) )
00075 {
00076   m_pCb = pMethod;
00077 }
00078 
00079 #if 0 //For doc only
00080 template<class T> 
00081 void DNSRequest::setOnReply( T* pItem, void (T::*pMethod)(DNSReply) )
00082 {
00083   m_pCbItem = (CDummy*) pItem;
00084   m_pCbMeth = (void (CDummy::*)(DNSReply)) pMethod;
00085 }
00086 #endif
00087 
00088 DNSRequestErr DNSRequest::getResult(IpAddr* pIp)
00089 {
00090   if(!m_pNetDnsRequest)
00091   {
00092     return DNS_SETUP;
00093   }
00094   m_pNetDnsRequest->getResult(pIp);
00095   return DNS_OK;
00096 }
00097   
00098 DNSRequestErr DNSRequest::close()
00099 {
00100   if(!m_pNetDnsRequest)
00101   {
00102     return DNS_SETUP;
00103   }
00104   m_pNetDnsRequest->close();
00105   m_pNetDnsRequest = NULL;
00106   return DNS_OK;
00107 }
00108   
00109 void DNSRequest::onNetDnsReply(NetDnsReply r)
00110 {
00111   if(m_pCbItem && m_pCbMeth)
00112     (m_pCbItem->*m_pCbMeth)((DNSReply) r);
00113   else if(m_pCb)
00114     m_pCb((DNSReply) r);
00115 }