servlib/discovery/BonjourDiscovery.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifndef _BONJOUR_DISCOVERY_H_
       
    18 #define _BONJOUR_DISCOVERY_H_
       
    19 
       
    20 #ifndef DTN_CONFIG_STATE
       
    21 #error "MUST INCLUDE dtn-config.h before including this file"
       
    22 #endif
       
    23 
       
    24 #ifdef OASYS_BONJOUR_ENABLED
       
    25 
       
    26 #include <dns_sd.h>
       
    27 #include <vector>
       
    28 
       
    29 #include <oasys/thread/Notifier.h>
       
    30 #include <oasys/thread/Thread.h>
       
    31 #include "Discovery.h"
       
    32 
       
    33 namespace dtn {
       
    34 
       
    35 /**
       
    36  * A Discovery instance that uses Mac OS X Bonjour (i.e. the Multicast
       
    37  * DNS protocol).
       
    38  */
       
    39 class BonjourDiscovery : public Discovery, public oasys::Thread {
       
    40 public:
       
    41     /// Constructor
       
    42     BonjourDiscovery(const std::string& name);
       
    43     virtual ~BonjourDiscovery();
       
    44 
       
    45     /// @{ Virtual from Discovery
       
    46     bool configure(int argc, const char* argv[]);
       
    47     void shutdown();
       
    48     /// @}
       
    49 
       
    50     /// Main Thread run function
       
    51     void run();
       
    52 
       
    53 private:
       
    54     oasys::Notifier notifier_;	///< Notifier used for shutdown
       
    55     bool shutdown_;		///< Bit used for shutdown
       
    56     typedef std::vector<DNSServiceRef> SvcVector;
       
    57     SvcVector svc_vec_;
       
    58 
       
    59     int ttl_;			///< TTL for announcement
       
    60 
       
    61     ///< Callback for registration reply
       
    62     static void register_reply_callback(DNSServiceRef sdRef,
       
    63                                         DNSServiceFlags flags,
       
    64                                         DNSServiceErrorType errorCode,
       
    65                                         const char *name,
       
    66                                         const char *regtype,
       
    67                                         const char *domain,
       
    68                                         void *context) 
       
    69     {
       
    70         ((BonjourDiscovery*)context)->
       
    71             handle_register_reply(sdRef, flags, errorCode, name,
       
    72                                   regtype, domain);
       
    73     }
       
    74 
       
    75     /// Registration reply handler
       
    76     void handle_register_reply(DNSServiceRef sdRef,
       
    77                                DNSServiceFlags flags,
       
    78                                DNSServiceErrorType errorCode,
       
    79                                const char *name,
       
    80                                const char *regtype,
       
    81                                const char *domain);
       
    82 
       
    83     ///< Callback for browse
       
    84     static void browse_callback(DNSServiceRef sdRef, 
       
    85                                 DNSServiceFlags flags, 
       
    86                                 uint32_t interfaceIndex, 
       
    87                                 DNSServiceErrorType errorCode, 
       
    88                                 const char *serviceName, 
       
    89                                 const char *regtype, 
       
    90                                 const char *replyDomain, 
       
    91                                 void *context)
       
    92     {
       
    93         ((BonjourDiscovery*)context)->
       
    94             handle_browse(sdRef, flags, interfaceIndex, errorCode, 
       
    95                           serviceName, regtype, replyDomain);
       
    96     }
       
    97 
       
    98     /// Browse handler
       
    99     void handle_browse(DNSServiceRef sdRef,
       
   100                        DNSServiceFlags flags, 
       
   101                        uint32_t interfaceIndex, 
       
   102                        DNSServiceErrorType errorCode, 
       
   103                        const char *serviceName, 
       
   104                        const char *regtype, 
       
   105                        const char *replyDomain);
       
   106 
       
   107     ///< Callback for resolve
       
   108     static void resolve_callback(DNSServiceRef sdRef, 
       
   109                                  DNSServiceFlags flags, 
       
   110                                  uint32_t interfaceIndex, 
       
   111                                  DNSServiceErrorType errorCode, 
       
   112                                  const char *fullname, 
       
   113                                  const char *hosttarget,
       
   114                                  uint16_t port,
       
   115                                  uint16_t txtlen,
       
   116                                  const char* txtRecord,
       
   117                                  void *context)
       
   118     {
       
   119         ((BonjourDiscovery*)context)->
       
   120             handle_resolve(sdRef, flags, interfaceIndex, errorCode, 
       
   121                            fullname, hosttarget, port, txtlen, txtRecord);
       
   122     }
       
   123 
       
   124     /// Resolve handler
       
   125     void handle_resolve(DNSServiceRef sdRef, 
       
   126                         DNSServiceFlags flags, 
       
   127                         uint32_t interfaceIndex, 
       
   128                         DNSServiceErrorType errorCode, 
       
   129                         const char *fullname, 
       
   130                         const char *hosttarget,
       
   131                         uint16_t port,
       
   132                         uint16_t txtlen,
       
   133                         const char* txtRecord);
       
   134 
       
   135     /**
       
   136      * For some lame reason, the dns service doesn't define a strerror
       
   137      * analog
       
   138      */
       
   139     static const char* dns_service_strerror(DNSServiceErrorType err);
       
   140 
       
   141     /**
       
   142      * Remove the given DNSServiceRef from the vector.
       
   143      */
       
   144     void remove_svc(DNSServiceRef sdRef);
       
   145 };
       
   146 
       
   147 } // namespace dtn
       
   148 
       
   149 #endif /* OASYS_BONJOUR_ENABLED */
       
   150 
       
   151 #endif /* _BONJOUR_DISCOVERY_H_ */