ZeroconfConnector.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #ifndef __ZEROCONFCONNECTOR_H__
00025 #define __ZEROCONFCONNECTOR_H__
00026 
00027 #undef HAVE_AVAHI
00028 
00029 #ifdef HAVE_AVAHI
00030 
00031 class BootstrapNodeHandle;
00032 class NodeHandle;
00033 
00034 #include <omnetpp.h>
00035 #include <oversim_mapset.h>
00036 #include <semaphore.h>
00037 
00038 #include <avahi-client/client.h>
00039 #include <avahi-client/lookup.h>
00040 #include <avahi-client/publish.h>
00041 
00042 #include <avahi-common/alternative.h>
00043 #include <avahi-common/thread-watch.h>
00044 #include <avahi-common/malloc.h>
00045 #include <avahi-common/error.h>
00046 
00047 #define AVAHI_INIT_FAILED       0
00048 #define AVAHI_INIT_SUCCEEDED    1
00049 
00050 struct EqualStr
00051 {
00052     bool operator()(const char* s1, const char* s2) const {
00053         return strcmp(s1, s2) == 0;
00054     }
00055 };
00056 
00057 typedef std::pair<char *, BootstrapNodeHandle *> LocalNodePair;
00058 
00059 
00060 typedef UNORDERED_MAP<char *, BootstrapNodeHandle*, HASH_NAMESPACE::hash<char *>,
00061                             EqualStr>  LocalBNodeSet;
00070 class ZeroconfConnector : public cSimpleModule
00071 {
00072     friend void entry_group_callback(AvahiEntryGroup *, AvahiEntryGroupState, AVAHI_GCC_UNUSED void *);
00073     friend void create_services(AvahiClient *, ZeroconfConnector *);
00074     friend void resolv_callback(AvahiServiceResolver *,
00075                                 AVAHI_GCC_UNUSED AvahiIfIndex,
00076                                 AVAHI_GCC_UNUSED AvahiProtocol,
00077                                 AvahiResolverEvent,
00078                                 const char *,
00079                                 const char *,
00080                                 const char *,
00081                                 const char *,
00082                                 const AvahiAddress *,
00083                                 uint16_t, AvahiStringList *,
00084                                 AvahiLookupResultFlags,
00085                                 AVAHI_GCC_UNUSED void*);
00086     friend void browse_callback(AvahiServiceBrowser *,
00087                                 AvahiIfIndex,
00088                                 AvahiProtocol,
00089                                 AvahiBrowserEvent,
00090                                 const char *,
00091                                 const char *,
00092                                 const char *,
00093                                 AVAHI_GCC_UNUSED AvahiLookupResultFlags,
00094                                 void *);
00095     friend void client_callback(AvahiClient *,
00096                                 AvahiClientState,
00097                                 AVAHI_GCC_UNUSED void *);
00098 
00099 public:
00100     ZeroconfConnector();
00101     ~ZeroconfConnector();
00102 
00108     int getInitResult();
00109 
00118     int insertNode(char *name, BootstrapNodeHandle *node);
00119 
00127     int removeNode(char *name);
00128 
00134     void announceService(const NodeHandle &node);
00135 
00139     void revokeService();
00140 
00141     /*
00142      * Returns true, if Zeroconf is used for bootstrapping
00143      *
00144      * @return true, if Zeroconf is enabled
00145      */
00146     bool isEnabled() { return enabled; };
00147 
00148 protected:
00149     virtual void initialize();
00150     virtual void handleMessage(cMessage *msg);
00151 
00152 private:
00153     int initResult;    //result of initialization
00154 
00155     sem_t nodeSetSem;        //semaphore that protects newSet
00156     LocalBNodeSet newSet;    //hash map to hold newly found boot nodes
00157 
00158     cMessage *pollingTimer;  //timer that controls periodic polling on newSet
00159 
00160     AvahiClient *client;    //avahi client
00161     AvahiEntryGroup *group;    //avahi group
00162     AvahiThreadedPoll *threadedPoll;    //avahi pool
00163 
00164     AvahiServiceBrowser *sbMDNS;    //mDNS service browser
00165     AvahiServiceBrowser *sbUDNS;    //uDNS service browser
00166 
00167     const char *serviceType;    //e.g. "_p2pbootstrap._udp"
00168     char *serviceName;    //name of the service
00169     const char *overlayName;    //e.g. "overlay.net"
00170     const char *overlayType;    //name of the overlay protocol
00171 
00172     const NodeHandle *thisNode;    //local overlay node
00173     bool enabled; // true, if ZeroconfConnector is enabled
00174 };
00175 
00176 #else
00177 
00178 #include <NodeHandle.h>
00179 
00188 class ZeroconfConnector : public cSimpleModule
00189 {
00190 public:
00191     ZeroconfConnector() {};
00192     ~ZeroconfConnector() {};
00193 
00199     void announceService(const NodeHandle &node) {};
00200 
00204     void revokeService() {};
00205 
00206     /*
00207      * Returns true, if Zeroconf is used for bootstrapping
00208      *
00209      * @return true, if Zeroconf is enabled
00210      */
00211     bool isEnabled() { return false; };
00212 };
00213 #endif
00214 
00215 #endif
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3