Skip to content

Commit 077e060

Browse files
committed
Add call ids
1 parent 2df5bd6 commit 077e060

File tree

2 files changed

+114
-30
lines changed

2 files changed

+114
-30
lines changed

Diff for: Firebase.cpp

+81-19
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,101 @@ String makeUrl(const String& path, const String& auth) {
3333

3434
} // namespace
3535

36-
Firebase::Firebase(const String& host) : host_(host) {
37-
http_.setReuse(true);
36+
Firebase::Firebase(const String& host) {
37+
connection_.reset(new FirebaseConnection(host));
3838
}
3939

4040
Firebase& Firebase::auth(const String& auth) {
41-
auth_ = auth;
41+
connection_->auth(auth);
4242
return *this;
4343
}
4444

4545
FirebaseCall Firebase::get(const String& path) {
46-
return FirebaseCall(host_, auth_, "GET", path, &http_);
46+
return FirebaseCall("GET", path, ++current_call_id_, connection_.get());
4747
}
4848

4949
FirebaseCall Firebase::push(const String& path, const String& value) {
50-
return FirebaseCall(host_, auth_, "POST", path, value, &http_);
50+
return FirebaseCall("POST", path, value, ++current_call_id_, connection_.get());
5151
}
5252

5353
FirebaseCall Firebase::remove(const String& path) {
54-
return FirebaseCall(host_, auth_, "DELETE", path, &http_);
54+
return FirebaseCall("DELETE", path, ++current_call_id_, connection_.get());
5555
}
56-
56+
/*
5757
FirebaseEventStream Firebase::stream(const String& path) {
5858
return FirebaseEventStream(host_, auth_, path);
59+
}*/
60+
61+
/* FirebaseConnection */
62+
FirebaseConnection::FirebaseConnection(const String& host) : host_(host) {
63+
http_.setReuse(true);
64+
}
65+
66+
FirebaseConnection& FirebaseConnection::auth(const String& auth) {
67+
auth_ = auth;
68+
return *this;
69+
}
70+
71+
int FirebaseConnection::sendRequest(const char* method, const String& path, const String& value) {
72+
const String url = makeUrl(path, auth_);
73+
http_.begin(host_.c_str(), kFirebasePort, url.c_str(), true, kFirebaseFingerprint);
74+
int status = http_.sendRequest(method, (uint8_t*)value.c_str(), value.length());
75+
if (status == HTTP_CODE_OK) {
76+
remaining_call_buffer_ = http_.getSize();
77+
}
78+
return status;
79+
}
80+
81+
String FirebaseConnection::getString() {
82+
remaining_call_buffer_ = 0;
83+
return http_.getString();
84+
}
85+
86+
bool FirebaseConnection::isOwner(int call_id) {
87+
return owning_call_id_ == call_id;
88+
}
89+
90+
void FirebaseConnection::setOwner(int call_id) {
91+
owning_call_id_ = call_id;
92+
drainResponseBuffer();
93+
}
94+
95+
void FirebaseConnection::drainResponseBuffer() {
96+
auto* stream = http_.getStreamPtr();
97+
Serial.println("Available ");
98+
Serial.println(stream->available());
99+
100+
101+
const int buffer_size = 128;
102+
uint8_t buffer[buffer_size];
103+
int read = 0;
104+
int to_read = (buffer_size < remaining_call_buffer_) ? buffer_size : remaining_call_buffer_;
105+
//TODO(edcoyne) This only reads what is available. Is this sufficient or should we wait?
106+
while (remaining_call_buffer_ > 0 && (read = stream->read(buffer, to_read) > 0)) {
107+
Serial.println("Draining ");
108+
Serial.println(remaining_call_buffer_);
109+
remaining_call_buffer_ -= read;
110+
to_read = (buffer_size < remaining_call_buffer_) ? buffer_size : remaining_call_buffer_;
111+
}
112+
Serial.println("Done draining ");
113+
Serial.println(remaining_call_buffer_);
59114
}
60115

61116
/* FirebaseCall */
62117

63-
FirebaseCall::FirebaseCall(const String& host, const String& auth,
64-
const char* method, const String& path, const String& value,
65-
HTTPClient* http) : http_(http) {
66-
const String url = makeUrl(path, auth);
67-
http_->begin(host.c_str(), kFirebasePort, url.c_str(), true, kFirebaseFingerprint);
68-
status_ = http_->sendRequest(method, (uint8_t*)value.c_str(), value.length());
118+
FirebaseCall::FirebaseCall(const char* method, const String& path, const String& value,
119+
int call_id, FirebaseConnection* connection)
120+
: connection_(connection), call_id_(call_id) {
121+
connection_->setOwner(call_id);
122+
status_ = connection_->sendRequest(method, path, value);
69123
if (isError()) {
70-
error_message_ = String(method) + " " + url + ": " + HTTPClient::errorToString(status_);
124+
error_message_ = String(method) + " " + path + ": " + HTTPClient::errorToString(status_);
71125
}
72126
}
73127

74-
FirebaseCall::FirebaseCall(const String& host, const String& auth,
75-
const char* method, const String& path,
76-
HTTPClient* http) : FirebaseCall(host, auth, method, path, "", http) {
77-
}
128+
FirebaseCall::FirebaseCall(const char* method, const String& path, int call_id,
129+
FirebaseConnection* connection)
130+
: FirebaseCall(method, path, "", call_id, connection) {}
78131

79132
bool FirebaseCall::isOk() const {
80133
return status_ == HTTP_CODE_OK;
@@ -89,7 +142,16 @@ String FirebaseCall::errorMessage() const {
89142
}
90143

91144
String FirebaseCall::rawResponse() {
92-
return http_->getString();
145+
if (!connection_->isOwner(call_id_)) {
146+
setErrorNotOwner();
147+
return "";
148+
}
149+
return connection_->getString();
150+
}
151+
152+
void FirebaseCall::setErrorNotOwner() {
153+
status_ = kStatusNotConnectionOwner;
154+
error_message_ = "Connection no longer owns connection";
93155
}
94156

95157
/* FirebaseEventStream */

Diff for: Firebase.h

+33-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@
3030
class FirebaseCall;
3131
class FirebaseEventStream;
3232

33+
class FirebaseConnection {
34+
public:
35+
FirebaseConnection(const String& host);
36+
FirebaseConnection& auth(const String& auth);
37+
38+
// Returns true if call with call_id owns the connection.
39+
bool isOwner(int call_id);
40+
void setOwner(int call_id);
41+
42+
int sendRequest(const char* method, const String& path, const String& value);
43+
String getString();
44+
private:
45+
void drainResponseBuffer();
46+
47+
int owning_call_id_ = 0;
48+
int remaining_call_buffer_ = 0;
49+
HTTPClient http_;
50+
String host_;
51+
String auth_;
52+
};
53+
3354
// Primary client to the Firebase backend.
3455
class Firebase {
3556
public:
@@ -49,21 +70,21 @@ class Firebase {
4970
// Starts a stream of events that effect object at "path".
5071
FirebaseEventStream stream(const String& path);
5172

73+
Firebase(const Firebase&) = delete;
74+
Firebase& operator=(const Firebase&) = delete;
5275
private:
53-
HTTPClient http_;
54-
String host_;
55-
String auth_;
76+
int current_call_id_ = 0;
77+
std::unique_ptr<FirebaseConnection> connection_;
5678
};
5779

5880
class FirebaseCall {
5981
public:
60-
FirebaseCall(const String& host, const String& auth,
61-
const char* method, const String& path, const String& value,
62-
HTTPClient* http);
63-
FirebaseCall(const String& host, const String& auth,
64-
const char* method, const String& path,
65-
HTTPClient* http);
82+
static const int kStatusNotConnectionOwner = -1000;
6683

84+
FirebaseCall(const char* method, const String& path, const String& value, int call_id,
85+
FirebaseConnection* connection);
86+
FirebaseCall(const char* method, const String& path, int call_id,
87+
FirebaseConnection* connection);
6788

6889
// True if there was an error completing call.
6990
bool isError() const;
@@ -81,10 +102,11 @@ class FirebaseCall {
81102
}
82103

83104
private:
84-
FirebaseCall(HTTPClient* http);
105+
void setErrorNotOwner();
85106

86-
HTTPClient* http_;
107+
FirebaseConnection* connection_;
87108

109+
int call_id_;
88110
int status_;
89111
String error_message_;
90112
};

0 commit comments

Comments
 (0)