27 extern void acquireMulticastLock();
28 extern void releaseMulticastLock();
32 const String& serviceDescription,
33 int broadcastPortToUse,
int connectionPort,
35 :
Thread (
"Discovery_broadcast"),
36 message (serviceTypeUID), broadcastPort (broadcastPortToUse),
37 minInterval (minTimeBetweenBroadcasts)
53 void NetworkServiceDiscovery::Advertiser::run()
68 void NetworkServiceDiscovery::Advertiser::sendBroadcast()
71 message.
setAttribute (
"address", localAddress.toString());
74 socket.
write (broadcastAddress.toString(), broadcastPort, data.toRawUTF8(), (int) data.getNumBytesAsUTF8());
79 :
Thread (
"Discovery_listen"), serviceTypeUID (serviceType)
82 acquireMulticastLock();
95 releaseMulticastLock();
99 void NetworkServiceDiscovery::AvailableServiceList::run()
106 auto bytesRead = socket.
read (buffer,
sizeof (buffer) - 1,
false);
111 if (xml->hasTagName (serviceTypeUID))
112 handleMessage (*xml);
115 removeTimedOutServices();
122 auto listCopy = services;
126 void NetworkServiceDiscovery::AvailableServiceList::handleAsyncUpdate()
132 void NetworkServiceDiscovery::AvailableServiceList::handleMessage (
const XmlElement& xml)
144 handleMessage (service);
148 static void sortServiceList (std::vector<NetworkServiceDiscovery::Service>& services)
156 std::sort (services.begin(), services.end(), compareServices);
159 void NetworkServiceDiscovery::AvailableServiceList::handleMessage (
const Service& service)
163 for (
auto& s : services)
168 || s.address != service.
address 169 || s.port != service.
port)
180 services.push_back (service);
181 sortServiceList (services);
185 void NetworkServiceDiscovery::AvailableServiceList::removeTimedOutServices()
187 const double timeoutSeconds = 5.0;
192 auto oldEnd = std::end (services);
193 auto newEnd = std::remove_if (std::begin (services), oldEnd,
196 if (newEnd != oldEnd)
198 services.erase (newEnd, oldEnd);
std::vector< Service > getServices() const
Returns a list of the currently known services.
Contains information about a service that has been found on the network.
bool stopThread(int timeOutMilliseconds)
Attempts to stop the thread running.
bool bindToPort(int localPortNumber)
Binds the socket to the specified local port.
Represents an IP address.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
void triggerAsyncUpdate()
Causes the callback to be triggered at a later time.
Used to build a tree of elements representing an XML document.
String instanceID
A UUID that identifies the particular instance of the Advertiser class.
void shutdown()
Closes the underlying socket object.
int getIntAttribute(StringRef attributeName, int defaultReturnValue=0) const
Returns the value of a named attribute as an integer.
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
A relative measure of time.
IPAddress address
The IP address of the advertiser.
std::function< void()> onChange
A lambda that can be set to receive a callback when the list changes.
Advertiser(const String &serviceTypeUID, const String &serviceDescription, int broadcastPort, int connectionPort, RelativeTime minTimeBetweenBroadcasts=RelativeTime::seconds(1.5))
Creates and starts an Advertiser thread, broadcasting with the given properties.
~Advertiser() override
Destructor.
int64 inMilliseconds() const noexcept
Returns the number of milliseconds this time represents.
A universally unique 128-bit identifier.
static RelativeTime seconds(double seconds) noexcept
Creates a new RelativeTime object representing a number of seconds.
int waitUntilReady(bool readyForReading, int timeoutMsecs)
Waits until the socket is ready for reading or writing.
int read(void *destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived)
Reads bytes from the socket.
Time lastSeen
The time of the last ping received from the advertiser.
~AvailableServiceList() override
Destructor.
void setAttribute(const Identifier &attributeName, const String &newValue)
Adds a named attribute to the element.
bool threadShouldExit() const
Checks whether the thread has been told to stop running.
int write(const String &remoteHostname, int remotePortNumber, const void *sourceBuffer, int numBytesToWrite)
Writes bytes to the socket from a buffer.
static IPAddress getLocalAddress(bool includeIPv6=false)
Returns the first 'real' address for the local machine.
const String & getStringAttribute(StringRef attributeName) const noexcept
Returns the value of a named attribute.
AvailableServiceList(const String &serviceTypeUID, int broadcastPort)
Creates an AvailableServiceList that will bind to the given port number and watch the network for Adv...
String trim() const
Returns a copy of this string with any whitespace characters removed from the start and end...
static IPAddress getInterfaceBroadcastAddress(const IPAddress &interfaceAddress)
If the IPAdress is the address of an interface on the machine, returns the associated broadcast addre...
Automatically locks and unlocks a mutex object.
int port
The port number of the advertiser.
bool wait(int timeOutMilliseconds) const
Suspends the execution of this thread until either the specified timeout period has elapsed...
A struct containing options for formatting the text when representing an XML element as a string...
String description
The service description as sent by the Advertiser.
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
void startThread()
Starts the thread running.
String toString(const TextFormat &format={}) const
Returns a text version of this XML element.