Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / tools / quic / test_tools / server_thread.cc
blob09cba688edce717e31223b4091de852f3ecfb383
1 // Copyright 2013 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.
5 #include "net/tools/quic/test_tools/server_thread.h"
7 #include "net/quic/test_tools/crypto_test_utils.h"
8 #include "net/tools/quic/quic_dispatcher.h"
9 #include "net/tools/quic/test_tools/quic_server_peer.h"
11 namespace net {
12 namespace tools {
13 namespace test {
15 ServerThread::ServerThread(QuicServer* server,
16 bool is_secure,
17 const IPEndPoint& address,
18 bool strike_register_no_startup_period)
19 : SimpleThread("server_thread"),
20 confirmed_(true, false),
21 pause_(true, false),
22 paused_(true, false),
23 resume_(true, false),
24 quit_(true, false),
25 server_(server),
26 address_(address),
27 port_(0),
28 initialized_(false) {
29 if (strike_register_no_startup_period) {
30 server_->SetStrikeRegisterNoStartupPeriod();
32 if (is_secure) {
33 // TODO(rtenneti): replace this with ProofSourceForTesting() when Chromium
34 // has a working ProofSourceForTesting().
35 server_->SetProofSource(
36 net::test::CryptoTestUtils::FakeProofSourceForTesting());
40 ServerThread::~ServerThread() {}
42 void ServerThread::Initialize() {
43 if (initialized_) {
44 return;
47 server_->Listen(address_);
49 port_lock_.Acquire();
50 port_ = server_->port();
51 port_lock_.Release();
53 initialized_ = true;
56 void ServerThread::Run() {
57 if (!initialized_) {
58 Initialize();
61 while (!quit_.IsSignaled()) {
62 if (pause_.IsSignaled() && !resume_.IsSignaled()) {
63 paused_.Signal();
64 resume_.Wait();
66 server_->WaitForEvents();
67 MaybeNotifyOfHandshakeConfirmation();
70 server_->Shutdown();
73 int ServerThread::GetPort() {
74 port_lock_.Acquire();
75 int rc = port_;
76 port_lock_.Release();
77 return rc;
80 void ServerThread::WaitForCryptoHandshakeConfirmed() {
81 confirmed_.Wait();
84 void ServerThread::Pause() {
85 DCHECK(!pause_.IsSignaled());
86 pause_.Signal();
87 paused_.Wait();
90 void ServerThread::Resume() {
91 DCHECK(!resume_.IsSignaled());
92 DCHECK(pause_.IsSignaled());
93 resume_.Signal();
96 void ServerThread::Quit() {
97 if (pause_.IsSignaled() && !resume_.IsSignaled()) {
98 resume_.Signal();
100 quit_.Signal();
103 void ServerThread::MaybeNotifyOfHandshakeConfirmation() {
104 if (confirmed_.IsSignaled()) {
105 // Only notify once.
106 return;
108 QuicDispatcher* dispatcher = QuicServerPeer::GetDispatcher(server());
109 if (dispatcher->session_map().empty()) {
110 // Wait for a session to be created.
111 return;
113 QuicSession* session = dispatcher->session_map().begin()->second;
114 if (session->IsCryptoHandshakeConfirmed()) {
115 confirmed_.Signal();
119 } // namespace test
120 } // namespace tools
121 } // namespace net