Wraps the DNSRequest class from NetServices to be synchronous, which is easier to use.

Dependents:   TwitPicExample SPIVFDclock RPC_mbed_client QRSS_Rx ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dnsresolve.h Source File

dnsresolve.h

00001 /*
00002 * DNSResolver library
00003 * Copyright (c) 2010 Hendrik Lipka
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 #ifndef __DNSRESOLVE_H__
00025 #define __DNSRESOLVE_H__
00026 
00027 #include "Timer.h"
00028 #include "wait_api.h"
00029 
00030 #include "DNSRequest.h"
00031 
00032 class DNSResolver {
00033 public:
00034     DNSResolver() {
00035         _request=new DNSRequest();
00036         _request->setOnReply(this, &DNSResolver::onReply);
00037     };
00038 
00039     ~DNSResolver() {
00040         delete _request;
00041     };
00042 
00043     IpAddr resolveName(const char* name) {
00044         _completed=0;
00045         IpAddr addr;
00046 
00047         DNSRequestErr r=_request->resolve(name);
00048         if (0!=r) {
00049             _request->close();
00050             return addr;
00051         }
00052 
00053         mbed::Timer tmr;
00054         tmr.start();
00055         while (0==_completed) {
00056             Net::poll();
00057             if (tmr.read()>5)
00058                 break;
00059             wait_us(100);
00060         }
00061 
00062         if (-1==_completed) {
00063             _request->close();
00064             return addr;
00065         }
00066 
00067         _request->getResult(&addr);
00068         _request->close();
00069         return addr;
00070     };
00071 
00072     void onReply(DNSReply reply) {
00073         if (reply==DNS_FOUND)
00074             _completed=1;
00075         else
00076             _completed=-1;
00077     };
00078 
00079 private:
00080     DNSRequest *_request;
00081     int _completed; // -1=err,0=running,1=ok
00082 };
00083 
00084 
00085 #endif