Refactor StandardStreamsCallbackInterface
The names OnStdout/OnStderr/OnStream are not clear with regards to the
direction of the data. Renaming to use send/receive makes it less
confusing
Test: NA
Bug: NA
Flag: EXEMPT (tool)
Change-Id: I2613d8235852cd393a6bd2b5dc44ea412138949e
diff --git a/bugreport_test.cpp b/bugreport_test.cpp
index a6be203..f98bb17 100644
--- a/bugreport_test.cpp
+++ b/bugreport_test.cpp
@@ -72,10 +72,10 @@
}
virtual Result Perform(const ArgumentTuple& args) {
if (type_ == kStreamStdout) {
- ::std::tr1::get<0>(args)->OnStdout(output_.c_str(), output_.size());
+ ::std::tr1::get<0>(args)->OnStdoutReceived(output_.c_str(), output_.size());
}
if (type_ == kStreamStderr) {
- ::std::tr1::get<0>(args)->OnStderr(output_.c_str(), output_.size());
+ ::std::tr1::get<0>(args)->OnStderrReceived(output_.c_str(), output_.size());
}
}
@@ -84,13 +84,13 @@
std::string output_;
};
-// Matcher used to emulated StandardStreamsCallbackInterface.OnStdout(buffer,
+// Matcher used to emulated StandardStreamsCallbackInterface.OnStdoutReceived(buffer,
// length)
Action<OnStandardStreamsCallbackFunction> WriteOnStdout(const std::string& output) {
return MakeAction(new OnStandardStreamsCallbackAction(kStreamStdout, output));
}
-// Matcher used to emulated StandardStreamsCallbackInterface.OnStderr(buffer,
+// Matcher used to emulated StandardStreamsCallbackInterface.OnStderrReceived(buffer,
// length)
Action<OnStandardStreamsCallbackFunction> WriteOnStderr(const std::string& output) {
return MakeAction(new OnStandardStreamsCallbackAction(kStreamStderr, output));
diff --git a/client/bugreport.cpp b/client/bugreport.cpp
index 23ecb55..15b13ca 100644
--- a/client/bugreport.cpp
+++ b/client/bugreport.cpp
@@ -54,7 +54,7 @@
SetLineMessage("generating");
}
- bool OnStdout(const char* buffer, size_t length) {
+ bool OnStdoutReceived(const char* buffer, size_t length) override {
for (size_t i = 0; i < length; i++) {
char c = buffer[i];
if (c == '\n') {
@@ -67,8 +67,8 @@
return true;
}
- bool OnStderr(const char* buffer, size_t length) {
- return OnStream(nullptr, stderr, buffer, length, false);
+ bool OnStderrReceived(const char* buffer, size_t length) override {
+ return SendTo(nullptr, stderr, buffer, length, false);
}
int Done(int unused_) {
diff --git a/client/commandline.cpp b/client/commandline.cpp
index 79d62cd..4661400 100644
--- a/client/commandline.cpp
+++ b/client/commandline.cpp
@@ -302,15 +302,15 @@
}
while (protocol->Read()) {
if (protocol->id() == ShellProtocol::kIdStdout) {
- if (!callback->OnStdout(protocol->data(), protocol->data_length())) {
- exit_code = SIGPIPE + 128;
- break;
- }
+ if (!callback->OnStdoutReceived(protocol->data(), protocol->data_length())) {
+ exit_code = SIGPIPE + 128;
+ break;
+ }
} else if (protocol->id() == ShellProtocol::kIdStderr) {
- if (!callback->OnStderr(protocol->data(), protocol->data_length())) {
- exit_code = SIGPIPE + 128;
- break;
- }
+ if (!callback->OnStderrReceived(protocol->data(), protocol->data_length())) {
+ exit_code = SIGPIPE + 128;
+ break;
+ }
} else if (protocol->id() == ShellProtocol::kIdExit) {
// data() returns a char* which doesn't have defined signedness.
// Cast to uint8_t to prevent 255 from being sign extended to INT_MIN,
@@ -338,8 +338,8 @@
if (length <= 0) {
break;
}
- if (!callback->OnStdout(buffer_ptr, length)) {
- break;
+ if (!callback->OnStdoutReceived(buffer_ptr, length)) {
+ break;
}
}
}
@@ -1377,13 +1377,13 @@
public:
AdbServerStateStreamsCallback() : DefaultStandardStreamsCallback(nullptr, nullptr) {}
- bool OnStdout(const char* buffer, size_t length) override {
- return OnStream(&output_, nullptr, buffer, length, false);
+ bool OnStdoutReceived(const char* buffer, size_t length) override {
+ return SendTo(&output_, nullptr, buffer, length, false);
}
int Done(int status) {
if (output_.size() < 4) {
- return OnStream(nullptr, stdout, output_.data(), output_.length(), false);
+ return SendTo(nullptr, stdout, output_.data(), output_.length(), false);
}
// Skip the 4-hex prefix
@@ -1395,7 +1395,7 @@
std::string string_proto;
google::protobuf::TextFormat::PrintToString(binary_proto, &string_proto);
- return OnStream(nullptr, stdout, string_proto.data(), string_proto.length(), false);
+ return SendTo(nullptr, stdout, string_proto.data(), string_proto.length(), false);
}
private:
diff --git a/client/commandline.h b/client/commandline.h
index 912c0c4..5b3e97b 100644
--- a/client/commandline.h
+++ b/client/commandline.h
@@ -38,11 +38,11 @@
}
// Handles the stdout output from devices supporting the Shell protocol.
// Returns true on success and false on failure.
- virtual bool OnStdout(const char* buffer, size_t length) = 0;
+ virtual bool OnStdoutReceived(const char* buffer, size_t length) = 0;
// Handles the stderr output from devices supporting the Shell protocol.
// Returns true on success and false on failure.
- virtual bool OnStderr(const char* buffer, size_t length) = 0;
+ virtual bool OnStderrReceived(const char* buffer, size_t length) = 0;
// Indicates the communication is finished and returns the appropriate error
// code.
@@ -52,8 +52,8 @@
virtual int Done(int status) = 0;
protected:
- static bool OnStream(std::string* string, FILE* stream, const char* buffer, size_t length,
- bool returnErrors) {
+ static bool SendTo(std::string* string, FILE* stream, const char* buffer, size_t length,
+ bool returnErrors) {
if (string != nullptr) {
string->append(buffer, length);
return true;
@@ -72,8 +72,8 @@
// stream or to a string passed to the constructor.
class DefaultStandardStreamsCallback : public StandardStreamsCallbackInterface {
public:
- // If |stdout_str| is non-null, OnStdout will append to it.
- // If |stderr_str| is non-null, OnStderr will append to it.
+ // If |stdout_str| is non-null, OnStdoutReceived will append to it.
+ // If |stderr_str| is non-null, OnStderrReceived will append to it.
DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str)
: stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(false) {
}
@@ -83,18 +83,23 @@
}
// Called when receiving from the device standard input stream
- bool OnStdout(const char* buffer, size_t length) {
- return OnStream(stdout_str_, stdout, buffer, length, returnErrors_);
+ bool OnStdoutReceived(const char* buffer, size_t length) override {
+ return SendToOut(buffer, length);
}
// Called when receiving from the device error input stream
- bool OnStderr(const char* buffer, size_t length) {
- return OnStream(stderr_str_, stderr, buffer, length, returnErrors_);
+ bool OnStderrReceived(const char* buffer, size_t length) override {
+ return SendToErr(buffer, length);
}
// Send to local standard input stream (or stdout_str if one was provided).
- bool OnStreamOut(const char* buffer, size_t length) {
- return OnStream(stdout_str_, stdout, buffer, length, returnErrors_);
+ bool SendToOut(const char* buffer, size_t length) {
+ return SendTo(stdout_str_, stdout, buffer, length, returnErrors_);
+ }
+
+ // Send to local standard error stream (or stderr_str if one was provided).
+ bool SendToErr(const char* buffer, size_t length) {
+ return SendTo(stderr_str_, stderr, buffer, length, returnErrors_);
}
int Done(int status) {
@@ -116,9 +121,9 @@
class SilentStandardStreamsCallbackInterface : public StandardStreamsCallbackInterface {
public:
SilentStandardStreamsCallbackInterface() = default;
- bool OnStdout(const char*, size_t) override final { return true; }
- bool OnStderr(const char*, size_t) override final { return true; }
- int Done(int status) override final { return status; }
+ bool OnStdoutReceived(const char*, size_t) final { return true; }
+ bool OnStderrReceived(const char*, size_t) final { return true; }
+ int Done(int status) final { return status; }
};
// Singleton.
@@ -132,7 +137,7 @@
explicit ProtoBinaryToText(const std::string& m, std::string* std_out = nullptr,
std::string* std_err = nullptr)
: DefaultStandardStreamsCallback(std_out, std_err), message(m) {}
- bool OnStdout(const char* b, const size_t l) override {
+ bool OnStdoutReceived(const char* b, size_t l) override {
constexpr size_t kHeader_size = 4;
// Add the incoming bytes to our internal buffer.
@@ -162,12 +167,12 @@
// Drop bytes that we just consumed.
buffer_.erase(buffer_.begin(), buffer_.begin() + kHeader_size + expected_size);
- OnStreamOut(message.data(), message.length());
- OnStreamOut(string_proto.data(), string_proto.length());
+ SendToOut(message.data(), message.length());
+ SendToOut(string_proto.data(), string_proto.length());
// Recurse if there is still data in our buffer (there may be more messages).
if (!buffer_.empty()) {
- OnStdout("", 0);
+ OnStdoutReceived("", 0);
}
return true;
diff --git a/client/commandline_test.cpp b/client/commandline_test.cpp
index 33afbe3..249a2f3 100644
--- a/client/commandline_test.cpp
+++ b/client/commandline_test.cpp
@@ -47,7 +47,7 @@
std::string err;
std::string message = "Testing123";
auto converter = ProtoBinaryToText<adb::proto::AppProcesses>(message, &out, &err);
- converter.OnStdout(hex4_proto.data(), hex4_proto.size());
+ converter.OnStdoutReceived(hex4_proto.data(), hex4_proto.size());
ASSERT_FALSE(out.empty());
ASSERT_TRUE(out.contains(message));
@@ -70,7 +70,7 @@
std::string message = "Testing123";
auto converter = ProtoBinaryToText<adb::proto::AppProcesses>(message, &out, &err);
for (auto i = 0u; i < hex4_proto.size(); i++) {
- converter.OnStdout(hex4_proto.data() + i, 1);
+ converter.OnStdoutReceived(hex4_proto.data() + i, 1);
}
ASSERT_FALSE(out.empty());
@@ -92,7 +92,7 @@
std::string err;
std::string message = "Testing 123";
auto converter = ProtoBinaryToText<adb::proto::AppProcesses>(message, &out, &err);
- converter.OnStdout(hex4_proto.data(), hex4_proto.size() / 2);
+ converter.OnStdoutReceived(hex4_proto.data(), hex4_proto.size() / 2);
ASSERT_TRUE(out.empty());
}
@@ -122,7 +122,7 @@
std::string err;
std::string message = "Testing123";
auto converter = ProtoBinaryToText<adb::proto::AppProcesses>(message, &out, &err);
- converter.OnStdout(two_messages.data(), two_messages.size());
+ converter.OnStdoutReceived(two_messages.data(), two_messages.size());
ASSERT_FALSE(out.empty());
ASSERT_EQ(2u, count_occurrences(out, message));
@@ -156,7 +156,7 @@
std::string err;
std::string message = "Testing123";
auto converter = ProtoBinaryToText<adb::proto::AppProcesses>(message, &out, &err);
- converter.OnStdout(two_messages.data(), two_messages.size());
+ converter.OnStdoutReceived(two_messages.data(), two_messages.size());
ASSERT_FALSE(out.empty());
ASSERT_EQ(1u, count_occurrences(out, message));
@@ -166,7 +166,7 @@
// Send the remainder of second proto
out.clear();
std::string remaining = hex4_proto2.substr(hex4_proto2.size() / 2, hex4_proto2.size());
- converter.OnStdout(remaining.data(), remaining.size());
+ converter.OnStdoutReceived(remaining.data(), remaining.size());
ASSERT_FALSE(out.empty());
ASSERT_EQ(1u, count_occurrences(out, message));
ASSERT_EQ(0u, count_occurrences(out, process_name1));