summaryrefslogtreecommitdiffstats
path: root/tools/android
diff options
context:
space:
mode:
authorfelipeg@chromium.org <felipeg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-11 13:08:36 +0000
committerfelipeg@chromium.org <felipeg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-11 13:08:36 +0000
commit15fc4ba956ff3d356a57dd8d3b2a9d27ff3b6a52 (patch)
treebf5e321bb48f8a9afb3b72b6a7593a30ac567cbd /tools/android
parentc194c76f047dcb4970c67617148d81dac2436a4d (diff)
downloadchromium_src-15fc4ba956ff3d356a57dd8d3b2a9d27ff3b6a52.zip
chromium_src-15fc4ba956ff3d356a57dd8d3b2a9d27ff3b6a52.tar.gz
chromium_src-15fc4ba956ff3d356a57dd8d3b2a9d27ff3b6a52.tar.bz2
Add Command class to Forwarder2.
Second CL of many , to implement Forwarder2. The big picture CL can be seem here: https://chromiumcodereview.appspot.com/10918057/ BUG=146502 Review URL: https://chromiumcodereview.appspot.com/10912177 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@155999 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/android')
-rw-r--r--tools/android/forwarder2/command.cc90
-rw-r--r--tools/android/forwarder2/command.h45
-rw-r--r--tools/android/forwarder2/forwarder.gyp2
3 files changed, 137 insertions, 0 deletions
diff --git a/tools/android/forwarder2/command.cc b/tools/android/forwarder2/command.cc
new file mode 100644
index 0000000..9b9e401
--- /dev/null
+++ b/tools/android/forwarder2/command.cc
@@ -0,0 +1,90 @@
+// Copyright (c) 2012 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 "tools/android/forwarder2/command.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "base/logging.h"
+#include "base/string_number_conversions.h"
+#include "base/string_piece.h"
+#include "tools/android/forwarder2/socket.h"
+
+using base::StringPiece;
+
+namespace {
+
+
+// Command format:
+// <port>:<type>
+//
+// Where:
+// <port> is a 5-chars zero-padded ASCII decimal integer
+// matching the target port for the command (e.g.
+// '08080' for port 8080)
+// <type> is a 3-char zero-padded ASCII decimal integer
+// matching a command::Type value (e.g. 002 for
+// ACK).
+// The column (:) is used as a separator for easier reading.
+const int kPortStringSize = 5;
+const int kCommandTypeStringSize = 2;
+// Command string size also includes the ':' separator char.
+const int kCommandStringSize = kPortStringSize + kCommandTypeStringSize + 1;
+
+} // namespace
+
+namespace forwarder2 {
+
+bool ReadCommand(Socket* socket,
+ int* port_out,
+ command::Type* command_type_out) {
+ char command_buffer[kCommandStringSize + 1];
+ // To make logging easier.
+ command_buffer[kCommandStringSize] = '\0';
+
+ if (socket->ReadNumBytes(command_buffer, kCommandStringSize) !=
+ kCommandStringSize) {
+ LOG(ERROR) << "Not enough data received from the socket.";
+ return false;
+ }
+
+ StringPiece port_str(command_buffer, kPortStringSize);
+ if (!StringToInt(port_str, port_out)) {
+ LOG(ERROR) << "Could not parse the command port string: "
+ << port_str;
+ return false;
+ }
+
+ StringPiece command_type_str(
+ &command_buffer[kPortStringSize + 1], kCommandTypeStringSize);
+ int command_type;
+ if (!StringToInt(command_type_str, &command_type)) {
+ LOG(ERROR) << "Could not parse the command type string: "
+ << command_type_str;
+ return false;
+ }
+ *command_type_out = static_cast<command::Type>(command_type);
+ return true;
+}
+
+bool SendCommand(command::Type command, int port, Socket* socket) {
+ char buffer[kCommandStringSize + 1];
+ int len = snprintf(buffer, sizeof(buffer), "%05d:%02d", port, command);
+ CHECK_EQ(len, kCommandStringSize);
+ // Write the full command minus the leading \0 char.
+ return socket->WriteNumBytes(buffer, len) == len;
+}
+
+bool ReceivedCommand(command::Type command, Socket* socket) {
+ int port;
+ command::Type received_command;
+ if (!ReadCommand(socket, &port, &received_command))
+ return false;
+ return received_command == command;
+}
+
+} // namespace forwarder
diff --git a/tools/android/forwarder2/command.h b/tools/android/forwarder2/command.h
new file mode 100644
index 0000000..df4e7b5
--- /dev/null
+++ b/tools/android/forwarder2/command.h
@@ -0,0 +1,45 @@
+// Copyright (c) 2012 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 TOOLS_ANDROID_FORWARDER2_COMMAND_H_
+#define TOOLS_ANDROID_FORWARDER2_COMMAND_H_
+
+#include "base/basictypes.h"
+
+namespace forwarder2 {
+
+class Socket;
+
+namespace command {
+
+enum Type {
+ ACCEPT_ERROR = 0,
+ ACCEPT_SUCCESS,
+ ACK,
+ ADB_DATA_SOCKET_ERROR,
+ ADB_DATA_SOCKET_SUCCESS,
+ BIND_ERROR,
+ BIND_SUCCESS,
+ DATA_CONNECTION,
+ HOST_SERVER_ERROR,
+ HOST_SERVER_SUCCESS,
+ KILL_ALL_LISTENERS,
+ LISTEN
+};
+
+} // namespace command
+
+bool ReadCommand(Socket* socket,
+ int* port_out,
+ command::Type* command_type_out);
+
+// Helper function to read the command from the |socket| and return true if the
+// |command| is equal to the given command parameter.
+bool ReceivedCommand(command::Type command, Socket* socket);
+
+bool SendCommand(command::Type command, int port, Socket* socket);
+
+} // namespace forwarder
+
+#endif // TOOLS_ANDROID_FORWARDER2_COMMAND_H_
diff --git a/tools/android/forwarder2/forwarder.gyp b/tools/android/forwarder2/forwarder.gyp
index 6bc6b15..7b62e1f 100644
--- a/tools/android/forwarder2/forwarder.gyp
+++ b/tools/android/forwarder2/forwarder.gyp
@@ -37,6 +37,7 @@
}],
],
'sources': [
+ 'command.cc',
'device_forwarder_main.cc',
'socket.cc',
],
@@ -53,6 +54,7 @@
'../../..',
],
'sources': [
+ 'command.cc',
'host_forwarder_main.cc',
'socket.cc',
],