Generic SmartRest library

Dependents:   SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming

Committer:
vwochnik
Date:
Wed Apr 16 08:30:49 2014 +0000
Revision:
1:3e7b4c9e0821
Parent:
0:744801d5734d
fix

Who changed what in which revision?

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