OpenShot Library | OpenShotAudio  0.2.2
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 
27 namespace 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 
36  @tags{Events}
37 */
39 {
40  /** An object which runs a thread to repeatedly broadcast the existence of a
41  discoverable service.
42 
43  To use, simply create an instance of an Advertiser and it'll broadcast until
44  you delete it.
45 
46  @tags{Events}
47  */
48  struct Advertiser : private Thread
49  {
50  /** Creates and starts an Advertiser thread, broadcasting with the given properties.
51  @param serviceTypeUID A user-supplied string to define the type of service this represents
52  @param serviceDescription A description string that will appear in the Service::description field for clients
53  @param broadcastPort The port number on which to broadcast the service discovery packets
54  @param connectionPort The port number that will be sent to appear in the Service::port field
55  @param minTimeBetweenBroadcasts The interval to wait between sending broadcast messages
56  */
57  Advertiser (const String& serviceTypeUID,
58  const String& serviceDescription,
59  int broadcastPort,
60  int connectionPort,
61  RelativeTime minTimeBetweenBroadcasts = RelativeTime::seconds (1.5));
62 
63  /** Destructor */
64  ~Advertiser() override;
65 
66  private:
67  XmlElement message;
68  const int broadcastPort;
69  const RelativeTime minInterval;
70  DatagramSocket socket { true };
71 
72  void run() override;
73  void sendBroadcast();
74  };
75 
76  //==============================================================================
77  /**
78  Contains information about a service that has been found on the network.
79 
80  @see AvailableServiceList, Advertiser
81 
82  @tags{Events}
83  */
84  struct Service
85  {
86  String instanceID; /**< A UUID that identifies the particular instance of the Advertiser class. */
87  String description; /**< The service description as sent by the Advertiser */
88  IPAddress address; /**< The IP address of the advertiser */
89  int port; /**< The port number of the advertiser */
90  Time lastSeen; /**< The time of the last ping received from the advertiser */
91  };
92 
93  //==============================================================================
94  /**
95  Watches the network for broadcasts from Advertiser objects, and keeps a list of
96  all the currently active instances.
97 
98  Just create an instance of AvailableServiceList and it will start listening - you
99  can register a callback with its onChange member to find out when services
100  appear/disappear, and you can call getServices() to find out the current list.
101 
102  @see Service, Advertiser
103 
104  @tags{Events}
105  */
106  struct AvailableServiceList : private Thread,
107  private AsyncUpdater
108  {
109  /** Creates an AvailableServiceList that will bind to the given port number and watch
110  the network for Advertisers broadcasting the given service type.
111 
112  This will only detect broadcasts from an Advertiser object with a matching
113  serviceTypeUID value, and where the broadcastPort matches.
114  */
115  AvailableServiceList (const String& serviceTypeUID, int broadcastPort);
116 
117  /** Destructor */
118  ~AvailableServiceList() override;
119 
120  /** A lambda that can be set to receive a callback when the list changes */
121  std::function<void()> onChange;
122 
123  /** Returns a list of the currently known services. */
124  std::vector<Service> getServices() const;
125 
126  private:
127  DatagramSocket socket { true };
128  String serviceTypeUID;
129  CriticalSection listLock;
130  std::vector<Service> services;
131 
132  void run() override;
133  void handleAsyncUpdate() override;
134  void handleMessage (const XmlElement&);
135  void handleMessage (const Service&);
136  void removeTimedOutServices();
137 
138  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableServiceList)
139  };
140 };
141 
142 } // namespace juce
143 
144 /** @}*/
A wrapper for a datagram (UDP) socket.
Definition: juce_Socket.h:207
Watches the network for broadcasts from Advertiser objects, and keeps a list of all the currently act...
Contains information about a service that has been found on the network.
Represents an IP address.
Used to build a tree of elements representing an XML document.
String instanceID
A UUID that identifies the particular instance of the Advertiser class.
Has a callback method that is triggered asynchronously.
The JUCE String class!
Definition: juce_String.h:42
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.
static RelativeTime seconds(double seconds) noexcept
Creates a new RelativeTime object representing a number of seconds.
Contains classes that implement a simple protocol for broadcasting the availability and location of a...
Time lastSeen
The time of the last ping received from the advertiser.
Encapsulates a thread.
Definition: juce_Thread.h:46
An object which runs a thread to repeatedly broadcast the existence of a discoverable service...
A re-entrant mutex.
Holds an absolute date and time.
Definition: juce_Time.h:40
int port
The port number of the advertiser.
String description
The service description as sent by the Advertiser.