summaryrefslogtreecommitdiffstats
path: root/cloud_print/gcp20/prototype/printer.cc
diff options
context:
space:
mode:
authormaksymb@chromium.org <maksymb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-22 16:49:06 +0000
committermaksymb@chromium.org <maksymb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-22 16:49:06 +0000
commitead61a438feb008581b82fddca34a18527c868d1 (patch)
tree97fee11962961af87dca2f24401c92eabfcebb7b /cloud_print/gcp20/prototype/printer.cc
parent98751ff18ca3721338c6103d8f78d09dcb6e2a53 (diff)
downloadchromium_src-ead61a438feb008581b82fddca34a18527c868d1.zip
chromium_src-ead61a438feb008581b82fddca34a18527c868d1.tar.gz
chromium_src-ead61a438feb008581b82fddca34a18527c868d1.tar.bz2
Finished DNS-SD server. Finished Privet-specified DNS-SD server.
Review URL: https://chromiumcodereview.appspot.com/16975004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@208064 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cloud_print/gcp20/prototype/printer.cc')
-rw-r--r--cloud_print/gcp20/prototype/printer.cc131
1 files changed, 131 insertions, 0 deletions
diff --git a/cloud_print/gcp20/prototype/printer.cc b/cloud_print/gcp20/prototype/printer.cc
new file mode 100644
index 0000000..4d39fe8
--- /dev/null
+++ b/cloud_print/gcp20/prototype/printer.cc
@@ -0,0 +1,131 @@
+// Copyright 2013 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/printer.h"
+
+#include <string>
+#include <vector>
+
+#include "base/command_line.h"
+#include "base/strings/string_number_conversions.h"
+#include "cloud_print/gcp20/prototype/service_parameters.h"
+#include "net/base/net_util.h"
+
+namespace {
+
+const char* kServiceType = "_privet._tcp.local";
+const char* kServiceNamePrefix = "first_gcp20_device";
+const char* kServiceDomainName = "my-privet-device.local";
+
+const uint16 kDefaultTTL = 60*60;
+const uint16 kDefaultHttpPort = 10101;
+
+uint16 ReadHttpPortFromCommandLine() {
+ uint32 http_port_tmp = kDefaultHttpPort;
+
+ std::string http_port_string_tmp =
+ CommandLine::ForCurrentProcess()->GetSwitchValueASCII("http-port");
+ base::StringToUint(http_port_string_tmp, &http_port_tmp);
+
+ if (http_port_tmp > kuint16max) {
+ LOG(ERROR) << "Port " << http_port_tmp << " is too large (maximum is " <<
+ kDefaultHttpPort << "). Using default port.";
+
+ http_port_tmp = kDefaultHttpPort;
+ }
+
+ VLOG(1) << "HTTP port for responses: " << http_port_tmp;
+ return static_cast<uint16>(http_port_tmp);
+}
+
+uint16 ReadTtlFromCommandLine() {
+ uint32 ttl = kDefaultTTL;
+
+ base::StringToUint(
+ CommandLine::ForCurrentProcess()->GetSwitchValueASCII("ttl"), &ttl);
+
+ VLOG(1) << "TTL for announcements: " << ttl;
+ return ttl;
+}
+
+// Returns local IP address number of first interface found (except loopback).
+// Return value is empty if no interface found. Possible interfaces names are
+// "eth0", "wlan0" etc. If interface name is empty, function will return IP
+// address of first interface found.
+net::IPAddressNumber GetLocalIp(const std::string& interface_name,
+ bool return_ipv6_number) {
+ net::NetworkInterfaceList interfaces;
+ bool success = net::GetNetworkList(&interfaces);
+ DCHECK(success);
+
+ size_t expected_address_size = return_ipv6_number ? net::kIPv6AddressSize
+ : net::kIPv4AddressSize;
+
+ for (net::NetworkInterfaceList::iterator iter = interfaces.begin();
+ iter != interfaces.end(); ++iter) {
+ if (iter->address.size() == expected_address_size &&
+ (interface_name.empty() || interface_name == iter->name)) {
+ LOG(INFO) << net::IPAddressToString(iter->address);
+ return iter->address;
+ }
+ }
+
+ return net::IPAddressNumber();
+}
+
+} // namespace
+
+Printer::Printer() : initialized_(false) {
+}
+
+Printer::~Printer() {
+ Stop();
+}
+
+bool Printer::Start() {
+ if (initialized_)
+ return true;
+
+ // TODO(maksymb): Add switch for command line to control interface name.
+ net::IPAddressNumber ip = GetLocalIp("", false);
+ if (ip.empty()) {
+ LOG(ERROR) << "No local IP found. Cannot start printer.";
+ return false;
+ }
+ VLOG(1) << "Local address: " << net::IPAddressToString(ip);
+
+ // Starting DNS-SD server.
+ initialized_ = dns_server_.Start(
+ ServiceParameters(kServiceType,
+ kServiceNamePrefix,
+ kServiceDomainName,
+ ip,
+ ReadHttpPortFromCommandLine()),
+ ReadTtlFromCommandLine(),
+ CreateTxt());
+
+ return initialized_;
+}
+
+void Printer::Stop() {
+ if (!initialized_)
+ return;
+
+ dns_server_.Shutdown();
+}
+
+std::vector<std::string> Printer::CreateTxt() const {
+ std::vector<std::string> txt;
+
+ txt.push_back("txtvers=1");
+ txt.push_back("ty=Google Cloud Ready Printer GCP2.0 Prototype");
+ txt.push_back("note=Virtual printer");
+ txt.push_back("url=https://www.google.com/cloudprint");
+ txt.push_back("type=printer");
+ txt.push_back("id=");
+ txt.push_back("cs=offline");
+
+ return txt;
+}
+