OpenShot Library | libopenshot-audio 0.2.0
juce_NetworkServiceDiscovery.h
1
2/** @weakgroup juce_events-interprocess
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 The code included in this file is provided under the terms of the ISC license
15 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16 To use, copy, modify, and/or distribute this software for any purpose with or
17 without fee is hereby granted provided that the above copyright notice and
18 this permission notice appear in all copies.
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29
30//==============================================================================
31/**
32 Contains classes that implement a simple protocol for broadcasting the availability
33 and location of a discoverable service on the local network, and for maintaining a
34 list of known services.
35*/
37{
38 /** An object which runs a thread to repeatedly broadcast the existence of a
39 discoverable service.
40
41 To use, simply create an instance of an Advertiser and it'll broadcast until
42 you delete it.
43 */
44 struct Advertiser : private Thread
45 {
46 /** Creates and starts an Advertiser thread, broadcasting with the given properties.
47 @param serviceTypeUID A user-supplied string to define the type of service this represents
48 @param serviceDescription A description string that will appear in the Service::description field for clients
49 @param broadcastPort The port number on which to broadcast the service discovery packets
50 @param connectionPort The port number that will be sent to appear in the Service::port field
51 @param minTimeBetweenBroadcasts The interval to wait between sending broadcast messages
52 */
53 Advertiser (const String& serviceTypeUID,
54 const String& serviceDescription,
55 int broadcastPort,
56 int connectionPort,
57 RelativeTime minTimeBetweenBroadcasts = RelativeTime::seconds (1.5));
58
59 /** Destructor */
60 ~Advertiser() override;
61
62 private:
63 XmlElement message;
64 const int broadcastPort;
65 const RelativeTime minInterval;
66 DatagramSocket socket { true };
67
68 void run() override;
69 void sendBroadcast();
70 };
71
72 //==============================================================================
73 /**
74 Contains information about a service that has been found on the network.
75 @see AvailableServiceList, Advertiser
76 */
77 struct Service
78 {
79 String instanceID; /**< A UUID that identifies the particular instance of the Advertiser class. */
80 String description; /**< The service description as sent by the Advertiser */
81 IPAddress address; /**< The IP address of the advertiser */
82 int port; /**< The port number of the advertiser */
83 Time lastSeen; /**< The time of the last ping received from the advertiser */
84 };
85
86 //==============================================================================
87 /**
88 Watches the network for broadcasts from Advertiser objects, and keeps a list of
89 all the currently active instances.
90
91 Just create an instance of AvailableServiceList and it will start listening - you
92 can register a callback with its onChange member to find out when services
93 appear/disappear, and you can call getServices() to find out the current list.
94 @see Service, Advertiser
95 */
96 struct AvailableServiceList : private Thread,
97 private AsyncUpdater
98 {
99 /** Creates an AvailableServiceList that will bind to the given port number and watch
100 the network for Advertisers broadcasting the given service type.
101
102 This will only detect broadcasts from an Advertiser object with a matching
103 serviceTypeUID value, and where the broadcastPort matches.
104 */
105 AvailableServiceList (const String& serviceTypeUID, int broadcastPort);
106
107 /** Destructor */
108 ~AvailableServiceList() override;
109
110 /** A lambda that can be set to recieve a callback when the list changes */
111 std::function<void()> onChange;
112
113 /** Returns a list of the currently known services. */
114 std::vector<Service> getServices() const;
115
116 private:
117 DatagramSocket socket { true };
118 String serviceTypeUID;
119 CriticalSection listLock;
120 std::vector<Service> services;
121
122 void run() override;
123 void handleAsyncUpdate() override;
124 void handleMessage (const XmlElement&);
125 void handleMessage (const Service&);
126 void removeTimedOutServices();
127
128 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableServiceList)
129 };
130};
131
132} // namespace juce
133
134/** @}*/
Has a callback method that is triggered asynchronously.
A wrapper for a datagram (UDP) socket.
Represents an IP address.
A relative measure of time.
static RelativeTime seconds(double seconds) noexcept
Creates a new RelativeTime object representing a number of seconds.
The JUCE String class!
Definition juce_String.h:43
Encapsulates a thread.
Definition juce_Thread.h:47
Holds an absolute date and time.
Definition juce_Time.h:41
Used to build a tree of elements representing an XML document.
String instanceID
A UUID that identifies the particular instance of the Advertiser class.
Time lastSeen
The time of the last ping received from the advertiser.
int port
The port number of the advertiser.
String description
The service description as sent by the Advertiser.
IPAddress address
The IP address of the advertiser.
Contains classes that implement a simple protocol for broadcasting the availability and location of a...
Contains information about a service that has been found on the network.
An object which runs a thread to repeatedly broadcast the existence of a discoverable service.
Watches the network for broadcasts from Advertiser objects, and keeps a list of all the currently act...
std::function< void()> onChange
A lambda that can be set to recieve a callback when the list changes.
std::vector< Service > getServices() const
Returns a list of the currently known services.