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"
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"
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(
37 net::IPAddressNumber ip
;
38 net::ParseIPLiteralToNumber(kServerHost
, &ip
);
39 net::QuicConfig config
;
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();
56 // Quic server is currently hardcoded to run on port 6121 of the localhost on
58 void StartQuicTestServer(JNIEnv
* env
,
59 const JavaParamRef
<jclass
>& /*jcaller*/,
60 const JavaParamRef
<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
);
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
,
74 const JavaParamRef
<jclass
>& /*jcaller*/) {
75 DCHECK(!g_quic_server_thread
->task_runner()->BelongsToCurrentThread());
76 g_quic_server_thread
->task_runner()->PostTask(
77 FROM_HERE
, base::Bind(&ShutdownOnServerThread
));
78 delete g_quic_server_thread
;
81 ScopedJavaLocalRef
<jstring
> GetServerHost(
83 const JavaParamRef
<jclass
>& /*jcaller*/) {
84 return base::android::ConvertUTF8ToJavaString(env
, kServerHost
);
87 int GetServerPort(JNIEnv
* env
, const JavaParamRef
<jclass
>& /*jcaller*/) {
91 bool RegisterQuicTestServer(JNIEnv
* env
) {
92 return RegisterNativesImpl(env
);