|
24 | 24 | ParseClient::ParseClient() { |
25 | 25 | } |
26 | 26 |
|
27 | | -void ParseClient::begin(const char *applicationId, const char *clientKey) { |
28 | | - Console.println("begin"); |
| 27 | +ParseClient::~ParseClient() { |
| 28 | + end(); |
| 29 | +} |
29 | 30 |
|
30 | | - // send the info to linux through Bridge |
31 | | - if(applicationId) { |
32 | | - Bridge.put("appId", applicationId); |
33 | | - } |
34 | | - if (clientKey) { |
35 | | - Bridge.put("clientKey", clientKey); |
36 | | - } |
| 31 | +void ParseClient::begin(const char *applicationId, const char *clientKey) { |
| 32 | + Console.println("begin"); |
| 33 | + |
| 34 | + // send the info to linux through Bridge |
| 35 | + if(applicationId) { |
| 36 | + Bridge.put("appId", applicationId); |
| 37 | + } |
| 38 | + if (clientKey) { |
| 39 | + Bridge.put("clientKey", clientKey); |
| 40 | + } |
37 | 41 | } |
38 | 42 |
|
39 | 43 | void ParseClient::setInstallationId(const char *installationId) { |
40 | | - this->installationId = installationId; |
41 | | - if (installationId) { |
42 | | - Bridge.put("installationId", installationId); |
43 | | - } else { |
44 | | - Bridge.put("installationId", ""); |
45 | | - } |
| 44 | + this->installationId = installationId; |
| 45 | + if (installationId) { |
| 46 | + Bridge.put("installationId", installationId); |
| 47 | + } else { |
| 48 | + Bridge.put("installationId", ""); |
| 49 | + } |
46 | 50 | } |
47 | 51 |
|
48 | 52 | const char* ParseClient::getInstallationId() { |
49 | | - if (!this->installationId) { |
50 | | - char *buf = new char[37]; |
| 53 | + if (!this->installationId) { |
| 54 | + char *buf = new char[37]; |
51 | 55 |
|
52 | | - requestClient.begin("parse_request"); |
53 | | - requestClient.addParameter("-i"); |
54 | | - requestClient.run(); |
| 56 | + requestClient.begin("parse_request"); |
| 57 | + requestClient.addParameter("-i"); |
| 58 | + requestClient.run(); |
55 | 59 | read(&requestClient, buf, 37); |
56 | | - this->installationId = buf; |
57 | | - } |
58 | | - return this->installationId; |
| 60 | + this->installationId = buf; |
| 61 | + } |
| 62 | + return this->installationId; |
59 | 63 | } |
60 | 64 |
|
61 | 65 | void ParseClient::setSessionToken(const char *sessionToken) { |
62 | | - if ((sessionToken != NULL) && (strlen(sessionToken) > 0 )){ |
63 | | - this->sessionToken = sessionToken; |
64 | | - Bridge.put("sessionToken", sessionToken); |
65 | | - } else { |
66 | | - Bridge.put("sessionToken", ""); |
67 | | - } |
| 66 | + if ((sessionToken != NULL) && (strlen(sessionToken) > 0 )){ |
| 67 | + this->sessionToken = sessionToken; |
| 68 | + Bridge.put("sessionToken", sessionToken); |
| 69 | + } else { |
| 70 | + Bridge.put("sessionToken", ""); |
| 71 | + } |
68 | 72 | } |
69 | 73 |
|
70 | 74 | void ParseClient::clearSessionToken() { |
71 | | - setSessionToken(NULL); |
| 75 | + setSessionToken(NULL); |
72 | 76 | } |
73 | 77 |
|
74 | 78 | const char* ParseClient::getSessionToken() { |
75 | | - if (!this->sessionToken) { |
76 | | - char *buf = new char[33]; |
77 | | - |
78 | | - requestClient.begin("parse_request"); |
79 | | - requestClient.addParameter("-s"); |
80 | | - requestClient.run(); |
81 | | - read(&requestClient, buf, 33); |
82 | | - this->sessionToken = buf; |
83 | | - } |
84 | | - return this->sessionToken; |
| 79 | + if (!this->sessionToken) { |
| 80 | + char *buf = new char[33]; |
| 81 | + |
| 82 | + requestClient.begin("parse_request"); |
| 83 | + requestClient.addParameter("-s"); |
| 84 | + requestClient.run(); |
| 85 | + read(&requestClient, buf, 33); |
| 86 | + this->sessionToken = buf; |
| 87 | + } |
| 88 | + return this->sessionToken; |
85 | 89 | } |
86 | 90 |
|
87 | 91 | ParseResponse ParseClient::sendRequest(const char* httpVerb, const char* httpPath, const char* requestBody, const char* urlParams) { |
88 | 92 | return sendRequest(String(httpVerb), String(httpPath), String(requestBody), String(urlParams)); |
89 | 93 | } |
90 | 94 |
|
91 | 95 | ParseResponse ParseClient::sendRequest(const String& httpVerb, const String& httpPath, const String& requestBody, const String& urlParams) { |
92 | | - requestClient.begin("parse_request"); // start a process that launch the "parse_request" command |
93 | | - |
94 | | - requestClient.addParameter("-v"); |
95 | | - requestClient.addParameter(httpVerb); |
96 | | - requestClient.addParameter("-e"); |
97 | | - requestClient.addParameter(httpPath); |
98 | | - if (requestBody != "") { |
99 | | - requestClient.addParameter("-d"); |
100 | | - requestClient.addParameter(requestBody); |
101 | | - } |
102 | | - if (urlParams != "") { |
103 | | - requestClient.addParameter("-p"); |
104 | | - requestClient.addParameter(urlParams); |
| 96 | + requestClient.begin("parse_request"); // start a process that launch the "parse_request" command |
| 97 | + |
| 98 | + requestClient.addParameter("-v"); |
| 99 | + requestClient.addParameter(httpVerb); |
| 100 | + requestClient.addParameter("-e"); |
| 101 | + requestClient.addParameter(httpPath); |
| 102 | + if (requestBody != "") { |
| 103 | + requestClient.addParameter("-d"); |
| 104 | + requestClient.addParameter(requestBody); |
| 105 | + } |
| 106 | + if (urlParams != "") { |
| 107 | + requestClient.addParameter("-p"); |
| 108 | + requestClient.addParameter(urlParams); |
105 | 109 | requestClient.runAsynchronously(); |
106 | | - } else { |
107 | | - requestClient.run(); // Run the process and wait for its termination |
108 | | - } |
| 110 | + } else { |
| 111 | + requestClient.run(); // Run the process and wait for its termination |
| 112 | + } |
109 | 113 |
|
110 | | - ParseResponse response(&requestClient); |
111 | | - return response; |
| 114 | + ParseResponse response(&requestClient); |
| 115 | + return response; |
112 | 116 | } |
113 | 117 |
|
114 | 118 | bool ParseClient::startPushService() { |
115 | | - pushClient.begin("parse_push"); // start a process that launch the "parse_request" command |
116 | | - pushClient.runAsynchronously(); |
117 | | - |
118 | | - while(1) { |
119 | | - if(pushClient.available()) { |
120 | | - // read the push starting result |
121 | | - char c = pushClient.read(); |
122 | | - while(pushClient.available()) { |
123 | | - pushClient.read(); |
124 | | - } |
125 | | - if (c == 's') { |
126 | | - pushClient.write('n'); // send a signal that ready to consume next available push |
127 | | - return true; |
128 | | - } else { |
129 | | - return false; |
130 | | - } |
131 | | - } |
132 | | - } |
| 119 | + pushClient.begin("parse_push"); // start a process that launch the "parse_request" command |
| 120 | + pushClient.runAsynchronously(); |
| 121 | + |
| 122 | + while(1) { |
| 123 | + if(pushClient.available()) { |
| 124 | + // read the push starting result |
| 125 | + char c = pushClient.read(); |
| 126 | + while(pushClient.available()) { |
| 127 | + pushClient.read(); |
| 128 | + } |
| 129 | + if (c == 's') { |
| 130 | + pushClient.write('n'); // send a signal that ready to consume next available push |
| 131 | + return true; |
| 132 | + } else { |
| 133 | + return false; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
133 | 137 | } |
134 | 138 |
|
135 | 139 | ParsePush ParseClient::nextPush() { |
136 | | - ParsePush push(&pushClient); |
137 | | - return push; |
| 140 | + ParsePush push(&pushClient); |
| 141 | + return push; |
138 | 142 | } |
139 | 143 |
|
140 | 144 | bool ParseClient::pushAvailable() { |
141 | | - pushClient.write('n'); // send a signal that ready to consume next available push |
142 | | - if(pushClient.available()) { |
143 | | - return 1; |
144 | | - } else { |
145 | | - return 0; |
146 | | - } |
| 145 | + pushClient.write('n'); // send a signal that ready to consume next available push |
| 146 | + if(pushClient.available()) { |
| 147 | + return 1; |
| 148 | + } else { |
| 149 | + return 0; |
| 150 | + } |
147 | 151 | } |
148 | 152 |
|
149 | 153 | void ParseClient::stopPushService() { |
150 | | - pushClient.close(); |
| 154 | + pushClient.close(); |
151 | 155 | } |
152 | 156 |
|
153 | 157 | void ParseClient::end() { |
154 | | - stopPushService(); |
| 158 | + if(installationId) { |
| 159 | + delete[] installationId; |
| 160 | + } |
| 161 | + if(sessionToken) { |
| 162 | + delete[] sessionToken; |
| 163 | + } |
| 164 | + |
| 165 | + stopPushService(); |
155 | 166 | } |
156 | 167 |
|
157 | 168 | void ParseClient::read(Process* client, char* buf, int len) { |
158 | 169 | memset(buf, 0, len); |
159 | 170 | int p = 0; |
160 | | - while(1) { |
161 | | - if(client->available()) { |
162 | | - while (p < (len-1) && client->available()) { |
163 | | - buf[p++] = client->read(); |
164 | | - } |
165 | | - break; |
166 | | - } |
167 | | - } |
| 171 | + while(1) { |
| 172 | + if(client->available()) { |
| 173 | + while (p < (len-1) && client->available()) { |
| 174 | + buf[p++] = client->read(); |
| 175 | + } |
| 176 | + break; |
| 177 | + } |
| 178 | + } |
168 | 179 | } |
169 | 180 |
|
170 | 181 | ParseClient Parse; |
0 commit comments