OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "quic_test_server.h" |
| 6 |
| 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/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" |
| 17 |
| 18 namespace cronet { |
| 19 |
| 20 namespace { |
| 21 |
| 22 static const char kServerHost[] = "127.0.0.1"; |
| 23 static const int kServerPort = 6121; |
| 24 |
| 25 base::Thread* g_quic_server_thread = nullptr; |
| 26 net::tools::QuicSimpleServer* g_quic_server = nullptr; |
| 27 |
| 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( |
| 33 file_dir.value()); |
| 34 net::IPAddressNumber ip; |
| 35 DCHECK(net::ParseIPLiteralToNumber(kServerHost, &ip)); |
| 36 net::QuicConfig config; |
| 37 g_quic_server = |
| 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"; |
| 41 } |
| 42 |
| 43 void ShutdownOnServerThread() { |
| 44 DCHECK(g_quic_server_thread->task_runner()->BelongsToCurrentThread()); |
| 45 g_quic_server->Shutdown(); |
| 46 delete g_quic_server; |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 // Quic server is currently hardcoded to run on port 6121 of the localhost on |
| 52 // the device. |
| 53 void StartQuicTestServer(JNIEnv* env, |
| 54 jclass jcaller, |
| 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)); |
| 65 } |
| 66 |
| 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; |
| 72 } |
| 73 |
| 74 jstring GetServerHost(JNIEnv* env, jclass jcaller) { |
| 75 return base::android::ConvertUTF8ToJavaString(env, kServerHost).Release(); |
| 76 } |
| 77 |
| 78 int GetServerPort(JNIEnv* env, jclass jcaller) { |
| 79 return kServerPort; |
| 80 } |
| 81 |
| 82 bool RegisterQuicTestServer(JNIEnv* env) { |
| 83 return RegisterNativesImpl(env); |
| 84 } |
| 85 |
| 86 } // namespace cronet |
OLD | NEW |