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/threading/thread.h"
12 #include "jni/QuicTestServer_jni.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_util.h"
15 #include "net/tools/quic/quic_in_memory_cache.h"
16 #include "net/tools/quic/quic_simple_server.h"
22 static const char kServerHost
[] = "127.0.0.1";
23 static const int kServerPort
= 6121;
25 base::Thread
* g_quic_server_thread
= nullptr;
26 net::tools::QuicSimpleServer
* g_quic_server
= nullptr;
28 void ServeFilesFromDirectory(const base::FilePath
& directory
) {
29 DCHECK(g_quic_server_thread
->task_runner()->BelongsToCurrentThread());
30 DCHECK(!g_quic_server
);
31 base::FilePath file_dir
= directory
.Append("quic_data");
32 net::tools::QuicInMemoryCache::GetInstance()->InitializeFromDirectory(
34 net::IPAddressNumber ip
;
35 DCHECK(net::ParseIPLiteralToNumber(kServerHost
, &ip
));
36 net::QuicConfig config
;
38 new net::tools::QuicSimpleServer(config
, net::QuicSupportedVersions());
39 DCHECK(g_quic_server
->Listen(net::IPEndPoint(ip
, kServerPort
)) >= 0) <<
40 "Quic server fails to start";
43 void ShutdownOnServerThread() {
44 DCHECK(g_quic_server_thread
->task_runner()->BelongsToCurrentThread());
45 g_quic_server
->Shutdown();
51 // Quic server is currently hardcoded to run on port 6121 of the localhost on
53 void StartQuicTestServer(JNIEnv
* env
,
55 jstring jtest_files_root
) {
56 DCHECK(!g_quic_server_thread
);
57 g_quic_server_thread
= new base::Thread("quic server thread");
58 base::Thread::Options thread_options
;
59 thread_options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
60 DCHECK(g_quic_server_thread
->StartWithOptions(thread_options
));
61 base::FilePath
test_files_root(
62 base::android::ConvertJavaStringToUTF8(env
, jtest_files_root
));
63 g_quic_server_thread
->task_runner()->PostTask(
64 FROM_HERE
, base::Bind(&ServeFilesFromDirectory
, test_files_root
));
67 void ShutdownQuicTestServer(JNIEnv
* env
, jclass jcaller
) {
68 DCHECK(!g_quic_server_thread
->task_runner()->BelongsToCurrentThread());
69 g_quic_server_thread
->task_runner()->PostTask(
70 FROM_HERE
, base::Bind(&ShutdownOnServerThread
));
71 delete g_quic_server_thread
;
74 jstring
GetServerHost(JNIEnv
* env
, jclass jcaller
) {
75 return base::android::ConvertUTF8ToJavaString(env
, kServerHost
).Release();
78 int GetServerPort(JNIEnv
* env
, jclass jcaller
) {
82 bool RegisterQuicTestServer(JNIEnv
* env
) {
83 return RegisterNativesImpl(env
);