1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef OSP_PUBLIC_NETWORK_SERVICE_MANAGER_H_
6 #define OSP_PUBLIC_NETWORK_SERVICE_MANAGER_H_
7 
8 #include <memory>
9 
10 #include "osp/public/protocol_connection_client.h"
11 #include "osp/public/protocol_connection_server.h"
12 #include "osp/public/service_listener.h"
13 #include "osp/public/service_publisher.h"
14 
15 namespace openscreen {
16 namespace osp {
17 
18 // Manages services run as part of the Open Screen Protocol Library.  Library
19 // embedders should pass instances of required services to Create(), which will
20 // instantiate the singleton instance of the NetworkServiceManager and take
21 // ownership of the services.
22 //
23 // NOTES: If we add more injectable services, consider implementing a struct to
24 // hold the config vs. passsing long argument lists.
25 class NetworkServiceManager final {
26  public:
27   // Creates the singleton instance of the NetworkServiceManager.  nullptr may
28   // be passed for services not provided by the embedder.
29   static NetworkServiceManager* Create(
30       std::unique_ptr<ServiceListener> mdns_listener,
31       std::unique_ptr<ServicePublisher> mdns_publisher,
32       std::unique_ptr<ProtocolConnectionClient> connection_client,
33       std::unique_ptr<ProtocolConnectionServer> connection_server);
34 
35   // Returns the singleton instance of the NetworkServiceManager previously
36   // created by Create().
37   static NetworkServiceManager* Get();
38 
39   // Destroys the NetworkServiceManager singleton and its owned services.  The
40   // services must be shut down and no I/O or asynchronous work should be done
41   // by the service instance destructors.
42   static void Dispose();
43 
44   // Returns an instance of the mDNS receiver listener, or nullptr if not
45   // provided.
46   ServiceListener* GetMdnsServiceListener();
47 
48   // Returns an instance of the mDNS receiver publisher, or nullptr if not
49   // provided.
50   ServicePublisher* GetMdnsServicePublisher();
51 
52   // Returns an instance of the protocol connection client, or nullptr if not
53   // provided.
54   ProtocolConnectionClient* GetProtocolConnectionClient();
55 
56   // Returns an instance of the protocol connection server, or nullptr if not
57   // provided.
58   ProtocolConnectionServer* GetProtocolConnectionServer();
59 
60  private:
61   NetworkServiceManager(
62       std::unique_ptr<ServiceListener> mdns_listener,
63       std::unique_ptr<ServicePublisher> mdns_publisher,
64       std::unique_ptr<ProtocolConnectionClient> connection_client,
65       std::unique_ptr<ProtocolConnectionServer> connection_server);
66 
67   ~NetworkServiceManager();
68 
69   std::unique_ptr<ServiceListener> mdns_listener_;
70   std::unique_ptr<ServicePublisher> mdns_publisher_;
71   std::unique_ptr<ProtocolConnectionClient> connection_client_;
72   std::unique_ptr<ProtocolConnectionServer> connection_server_;
73 };
74 
75 }  // namespace osp
76 }  // namespace openscreen
77 
78 #endif  // OSP_PUBLIC_NETWORK_SERVICE_MANAGER_H_
79