Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit077e060

Browse files
committed
Add call ids
1 parent2df5bd6 commit077e060

File tree

2 files changed

+114
-30
lines changed

2 files changed

+114
-30
lines changed

‎Firebase.cpp

Lines changed: 81 additions & 19 deletions
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(newFirebaseConnection(host));
3838
}
3939

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

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

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

5353
FirebaseCallFirebase::remove(const String& path) {
54-
returnFirebaseCall(host_, auth_,"DELETE", path,&http_);
54+
returnFirebaseCall("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+
intFirebaseConnection::sendRequest(constchar* 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+
StringFirebaseConnection::getString() {
82+
remaining_call_buffer_ =0;
83+
return http_.getString();
84+
}
85+
86+
boolFirebaseConnection::isOwner(int call_id) {
87+
return owning_call_id_ == call_id;
88+
}
89+
90+
voidFirebaseConnection::setOwner(int call_id) {
91+
owning_call_id_ = call_id;
92+
drainResponseBuffer();
93+
}
94+
95+
voidFirebaseConnection::drainResponseBuffer() {
96+
auto* stream = http_.getStreamPtr();
97+
Serial.println("Available");
98+
Serial.println(stream->available());
99+
100+
101+
constint 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-
constchar* 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(constchar* 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-
constchar* method,const String& path,
76-
HTTPClient* http) : FirebaseCall(host, auth, method, path,"", http) {
77-
}
128+
FirebaseCall::FirebaseCall(constchar* method,const String& path,int call_id,
129+
FirebaseConnection* connection)
130+
: FirebaseCall(method, path,"", call_id, connection) {}
78131

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

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

95157
/* FirebaseEventStream*/

‎Firebase.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@
3030
classFirebaseCall;
3131
classFirebaseEventStream;
3232

33+
classFirebaseConnection {
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+
boolisOwner(int call_id);
40+
voidsetOwner(int call_id);
41+
42+
intsendRequest(constchar* method,const String& path,const String& value);
43+
StringgetString();
44+
private:
45+
voiddrainResponseBuffer();
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
classFirebase {
3556
public:
@@ -49,21 +70,21 @@ class Firebase {
4970
// Starts a stream of events that effect object at "path".
5071
FirebaseEventStreamstream(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
classFirebaseCall {
5981
public:
60-
FirebaseCall(const String& host,const String& auth,
61-
constchar* method,const String& path,const String& value,
62-
HTTPClient* http);
63-
FirebaseCall(const String& host,const String& auth,
64-
constchar* method,const String& path,
65-
HTTPClient* http);
82+
staticconstintkStatusNotConnectionOwner = -1000;
6683

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

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

83104
private:
84-
FirebaseCall(HTTPClient* http);
105+
voidsetErrorNotOwner();
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)

[8]ページ先頭

©2009-2025 Movatter.jp