Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
service_browse_handler.cpp
1 
2 /***************************************************************************
3  * service_browse_handler.cpp - Webview service browser
4  *
5  * Created: Thu Jul 02 18:00:20 2009 (RoboCup 2009, Graz)
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "service_browse_handler.h"
24 
25 #include <logging/logger.h>
26 
27 using namespace fawkes;
28 
29 /** @class WebviewServiceBrowseHandler "service_browse_handler.h"
30  * Browse handler to detect other Webview instances on the network.
31  * This browse handler is used to compile a list of other webview instances
32  * on the local network. It is used to show a list of hosts in the footer of
33  * webview pages.
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor.
38  * @param logger logger for informational logging
39  * @param webview_service service of our own service as it was announced on the
40  * network, used to filter it out from the list of services.
41  */
43  fawkes::NetworkService *webview_service)
44 {
45  __logger = logger;
46  __webview_service = webview_service;
47 }
48 
49 
50 void
52 {
53  //__logger->log_debug("WebviewServiceBrowseHandler", "All for now");
54 }
55 
56 
57 void
59 {
60  //__logger->log_debug("WebviewServiceBrowseHandler", "Cache exhausted");
61 }
62 
63 
64 void
66  const char *type,
67  const char *domain)
68 {
69  __logger->log_warn("WebviewServiceBrowseHandler", "Browsing for %s.%s in domain %s failed",
70  name, type, domain);
71 }
72 
73 
74 void
76  const char *type,
77  const char *domain,
78  const char *host_name,
79  const struct sockaddr *addr,
80  const socklen_t addr_size,
81  uint16_t port,
82  std::list<std::string> &txt,
83  int flags)
84 {
85  if (__service_list.find(name) != __service_list.end()) {
86  delete __service_list[name];
87  __service_list.erase(name);
88  }
89  // Check for fawkesver txt record
90  for (std::list<std::string>::iterator i = txt.begin(); i != txt.end(); ++i) {
91  std::string::size_type eqind = i->find("=");
92  if (eqind != std::string::npos) {
93  std::string key = i->substr(0, eqind);
94  std::string val = i->substr(eqind + 1);
95  if (key == "fawkesver") {
96  NetworkService *s = new NetworkService(name, type, domain, host_name, port,
97  addr, addr_size, txt);
98 
99  if (! (*s == *__webview_service)) {
100  __logger->log_debug("WebviewServiceBrowseHandler", "Service %s.%s on %s:%u added",
101  name, type, host_name, port);
102  __service_list[name] = s;
103  } else {
104  delete s;
105  }
106  break;
107  }
108  }
109  }
110 }
111 
112 
113 void
115  const char *type,
116  const char *domain)
117 {
118  if (__service_list.find(name) != __service_list.end()) {
119  delete __service_list[name];
120  __service_list.erase(name);
121  }
122  __logger->log_debug("WebviewServiceBrowseHandler", "Service %s.%s has been removed",
123  name, type);
124 }
125 
126 
127 /** Get the service list.
128  * @return a list of services found on the network.
129  */
132 {
133  return __service_list;
134 }