Commit 8e584114 authored by vitalybuka's avatar vitalybuka Committed by Commit bot

GCP 2.0 prototype switches code cleanup.

TBR=gene@chromium.org

Review URL: https://codereview.chromium.org/587103002

Cr-Commit-Position: refs/heads/master@{#295925}
parent b44e7c8f
...@@ -7,20 +7,16 @@ ...@@ -7,20 +7,16 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
namespace command_line_reader { namespace command_line_reader {
const char kHttpPortSwitch[] = "http-port";
const char kTtlSwitch[] = "ttl";
const char kServiceNameSwitch[] = "service-name";
const char kDomainNameSwitch[] = "domain-name";
const char kStatePathSwitch[] = "state-path";
uint16 ReadHttpPort(uint16 default_value) { uint16 ReadHttpPort(uint16 default_value) {
uint32 http_port = 0; uint32 http_port = 0;
std::string http_port_string = std::string http_port_string =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kHttpPortSwitch); CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kHttpPort);
if (!base::StringToUint(http_port_string, &http_port)) if (!base::StringToUint(http_port_string, &http_port))
http_port = default_value; http_port = default_value;
...@@ -38,8 +34,8 @@ uint32 ReadTtl(uint32 default_value) { ...@@ -38,8 +34,8 @@ uint32 ReadTtl(uint32 default_value) {
uint32 ttl = 0; uint32 ttl = 0;
if (!base::StringToUint( if (!base::StringToUint(
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kTtlSwitch), CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kTtl),
&ttl)) { &ttl)) {
ttl = default_value; ttl = default_value;
} }
...@@ -49,14 +45,16 @@ uint32 ReadTtl(uint32 default_value) { ...@@ -49,14 +45,16 @@ uint32 ReadTtl(uint32 default_value) {
std::string ReadServiceNamePrefix(const std::string& default_value) { std::string ReadServiceNamePrefix(const std::string& default_value) {
std::string service_name = std::string service_name =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kServiceNameSwitch); CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kServiceName);
return service_name.empty() ? default_value : service_name; return service_name.empty() ? default_value : service_name;
} }
std::string ReadDomainName(const std::string& default_value) { std::string ReadDomainName(const std::string& default_value) {
std::string domain_name = std::string domain_name =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kDomainNameSwitch); CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kDomainName);
if (domain_name.empty()) if (domain_name.empty())
return default_value; return default_value;
...@@ -77,8 +75,8 @@ std::string ReadDomainName(const std::string& default_value) { ...@@ -77,8 +75,8 @@ std::string ReadDomainName(const std::string& default_value) {
} }
std::string ReadStatePath(const std::string& default_value) { std::string ReadStatePath(const std::string& default_value) {
std::string filename = std::string filename = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kStatePathSwitch); switches::kStatePath);
if (filename.empty()) if (filename.empty())
return default_value; return default_value;
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "cloud_print/gcp20/prototype/dns_packet_parser.h" #include "cloud_print/gcp20/prototype/dns_packet_parser.h"
#include "cloud_print/gcp20/prototype/dns_response_builder.h" #include "cloud_print/gcp20/prototype/dns_response_builder.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "net/base/dns_util.h" #include "net/base/dns_util.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/net_util.h" #include "net/base/net_util.h"
...@@ -97,7 +98,7 @@ void DnsSdServer::UpdateMetadata(const std::vector<std::string>& metadata) { ...@@ -97,7 +98,7 @@ void DnsSdServer::UpdateMetadata(const std::vector<std::string>& metadata) {
// then send it now. // then send it now.
uint32 current_ttl = GetCurrentTLL(); uint32 current_ttl = GetCurrentTLL();
if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) { if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoAnnouncement)) {
DnsResponseBuilder builder(current_ttl); DnsResponseBuilder builder(current_ttl);
builder.AppendTxt(serv_params_.service_name_, current_ttl, metadata_, true); builder.AppendTxt(serv_params_.service_name_, current_ttl, metadata_, true);
...@@ -183,7 +184,7 @@ void DnsSdServer::ProcessMessage(int len, net::IOBufferWithSize* buf) { ...@@ -183,7 +184,7 @@ void DnsSdServer::ProcessMessage(int len, net::IOBufferWithSize* buf) {
VLOG(1) << "Current TTL for respond: " << current_ttl; VLOG(1) << "Current TTL for respond: " << current_ttl;
bool unicast_respond = bool unicast_respond =
CommandLine::ForCurrentProcess()->HasSwitch("unicast-respond"); CommandLine::ForCurrentProcess()->HasSwitch(switches::kUnicastRespond);
socket_->SendTo(buffer.get(), buffer.get()->size(), socket_->SendTo(buffer.get(), buffer.get()->size(),
unicast_respond ? recv_address_ : multicast_address_, unicast_respond ? recv_address_ : multicast_address_,
base::Bind(&DoNothingAfterSendToSocket)); base::Bind(&DoNothingAfterSendToSocket));
...@@ -204,7 +205,8 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query, ...@@ -204,7 +205,8 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query,
builder->AppendPtr(query.qname, current_ttl, builder->AppendPtr(query.qname, current_ttl,
serv_params_.service_name_, true); serv_params_.service_name_, true);
if (CommandLine::ForCurrentProcess()->HasSwitch("extended-response")) { if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kExtendedResponce)) {
builder->AppendSrv(serv_params_.service_name_, current_ttl, builder->AppendSrv(serv_params_.service_name_, current_ttl,
kSrvPriority, kSrvWeight, serv_params_.http_port_, kSrvPriority, kSrvWeight, serv_params_.http_port_,
serv_params_.service_domain_name_, false); serv_params_.service_domain_name_, false);
...@@ -278,7 +280,7 @@ void DnsSdServer::OnDatagramReceived() { ...@@ -278,7 +280,7 @@ void DnsSdServer::OnDatagramReceived() {
} }
void DnsSdServer::SendAnnouncement(uint32 ttl) { void DnsSdServer::SendAnnouncement(uint32 ttl) {
if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) { if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoAnnouncement)) {
DnsResponseBuilder builder(ttl); DnsResponseBuilder builder(ttl);
builder.AppendPtr(serv_params_.service_type_, ttl, builder.AppendPtr(serv_params_.service_type_, ttl,
......
...@@ -12,38 +12,11 @@ ...@@ -12,38 +12,11 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "cloud_print/gcp20/prototype/printer.h" #include "cloud_print/gcp20/prototype/printer.h"
namespace { namespace {
const char kHelpMessage[] =
"usage: gcp20_device [switches] [options]\n"
"\n"
"switches:\n"
" --disable-confirmation disables confirmation of registration\n"
" --disable-method-check disables HTTP method checking (POST, GET)\n"
" --disable-x-token disables checking of X-Privet-Token "
"HTTP header\n"
" -h, --help prints this message\n"
" --no-announcement disables DNS announcements\n"
" --extended-response responds to PTR with additional records\n"
" --simulate-printing-errors simulates some errors for local printing\n"
" --unicast-respond DNS responses will be sent in unicast "
"instead of multicast\n"
"\n"
"options:\n"
" --domain-name=<name> sets, should ends with '.local'\n"
" --http-port=<value> sets port for HTTP server\n"
" --service-name=<name> sets DNS service name\n"
" --state-path=<path> sets path to file with registration state\n"
" --ttl=<value> sets TTL for DNS announcements\n"
"\n"
"WARNING: mDNS probing is not implemented\n";
void PrintHelp() {
printf("%s", kHelpMessage);
}
void StartPrinter(Printer* printer) { void StartPrinter(Printer* printer) {
bool success = printer->Start(); bool success = printer->Start();
DCHECK(success); DCHECK(success);
...@@ -83,9 +56,9 @@ int main(int argc, char* argv[]) { ...@@ -83,9 +56,9 @@ int main(int argc, char* argv[]) {
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
logging::InitLogging(settings); logging::InitLogging(settings);
if (CommandLine::ForCurrentProcess()->HasSwitch("h") || if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelp) ||
CommandLine::ForCurrentProcess()->HasSwitch("help")) { CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelpShort)) {
PrintHelp(); switches::PrintUsage();
return 0; return 0;
} }
......
...@@ -49,6 +49,8 @@ ...@@ -49,6 +49,8 @@
'dns_response_builder.h', 'dns_response_builder.h',
'dns_sd_server.cc', 'dns_sd_server.cc',
'dns_sd_server.h', 'dns_sd_server.h',
'gcp20_switches.cc',
'gcp20_switches.h',
'local_settings.h', 'local_settings.h',
'local_print_job.cc', 'local_print_job.cc',
'local_print_job.h', 'local_print_job.h',
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
namespace switches {
const char kDisableConfirmation[] = "disable-confirmation";
const char kDisableMethodCheck[] = "disable-method-check";
const char kDisableXTocken[] = "disable-x-token";
const char kDomainName[] = "domain-name";
const char kExtendedResponce[] = "extended-response";
const char kHelpShort[] = "h";
const char kHelp[] = "help";
const char kHttpPort[] = "http-port";
const char kNoAnnouncement[] = "no-announcement";
const char kServiceName[] = "service-name";
const char kSimulatePrintingErrors[] = "simulate-printing-errors";
const char kStatePath[] = "state-path";
const char kTtl[] = "ttl";
const char kUnicastRespond[] = "unicast-respond";
const struct {
const char* const name;
const char* const description;
const char* const arg;
} kHelpStrings[] = {
{kDisableConfirmation, "disables confirmation of registration", NULL},
{kDisableMethodCheck, "disables HTTP method checking (POST, GET)", NULL},
{kDisableXTocken, "disables checking of X-Privet-Token HTTP header", NULL},
{kNoAnnouncement, "disables DNS announcements", NULL},
{kExtendedResponce, "responds to PTR with additional records", NULL},
{kSimulatePrintingErrors, "simulates some errors for local printing", NULL},
{kUnicastRespond,
"DNS responses will be sent in unicast instead of multicast", NULL},
{kDomainName, "sets, should ends with '.local'", "DOMAIN"},
{kHttpPort, "sets port for HTTP server", "PORT"},
{kServiceName, "sets DNS service name", "SERVICE"},
{kStatePath, "sets path to file with registration state", "PATH"},
{kTtl, "sets TTL for DNS announcements", "TTL"},
};
void PrintUsage() {
base::FilePath exe;
PathService::Get(base::FILE_EXE, &exe);
printf("usage: %s [OPTION]...\n\n", exe.BaseName().MaybeAsASCII().c_str());
for (size_t i = 0; i < arraysize(kHelpStrings); ++i) {
std::string name = kHelpStrings[i].name;
if (kHelpStrings[i].arg) {
name += '=';
name += kHelpStrings[i].arg;
}
name.resize(27, ' ');
printf(" --%s%s\n", name.c_str(), kHelpStrings[i].description);
}
printf("\n WARNING: mDNS probing is not implemented\n");
}
} // namespace switches
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CLOUD_PRINT_GCP20_PROTOTYPE_GCP20_SWITCHES_H_
#define CLOUD_PRINT_GCP20_PROTOTYPE_GCP20_SWITCHES_H_
namespace switches {
extern const char kDisableConfirmation[];
extern const char kDisableMethodCheck[];
extern const char kDisableXTocken[];
extern const char kDomainName[];
extern const char kExtendedResponce[];
extern const char kHelpShort[];
extern const char kHelp[];
extern const char kHttpPort[];
extern const char kNoAnnouncement[];
extern const char kServiceName[];
extern const char kSimulatePrintingErrors[];
extern const char kStatePath[];
extern const char kTtl[];
extern const char kUnicastRespond[];
void PrintUsage();
} // namespace switches
#endif // CLOUD_PRINT_GCP20_PROTOTYPE_GCP20_SWITCHES_H_
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
namespace { namespace {
...@@ -84,7 +85,8 @@ LocalPrintJob::CreateResult PrintJobHandler::CreatePrintJob( ...@@ -84,7 +85,8 @@ LocalPrintJob::CreateResult PrintJobHandler::CreatePrintJob(
return LocalPrintJob::CREATE_INVALID_TICKET; return LocalPrintJob::CREATE_INVALID_TICKET;
// Let's simulate at least some errors just for testing. // Let's simulate at least some errors just for testing.
if (CommandLine::ForCurrentProcess()->HasSwitch("simulate-printing-errors")) { if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSimulatePrintingErrors)) {
if (base::RandDouble() <= kPaperJamProbability) { if (base::RandDouble() <= kPaperJamProbability) {
*error_description = "Paper jam, try again"; *error_description = "Paper jam, try again";
return LocalPrintJob::CREATE_PRINTER_ERROR; return LocalPrintJob::CREATE_PRINTER_ERROR;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "net/base/ip_endpoint.h" #include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/url_util.h" #include "net/base/url_util.h"
...@@ -135,7 +136,7 @@ void PrivetHttpServer::OnHttpRequest(int connection_id, ...@@ -135,7 +136,7 @@ void PrivetHttpServer::OnHttpRequest(int connection_id,
if (!ValidateRequestMethod(connection_id, url.path(), info.method)) if (!ValidateRequestMethod(connection_id, url.path(), info.method))
return; return;
if (!CommandLine::ForCurrentProcess()->HasSwitch("disable-x-token")) { if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableXTocken)) {
net::HttpServerRequestInfo::HeadersMap::const_iterator iter = net::HttpServerRequestInfo::HeadersMap::const_iterator iter =
info.headers.find("x-privet-token"); info.headers.find("x-privet-token");
if (iter == info.headers.end()) { if (iter == info.headers.end()) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment