mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
AbstractClient.h@0:099f76422485, 2014-07-03 (annotated)
- Committer:
- Cumulocity
- Date:
- Thu Jul 03 20:38:04 2014 +0200
- Revision:
- 0:099f76422485
- Child:
- 1:9a11a331e340
Updated from revision 0413a0179eb6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 0:099f76422485 | 1 | /* |
Cumulocity | 0:099f76422485 | 2 | * AbstractClient.h |
Cumulocity | 0:099f76422485 | 3 | * |
Cumulocity | 0:099f76422485 | 4 | * Created on: Nov 1, 2013 |
Cumulocity | 0:099f76422485 | 5 | * * Authors: Vincent Wochnik <v.wochnik@gmail.com> |
Cumulocity | 0:099f76422485 | 6 | * |
Cumulocity | 0:099f76422485 | 7 | * Copyright (c) 2013 Cumulocity GmbH |
Cumulocity | 0:099f76422485 | 8 | * |
Cumulocity | 0:099f76422485 | 9 | * Permission is hereby granted, free of charge, to any person obtaining |
Cumulocity | 0:099f76422485 | 10 | * a copy of this software and associated documentation files (the |
Cumulocity | 0:099f76422485 | 11 | * "Software"), to deal in the Software without restriction, including |
Cumulocity | 0:099f76422485 | 12 | * without limitation the rights to use, copy, modify, merge, publish, |
Cumulocity | 0:099f76422485 | 13 | * distribute, sublicense, and/or sell copies of the Software, and to |
Cumulocity | 0:099f76422485 | 14 | * permit persons to whom the Software is furnished to do so, subject to |
Cumulocity | 0:099f76422485 | 15 | * the following conditions: |
Cumulocity | 0:099f76422485 | 16 | * |
Cumulocity | 0:099f76422485 | 17 | * The above copyright notice and this permission notice shall be |
Cumulocity | 0:099f76422485 | 18 | * included in all copies or substantial portions of the Software. |
Cumulocity | 0:099f76422485 | 19 | * |
Cumulocity | 0:099f76422485 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
Cumulocity | 0:099f76422485 | 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Cumulocity | 0:099f76422485 | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Cumulocity | 0:099f76422485 | 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
Cumulocity | 0:099f76422485 | 24 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
Cumulocity | 0:099f76422485 | 25 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
Cumulocity | 0:099f76422485 | 26 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Cumulocity | 0:099f76422485 | 27 | */ |
Cumulocity | 0:099f76422485 | 28 | |
Cumulocity | 0:099f76422485 | 29 | /** |
Cumulocity | 0:099f76422485 | 30 | * @file AbstractClient.h |
Cumulocity | 0:099f76422485 | 31 | * An abstraction layer for the SmartREST http client. |
Cumulocity | 0:099f76422485 | 32 | */ |
Cumulocity | 0:099f76422485 | 33 | |
Cumulocity | 0:099f76422485 | 34 | #ifndef ABSTRACTCLIENT_H |
Cumulocity | 0:099f76422485 | 35 | #define ABSTRACTCLIENT_H |
Cumulocity | 0:099f76422485 | 36 | |
Cumulocity | 0:099f76422485 | 37 | #include "config.h" |
Cumulocity | 0:099f76422485 | 38 | #include "DataGenerator.h" |
Cumulocity | 0:099f76422485 | 39 | #include "AbstractDataSource.h" |
Cumulocity | 0:099f76422485 | 40 | #include "AbstractDataSink.h" |
Cumulocity | 0:099f76422485 | 41 | |
Cumulocity | 0:099f76422485 | 42 | /** Return value indicating that the operation has been successful. */ |
Cumulocity | 0:099f76422485 | 43 | #define CLIENT_OK 0 |
Cumulocity | 0:099f76422485 | 44 | /** Return value indicating that there has been a connection error. */ |
Cumulocity | 0:099f76422485 | 45 | #define CLIENT_CONNECTION_ERROR 1 |
Cumulocity | 0:099f76422485 | 46 | /** Return value indicating that an internal error occurred. */ |
Cumulocity | 0:099f76422485 | 47 | #define CLIENT_INTERNAL_ERROR 2 |
Cumulocity | 0:099f76422485 | 48 | |
Cumulocity | 0:099f76422485 | 49 | /* |
Cumulocity | 0:099f76422485 | 50 | * An abstraction layer for the SmartREST http client. |
Cumulocity | 0:099f76422485 | 51 | * Constructor arguments such as username and password strings must |
Cumulocity | 0:099f76422485 | 52 | * not be cpoied but instead stored as pointer as they are subject to |
Cumulocity | 0:099f76422485 | 53 | * change. |
Cumulocity | 0:099f76422485 | 54 | * |
Cumulocity | 0:099f76422485 | 55 | * All methods have to be called in the following order: |
Cumulocity | 0:099f76422485 | 56 | * - beginRequest() |
Cumulocity | 0:099f76422485 | 57 | * - sendIdentifier() (optional) |
Cumulocity | 0:099f76422485 | 58 | * - sendData() (optional) |
Cumulocity | 0:099f76422485 | 59 | * - endRequest() |
Cumulocity | 0:099f76422485 | 60 | * - awaitResponse() |
Cumulocity | 0:099f76422485 | 61 | * - receiveData() (optional) |
Cumulocity | 0:099f76422485 | 62 | * - stop() |
Cumulocity | 0:099f76422485 | 63 | * |
Cumulocity | 0:099f76422485 | 64 | * Additionally, stop() can be called at any time to reset the state of |
Cumulocity | 0:099f76422485 | 65 | * this instance. |
Cumulocity | 0:099f76422485 | 66 | */ |
Cumulocity | 0:099f76422485 | 67 | class AbstractClient |
Cumulocity | 0:099f76422485 | 68 | { |
Cumulocity | 0:099f76422485 | 69 | public: |
Cumulocity | 0:099f76422485 | 70 | virtual ~AbstractClient() { }; |
Cumulocity | 0:099f76422485 | 71 | |
Cumulocity | 0:099f76422485 | 72 | /** |
Cumulocity | 0:099f76422485 | 73 | * Begins a new request. This method has to be the first call for |
Cumulocity | 0:099f76422485 | 74 | * any request. A connection to the server will be established |
Cumulocity | 0:099f76422485 | 75 | * and the start of a HTTP post request will be sent over the |
Cumulocity | 0:099f76422485 | 76 | * connection including Host and Authorization headers. |
Cumulocity | 0:099f76422485 | 77 | */ |
Cumulocity | 0:099f76422485 | 78 | virtual uint8_t beginRequest() = 0; |
Cumulocity | 0:099f76422485 | 79 | |
Cumulocity | 0:099f76422485 | 80 | /** |
Cumulocity | 0:099f76422485 | 81 | * Sends the X-Id device identifier header. |
Cumulocity | 0:099f76422485 | 82 | * @param identifier the identifier to be sent. The value has to remain |
Cumulocity | 0:099f76422485 | 83 | * valid for the entire request. |
Cumulocity | 0:099f76422485 | 84 | */ |
Cumulocity | 0:099f76422485 | 85 | virtual uint8_t sendIdentifier(const char*) = 0; |
Cumulocity | 0:099f76422485 | 86 | |
Cumulocity | 0:099f76422485 | 87 | /** |
Cumulocity | 0:099f76422485 | 88 | * Sends POST data over the connection. Before the data is sent, |
Cumulocity | 0:099f76422485 | 89 | * a Content-Length header is also sent over the connection. if the |
Cumulocity | 0:099f76422485 | 90 | * estimated length does not match the actual conten length, |
Cumulocity | 0:099f76422485 | 91 | * unexpected behavior will occur. |
Cumulocity | 0:099f76422485 | 92 | * @param generator the data generator for the data to be sent |
Cumulocity | 0:099f76422485 | 93 | */ |
Cumulocity | 0:099f76422485 | 94 | virtual uint8_t sendData(DataGenerator& generator) = 0; |
Cumulocity | 0:099f76422485 | 95 | |
Cumulocity | 0:099f76422485 | 96 | /** |
Cumulocity | 0:099f76422485 | 97 | * Finishes the request. In case no data has been sent over the |
Cumulocity | 0:099f76422485 | 98 | * connection, this method will send twice carriage-return new-line. |
Cumulocity | 0:099f76422485 | 99 | */ |
Cumulocity | 0:099f76422485 | 100 | virtual uint8_t endRequest() = 0; |
Cumulocity | 0:099f76422485 | 101 | |
Cumulocity | 0:099f76422485 | 102 | /** |
Cumulocity | 0:099f76422485 | 103 | * Blocks until the start of a response has been received. |
Cumulocity | 0:099f76422485 | 104 | */ |
Cumulocity | 0:099f76422485 | 105 | virtual uint8_t awaitResponse() = 0; |
Cumulocity | 0:099f76422485 | 106 | |
Cumulocity | 0:099f76422485 | 107 | /** |
Cumulocity | 0:099f76422485 | 108 | * Returns a data source for reading data. |
Cumulocity | 0:099f76422485 | 109 | * When no data can be read for whatever reason, |
Cumulocity | 0:099f76422485 | 110 | * a data source with an error state may be returned. |
Cumulocity | 0:099f76422485 | 111 | * @return the data source to read from |
Cumulocity | 0:099f76422485 | 112 | */ |
Cumulocity | 0:099f76422485 | 113 | virtual AbstractDataSource& receiveData() = 0; |
Cumulocity | 0:099f76422485 | 114 | |
Cumulocity | 0:099f76422485 | 115 | /** |
Cumulocity | 0:099f76422485 | 116 | * Resets the connection. This method can be called at any time |
Cumulocity | 0:099f76422485 | 117 | * and resets the state of this instance. |
Cumulocity | 0:099f76422485 | 118 | */ |
Cumulocity | 0:099f76422485 | 119 | virtual void stop() = 0; |
Cumulocity | 0:099f76422485 | 120 | }; |
Cumulocity | 0:099f76422485 | 121 | |
Cumulocity | 0:099f76422485 | 122 | #endif |