NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Revision:
2:a4f97773c90f
Parent:
0:632c9925f013
Child:
3:95e0bc00a1bb
--- a/if/lwip/lwipNetDnsRequest.cpp	Mon Jun 14 10:33:54 2010 +0000
+++ b/if/lwip/lwipNetDnsRequest.cpp	Fri Jun 18 09:22:54 2010 +0000
@@ -31,12 +31,12 @@
 //#define __DEBUG
 #include "dbg/dbg.h"
 
-LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false)
+LwipNetDnsRequest::LwipNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
 {
   DBG("New LwipNetDnsRequest %p\n", this);
 }
 
-LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false)
+LwipNetDnsRequest::LwipNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), m_state(LWIPNETDNS_START), m_cbFired(false), m_closing(false)
 {
   DBG("New LwipNetDnsRequest %p\n", this);
 }
@@ -56,6 +56,7 @@
 void LwipNetDnsRequest::poll()
 {
   err_t  err;
+  dnsTick();
   switch(m_state)
   {
   case LWIPNETDNS_START: //First req, let's call dns_gethostbyname
@@ -106,16 +107,36 @@
     }  
     break; 
   }
-//  return m_state;
+  if(m_closing && (m_state!=LWIPNETDNS_PROCESSING)) //Check wether the closure has been reqd
+  {
+    DBG("LwipNetDnsRequest: Closing in poll()\n");
+    NetDnsRequest::close();
+  }
+}
+
+void LwipNetDnsRequest::close()
+{
+  DBG("LwipNetDnsRequest: Close req\n");
+  if(m_state!=LWIPNETDNS_PROCESSING)
+  {
+    DBG("LwipNetDnsRequest: Closing in close()\n");
+    NetDnsRequest::close();
+  }
+  else //Cannot close rightaway, waiting for callback from underlying layer
+  {
+    m_closing = true;
+  }
 }
 
 void LwipNetDnsRequest::foundCb(const char *name, ip_addr_t *ipaddr)
 {
   if( ipaddr == NULL )
   {
+    DBG("LwipNetDnsRequest: Callback: Name not found\n");
     m_state = LWIPNETDNS_NOTFOUND;
     return;
   }
+  DBG("LwipNetDnsRequest: Callback: Resolved\n");
   m_ip = IpAddr(ipaddr);
   m_state = LWIPNETDNS_OK;
 }
@@ -123,9 +144,26 @@
 
 void LwipNetDnsRequest::sFoundCb(const char *name, ip_addr_t *ipaddr, void *arg)
 {
+  DBG("LwipNetDnsRequest: Static callback\n");
   LwipNetDnsRequest* pMe = (LwipNetDnsRequest*) arg;
   return pMe->foundCb( name, ipaddr );
 }
 
+void LwipNetDnsRequest::dnsTick()
+{
+  static Timer* pTmr = NULL;
+  if(!pTmr)
+  {
+    pTmr = new Timer;
+    pTmr->start();
+  }
+  if(pTmr->read_ms() >= DNS_TMR_INTERVAL)
+  {
+    pTmr->reset();
+    dns_tmr();
+    DBG("DNS Tick\n");
+  }
+}
+
 #endif