Skip to content

Commit

Permalink
GCP 2.0 prototype switches code cleanup.
Browse files Browse the repository at this point in the history
TBR=gene@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#295925}
  • Loading branch information
vitalybuka authored and Commit bot committed Sep 22, 2014
1 parent b44e7c8 commit 8e58411
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 50 deletions.
24 changes: 11 additions & 13 deletions cloud_print/gcp20/prototype/command_line_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"

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) {
uint32 http_port = 0;

std::string http_port_string =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kHttpPortSwitch);
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kHttpPort);

if (!base::StringToUint(http_port_string, &http_port))
http_port = default_value;
Expand All @@ -38,8 +34,8 @@ uint32 ReadTtl(uint32 default_value) {
uint32 ttl = 0;

if (!base::StringToUint(
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kTtlSwitch),
&ttl)) {
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(switches::kTtl),
&ttl)) {
ttl = default_value;
}

Expand All @@ -49,14 +45,16 @@ uint32 ReadTtl(uint32 default_value) {

std::string ReadServiceNamePrefix(const std::string& default_value) {
std::string service_name =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kServiceNameSwitch);
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kServiceName);

return service_name.empty() ? default_value : service_name;
}

std::string ReadDomainName(const std::string& default_value) {
std::string domain_name =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kDomainNameSwitch);
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kDomainName);

if (domain_name.empty())
return default_value;
Expand All @@ -77,8 +75,8 @@ std::string ReadDomainName(const std::string& default_value) {
}

std::string ReadStatePath(const std::string& default_value) {
std::string filename =
CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kStatePathSwitch);
std::string filename = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kStatePath);

if (filename.empty())
return default_value;
Expand Down
10 changes: 6 additions & 4 deletions cloud_print/gcp20/prototype/dns_sd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/strings/stringprintf.h"
#include "cloud_print/gcp20/prototype/dns_packet_parser.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/net_errors.h"
#include "net/base/net_util.h"
Expand Down Expand Up @@ -97,7 +98,7 @@ void DnsSdServer::UpdateMetadata(const std::vector<std::string>& metadata) {
// then send it now.

uint32 current_ttl = GetCurrentTLL();
if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoAnnouncement)) {
DnsResponseBuilder builder(current_ttl);

builder.AppendTxt(serv_params_.service_name_, current_ttl, metadata_, true);
Expand Down Expand Up @@ -183,7 +184,7 @@ void DnsSdServer::ProcessMessage(int len, net::IOBufferWithSize* buf) {
VLOG(1) << "Current TTL for respond: " << current_ttl;

bool unicast_respond =
CommandLine::ForCurrentProcess()->HasSwitch("unicast-respond");
CommandLine::ForCurrentProcess()->HasSwitch(switches::kUnicastRespond);
socket_->SendTo(buffer.get(), buffer.get()->size(),
unicast_respond ? recv_address_ : multicast_address_,
base::Bind(&DoNothingAfterSendToSocket));
Expand All @@ -204,7 +205,8 @@ void DnsSdServer::ProccessQuery(uint32 current_ttl, const DnsQueryRecord& query,
builder->AppendPtr(query.qname, current_ttl,
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,
kSrvPriority, kSrvWeight, serv_params_.http_port_,
serv_params_.service_domain_name_, false);
Expand Down Expand Up @@ -278,7 +280,7 @@ void DnsSdServer::OnDatagramReceived() {
}

void DnsSdServer::SendAnnouncement(uint32 ttl) {
if (!CommandLine::ForCurrentProcess()->HasSwitch("no-announcement")) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoAnnouncement)) {
DnsResponseBuilder builder(ttl);

builder.AppendPtr(serv_params_.service_type_, ttl,
Expand Down
35 changes: 4 additions & 31 deletions cloud_print/gcp20/prototype/gcp20_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,11 @@
#include "base/run_loop.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "cloud_print/gcp20/prototype/printer.h"

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) {
bool success = printer->Start();
DCHECK(success);
Expand Down Expand Up @@ -83,9 +56,9 @@ int main(int argc, char* argv[]) {
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
logging::InitLogging(settings);

if (CommandLine::ForCurrentProcess()->HasSwitch("h") ||
CommandLine::ForCurrentProcess()->HasSwitch("help")) {
PrintHelp();
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelp) ||
CommandLine::ForCurrentProcess()->HasSwitch(switches::kHelpShort)) {
switches::PrintUsage();
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions cloud_print/gcp20/prototype/gcp20_device.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
'dns_response_builder.h',
'dns_sd_server.cc',
'dns_sd_server.h',
'gcp20_switches.cc',
'gcp20_switches.h',
'local_settings.h',
'local_print_job.cc',
'local_print_job.h',
Expand Down
63 changes: 63 additions & 0 deletions cloud_print/gcp20/prototype/gcp20_switches.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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
29 changes: 29 additions & 0 deletions cloud_print/gcp20/prototype/gcp20_switches.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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_
4 changes: 3 additions & 1 deletion cloud_print/gcp20/prototype/print_job_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"

namespace {

Expand Down Expand Up @@ -84,7 +85,8 @@ LocalPrintJob::CreateResult PrintJobHandler::CreatePrintJob(
return LocalPrintJob::CREATE_INVALID_TICKET;

// 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) {
*error_description = "Paper jam, try again";
return LocalPrintJob::CREATE_PRINTER_ERROR;
Expand Down
3 changes: 2 additions & 1 deletion cloud_print/gcp20/prototype/privet_http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "cloud_print/gcp20/prototype/gcp20_switches.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/url_util.h"
Expand Down Expand Up @@ -135,7 +136,7 @@ void PrivetHttpServer::OnHttpRequest(int connection_id,
if (!ValidateRequestMethod(connection_id, url.path(), info.method))
return;

if (!CommandLine::ForCurrentProcess()->HasSwitch("disable-x-token")) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableXTocken)) {
net::HttpServerRequestInfo::HeadersMap::const_iterator iter =
info.headers.find("x-privet-token");
if (iter == info.headers.end()) {
Expand Down

0 comments on commit 8e58411

Please sign in to comment.