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 #include "osp/impl/service_publisher_impl.h" 6 7 #include <utility> 8 9 #include "util/osp_logging.h" 10 11 namespace openscreen { 12 namespace osp { 13 namespace { 14 IsTransitionValid(ServicePublisher::State from,ServicePublisher::State to)15bool IsTransitionValid(ServicePublisher::State from, 16 ServicePublisher::State to) { 17 using State = ServicePublisher::State; 18 switch (from) { 19 case State::kStopped: 20 return to == State::kStarting || to == State::kStopping; 21 case State::kStarting: 22 return to == State::kRunning || to == State::kStopping || 23 to == State::kSuspended; 24 case State::kRunning: 25 return to == State::kSuspended || to == State::kStopping; 26 case State::kStopping: 27 return to == State::kStopped; 28 case State::kSuspended: 29 return to == State::kRunning || to == State::kStopping; 30 default: 31 OSP_DCHECK(false) << "unknown State value: " << static_cast<int>(from); 32 break; 33 } 34 return false; 35 } 36 37 } // namespace 38 39 ServicePublisherImpl::Delegate::Delegate() = default; 40 ServicePublisherImpl::Delegate::~Delegate() = default; 41 SetPublisherImpl(ServicePublisherImpl * publisher)42void ServicePublisherImpl::Delegate::SetPublisherImpl( 43 ServicePublisherImpl* publisher) { 44 OSP_DCHECK(!publisher_); 45 publisher_ = publisher; 46 } 47 ServicePublisherImpl(Observer * observer,std::unique_ptr<Delegate> delegate)48ServicePublisherImpl::ServicePublisherImpl(Observer* observer, 49 std::unique_ptr<Delegate> delegate) 50 : ServicePublisher(observer), delegate_(std::move(delegate)) { 51 delegate_->SetPublisherImpl(this); 52 } 53 54 ServicePublisherImpl::~ServicePublisherImpl() = default; 55 Start()56bool ServicePublisherImpl::Start() { 57 if (state_ != State::kStopped) 58 return false; 59 state_ = State::kStarting; 60 delegate_->StartPublisher(config_); 61 return true; 62 } StartAndSuspend()63bool ServicePublisherImpl::StartAndSuspend() { 64 if (state_ != State::kStopped) 65 return false; 66 state_ = State::kStarting; 67 delegate_->StartAndSuspendPublisher(config_); 68 return true; 69 } Stop()70bool ServicePublisherImpl::Stop() { 71 if (state_ == State::kStopped || state_ == State::kStopping) 72 return false; 73 74 state_ = State::kStopping; 75 delegate_->StopPublisher(); 76 return true; 77 } Suspend()78bool ServicePublisherImpl::Suspend() { 79 if (state_ != State::kRunning && state_ != State::kStarting) 80 return false; 81 82 delegate_->SuspendPublisher(); 83 return true; 84 } Resume()85bool ServicePublisherImpl::Resume() { 86 if (state_ != State::kSuspended) 87 return false; 88 89 delegate_->ResumePublisher(config_); 90 return true; 91 } 92 SetState(State state)93void ServicePublisherImpl::SetState(State state) { 94 OSP_DCHECK(IsTransitionValid(state_, state)); 95 state_ = state; 96 if (observer_) 97 MaybeNotifyObserver(); 98 } 99 MaybeNotifyObserver()100void ServicePublisherImpl::MaybeNotifyObserver() { 101 OSP_DCHECK(observer_); 102 switch (state_) { 103 case State::kRunning: 104 observer_->OnStarted(); 105 break; 106 case State::kStopped: 107 observer_->OnStopped(); 108 break; 109 case State::kSuspended: 110 observer_->OnSuspended(); 111 break; 112 default: 113 break; 114 } 115 } 116 117 } // namespace osp 118 } // namespace openscreen 119