Skip to content

Commit

Permalink
FirebaseArduino: allow mixing stream and non-stream commands
Browse files Browse the repository at this point in the history
With this change non-streaming operation will stop the stream and
reuse the underlying http client.

Call FirebaseArduino::stream again in order to reuse the stream.

Fixes: #48
  • Loading branch information
proppy committed Nov 2, 2017
1 parent c73ca0e commit d62b9cb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/Firebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
const char* method, const std::string& path,
const std::string& data, FirebaseHttpClient* http) : http_(http) {
std::string path_with_auth = makeFirebaseURL(path, auth);
if ((method == "STREAM") && (path == http->getStreamingPath())){
// already streaming requested path.
return;
}
if (http_->isStreaming()) {
// closing streaming connection.
http_->setReuseConnection(false);
http_->end();
}
http_->setReuseConnection(true);
http_->begin(host, path_with_auth);

Expand Down Expand Up @@ -130,11 +139,14 @@ FirebaseCall::FirebaseCall(const std::string& host, const std::string& auth,
// if not streaming.
if (!followRedirect) {
response_ = http_->getString();
http_->setStreaming("");
} else {
http_->setStreaming(path);
}
}

FirebaseCall::~FirebaseCall() {
if (http_) {
if (http_ && !http_->isStreaming()) {
http_->end();
}
}
Expand Down Expand Up @@ -189,6 +201,9 @@ FirebaseStream::FirebaseStream(const std::string& host, const std::string& auth,
}

bool FirebaseStream::available() {
if (http_->getStreamPtr() == nullptr) {
return false;
}
return http_->getStreamPtr()->available();
}

Expand Down
3 changes: 3 additions & 0 deletions src/FirebaseArduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ void FirebaseArduino::stream(const String& path) {
}

bool FirebaseArduino::available() {
if (http_->getStreamPtr() == nullptr) {
return false;
}
return http_->getStreamPtr()->available();
}

Expand Down
3 changes: 0 additions & 3 deletions src/FirebaseArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ class FirebaseArduino {
* You should check success() after calling.
* This changes the state of this object. Once this is called you may start
* monitoring available() and calling readEvent() to get new events.
* WARNING: Currently you cannot make another call while the stream is
* running, otherwise you will crash due to memory issues. See:
* https://github.com/googlesamples/firebase-arduino/issues/48
* \param path The path inside of your db to the node you wish to monitor.
*/
void stream(const String& path);
Expand Down
11 changes: 11 additions & 0 deletions src/FirebaseHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ class FirebaseHttpClient {

virtual std::string errorToString(int error_code) = 0;

bool isStreaming() const {
return _streaming != "";
}
std::string getStreamingPath() const {
return _streaming;
}
void setStreaming(const std::string& path) {
_streaming = path;
}
protected:
std::string _streaming = "";

static const uint16_t kFirebasePort = 443;
};

Expand Down

0 comments on commit d62b9cb

Please sign in to comment.