NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers netdnsrequest.cpp Source File

netdnsrequest.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 "netdnsrequest.h"
00025 #include <string.h>
00026 
00027 NetDnsRequest::NetDnsRequest(const char* hostname) : NetService(), m_ip(0,0,0,0), m_pCbItem(NULL), m_pCbMeth(NULL), m_pHost(NULL), m_closed(false)
00028 {
00029   m_hostname = new char[strlen(hostname)+1];
00030   strcpy( m_hostname, hostname );
00031 }
00032 
00033 NetDnsRequest::NetDnsRequest(Host* pHost)  : NetService(), m_ip(0,0,0,0), m_pCbItem(NULL), m_pCbMeth(NULL), m_closed(false)
00034 {
00035   m_hostname = (char*) pHost->getName();
00036   m_pHost = pHost;
00037 }
00038 
00039 NetDnsRequest::~NetDnsRequest()
00040 {
00041   close();
00042 }
00043    
00044 void NetDnsRequest::getResult(IpAddr* pIp)
00045 {
00046   if( pIp != NULL )
00047     *pIp = m_ip;
00048 }
00049 
00050 void NetDnsRequest::close()
00051 {
00052   if(m_closed)
00053     return;
00054   m_closed = true;
00055   if( !m_pHost )
00056     delete[] m_hostname;
00057   NetService::close();
00058 }
00059   
00060 void NetDnsRequest::onReply(NetDnsReply reply) //Must be called by impl when the request completes
00061 {
00062   if( m_pHost )
00063     m_pHost->setIp(m_ip);
00064   if(m_pCbItem && m_pCbMeth)
00065     (m_pCbItem->*m_pCbMeth)(reply);
00066 }