OverSim
ZeroconfConnector.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #ifndef __ZEROCONFCONNECTOR_H__
25 #define __ZEROCONFCONNECTOR_H__
26 
27 #undef HAVE_AVAHI
28 
29 #ifdef HAVE_AVAHI
30 
32 class NodeHandle;
33 
34 #include <omnetpp.h>
35 #include <oversim_mapset.h>
36 #include <semaphore.h>
37 
38 #include <avahi-client/client.h>
39 #include <avahi-client/lookup.h>
40 #include <avahi-client/publish.h>
41 
42 #include <avahi-common/alternative.h>
43 #include <avahi-common/thread-watch.h>
44 #include <avahi-common/malloc.h>
45 #include <avahi-common/error.h>
46 
47 #define AVAHI_INIT_FAILED 0
48 #define AVAHI_INIT_SUCCEEDED 1
49 
50 struct EqualStr
51 {
52  bool operator()(const char* s1, const char* s2) const {
53  return strcmp(s1, s2) == 0;
54  }
55 };
56 
57 typedef std::pair<char *, BootstrapNodeHandle *> LocalNodePair;
58 
59 
60 typedef UNORDERED_MAP<char *, BootstrapNodeHandle*, HASH_NAMESPACE::hash<char *>,
61  EqualStr> LocalBNodeSet;
70 class ZeroconfConnector : public cSimpleModule
71 {
72  friend void entry_group_callback(AvahiEntryGroup *, AvahiEntryGroupState, AVAHI_GCC_UNUSED void *);
73  friend void create_services(AvahiClient *, ZeroconfConnector *);
74  friend void resolv_callback(AvahiServiceResolver *,
75  AVAHI_GCC_UNUSED AvahiIfIndex,
76  AVAHI_GCC_UNUSED AvahiProtocol,
77  AvahiResolverEvent,
78  const char *,
79  const char *,
80  const char *,
81  const char *,
82  const AvahiAddress *,
83  uint16_t, AvahiStringList *,
84  AvahiLookupResultFlags,
85  AVAHI_GCC_UNUSED void*);
86  friend void browse_callback(AvahiServiceBrowser *,
87  AvahiIfIndex,
88  AvahiProtocol,
89  AvahiBrowserEvent,
90  const char *,
91  const char *,
92  const char *,
93  AVAHI_GCC_UNUSED AvahiLookupResultFlags,
94  void *);
95  friend void client_callback(AvahiClient *,
96  AvahiClientState,
97  AVAHI_GCC_UNUSED void *);
98 
99 public:
102 
108  int getInitResult();
109 
118  int insertNode(char *name, BootstrapNodeHandle *node);
119 
127  int removeNode(char *name);
128 
134  void announceService(const NodeHandle &node);
135 
139  void revokeService();
140 
141  /*
142  * Returns true, if Zeroconf is used for bootstrapping
143  *
144  * @return true, if Zeroconf is enabled
145  */
146  bool isEnabled() { return enabled; };
147 
148 protected:
149  virtual void initialize();
150  virtual void handleMessage(cMessage *msg);
151 
152 private:
153  int initResult; //result of initialization
154 
155  sem_t nodeSetSem; //semaphore that protects newSet
156  LocalBNodeSet newSet; //hash map to hold newly found boot nodes
157 
158  cMessage *pollingTimer; //timer that controls periodic polling on newSet
159 
160  AvahiClient *client; //avahi client
161  AvahiEntryGroup *group; //avahi group
162  AvahiThreadedPoll *threadedPoll; //avahi pool
163 
164  AvahiServiceBrowser *sbMDNS; //mDNS service browser
165  AvahiServiceBrowser *sbUDNS; //uDNS service browser
166 
167  const char *serviceType; //e.g. "_p2pbootstrap._udp"
168  char *serviceName; //name of the service
169  const char *overlayName; //e.g. "overlay.net"
170  const char *overlayType; //name of the overlay protocol
171 
172  const NodeHandle *thisNode; //local overlay node
173  bool enabled; // true, if ZeroconfConnector is enabled
174 };
175 
176 #else
177 
178 #include <NodeHandle.h>
179 
188 class ZeroconfConnector : public cSimpleModule
189 {
190 public:
193 
199  void announceService(const NodeHandle &node) {};
200 
204  void revokeService() {};
205 
206  /*
207  * Returns true, if Zeroconf is used for bootstrapping
208  *
209  * @return true, if Zeroconf is enabled
210  */
211  bool isEnabled() { return false; };
212 };
213 #endif
214 
215 #endif