OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 <iostream> | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/bind.h" | |
9 #include "base/command_line.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/message_loop/message_loop_proxy.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "sync/test/fake_server/fake_sync_server_http_handler.h" | |
14 | |
15 const char kPortNumberSwitch[] = "port"; | |
16 | |
17 // This runs a FakeSyncServerHttpHandler as a command-line app. | |
18 int main(int argc, char* argv[]) { | |
19 using fake_server::FakeSyncServerHttpHandler; | |
20 | |
21 CommandLine::Init(argc, argv); | |
22 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
23 logging::LoggingSettings settings; | |
24 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
25 logging::InitLogging(settings); | |
26 | |
27 FakeSyncServerHttpHandler* server; | |
28 | |
29 // Check if a port was specified on the command line | |
30 if (command_line->HasSwitch(kPortNumberSwitch)) { | |
31 std::string requested_port = | |
32 command_line->GetSwitchValueASCII(kPortNumberSwitch); | |
33 int port; | |
34 if (!base::StringToInt(requested_port, &port)) { | |
35 LOG(ERROR) << "Invalid --" << kPortNumberSwitch << " specified: \"" | |
36 << requested_port << "\""; | |
37 return -1; | |
38 } | |
39 | |
40 server = new FakeSyncServerHttpHandler(port); | |
41 } else { | |
42 LOG(INFO) << "Selecting an avilable port. Pass --" << kPortNumberSwitch | |
43 << "=<port number> to specify your own."; | |
44 server = new FakeSyncServerHttpHandler(); | |
45 } | |
46 | |
47 base::WeakPtrFactory<FakeSyncServerHttpHandler> server_ptr_factory(server); | |
48 | |
49 // An HttpServer must be run on a IO MessageLoop. | |
50 base::AtExitManager exit_manager; // Debug builds demand that ExitManager | |
51 // be initialized before MessageLoop. | |
52 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | |
53 bool posted = message_loop.current()->message_loop_proxy()->PostTask( | |
54 FROM_HERE, | |
55 base::Bind(&FakeSyncServerHttpHandler::Start, | |
56 server_ptr_factory.GetWeakPtr())); | |
57 CHECK(posted) << "Failed to start the HTTP server. PostTask returned false."; | |
58 message_loop.current()->Run(); | |
59 | |
60 return 0; | |
61 } | |
OLD | NEW |