]> git.tdb.fi Git - netvis.git/blob - source/resolver.cpp
Improve the algorithm for deleting hosts
[netvis.git] / source / resolver.cpp
1 /* $Id$
2
3 This file is part of NetVis
4 Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed unter the GPL
6 */
7
8 #include <netinet/in.h>
9 #include <netdb.h>
10 #include <msp/time/units.h>
11 #include <msp/time/utils.h>
12 #include "host.h"
13 #include "resolver.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 Resolver::Resolver():
19         done(false)
20 {
21         launch();
22 }
23
24 Resolver::~Resolver()
25 {
26         done=true;
27         join();
28 }
29
30 void Resolver::push(Host *h)
31 {
32         MutexLock l_(mutex);
33
34         in_queue.push_back(h);
35 }
36
37 void Resolver::tick()
38 {
39         MutexLock l_(mutex);
40
41         for(list<Result>::iterator i=out_queue.begin(); i!=out_queue.end(); ++i)
42                 i->host->set_name(i->name);
43         out_queue.clear();
44 }
45
46 void Resolver::main()
47 {
48         while(!done)
49         {
50                 while(1)
51                 {
52                         Host *host;
53                         {
54                                 MutexLock l_(mutex);
55                                 if(in_queue.empty())
56                                         break;
57                                 host=in_queue.front();
58                                 in_queue.erase(in_queue.begin());
59                         }
60                         
61                         sockaddr_in addr;
62                         addr.sin_family=AF_INET;
63                         addr.sin_addr.s_addr=htonl(host->get_address());
64                         char buf[128];
65                         int err=getnameinfo(reinterpret_cast<sockaddr *>(&addr), sizeof(addr), buf, sizeof(buf), 0, 0, NI_NOFQDN|NI_NAMEREQD);
66                         if(err==0)
67                         {
68                                 MutexLock l_(mutex);
69                                 out_queue.push_back(Result(host, buf));
70                         }
71                 }
72
73                 Time::sleep(Time::sec);
74         }
75 }