Skip to content

Commit 6f65250

Browse files
committed
Revert "Add call ids"
This reverts commit 077e060.
1 parent 077e060 commit 6f65250

File tree

2 files changed

+30
-114
lines changed

2 files changed

+30
-114
lines changed

Firebase.cpp

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

3434
} // namespace
3535

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

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

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

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

5353
FirebaseCall Firebase::remove(const String& path) {
54-
return FirebaseCall("DELETE", path, ++current_call_id_, connection_.get());
54+
return FirebaseCall(host_, auth_, "DELETE", path, &http_);
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_);
11459
}
11560

11661
/* FirebaseCall */
11762

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);
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());
12369
if (isError()) {
124-
error_message_ = String(method) + " " + path + ": " + HTTPClient::errorToString(status_);
70+
error_message_ = String(method) + " " + url + ": " + HTTPClient::errorToString(status_);
12571
}
12672
}
12773

128-
FirebaseCall::FirebaseCall(const char* method, const String& path, int call_id,
129-
FirebaseConnection* connection)
130-
: FirebaseCall(method, path, "", call_id, connection) {}
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+
}
13178

13279
bool FirebaseCall::isOk() const {
13380
return status_ == HTTP_CODE_OK;
@@ -142,16 +89,7 @@ String FirebaseCall::errorMessage() const {
14289
}
14390

14491
String FirebaseCall::rawResponse() {
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";
92+
return http_->getString();
15593
}
15694

15795
/* FirebaseEventStream */

Firebase.h

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@
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-
5433
// Primary client to the Firebase backend.
5534
class Firebase {
5635
public:
@@ -70,21 +49,21 @@ class Firebase {
7049
// Starts a stream of events that effect object at "path".
7150
FirebaseEventStream stream(const String& path);
7251

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

8058
class FirebaseCall {
8159
public:
82-
static const int kStatusNotConnectionOwner = -1000;
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);
8366

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);
8867

8968
// True if there was an error completing call.
9069
bool isError() const;
@@ -102,11 +81,10 @@ class FirebaseCall {
10281
}
10382

10483
private:
105-
void setErrorNotOwner();
84+
FirebaseCall(HTTPClient* http);
10685

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

109-
int call_id_;
11088
int status_;
11189
String error_message_;
11290
};

0 commit comments

Comments
 (0)