gloox
1.0.16
|
#include <linklocalmanager.h>
Public Member Functions | |
Manager (const std::string &user, ConnectionHandler *connHandler, const LogSink &logInstance) | |
virtual | ~Manager () |
void | addTXTData (const std::string &key, const std::string &value) |
void | removeTXTData (const std::string &key) |
void | registerService () |
void | deregisterService () |
void | setUser (const std::string &user) |
void | setHost (const std::string &host) |
void | setDomain (const std::string &domain) |
void | setPort (const int port) |
void | setInterface (unsigned iface) |
bool | startBrowsing () |
void | stopBrowsing () |
int | socket () const |
void | recv (int timeout) |
void | registerLinkLocalHandler (Handler *handler) |
This is a manager for server-less messaging (XEP-0174).
Enable compilation of this code with the --enable-mdns
switch to configure
, or add #define
HAVE_MDNS
to your platform's config.h
. dns_sd.h
, libdns_sd.so
, as well as the mdnsd
daemon from Apple's bonjour distribution are required. The mdnsd
daemon has to be running on the local host.
You can use the Manager to browse the local network for XMPP services. First, create a new instance, register a LinkLocal::Handler, and call startBrowsing().
Then you will need to call recv()
periodcally. The handler will then receive lists of available or removed services. Check the flag
member of the Service struct.
Using the info from the Service struct you can initiate a connection to the remote entity. First, create a new instance of LinkLocal::Client and register some basic handlers like you would with a normal gloox::Client:
Then call connect() and pass the paramters from the Service struct that you received in handleBrowseReply().
Put your LinkLocal::Client instance in your event loop (or in a separate thread) and call recv() periodically.
To advertise your own XMPP service you can (re-)use the same Manager instance from 'browsing the local network' above.
You can publish some basic info about your service in a DNS TXT record. The Manager offers the addTXTData() function for that. See http://xmpp.org/registrar/linklocal.html for a list of official parameters.
Then, to start publishing the availability of your service as well as the TXT record with the additional info you just call registerService()
.
Other entities on the network will now be informed about the availability of your service.
The second argument to Manager's constructor is a ConnectionHandler-derived class that will receive incoming connections.
When registerService() gets called, the Manager will also start a local server that will accept incoming connections. By default, it will listen on port 5562.
In handleIncomingConnection() you should create a new LinkLocal::Client and register some basic handlers:
Finally you have to attach the incoming connection to the Client instance, and call connect().
Add the Client to your event loop to call recv() periodically.
linklocal_example.cpp
in src/examples/
for a (very) simple implementation of a bot handling both incoming and outgoing connections.Definition at line 167 of file linklocalmanager.h.
Manager | ( | const std::string & | user, |
ConnectionHandler * | connHandler, | ||
const LogSink & | logInstance | ||
) |
Creates a new Link-local Manager instance. You can call registerService()
and/or startBrowsing()
immediately on a new Manager object, it will use sane defaults.
user | The username to advertise, preferably (as per XEP-0174) the locally logged-in user. This is just the local part of the local JID. |
connHandler | A pointer to a ConnectionHandler that will receive incoming connections. |
logInstance | The log target. Obtain it from ClientBase::logInstance(). |
Definition at line 45 of file linklocalmanager.cpp.
|
virtual |
Virtual destructor.
deregisterService()
and stopBrowsing()
will be called automatically if necessary. Definition at line 55 of file linklocalmanager.cpp.
void addTXTData | ( | const std::string & | key, |
const std::string & | value | ||
) |
Lets you add additional data to the published TXT record.
txtvers=1
parameter is included by default and cannot be changed. key | The key of a key=value parameter pair. Must be non-empty. If the given key has been set before, its value will be overwritten by the new value. |
value | The value of a key=value parameter pair. Must be non-empty. |
registerService()
. You will have to call registerService()
again to update the TXT record. Definition at line 61 of file linklocalmanager.cpp.
void deregisterService | ( | ) |
Removes the published DNS records and thereby stops advertising link-local messaging capabilities.
Definition at line 117 of file linklocalmanager.cpp.
void recv | ( | int | timeout | ) |
Checks once for new data on the socket used for browsing.
timeout | The timeout for select() in microseconds. Use -1 if blocking behavior is acceptable. |
Definition at line 156 of file linklocalmanager.cpp.
|
inline |
Registers a handler that will be notfied about entities found on the network that match the given browse criteria.
handler | The handler to register. |
Definition at line 308 of file linklocalmanager.h.
void registerService | ( | ) |
Starts advertising link-local messaging capabilities by publishing a number of DNS records, as per XEP-0174. You can call this function again to publish any values you updated after the first call.
Definition at line 74 of file linklocalmanager.cpp.
void removeTXTData | ( | const std::string & | key | ) |
Lets you remove TXT record data by key.
txtvers=1
parameter is included by default and cannot be removed. key | The key of the key=value parameter pair that should be removed. Must be non-empty. |
registerService()
. You will have to call registerService()
again to update the TXT record. Definition at line 69 of file linklocalmanager.cpp.
|
inline |
This function lets you set an alternate browse domain. The default domain should work in most cases.
domain | The browse domain to set. |
registerService()
. You will have to call registerService()
again to update the domain. The same applies to startBrowsing()
. Definition at line 247 of file linklocalmanager.h.
|
inline |
Lets you specify an alternate host name to advertise. By default the local machine's hostname as returned by gethostname()
will be used.
host | The hostname to use. |
registerService()
. You will have to call registerService()
again to update the hostname. Definition at line 238 of file linklocalmanager.h.
|
inline |
This function can be used to set a non-default interface. Use if_nametoindex()
to find the index for a specific named device. By default DNS records will be broadcast on all available interfaces, and all available interfaces will be used or browsing services.
iface | The interface to use. If you set an interface here, and want to change it back to 'any', use 0. Use -1 to broadcast only on the local machine. |
registerService()
. You will have to call registerService()
again to use the new interface. The same applies to startBrowsing()
. Definition at line 272 of file linklocalmanager.h.
|
inline |
Lets you specifiy a port to listen on for incoming server-less XMPP connections. Default for this implementation is port 5562, but any unused port can be used.
addTXTData()
with a key of "port.p2pj"
. port | The port to use. |
registerService()
. You will have to call registerService()
again to update the port. Definition at line 259 of file linklocalmanager.h.
|
inline |
Lets you specify a new username.
user | The new username. |
registerService()
. You will have to call registerService()
again to update the username. Definition at line 229 of file linklocalmanager.h.
|
inline |
Exposes the socket used for browsing so you can have it checked in your own event loop, if desired. If your event loop signals new data on the socket, you should NOT try to read from it directly. Instead you should call recv()
. As an alternative to using the raw socket you could also put the Manager in a separate thread and call recv()
repeatedly, or achieve this in any other way.
Definition at line 294 of file linklocalmanager.h.
bool startBrowsing | ( | ) |
Starts looking for other entities of type _presence
. You have to set a handler for results using registerLinkLocalHandler()
before calling this function. You can call this function again to re-start browsing with updated parameters.
Definition at line 126 of file linklocalmanager.cpp.
void stopBrowsing | ( | ) |
Stops the browsing.
Definition at line 147 of file linklocalmanager.cpp.