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