168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// found in the LICENSE file.
468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "net/tools/quic/test_tools/server_thread.h"
668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "net/tools/quic/quic_dispatcher.h"
8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "net/tools/quic/test_tools/quic_server_peer.h"
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace net {
1168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace tools {
1268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace test {
1368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)ServerThread::ServerThread(QuicServer* server,
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           IPEndPoint address,
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           bool strike_register_no_startup_period)
1768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    : SimpleThread("server_thread"),
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      confirmed_(true, false),
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      pause_(true, false),
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      paused_(true, false),
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      resume_(true, false),
2268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      quit_(true, false),
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      server_(server),
2468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      address_(address),
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      port_(0),
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      initialized_(false) {
2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (strike_register_no_startup_period) {
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    server_->SetStrikeRegisterNoStartupPeriod();
2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
3168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ServerThread::~ServerThread() {}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ServerThread::Initialize() {
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (initialized_) {
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  server_->Listen(address_);
4068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
4168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  port_lock_.Acquire();
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  port_ = server_->port();
4368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  port_lock_.Release();
4468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  initialized_ = true;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ServerThread::Run() {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!initialized_) {
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Initialize();
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  while (!quit_.IsSignaled()) {
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (pause_.IsSignaled() && !resume_.IsSignaled()) {
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      paused_.Signal();
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      resume_.Wait();
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    server_->WaitForEvents();
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    MaybeNotifyOfHandshakeConfirmation();
6068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  server_->Shutdown();
6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
6468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
6568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)int ServerThread::GetPort() {
6668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  port_lock_.Acquire();
6768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int rc = port_;
6868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  port_lock_.Release();
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return rc;
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ServerThread::WaitForCryptoHandshakeConfirmed() {
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  confirmed_.Wait();
74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ServerThread::Pause() {
77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(!pause_.IsSignaled());
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  pause_.Signal();
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  paused_.Wait();
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ServerThread::Resume() {
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(!resume_.IsSignaled());
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DCHECK(pause_.IsSignaled());
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  resume_.Signal();
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ServerThread::Quit() {
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (pause_.IsSignaled() && !resume_.IsSignaled()) {
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    resume_.Signal();
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  quit_.Signal();
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void ServerThread::MaybeNotifyOfHandshakeConfirmation() {
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (confirmed_.IsSignaled()) {
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Only notify once.
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  QuicDispatcher* dispatcher = QuicServerPeer::GetDispatcher(server());
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (dispatcher->session_map().empty()) {
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Wait for a session to be created.
103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return;
104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
105f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  QuicSession* session = dispatcher->session_map().begin()->second;
106f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (session->IsCryptoHandshakeConfirmed()) {
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    confirmed_.Signal();
108f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
11168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}  // namespace test
11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}  // namespace tools
11368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}  // namespace net
114