Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / components / cronet / android / test / quic_test_server.cc
blob1b2cfac94cd3eb888ee71b1f4683d6b1c93b3fd8
1 // Copyright 2015 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 "quic_test_server.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/threading/thread.h"
13 #include "jni/QuicTestServer_jni.h"
14 #include "net/base/ip_endpoint.h"
15 #include "net/base/net_util.h"
16 #include "net/tools/quic/quic_in_memory_cache.h"
17 #include "net/tools/quic/quic_simple_server.h"
19 namespace cronet {
21 namespace {
23 static const char kServerHost[] = "127.0.0.1";
24 static const int kServerPort = 6121;
26 base::Thread* g_quic_server_thread = nullptr;
27 net::tools::QuicSimpleServer* g_quic_server = nullptr;
29 void ServeFilesFromDirectory(
30 const base::FilePath& directory) {
31 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
32 DCHECK(!g_quic_server);
33 base::FilePath file_dir = directory.Append("quic_data");
34 CHECK(base::PathExists(file_dir)) << "Quic data does not exist";
35 net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory(
36 file_dir.value());
37 net::IPAddressNumber ip;
38 net::ParseIPLiteralToNumber(kServerHost, &ip);
39 net::QuicConfig config;
40 g_quic_server =
41 new net::tools::QuicSimpleServer(config, net::QuicSupportedVersions());
42 int rv = g_quic_server->Listen(net::IPEndPoint(ip, kServerPort));
43 CHECK_GE(rv, 0) << "Quic server fails to start";
44 JNIEnv* env = base::android::AttachCurrentThread();
45 Java_QuicTestServer_onServerStarted(env);
48 void ShutdownOnServerThread() {
49 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread());
50 g_quic_server->Shutdown();
51 delete g_quic_server;
54 } // namespace
56 // Quic server is currently hardcoded to run on port 6121 of the localhost on
57 // the device.
58 void StartQuicTestServer(JNIEnv* env,
59 jclass /*jcaller*/,
60 jstring jtest_files_root) {
61 DCHECK(!g_quic_server_thread);
62 g_quic_server_thread = new base::Thread("quic server thread");
63 base::Thread::Options thread_options;
64 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
65 bool started = g_quic_server_thread->StartWithOptions(thread_options);
66 DCHECK(started);
67 base::FilePath test_files_root(
68 base::android::ConvertJavaStringToUTF8(env, jtest_files_root));
69 g_quic_server_thread->task_runner()->PostTask(
70 FROM_HERE, base::Bind(&ServeFilesFromDirectory, test_files_root));
73 void ShutdownQuicTestServer(JNIEnv* env, jclass /*jcaller*/) {
74 DCHECK(!g_quic_server_thread->task_runner()->BelongsToCurrentThread());
75 g_quic_server_thread->task_runner()->PostTask(
76 FROM_HERE, base::Bind(&ShutdownOnServerThread));
77 delete g_quic_server_thread;
80 jstring GetServerHost(JNIEnv* env, jclass /*jcaller*/) {
81 return base::android::ConvertUTF8ToJavaString(env, kServerHost).Release();
84 int GetServerPort(JNIEnv* env, jclass /*jcaller*/) {
85 return kServerPort;
88 bool RegisterQuicTestServer(JNIEnv* env) {
89 return RegisterNativesImpl(env);
92 } // namespace cronet