Cumulocity Official / MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AbstractClient.h Source File

AbstractClient.h

Go to the documentation of this file.
00001 /*
00002  * AbstractClient.h
00003  *
00004  * Created on: Nov 1, 2013
00005  * * Authors: Vincent Wochnik <v.wochnik@gmail.com>
00006  *
00007  * Copyright (c) 2013 Cumulocity GmbH
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining
00010  * a copy of this software and associated documentation files (the
00011  * "Software"), to deal in the Software without restriction, including
00012  * without limitation the rights to use, copy, modify, merge, publish,
00013  * distribute, sublicense, and/or sell copies of the Software, and to
00014  * permit persons to whom the Software is furnished to do so, subject to
00015  * the following conditions:
00016  *
00017  * The above copyright notice and this permission notice shall be
00018  * included in all copies or substantial portions of the Software.
00019  *
00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00021  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00022  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00023  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00024  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00025  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00026  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 /**
00030  * @file AbstractClient.h
00031  * An abstraction layer for the SmartREST http client.
00032  */
00033 
00034 #ifndef ABSTRACTCLIENT_H
00035 #define ABSTRACTCLIENT_H
00036 
00037 #include "config.h"
00038 #include "DataGenerator.h"
00039 #include "AbstractDataSource.h"
00040 #include "AbstractDataSink.h"
00041 
00042 /** Return value indicating that the operation has been successful. */
00043 #define CLIENT_OK 0
00044 /** Return value indicating that there has been a connection error. */
00045 #define CLIENT_CONNECTION_ERROR 1
00046 /** Return value indicating that an internal error occurred. */
00047 #define CLIENT_INTERNAL_ERROR 2
00048 
00049 /*
00050  * An abstraction layer for the SmartREST http client.
00051  * 
00052  * All methods have to be called in the following order:
00053  * - setAuthorization()
00054  * - beginRequest() or beginStream()
00055  * - sendIdentifier() (optional)
00056  * - sendData() (optional if in request mode)
00057  * - endRequest()
00058  * - awaitResponse()
00059  * - receiveData() (optional)
00060  * - stop()
00061  * 
00062  * Additionally, stop() can be called at any time to reset the state of
00063  * this instance.
00064  * A return value of any function unequal to CLIENT_OK must follow a
00065  * call to stop() before starting another request.
00066  */
00067 class AbstractClient
00068 {
00069     public:
00070         virtual ~AbstractClient() { };
00071 
00072         /**
00073          * Sets authorization username and password. This method can only be
00074          * called when no request is processing. Values set using this method
00075          * must be kept in memory until either new values are set or the object
00076          * is being destroyed.
00077          * @param username the username used for further request authorization
00078          * @param password the password used for further request authorization
00079          */
00080         virtual uint8_t setAuthorization(const char*, const char*) = 0;
00081 
00082         /**
00083          * Begins a new request. This method has to be the first call for
00084          * any request. A connection to the server will be established
00085          * and the start of a HTTP post request will be sent over the
00086          * connection including Host and Authorization headers.
00087          */
00088         virtual uint8_t beginRequest() = 0;
00089 
00090         /**
00091          * Begins a new stream. This method has to be the first call for
00092          * any request. A connection to the server will be established
00093          * and the start of a HTTP post request will be sent over the
00094          * connection including Host and Authorization headers.
00095          */
00096         virtual uint8_t beginStream(const char*) = 0;
00097 
00098         /**
00099          * Sends the X-Id device identifier header.
00100          * @param identifier the identifier to be sent. The value has to remain
00101          *                   valid for the entire request. If null or empty, no
00102          *                   identifier is sent.
00103          */
00104         virtual uint8_t sendIdentifier(const char*) = 0;
00105 
00106         /**
00107          * Sends POST data over the connection. Before the data is sent,
00108          * a Content-Length header is also sent over the connection. if the
00109          * estimated length does not match the actual conten length,
00110          * unexpected behavior will occur.
00111          * @param generator the data generator for the data to be sent
00112          */
00113         virtual uint8_t sendData(const DataGenerator&) = 0;
00114 
00115         /**
00116          * Finishes the request. In case no data has been sent over the
00117          * connection, this method will send twice carriage-return new-line.
00118          */
00119         virtual uint8_t endRequest() = 0;
00120 
00121         /**
00122          * Blocks until the start of a response has been received.
00123          */
00124         virtual uint8_t awaitResponse() = 0;
00125 
00126         /**
00127          * Returns a data source for reading data.
00128          * When no data can be read for whatever reason,
00129          * a data source with an error state may be returned.
00130          * @return the data source to read from
00131          */
00132         virtual AbstractDataSource& receiveData() = 0;
00133 
00134         /**
00135          * Resets the connection. This method can be called at any time
00136          * and resets the state of this instance.
00137          */
00138         virtual void stop() = 0;
00139 };
00140 
00141 #endif