Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / tools / quic / test_tools / server_thread.cc
blobf654b7df1902f9a2bbdd197d0f9ca64a8cf33475
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/tools/quic/quic_dispatcher.h"
8 #include "net/tools/quic/test_tools/quic_server_peer.h"
10 namespace net {
11 namespace tools {
12 namespace test {
14 ServerThread::ServerThread(QuicServer* server,
15 IPEndPoint address,
16 bool strike_register_no_startup_period)
17 : SimpleThread("server_thread"),
18 confirmed_(true, false),
19 pause_(true, false),
20 paused_(true, false),
21 resume_(true, false),
22 quit_(true, false),
23 server_(server),
24 address_(address),
25 port_(0),
26 initialized_(false) {
27 if (strike_register_no_startup_period) {
28 server_->SetStrikeRegisterNoStartupPeriod();
32 ServerThread::~ServerThread() {}
34 void ServerThread::Initialize() {
35 if (initialized_) {
36 return;
39 server_->Listen(address_);
41 port_lock_.Acquire();
42 port_ = server_->port();
43 port_lock_.Release();
45 initialized_ = true;
48 void ServerThread::Run() {
49 if (!initialized_) {
50 Initialize();
53 while (!quit_.IsSignaled()) {
54 if (pause_.IsSignaled() && !resume_.IsSignaled()) {
55 paused_.Signal();
56 resume_.Wait();
58 server_->WaitForEvents();
59 MaybeNotifyOfHandshakeConfirmation();
62 server_->Shutdown();
65 int ServerThread::GetPort() {
66 port_lock_.Acquire();
67 int rc = port_;
68 port_lock_.Release();
69 return rc;
72 void ServerThread::WaitForCryptoHandshakeConfirmed() {
73 confirmed_.Wait();
76 void ServerThread::Pause() {
77 DCHECK(!pause_.IsSignaled());
78 pause_.Signal();
79 paused_.Wait();
82 void ServerThread::Resume() {
83 DCHECK(!resume_.IsSignaled());
84 DCHECK(pause_.IsSignaled());
85 resume_.Signal();
88 void ServerThread::Quit() {
89 if (pause_.IsSignaled() && !resume_.IsSignaled()) {
90 resume_.Signal();
92 quit_.Signal();
95 void ServerThread::MaybeNotifyOfHandshakeConfirmation() {
96 if (confirmed_.IsSignaled()) {
97 // Only notify once.
98 return;
100 QuicDispatcher* dispatcher = QuicServerPeer::GetDispatcher(server());
101 if (dispatcher->session_map().empty()) {
102 // Wait for a session to be created.
103 return;
105 QuicSession* session = dispatcher->session_map().begin()->second;
106 if (session->IsCryptoHandshakeConfirmed()) {
107 confirmed_.Signal();
111 } // namespace test
112 } // namespace tools
113 } // namespace net