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