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_SERVICE_PUBLISHER_H_
6 #define OSP_PUBLIC_SERVICE_PUBLISHER_H_
7 
8 #include <cstdint>
9 #include <string>
10 #include <vector>
11 
12 #include "osp/public/timestamp.h"
13 #include "platform/api/network_interface.h"
14 #include "platform/base/macros.h"
15 
16 namespace openscreen {
17 namespace osp {
18 
19 // Used to report an error from a ServiceListener implementation.
20 struct ServicePublisherError {
21   // TODO(mfoltz): Add additional error types, as implementations progress.
22   enum class Code {
23     kNone = 0,
24   };
25 
26   ServicePublisherError();
27   ServicePublisherError(Code error, const std::string& message);
28   ServicePublisherError(const ServicePublisherError& other);
29   ~ServicePublisherError();
30 
31   ServicePublisherError& operator=(const ServicePublisherError& other);
32 
33   Code error;
34   std::string message;
35 };
36 
37 class ServicePublisher {
38  public:
39   enum class State {
40     kStopped = 0,
41     kStarting,
42     kRunning,
43     kStopping,
44     kSuspended,
45   };
46 
47   struct Metrics {
48     Metrics();
49     ~Metrics();
50 
51     // The range of time over which the metrics were collected; end_timestamp >
52     // start_timestamp
53     timestamp_t start_timestamp = 0;
54     timestamp_t end_timestamp = 0;
55 
56     // The number of packets and bytes sent since the service started.
57     uint64_t num_packets_sent = 0;
58     uint64_t num_bytes_sent = 0;
59 
60     // The number of packets and bytes received since the service started.
61     uint64_t num_packets_received = 0;
62     uint64_t num_bytes_received = 0;
63   };
64 
65   class Observer {
66    public:
67     virtual ~Observer() = default;
68 
69     // Called when the state becomes kRunning.
70     virtual void OnStarted() = 0;
71     // Called when the state becomes kStopped.
72     virtual void OnStopped() = 0;
73     // Called when the state becomes kSuspended.
74     virtual void OnSuspended() = 0;
75 
76     // Reports an error.
77     virtual void OnError(ServicePublisherError) = 0;
78 
79     // Reports metrics.
80     virtual void OnMetrics(Metrics) = 0;
81   };
82 
83   struct Config {
84     Config();
85     ~Config();
86 
87     // The human readable friendly name of the service being published in
88     // UTF-8.
89     std::string friendly_name;
90 
91     // The DNS hostname (as a single label) that should be used to advertise the
92     // host's interface addresses.
93     std::string hostname;
94 
95     // The DNS domain name label that should be used to identify this service
96     // within the openscreen service type.
97     // TODO(btolsch): This could be derived from |friendly_name| but we will
98     // leave it as an arbitrary name until the spec is finalized.
99     std::string service_instance_name;
100 
101     // The port where openscreen connections are accepted.
102     // Normally this should not be set, and must be identical to the port
103     // configured in the ProtocolConnectionServer.
104     uint16_t connection_server_port = 0;
105 
106     // A list of network interface names that the publisher should use.
107     // By default, all enabled Ethernet and WiFi interfaces are used.
108     // This configuration must be identical to the interfaces configured
109     // in the ScreenConnectionServer.
110     std::vector<NetworkInterfaceIndex> network_interface_indices;
111   };
112 
113   virtual ~ServicePublisher();
114 
115   // Starts publishing this service using the config object.
116   // Returns true if state() == kStopped and the service will be started, false
117   // otherwise.
118   virtual bool Start() = 0;
119 
120   // Starts publishing this service, but then immediately suspends the
121   // publisher. No announcements will be sent until Resume() is called. Returns
122   // true if state() == kStopped and the service will be started, false
123   // otherwise.
124   virtual bool StartAndSuspend() = 0;
125 
126   // Stops publishing this service.
127   // Returns true if state() != (kStopped|kStopping).
128   virtual bool Stop() = 0;
129 
130   // Suspends publishing, for example, if the service is in a power saving
131   // mode. Returns true if state() == (kRunning|kStarting), meaning the
132   // suspension will take effect.
133   virtual bool Suspend() = 0;
134 
135   // Resumes publishing.  Returns true if state() == kSuspended.
136   virtual bool Resume() = 0;
137 
138   // Returns the current state of the publisher.
state()139   State state() const { return state_; }
140 
141   // Returns the last error reported by this publisher.
last_error()142   ServicePublisherError last_error() const { return last_error_; }
143 
144  protected:
145   explicit ServicePublisher(Observer* observer);
146 
147   State state_;
148   ServicePublisherError last_error_;
149   Observer* observer_;
150 
151   OSP_DISALLOW_COPY_AND_ASSIGN(ServicePublisher);
152 };
153 
154 }  // namespace osp
155 }  // namespace openscreen
156 
157 #endif  // OSP_PUBLIC_SERVICE_PUBLISHER_H_
158