< Previous by Date Date Index Next by Date >
  Thread Index  

[reSIProcate] how to implement DnsInterface without making it a new thread?


Hi again,

I have another question about DnsInterface.  The testDns.cxx example client
program creates a subclass of DnsInterface that inherits from ThreadIf,
making it a run as a separate thread.  In my application, I'll already be
calling the DnsInterface subclass from a unique worker thread, so I don't
believe I need an extra thread for it, assuming everything is thread-safe
already.

I considered something like this:

//class ResipDnsInterface : public DnsInterface, public ThreadIf
class ResipDnsInterface : public DnsInterface
{
   public:
[... Other stuff same as before ...]

      // this is similar to the thread() method in the original
      void collectResults(ResipDnsHandler* handler)
      {
         while (!handler->complete())
         {
            DebugLog (<< "ResipDnsInterface::collectResults - here"); //+++
            FdSet fdset;
            buildFdSet(fdset);
            mStub.buildFdSet(fdset);
            fdset.selectMilliSeconds(1);
            process(fdset);
            mStub.process(fdset);
            nanosleep(&delay, 0); // sleep 3ms or so before the next time
around
         }
      }
}

Then in my lookup() method I do this:

void
ResipDns::lookup(const string& name, IpAddrStringVec& ipAddrStringVec)
{
   DnsStub* stub = new DnsStub;
   ResipDnsInterface dns(*stub);
   Uri uri;

   vector<Data> enumSuffixes;
   enumSuffixes.push_back(ENUM_SUFFIX);
   stub->setEnumSuffixes(enumSuffixes);

   ResipDnsQuery query;
   query.handler = new ResipDnsHandler;
   try
   {
      Data input(name);
      uri = Uri(input);
      query.uri = uri;
      DnsResult* res = dns.createDnsResult(query.handler);
      query.result = res;
      dns.lookup(res, uri);
   }
   catch (ParseBuffer::Exception& e)
   {
      CerrLog (<< "ResipDns::lookup - Couldn't parse arg " << name << ": "
<< e.getMessage());
      delete stub;
      return;
   }

   dns.collectResults(query.handler);
   [... Other stuff to store results in output vector and clean up ...]
}

Does this make sense?  I think it would be better if it were not necessary
to do the while loop in the collectResults() method.  I find that no matter
how big/small I make the delay, it always goes through the loop 3 times.
I'd prefer to just call a method and have it take however long it needs,
without a loop.  Has anyone else used it that way?  My
ResipDnsHandler::handle method is nearly identical to the one in the test
code, maybe it needs to change?

Regards,
Dave