Fawkes API  Fawkes Development Version
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 WebviewServiceBrowseHandler::~WebviewServiceBrowseHandler()
50 {
51  for (ServiceList::iterator s = __service_list.begin(); s != __service_list.end(); ++s) {
52  delete s->second;
53  }
54  __service_list.clear();
55 }
56 
57 void
59 {
60  //__logger->log_debug("WebviewServiceBrowseHandler", "All for now");
61 }
62 
63 
64 void
66 {
67  //__logger->log_debug("WebviewServiceBrowseHandler", "Cache exhausted");
68 }
69 
70 
71 void
73  const char *type,
74  const char *domain)
75 {
76  __logger->log_warn("WebviewServiceBrowseHandler", "Browsing for %s.%s in domain %s failed",
77  name, type, domain);
78 }
79 
80 
81 void
83  const char *type,
84  const char *domain,
85  const char *host_name,
86  const char *interface,
87  const struct sockaddr *addr,
88  const socklen_t addr_size,
89  uint16_t port,
90  std::list<std::string> &txt,
91  int flags)
92 {
93  if (__service_list.find(name) != __service_list.end()) {
94  delete __service_list[name];
95  __service_list.erase(name);
96  }
97  // Check for fawkesver txt record
98  for (std::list<std::string>::iterator i = txt.begin(); i != txt.end(); ++i) {
99  std::string::size_type eqind = i->find("=");
100  if (eqind != std::string::npos) {
101  std::string key = i->substr(0, eqind);
102  std::string val = i->substr(eqind + 1);
103  if (key == "fawkesver") {
104  NetworkService *s = new NetworkService(name, type, domain, host_name, port,
105  addr, addr_size, txt);
106 
107  if (! (*s == *__webview_service)) {
108  __logger->log_debug("WebviewServiceBrowseHandler", "Service %s.%s on %s:%u added",
109  name, type, host_name, port);
110  __service_list[name] = s;
111  } else {
112  delete s;
113  }
114  break;
115  }
116  }
117  }
118 }
119 
120 
121 void
123  const char *type,
124  const char *domain)
125 {
126  if (__service_list.find(name) != __service_list.end()) {
127  delete __service_list[name];
128  __service_list.erase(name);
129  }
130  __logger->log_debug("WebviewServiceBrowseHandler", "Service %s.%s has been removed",
131  name, type);
132 }
133 
134 
135 /** Get the service list.
136  * @return a list of services found on the network.
137  */
140 {
141  return __service_list;
142 }
virtual void service_added(const char *name, const char *type, const char *domain, const char *host_name, const char *interface, const struct sockaddr *addr, const socklen_t addr_size, uint16_t port, std::list< std::string > &txt, int flags)
A service has been announced on the network.
Fawkes library namespace.
virtual void service_removed(const char *name, const char *type, const char *domain)
A service has been removed from the network.
virtual void cache_exhausted()
Cache exhausted.
Representation of a service announced or found via service discovery (i.e.
Definition: service.h:37
WebviewServiceBrowseHandler(fawkes::Logger *logger, fawkes::NetworkService *webview_service)
Constructor.
ServiceList & service_list()
Get the service list.
virtual void all_for_now()
All results have been retrieved.
std::map< std::string, fawkes::NetworkService * > ServiceList
A map of services.
virtual void browse_failed(const char *name, const char *type, const char *domain)
Failed to browse for a given service.
Interface for logging.
Definition: logger.h:34