| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Broker RPC Server implementation. | 5 // Broker RPC Server implementation. |
| 6 | 6 |
| 7 #include "ceee/ie/broker/broker_rpc_server.h" | 7 #include "ceee/ie/broker/broker_rpc_server.h" |
| 8 | 8 |
| 9 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
| 10 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 11 #include "base/logging.h" | 12 #include "base/process_util.h" |
| 12 #include "base/win_util.h" | 13 #include "base/win_util.h" |
| 13 #include "broker_rpc_lib.h" // NOLINT | 14 #include "broker_rpc_lib.h" // NOLINT |
| 14 #include "ceee/common/com_utils.h" | 15 #include "ceee/common/com_utils.h" |
| 15 #include "ceee/ie/broker/broker_module_util.h" | 16 #include "ceee/ie/broker/broker_module_util.h" |
| 16 #include "ceee/ie/broker/broker_rpc_utils.h" | 17 #include "ceee/ie/broker/broker_rpc_utils.h" |
| 17 #include "ceee/ie/broker/chrome_postman.h" | 18 #include "ceee/ie/broker/chrome_postman.h" |
| 18 | 19 |
| 20 |
| 21 namespace { |
| 19 // This lock ensures that histograms created by the broker are thread safe. | 22 // This lock ensures that histograms created by the broker are thread safe. |
| 20 // The histograms created here can be initialized on multiple threads. | 23 // The histograms created here can be initialized on multiple threads. |
| 21 Lock g_metrics_lock; | 24 Lock g_metrics_lock; |
| 22 | 25 |
| 26 RPC_STATUS PrepareEndpoint(std::wstring endpoint) { |
| 27 std::wstring protocol = kRpcProtocol; |
| 28 DCHECK(!protocol.empty()); |
| 29 DCHECK(!endpoint.empty()); |
| 30 if (protocol.empty() || endpoint.empty()) |
| 31 return false; |
| 32 VLOG(1) << "RPC server is starting. Endpoint: " << endpoint; |
| 33 // Tell RPC runtime to use local interprocess communication for given |
| 34 // end point. |
| 35 RPC_STATUS status = ::RpcServerUseProtseqEp( |
| 36 reinterpret_cast<RPC_WSTR>(&protocol[0]), |
| 37 RPC_C_PROTSEQ_MAX_REQS_DEFAULT, |
| 38 reinterpret_cast<RPC_WSTR>(&endpoint[0]), |
| 39 NULL); |
| 40 LOG_IF(ERROR, RPC_S_OK != status && RPC_S_DUPLICATE_ENDPOINT != status) << |
| 41 "Failed to set protocol for RPC end point. RPC_STATUS=0x" << |
| 42 com::LogWe(status); |
| 43 // This is not an error because unittest may start several servers. For |
| 44 // ceee_broker this is an error because we should not have several instances |
| 45 // of broker for the same endpoint. However BrokerRpcServer will fail anyway |
| 46 // while starting to listen. |
| 47 if (RPC_S_DUPLICATE_ENDPOINT == status) |
| 48 status = RPC_S_OK; |
| 49 return status; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 23 BrokerRpcServer::BrokerRpcServer() | 54 BrokerRpcServer::BrokerRpcServer() |
| 24 : is_started_(false), | 55 : is_started_(false), |
| 25 current_thread_(::GetCurrentThreadId()) { | 56 current_thread_(::GetCurrentThreadId()) { |
| 26 } | 57 } |
| 27 | 58 |
| 28 BrokerRpcServer::~BrokerRpcServer() { | 59 BrokerRpcServer::~BrokerRpcServer() { |
| 29 DCHECK(current_thread_ == ::GetCurrentThreadId()); | 60 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 30 Stop(); | 61 Stop(); |
| 31 } | 62 } |
| 32 | 63 |
| 33 bool BrokerRpcServer::Start() { | 64 bool BrokerRpcServer::Start() { |
| 34 DCHECK(current_thread_ == ::GetCurrentThreadId()); | 65 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 35 | 66 |
| 36 if (is_started()) | 67 if (is_started()) |
| 37 return true; | 68 return true; |
| 38 | 69 |
| 39 std::wstring end_point = GetRpcEndPointAddress(); | 70 std::wstring endpoint = GetRpcEndpointAddress(); |
| 40 std::wstring protocol = kRpcProtocol; | 71 RPC_STATUS status = PrepareEndpoint(endpoint); |
| 41 DCHECK(!protocol.empty()); | |
| 42 DCHECK(!end_point.empty()); | |
| 43 if (protocol.empty() || end_point.empty()) | |
| 44 return false; | |
| 45 | 72 |
| 46 LOG(INFO) << "RPC server is starting. Endpoint: " << end_point; | 73 if (RPC_S_OK == status) { |
| 47 // Tell RPC runtime to use local interprocess communication for given | |
| 48 // end point. | |
| 49 RPC_STATUS status = ::RpcServerUseProtseqEp( | |
| 50 reinterpret_cast<RPC_WSTR>(&protocol[0]), | |
| 51 RPC_C_PROTSEQ_MAX_REQS_DEFAULT, | |
| 52 reinterpret_cast<RPC_WSTR>(&end_point[0]), | |
| 53 NULL); | |
| 54 LOG_IF(ERROR, RPC_S_OK != status && RPC_S_DUPLICATE_ENDPOINT != status) << | |
| 55 "Failed to set protocol for RPC end point. RPC_STATUS=0x" << | |
| 56 com::LogWe(status); | |
| 57 if (RPC_S_OK == status || RPC_S_DUPLICATE_ENDPOINT == status) { | |
| 58 // Register RPC interface with the RPC runtime. | 74 // Register RPC interface with the RPC runtime. |
| 59 status = ::RpcServerRegisterIfEx(BrokerRpcServer_CeeeBroker_v1_1_s_ifspec, | 75 status = ::RpcServerRegisterIfEx(BrokerRpcServer_CeeeBroker_v1_1_s_ifspec, |
| 60 NULL, NULL, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL); | 76 NULL, NULL, 0, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL); |
| 61 LOG_IF(ERROR, RPC_S_OK != status) << | 77 LOG_IF(ERROR, RPC_S_OK != status) << |
| 62 "Failed to register RPC interface. RPC_STATUS=0x" << com::LogWe(status); | 78 "Failed to register RPC interface. RPC_STATUS=0x" << com::LogWe(status); |
| 63 if (RPC_S_OK == status) { | 79 if (RPC_S_OK == status) { |
| 64 // Start listen for RPC calls. | 80 // Start listen for RPC calls. |
| 65 status = ::RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE); | 81 status = ::RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE); |
| 66 LOG_IF(ERROR, RPC_S_OK != status) << | 82 LOG_IF(ERROR, RPC_S_OK != status) << |
| 67 "Failed to start listening. RPC_STATUS=0x" << com::LogWe(status); | 83 "Failed to start listening. RPC_STATUS=0x" << com::LogWe(status); |
| 68 if (RPC_S_OK == status) { | 84 if (RPC_S_OK == status) { |
| 69 LOG(INFO) << "RPC server is started. Endpoint: " << end_point; | 85 VLOG(1) << "RPC server is started. Endpoint: " << endpoint; |
| 70 is_started_ = true; | 86 is_started_ = true; |
| 71 } | 87 } |
| 72 } | 88 } |
| 73 } | 89 } |
| 74 if (!is_started()) | 90 if (!is_started()) |
| 75 Stop(); | 91 Stop(); |
| 76 | 92 |
| 77 return is_started(); | 93 return is_started(); |
| 78 } | 94 } |
| 79 | 95 |
| 80 bool BrokerRpcServer::Stop() { | 96 bool BrokerRpcServer::Stop() { |
| 81 DCHECK(current_thread_ == ::GetCurrentThreadId()); | 97 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 82 is_started_ = false; | 98 is_started_ = false; |
| 83 // Stop server listening for RPC. | 99 // Stop server listening for RPC. |
| 84 RPC_STATUS status = ::RpcMgmtStopServerListening(NULL); | 100 RPC_STATUS status = ::RpcMgmtStopServerListening(NULL); |
| 85 LOG_IF(WARNING, RPC_S_OK != status) << | 101 LOG_IF(WARNING, RPC_S_OK != status && RPC_S_NOT_LISTENING != status) << |
| 86 "Failed to stop listening. RPC_STATUS=0x" << com::LogWe(status); | 102 "Failed to stop listening. RPC_STATUS=0x" << com::LogWe(status); |
| 87 // Wait while server stops listening threads. | 103 // Wait while server stops listening threads. |
| 88 status = ::RpcMgmtWaitServerListen(); | 104 status = ::RpcMgmtWaitServerListen(); |
| 89 LOG_IF(WARNING, RPC_S_OK != status) << | 105 LOG_IF(WARNING, RPC_S_OK != status && RPC_S_NOT_LISTENING != status) << |
| 90 "Failed to wait server listen. RPC_STATUS=0x" << com::LogWe(status); | 106 "Failed to wait server listen. RPC_STATUS=0x" << com::LogWe(status); |
| 91 // Unregister RPC interface. | 107 // Unregister RPC interface. |
| 92 status = ::RpcServerUnregisterIf( | 108 status = ::RpcServerUnregisterIf( |
| 93 BrokerRpcServer_CeeeBroker_v1_1_s_ifspec, NULL, FALSE); | 109 BrokerRpcServer_CeeeBroker_v1_1_s_ifspec, NULL, FALSE); |
| 94 LOG_IF(WARNING, RPC_S_OK != status) << | 110 LOG_IF(WARNING, RPC_S_OK != status || RPC_S_UNKNOWN_MGR_TYPE != status || |
| 95 "Failed to unregister interface. RPC_STATUS=0x" << com::LogWe(status); | 111 RPC_S_UNKNOWN_IF != status) << |
| 112 "Failed to unregister interface. RPC_STATUS=0x" << com::LogWe(status); |
| 113 |
| 96 return RPC_S_OK == status; | 114 return RPC_S_OK == status; |
| 97 } | 115 } |
| 98 | 116 |
| 99 bool BrokerRpcServer::is_started() const { | 117 bool BrokerRpcServer::is_started() const { |
| 100 DCHECK(current_thread_ == ::GetCurrentThreadId()); | 118 DCHECK(current_thread_ == ::GetCurrentThreadId()); |
| 101 return is_started_; | 119 return is_started_; |
| 102 } | 120 } |
| 103 | 121 |
| 104 static base::AtomicSequenceNumber current_broker_rpc_context( | 122 static base::AtomicSequenceNumber current_broker_rpc_context( |
| 105 base::LINKER_INITIALIZED); | 123 base::LINKER_INITIALIZED); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 // We can't unfortunately use the HISTOGRAM_*_COUNT here because they use | 177 // We can't unfortunately use the HISTOGRAM_*_COUNT here because they use |
| 160 // static variables to save time. | 178 // static variables to save time. |
| 161 AutoLock lock(g_metrics_lock); | 179 AutoLock lock(g_metrics_lock); |
| 162 scoped_refptr<base::Histogram> counter = | 180 scoped_refptr<base::Histogram> counter = |
| 163 base::Histogram::FactoryGet(name, min, max, bucket_count, | 181 base::Histogram::FactoryGet(name, min, max, bucket_count, |
| 164 base::Histogram::kUmaTargetedHistogramFlag); | 182 base::Histogram::kUmaTargetedHistogramFlag); |
| 165 DCHECK_EQ(name, counter->histogram_name()); | 183 DCHECK_EQ(name, counter->histogram_name()); |
| 166 if (counter.get()) | 184 if (counter.get()) |
| 167 counter->AddTime(base::TimeDelta::FromMilliseconds(sample)); | 185 counter->AddTime(base::TimeDelta::FromMilliseconds(sample)); |
| 168 } | 186 } |
| OLD | NEW |