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 
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 */
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 /** @}*/
juce::RelativeTime::seconds
static RelativeTime seconds(double seconds) noexcept
Creates a new RelativeTime object representing a number of seconds.
Definition: juce_RelativeTime.cpp:33
juce::NetworkServiceDiscovery::AvailableServiceList::~AvailableServiceList
~AvailableServiceList() override
Destructor.
Definition: juce_NetworkServiceDiscovery.cpp:80
juce::NetworkServiceDiscovery::AvailableServiceList
Watches the network for broadcasts from Advertiser objects, and keeps a list of all the currently act...
Definition: juce_NetworkServiceDiscovery.h:96
juce::Thread
Encapsulates a thread.
Definition: juce_Thread.h:46
juce::IPAddress
Represents an IP address.
Definition: juce_IPAddress.h:36
juce::RelativeTime
A relative measure of time.
Definition: juce_RelativeTime.h:40
juce::NetworkServiceDiscovery::Advertiser::Advertiser
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.
Definition: juce_NetworkServiceDiscovery.cpp:26
juce::NetworkServiceDiscovery::AvailableServiceList::AvailableServiceList
AvailableServiceList(const String &serviceTypeUID, int broadcastPort)
Creates an AvailableServiceList that will bind to the given port number and watch the network for Adv...
Definition: juce_NetworkServiceDiscovery.cpp:73
juce::XmlElement
Used to build a tree of elements representing an XML document.
Definition: juce_XmlElement.h:141
juce::NetworkServiceDiscovery::Advertiser::~Advertiser
~Advertiser() override
Destructor.
Definition: juce_NetworkServiceDiscovery.cpp:42
juce::NetworkServiceDiscovery::AvailableServiceList::getServices
std::vector< Service > getServices() const
Returns a list of the currently known services.
Definition: juce_NetworkServiceDiscovery.cpp:106
juce::NetworkServiceDiscovery::Advertiser
An object which runs a thread to repeatedly broadcast the existence of a discoverable service.
Definition: juce_NetworkServiceDiscovery.h:44
juce::Time
Holds an absolute date and time.
Definition: juce_Time.h:40
juce::NetworkServiceDiscovery
Contains classes that implement a simple protocol for broadcasting the availability and location of a...
Definition: juce_NetworkServiceDiscovery.h:36
juce::NetworkServiceDiscovery::Service::instanceID
String instanceID
A UUID that identifies the particular instance of the Advertiser class.
Definition: juce_NetworkServiceDiscovery.h:79
juce::AsyncUpdater
Has a callback method that is triggered asynchronously.
Definition: juce_AsyncUpdater.h:42
juce::NetworkServiceDiscovery::Service::port
int port
The port number of the advertiser.
Definition: juce_NetworkServiceDiscovery.h:82
juce::NetworkServiceDiscovery::Service::description
String description
The service description as sent by the Advertiser.
Definition: juce_NetworkServiceDiscovery.h:80
juce::NetworkServiceDiscovery::Service::lastSeen
Time lastSeen
The time of the last ping received from the advertiser.
Definition: juce_NetworkServiceDiscovery.h:83
juce::String
The JUCE String class!
Definition: juce_String.h:42
juce::CriticalSection
A re-entrant mutex.
Definition: juce_CriticalSection.h:46
juce::NetworkServiceDiscovery::AvailableServiceList::onChange
std::function< void()> onChange
A lambda that can be set to recieve a callback when the list changes.
Definition: juce_NetworkServiceDiscovery.h:111
juce::NetworkServiceDiscovery::Service
Contains information about a service that has been found on the network.
Definition: juce_NetworkServiceDiscovery.h:77
juce::NetworkServiceDiscovery::Service::address
IPAddress address
The IP address of the advertiser.
Definition: juce_NetworkServiceDiscovery.h:81
juce::DatagramSocket
A wrapper for a datagram (UDP) socket.
Definition: juce_Socket.h:205