Skip to content

Commit

Permalink
Merge pull request #330 from kotl/master
Browse files Browse the repository at this point in the history
Add bi-directional (stream + get/set) support and refactoring of the internal classes
  • Loading branch information
proppy committed Jun 15, 2018
2 parents 96eab42 + a7bf160 commit de3874e
Show file tree
Hide file tree
Showing 24 changed files with 272 additions and 499 deletions.
8 changes: 4 additions & 4 deletions contrib/src/modem/command.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MODEM_COMMAND_H
#define MODEM_COMMAND_H

#include "Firebase.h"
#include "FirebaseArduino.h"
#include "modem/output-stream.h"
#include "modem/input-stream.h"

Expand All @@ -10,19 +10,19 @@ namespace modem {

class Command {
public:
Command(Firebase* fbase) : fbase_(fbase) {}
Command(FirebaseArduino* fbase) : fbase_(fbase) {}

// Execute command, reading any additional data needed from stream.
// Return false if execution failed.
virtual bool execute(const String& command,
InputStream* in, OutputStream* out) = 0;
protected:
Firebase& fbase() {
FirebaseArduino& fbase() {
return *fbase_;
}

private:
Firebase* fbase_;
FirebaseArduino* fbase_;
};

} // modem
Expand Down
2 changes: 1 addition & 1 deletion contrib/src/modem/db/DatabaseProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void DatabaseProtocol::Execute(const String& command_name, InputStream* in,
}

std::unique_ptr<Command> DatabaseProtocol::CreateCommand(const String& text,
Firebase* fbase) {
FirebaseArduino* fbase) {
std::unique_ptr<Command> command;
if (text == "GET") {
command.reset(new GetCommand(fbase));
Expand Down
4 changes: 2 additions & 2 deletions contrib/src/modem/db/DatabaseProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class DatabaseProtocol : public SerialProtocol {
const std::vector<String>& commands() const override;
void Execute(const String& command, InputStream* in, OutputStream* out) override;
private:
std::unique_ptr<Command> CreateCommand(const String& text, Firebase* fbase);
std::unique_ptr<Command> CreateCommand(const String& text, FirebaseArduino* fbase);

std::unique_ptr<Firebase> fbase_;
std::unique_ptr<FirebaseArduino> fbase_;
};


Expand Down
5 changes: 3 additions & 2 deletions contrib/src/modem/db/begin-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ bool BeginCommand::execute(const String& command,
return false;
}

new_firebase_.reset(new Firebase(host.c_str(), auth.c_str()));
new_firebase_.reset(new FirebaseArduino());
new_firebase_.get()->begin(host.c_str(), auth.c_str());

out->println("+OK");
return true;
}

std::unique_ptr<Firebase> BeginCommand::firebase() {
std::unique_ptr<FirebaseArduino> BeginCommand::firebase() {
return std::move(new_firebase_);
}

Expand Down
16 changes: 8 additions & 8 deletions contrib/src/modem/db/commands.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MODEM_DB_COMMANDS_H
#define MODEM_DB_COMMANDS_H

#include "Firebase.h"
#include "FirebaseArduino.h"
#include "modem/command.h"
#include "modem/output-stream.h"
#include "modem/input-stream.h"
Expand All @@ -11,28 +11,28 @@ namespace modem {

class GetCommand : public Command {
public:
GetCommand(Firebase* fbase) : Command(fbase) {}
GetCommand(FirebaseArduino* fbase) : Command(fbase) {}

bool execute(const String& command, InputStream* in, OutputStream* out);
};

class SetCommand : public Command {
public:
SetCommand(Firebase* fbase) : Command(fbase) {}
SetCommand(FirebaseArduino* fbase) : Command(fbase) {}

bool execute(const String& command, InputStream* in, OutputStream* out);
};

class RemoveCommand : public Command {
public:
RemoveCommand(Firebase* fbase) : Command(fbase) {}
RemoveCommand(FirebaseArduino* fbase) : Command(fbase) {}

bool execute(const String& command, InputStream* in, OutputStream* out);
};

class PushCommand : public Command {
public:
PushCommand(Firebase* fbase) : Command(fbase) {}
PushCommand(FirebaseArduino* fbase) : Command(fbase) {}

bool execute(const String& command, InputStream* in, OutputStream* out);
};
Expand All @@ -44,15 +44,15 @@ class BeginCommand : public Command {
bool execute(const String& command, InputStream* in, OutputStream* out);

// This can only be called once.
std::unique_ptr<Firebase> firebase();
std::unique_ptr<FirebaseArduino> firebase();

private:
std::unique_ptr<Firebase> new_firebase_;
std::unique_ptr<FirebaseArduino> new_firebase_;
};

class StreamCommand : public Command {
public:
StreamCommand(Firebase* fbase) : Command(fbase) {}
StreamCommand(FirebaseArduino* fbase) : Command(fbase) {}

bool execute(const String& command, InputStream* in, OutputStream* out);
};
Expand Down
10 changes: 4 additions & 6 deletions contrib/src/modem/db/get-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ bool GetCommand::execute(const String& command,
return false;
}

std::string path = in->readLine().c_str();
std::unique_ptr<FirebaseGet> get(fbase().getPtr(path));

if (get->error()) {
String path = in->readLine();
String value = fbase().getString(path);
if (fbase().error().length() != 0) {
out->print("-FAIL ");
out->println(get->error().message().c_str());
out->println(fbase().error().c_str());
return false;
}

String value(get->response().c_str());
// TODO implement json parsing to pull and process value.
out->print("+");
out->println(value);
Expand Down
11 changes: 5 additions & 6 deletions contrib/src/modem/db/push-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ bool PushCommand::execute(const String& command,
return false;
}

std::string path(in->readStringUntil(' ').c_str());
std::string data(in->readLine().c_str());
String path = in->readStringUntil(' ');
String data = in->readLine();

std::unique_ptr<FirebasePush> push(
fbase().pushPtr(path, EncodeForJson(data)));
fbase().pushString(path, data);

if (push->error()) {
if (fbase().error().length() != 0) {
out->print("-FAIL ");
out->println(push->error().message().c_str());
out->println(fbase().error().c_str());
return false;
} else {
out->println("+OK");
Expand Down
6 changes: 3 additions & 3 deletions contrib/src/modem/db/remove-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ bool RemoveCommand::execute(const String& command,
}

String path = in->readLine();
std::unique_ptr<FirebaseRemove> get(fbase().removePtr(path.c_str()));
fbase().remove(path);

if (get->error()) {
if (fbase().error().length() != 0) {
out->print("-FAIL ");
out->println(get->error().message().c_str());
out->println(fbase().error().c_str());
return false;
}

Expand Down
11 changes: 5 additions & 6 deletions contrib/src/modem/db/set-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ bool SetCommand::execute(const String& command,
return false;
}

std::string path(in->readStringUntil(' ').c_str());
std::string data(in->readLine().c_str());
String path = in->readStringUntil(' ');
String data = in->readLine();

std::unique_ptr<FirebaseSet> set(fbase().setPtr(path,
EncodeForJson(data)));
fbase().setString(path, data);

if (set->error()) {
if (fbase().error().length() != 0) {
out->print("-FAIL ");
out->println(set->error().message().c_str());
out->println(fbase().error().c_str());
return false;
} else {
out->println("+OK");
Expand Down
21 changes: 9 additions & 12 deletions contrib/src/modem/db/stream-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ bool StreamCommand::execute(const String& command,
return false;
}

std::string path = in->readLine().c_str();
std::unique_ptr<FirebaseStream> stream(fbase().streamPtr(path));
String path = in->readLine().c_str();
fbase().stream(path);

if (stream->error()) {
if (fbase().error().length() != 0) {
out->print("-FAIL ");
out->println(stream->error().message().c_str());
out->println(fbase().error().c_str());
return false;
}

bool running = true;
DynamicJsonBuffer buffer;
while(running) {
if (stream->available()) {
std::string json;
FirebaseStream::Event event = stream->read(json);
if (fbase().available()) {
FirebaseObject event = fbase().readEvent();
out->print("+");
out->print(FirebaseStream::EventToName(event).c_str());
out->print(event.getString("type").c_str());
out->print(" ");
const auto& object = buffer.parseObject(json.c_str());
String data = object["data"].asString();
out->println(object["path"].asString());
String data = event.getString("data");
out->println(event.getString("path"));
out->println(data.length());
out->println(data);
} else if (in->available()) {
Expand Down
48 changes: 11 additions & 37 deletions contrib/test/mock-firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,22 @@

#include <memory>
#include "gtest/gtest.h"
#include "Firebase.h"
#include "FirebaseArduino.h"

namespace firebase {
namespace modem {

class MockFirebase : public Firebase {
class MockFirebase : public FirebaseArduino {
public:
MOCK_METHOD1(getPtr, std::unique_ptr<FirebaseGet>(const std::string&));
MOCK_METHOD2(setPtr, std::unique_ptr<FirebaseSet>(const std::string&, const std::string&));
MOCK_METHOD2(pushPtr, std::unique_ptr<FirebasePush>(const std::string&, const std::string&));
MOCK_METHOD1(removePtr, std::unique_ptr<FirebaseRemove>(const std::string&));
MOCK_METHOD1(streamPtr, std::unique_ptr<FirebaseStream>(const std::string&));
};

class MockFirebaseGet : public FirebaseGet {
public:
MOCK_CONST_METHOD0(response, const std::string&());
MOCK_CONST_METHOD0(error, const FirebaseError&());
};

class MockFirebaseSet : public FirebaseSet {
public:
MOCK_CONST_METHOD0(json, const std::string&());
MOCK_CONST_METHOD0(error, const FirebaseError&());
};

class MockFirebasePush : public FirebasePush {
public:
MOCK_CONST_METHOD0(name, const std::string&());
MOCK_CONST_METHOD0(error, const FirebaseError&());
};

class MockFirebaseRemove : public FirebaseRemove {
public:
MOCK_CONST_METHOD0(error, const FirebaseError&());
};

class MockFirebaseStream : public FirebaseStream {
public:
MOCK_METHOD0(available, bool());
MOCK_METHOD1(read, Event(std::string& event));
MOCK_CONST_METHOD0(error, const FirebaseError&());
MOCK_METHOD0(error, const String &());
MOCK_METHOD1(getString, String (const String& path));
MOCK_METHOD2(pushString, String (const String& path, const String& data));
MOCK_METHOD1(remove, void(const String& path));
MOCK_METHOD2(setString, void(const String& path, const String& data));
MOCK_METHOD0(available, bool ());
MOCK_METHOD0(readEvent, FirebaseObject ());
MOCK_METHOD2(begin, void (const String& host, const String& auth));
MOCK_METHOD1(stream, void (const String& path));
};

} // modem
Expand Down
22 changes: 14 additions & 8 deletions contrib/test/modem/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ CXXFLAGS += -g -Wall -Wextra -pthread -std=c++11
# All tests produced by this Makefile. Remember to add new tests you
# created to the list.
TESTS = get-command_test set-command_test remove-command_test \
push-command_test begin-command_test stream-command_test \
serial-transceiver_test
push-command_test begin-command_test \
serial-transceiver_test stream-command_test

# All Google Test headers. Usually you shouldn't change this
# definition.
Expand Down Expand Up @@ -130,6 +130,12 @@ arduino_mock_all.a : ArduinoMockAll.o

# Builds shared objects.

FirebaseArduino.o : $(FIREBASE_SRC_ROOT)/FirebaseArduino.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(FIREBASE_SRC_ROOT)/FirebaseArduino.cpp

FirebaseObject.o : $(FIREBASE_SRC_ROOT)/FirebaseObject.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(FIREBASE_SRC_ROOT)/FirebaseObject.cpp

Firebase.o : $(FIREBASE_SRC_ROOT)/Firebase.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(FIREBASE_SRC_ROOT)/Firebase.cpp

Expand All @@ -144,7 +150,7 @@ get-command.o : $(SRC_ROOT)/modem/db/get-command.cpp
get-command_test.o : $(TEST_DIR)/get-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/get-command_test.cpp

get-command_test : get-command_test.o Firebase.o FirebaseHttpClient_dummy.o get-command.o gmock_main.a \
get-command_test : get-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o get-command.o gmock_main.a \
arduino_mock_all.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Expand All @@ -155,7 +161,7 @@ set-command.o : $(SRC_ROOT)/modem/db/set-command.cpp
set-command_test.o : $(TEST_DIR)/set-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/set-command_test.cpp

set-command_test : set-command.o set-command_test.o Firebase.o FirebaseHttpClient_dummy.o gmock_main.a \
set-command_test : set-command.o set-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
arduino_mock_all.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Expand All @@ -166,7 +172,7 @@ remove-command.o : $(SRC_ROOT)/modem/db/remove-command.cpp
remove-command_test.o : $(TEST_DIR)/remove-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/remove-command_test.cpp

remove-command_test : remove-command.o remove-command_test.o Firebase.o FirebaseHttpClient_dummy.o gmock_main.a \
remove-command_test : remove-command.o remove-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
arduino_mock_all.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Expand All @@ -177,7 +183,7 @@ push-command.o : $(SRC_ROOT)/modem/db/push-command.cpp
push-command_test.o : $(TEST_DIR)/push-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/push-command_test.cpp

push-command_test : push-command.o push-command_test.o Firebase.o FirebaseHttpClient_dummy.o gmock_main.a \
push-command_test : push-command.o push-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
arduino_mock_all.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Expand All @@ -188,7 +194,7 @@ begin-command.o : $(SRC_ROOT)/modem/db/begin-command.cpp
begin-command_test.o : $(TEST_DIR)/begin-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/begin-command_test.cpp

begin-command_test : begin-command.o begin-command_test.o Firebase.o FirebaseHttpClient_dummy.o gmock_main.a \
begin-command_test : begin-command.o begin-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
arduino_mock_all.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Expand All @@ -199,7 +205,7 @@ stream-command.o : $(SRC_ROOT)/modem/db/stream-command.cpp
stream-command_test.o : $(TEST_DIR)/stream-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/stream-command_test.cpp

stream-command_test : stream-command.o stream-command_test.o Firebase.o FirebaseHttpClient_dummy.o gmock_main.a \
stream-command_test : stream-command.o stream-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
arduino_mock_all.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

Expand Down
Loading

0 comments on commit de3874e

Please sign in to comment.