@@ -33,48 +33,101 @@ String makeUrl(const String& path, const String& auth) {
33
33
34
34
} // namespace
35
35
36
- Firebase::Firebase (const String& host) : host_(host) {
37
- http_. setReuse ( true );
36
+ Firebase::Firebase (const String& host) {
37
+ connection_. reset ( new FirebaseConnection (host) );
38
38
}
39
39
40
40
Firebase& Firebase::auth (const String& auth) {
41
- auth_ = auth;
41
+ connection_-> auth (auth) ;
42
42
return *this ;
43
43
}
44
44
45
45
FirebaseCall Firebase::get (const String& path) {
46
- return FirebaseCall (host_, auth_, " GET" , path, &http_ );
46
+ return FirebaseCall (" GET" , path, ++current_call_id_, connection_. get () );
47
47
}
48
48
49
49
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 () );
51
51
}
52
52
53
53
FirebaseCall Firebase::remove (const String& path) {
54
- return FirebaseCall (host_, auth_, " DELETE" , path, &http_ );
54
+ return FirebaseCall (" DELETE" , path, ++current_call_id_, connection_. get () );
55
55
}
56
-
56
+ /*
57
57
FirebaseEventStream Firebase::stream(const String& path) {
58
58
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_);
59
114
}
60
115
61
116
/* FirebaseCall */
62
117
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);
69
123
if (isError ()) {
70
- error_message_ = String (method) + " " + url + " : " + HTTPClient::errorToString (status_);
124
+ error_message_ = String (method) + " " + path + " : " + HTTPClient::errorToString (status_);
71
125
}
72
126
}
73
127
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) {}
78
131
79
132
bool FirebaseCall::isOk () const {
80
133
return status_ == HTTP_CODE_OK;
@@ -89,7 +142,16 @@ String FirebaseCall::errorMessage() const {
89
142
}
90
143
91
144
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" ;
93
155
}
94
156
95
157
/* FirebaseEventStream */
0 commit comments