Home | History | Annotate | Download | only in prototype
      1 // Copyright 2013 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 <signal.h>
      6 
      7 #include "base/at_exit.h"
      8 #include "base/bind.h"
      9 #include "base/command_line.h"
     10 #include "base/logging.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/run_loop.h"
     13 #include "base/threading/platform_thread.h"
     14 #include "base/time/time.h"
     15 #include "cloud_print/gcp20/prototype/printer.h"
     16 
     17 namespace {
     18 
     19 const char kHelpMessage[] =
     20     "usage: gcp20_device [switches] [options]\n"
     21     "\n"
     22     "switches:\n"
     23     "  --disable-confirmation     disables confirmation of registration\n"
     24     "  --disable-method-check     disables HTTP method checking (POST, GET)\n"
     25     "  --disable-x-token          disables checking of X-Privet-Token "
     26                                  "HTTP header\n"
     27     "  -h, --help                 prints this message\n"
     28     "  --no-announcement          disables DNS announcements\n"
     29     "  --extended-response        responds to PTR with additional records\n"
     30     "  --simulate-printing-errors simulates some errors for local printing\n"
     31     "  --unicast-respond          DNS responses will be sent in unicast "
     32                                  "instead of multicast\n"
     33     "\n"
     34     "options:\n"
     35     "  --domain-name=<name>       sets, should ends with '.local'\n"
     36     "  --http-port=<value>        sets port for HTTP server\n"
     37     "  --service-name=<name>      sets DNS service name\n"
     38     "  --state-path=<path>        sets path to file with registration state\n"
     39     "  --ttl=<value>              sets TTL for DNS announcements\n"
     40     "\n"
     41     "WARNING: mDNS probing is not implemented\n";
     42 
     43 void PrintHelp() {
     44   printf("%s", kHelpMessage);
     45 }
     46 
     47 void StartPrinter(Printer* printer) {
     48   bool success = printer->Start();
     49   DCHECK(success);
     50 }
     51 
     52 base::RunLoop* g_runner = NULL;
     53 Printer* g_printer = NULL;
     54 base::MessageLoop* g_message_loop;
     55 
     56 void StopLoop() {
     57   // Always do after printer.Stop() to make sure XMPP will
     58   // be disabled fully before |Quit| will be called
     59   // (XMPP disables itself via MessageLoop call).
     60   g_message_loop->PostTask(FROM_HERE, g_runner->QuitClosure());
     61   g_message_loop = NULL;
     62   g_runner = NULL;
     63 }
     64 
     65 void OnAbort(int val) {
     66   if (g_printer) {
     67     g_message_loop->PostTask(
     68         FROM_HERE,
     69         base::Bind(&Printer::Stop, base::Unretained(g_printer)));
     70     g_message_loop->PostTask(FROM_HERE, base::Bind(&StopLoop));
     71     g_printer = NULL;
     72   }
     73 }
     74 
     75 }  // namespace
     76 
     77 int main(int argc, char* argv[]) {
     78   base::AtExitManager at_exit;
     79   Printer printer;
     80   CommandLine::Init(argc, argv);
     81 
     82   logging::LoggingSettings settings;
     83   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
     84   logging::InitLogging(settings);
     85 
     86   if (CommandLine::ForCurrentProcess()->HasSwitch("h") ||
     87       CommandLine::ForCurrentProcess()->HasSwitch("help")) {
     88     PrintHelp();
     89     return 0;
     90   }
     91 
     92   signal(SIGINT, OnAbort);  // Handle Ctrl+C signal.
     93 
     94   base::MessageLoopForIO loop;
     95   g_message_loop = &loop;
     96   g_message_loop->PostTask(FROM_HERE, base::Bind(&StartPrinter, &printer));
     97   base::RunLoop runner;
     98   g_printer = &printer;
     99   g_runner = &runner;
    100   runner.Run();
    101 
    102   return 0;
    103 }
    104 
    105