Chromium Code Reviews| 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()) | |
|
MAD
2010/12/01 19:59:40
I think the test for protocol not being empty is n
Vitaly Buka corp
2010/12/01 20:50:30
But we will crash on protocol[0] if system out of
MAD
2010/12/01 21:09:16
Thus, another reason not to copy this in a std::ws
| |
| 31 return false; | |
| 32 VLOG(2) << "RPC server is starting. Endpoint: " << endpoint; | |
|
Sigurður Ásgeirsson
2010/12/01 19:29:38
nit: for the time being, please use VLOG(1), as Sa
Vitaly Buka corp
2010/12/01 20:50:30
Done.
| |
| 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 error because unittest may start we start several servers. | |
|
MAD
2010/12/01 19:59:40
not error -> not an error
may start we start sever
Vitaly Buka corp
2010/12/01 20:50:30
Done.
Vitaly Buka corp
2010/12/01 20:50:30
Done.
| |
| 44 // For ceee_broker this is error because we should not have several instancess | |
|
MAD
2010/12/01 19:59:40
this is error -> this is an error
instancess -> in
Vitaly Buka corp
2010/12/01 20:50:30
Done.
| |
| 45 // of broker for the same endpoint.However BrokerRpcServer with fail anyway | |
|
MAD
2010/12/01 19:59:40
endpoint.However -> endpoint. However
BrokerRpcSer
Vitaly Buka corp
2010/12/01 20:50:30
Done.
| |
| 46 //starting listening. | |
|
Sigurður Ásgeirsson
2010/12/01 19:29:38
nit: need one space after //
MAD
2010/12/01 19:59:40
starting listening -> while starting to listen.
Vitaly Buka corp
2010/12/01 20:50:30
Done.
| |
| 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(2) << "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 14 matching lines...) Expand all Loading... | |
| 120 } | 138 } |
| 121 | 139 |
| 122 // Called when client process terminated without releasing context handle. | 140 // Called when client process terminated without releasing context handle. |
| 123 void __RPC_USER BrokerContextHandle_rundown(BrokerContextHandle context) { | 141 void __RPC_USER BrokerContextHandle_rundown(BrokerContextHandle context) { |
| 124 DCHECK(context != NULL); | 142 DCHECK(context != NULL); |
| 125 ceee_module_util::UnlockModule(); | 143 ceee_module_util::UnlockModule(); |
| 126 } | 144 } |
| 127 | 145 |
| 128 void BrokerRpcServer_FireEvent( | 146 void BrokerRpcServer_FireEvent( |
| 129 handle_t binding_handle, | 147 handle_t binding_handle, |
| 148 BrokerContextHandle context, | |
| 130 const char* event_name, | 149 const char* event_name, |
| 131 const char* event_args) { | 150 const char* event_args) { |
| 132 DCHECK(ChromePostman::GetInstance()); | 151 DCHECK(ChromePostman::GetInstance()); |
| 133 if (ChromePostman::GetInstance()) | 152 if (ChromePostman::GetInstance()) |
| 134 ChromePostman::GetInstance()->FireEvent(event_name, event_args); | 153 ChromePostman::GetInstance()->FireEvent(event_name, event_args); |
| 135 } | 154 } |
| 136 | 155 |
| 137 void BrokerRpcServer_SendUmaHistogramTimes(handle_t binding_handle, | 156 void BrokerRpcServer_SendUmaHistogramTimes(handle_t binding_handle, |
| 138 const char* name, | 157 const char* name, |
| 139 int sample) { | 158 int sample) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 158 // 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 |
| 159 // static variables to save time. | 178 // static variables to save time. |
| 160 AutoLock lock(g_metrics_lock); | 179 AutoLock lock(g_metrics_lock); |
| 161 scoped_refptr<base::Histogram> counter = | 180 scoped_refptr<base::Histogram> counter = |
| 162 base::Histogram::FactoryGet(name, min, max, bucket_count, | 181 base::Histogram::FactoryGet(name, min, max, bucket_count, |
| 163 base::Histogram::kUmaTargetedHistogramFlag); | 182 base::Histogram::kUmaTargetedHistogramFlag); |
| 164 DCHECK_EQ(name, counter->histogram_name()); | 183 DCHECK_EQ(name, counter->histogram_name()); |
| 165 if (counter.get()) | 184 if (counter.get()) |
| 166 counter->AddTime(base::TimeDelta::FromMilliseconds(sample)); | 185 counter->AddTime(base::TimeDelta::FromMilliseconds(sample)); |
| 167 } | 186 } |
| OLD | NEW |