HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1
mr_q 0:d8f2f7d5f31b 2 /*
mr_q 0:d8f2f7d5f31b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
mr_q 0:d8f2f7d5f31b 4
mr_q 0:d8f2f7d5f31b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
mr_q 0:d8f2f7d5f31b 6 of this software and associated documentation files (the "Software"), to deal
mr_q 0:d8f2f7d5f31b 7 in the Software without restriction, including without limitation the rights
mr_q 0:d8f2f7d5f31b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mr_q 0:d8f2f7d5f31b 9 copies of the Software, and to permit persons to whom the Software is
mr_q 0:d8f2f7d5f31b 10 furnished to do so, subject to the following conditions:
mr_q 0:d8f2f7d5f31b 11
mr_q 0:d8f2f7d5f31b 12 The above copyright notice and this permission notice shall be included in
mr_q 0:d8f2f7d5f31b 13 all copies or substantial portions of the Software.
mr_q 0:d8f2f7d5f31b 14
mr_q 0:d8f2f7d5f31b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mr_q 0:d8f2f7d5f31b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mr_q 0:d8f2f7d5f31b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mr_q 0:d8f2f7d5f31b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mr_q 0:d8f2f7d5f31b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mr_q 0:d8f2f7d5f31b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mr_q 0:d8f2f7d5f31b 21 THE SOFTWARE.
mr_q 0:d8f2f7d5f31b 22 */
mr_q 0:d8f2f7d5f31b 23
mr_q 0:d8f2f7d5f31b 24 /** \file
mr_q 0:d8f2f7d5f31b 25 MySQL Client header file
mr_q 0:d8f2f7d5f31b 26 */
mr_q 0:d8f2f7d5f31b 27
mr_q 0:d8f2f7d5f31b 28 #ifndef MYSQL_CLIENT_H
mr_q 0:d8f2f7d5f31b 29 #define MYSQL_CLIENT_H
mr_q 0:d8f2f7d5f31b 30
mr_q 0:d8f2f7d5f31b 31 #include "core/net.h"
mr_q 0:d8f2f7d5f31b 32 #include "core/netservice.h"
mr_q 0:d8f2f7d5f31b 33 #include "api/TCPSocket.h"
mr_q 0:d8f2f7d5f31b 34 #include "api/DNSRequest.h"
mr_q 0:d8f2f7d5f31b 35 #include "mbed.h"
mr_q 0:d8f2f7d5f31b 36
mr_q 0:d8f2f7d5f31b 37 #include <string>
mr_q 0:d8f2f7d5f31b 38 using std::string;
mr_q 0:d8f2f7d5f31b 39
mr_q 0:d8f2f7d5f31b 40 #include <map>
mr_q 0:d8f2f7d5f31b 41 using std::map;
mr_q 0:d8f2f7d5f31b 42
mr_q 0:d8f2f7d5f31b 43 typedef unsigned char byte;
mr_q 0:d8f2f7d5f31b 44
mr_q 0:d8f2f7d5f31b 45 ///MySQL client results
mr_q 0:d8f2f7d5f31b 46 enum MySQLResult
mr_q 0:d8f2f7d5f31b 47 {
mr_q 0:d8f2f7d5f31b 48 MYSQL_OK, ///<Success
mr_q 0:d8f2f7d5f31b 49 MYSQL_PROCESSING, ///<Processing
mr_q 0:d8f2f7d5f31b 50 MYSQL_PRTCL, ///<Protocol error
mr_q 0:d8f2f7d5f31b 51 MYSQL_SETUP, ///<Not properly configured
mr_q 0:d8f2f7d5f31b 52 MYSQL_DNS, ///<Could not resolve name
mr_q 0:d8f2f7d5f31b 53 MYSQL_AUTHFAILED, ///<Auth failure
mr_q 0:d8f2f7d5f31b 54 MYSQL_READY, ///<Ready to send commands
mr_q 0:d8f2f7d5f31b 55 MYSQL_SQL, ///<SQL Error
mr_q 0:d8f2f7d5f31b 56 MYSQL_TIMEOUT, ///<Connection timeout
mr_q 0:d8f2f7d5f31b 57 MYSQL_CONN ///<Connection error
mr_q 0:d8f2f7d5f31b 58 };
mr_q 0:d8f2f7d5f31b 59
mr_q 0:d8f2f7d5f31b 60 ///A MySQL Client
mr_q 0:d8f2f7d5f31b 61 /**
mr_q 0:d8f2f7d5f31b 62 This MySQL client implements a limited subset of the MySQL internal client/server protocol (including authentication), for server versions 4.1 and newer.
mr_q 0:d8f2f7d5f31b 63 */
mr_q 0:d8f2f7d5f31b 64 class MySQLClient : protected NetService
mr_q 0:d8f2f7d5f31b 65 {
mr_q 0:d8f2f7d5f31b 66 public:
mr_q 0:d8f2f7d5f31b 67 ///Instantiates the MySQL client
mr_q 0:d8f2f7d5f31b 68 MySQLClient();
mr_q 0:d8f2f7d5f31b 69 virtual ~MySQLClient();
mr_q 0:d8f2f7d5f31b 70
mr_q 0:d8f2f7d5f31b 71 //High Level setup functions
mr_q 0:d8f2f7d5f31b 72
mr_q 0:d8f2f7d5f31b 73 ///Opens a connection to a server
mr_q 0:d8f2f7d5f31b 74 /**
mr_q 0:d8f2f7d5f31b 75 Opens a connection to the server host using the provided username, password passowrd and selecting database
mr_q 0:d8f2f7d5f31b 76 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
mr_q 0:d8f2f7d5f31b 77 @param host : server
mr_q 0:d8f2f7d5f31b 78 @param user : username
mr_q 0:d8f2f7d5f31b 79 @param db : database to use
mr_q 0:d8f2f7d5f31b 80 @param pMethod : callback to call on each request completion
mr_q 0:d8f2f7d5f31b 81 */
mr_q 0:d8f2f7d5f31b 82 MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking
mr_q 0:d8f2f7d5f31b 83
mr_q 0:d8f2f7d5f31b 84 ///Opens a connection to a server
mr_q 0:d8f2f7d5f31b 85 /**
mr_q 0:d8f2f7d5f31b 86 Opens a connection to the server host using the provided username, password passowrd and selecting database
mr_q 0:d8f2f7d5f31b 87 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
mr_q 0:d8f2f7d5f31b 88 @param host : server
mr_q 0:d8f2f7d5f31b 89 @param user : username
mr_q 0:d8f2f7d5f31b 90 @param db : database to use
mr_q 0:d8f2f7d5f31b 91 @param pItem : callback's class instance
mr_q 0:d8f2f7d5f31b 92 @param pMethod : callback's method to call on each request completion
mr_q 0:d8f2f7d5f31b 93 */
mr_q 0:d8f2f7d5f31b 94 template<class T>
mr_q 0:d8f2f7d5f31b 95 MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking
mr_q 0:d8f2f7d5f31b 96 {
mr_q 0:d8f2f7d5f31b 97 setOnResult(pItem, pMethod);
mr_q 0:d8f2f7d5f31b 98 setup(host, user, password, db);
mr_q 0:d8f2f7d5f31b 99 return MYSQL_PROCESSING;
mr_q 0:d8f2f7d5f31b 100 }
mr_q 0:d8f2f7d5f31b 101
mr_q 0:d8f2f7d5f31b 102
mr_q 0:d8f2f7d5f31b 103 ///Executes an SQL command
mr_q 0:d8f2f7d5f31b 104 /**
mr_q 0:d8f2f7d5f31b 105 Executes an SQL request on the SQL server
mr_q 0:d8f2f7d5f31b 106 This is a non-blocking function
mr_q 0:d8f2f7d5f31b 107 On completion, the callback set in the open function is fired with the result of the command in parameter
mr_q 0:d8f2f7d5f31b 108 @param sqlCommand SQL request to execute
mr_q 0:d8f2f7d5f31b 109 */
mr_q 0:d8f2f7d5f31b 110 MySQLResult sql(string& sqlCommand);
mr_q 0:d8f2f7d5f31b 111
mr_q 0:d8f2f7d5f31b 112 ///Closes the connection to the server
mr_q 0:d8f2f7d5f31b 113 MySQLResult exit();
mr_q 0:d8f2f7d5f31b 114
mr_q 0:d8f2f7d5f31b 115 void setOnResult( void (*pMethod)(MySQLResult) );
mr_q 0:d8f2f7d5f31b 116 class CDummy;
mr_q 0:d8f2f7d5f31b 117 template<class T>
mr_q 0:d8f2f7d5f31b 118 void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) )
mr_q 0:d8f2f7d5f31b 119 {
mr_q 0:d8f2f7d5f31b 120 m_pCb = NULL;
mr_q 0:d8f2f7d5f31b 121 m_pCbItem = (CDummy*) pItem;
mr_q 0:d8f2f7d5f31b 122 m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod;
mr_q 0:d8f2f7d5f31b 123 }
mr_q 0:d8f2f7d5f31b 124
mr_q 0:d8f2f7d5f31b 125 ///Setups timeout
mr_q 0:d8f2f7d5f31b 126 /**
mr_q 0:d8f2f7d5f31b 127 @param ms : time of connection inactivity in ms after which the request should timeout
mr_q 0:d8f2f7d5f31b 128 */
mr_q 0:d8f2f7d5f31b 129 void setTimeout(int ms);
mr_q 0:d8f2f7d5f31b 130
mr_q 0:d8f2f7d5f31b 131 virtual void poll(); //Called by NetServices
mr_q 0:d8f2f7d5f31b 132
mr_q 0:d8f2f7d5f31b 133 protected:
mr_q 0:d8f2f7d5f31b 134 void resetTimeout();
mr_q 0:d8f2f7d5f31b 135
mr_q 0:d8f2f7d5f31b 136 void init();
mr_q 0:d8f2f7d5f31b 137 void close();
mr_q 0:d8f2f7d5f31b 138
mr_q 0:d8f2f7d5f31b 139 void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary
mr_q 0:d8f2f7d5f31b 140 void connect(); //Start Connection
mr_q 0:d8f2f7d5f31b 141
mr_q 0:d8f2f7d5f31b 142 void handleHandshake();
mr_q 0:d8f2f7d5f31b 143 void sendAuth();
mr_q 0:d8f2f7d5f31b 144
mr_q 0:d8f2f7d5f31b 145 void handleAuthResult();
mr_q 0:d8f2f7d5f31b 146 void sendAuth323();
mr_q 0:d8f2f7d5f31b 147
mr_q 0:d8f2f7d5f31b 148 void sendCommand(byte command, byte* arg, int len);
mr_q 0:d8f2f7d5f31b 149 void handleCommandResult();
mr_q 0:d8f2f7d5f31b 150
mr_q 0:d8f2f7d5f31b 151 void readData(); //Copy to buf
mr_q 0:d8f2f7d5f31b 152 void writeData(); //Copy from buf
mr_q 0:d8f2f7d5f31b 153
mr_q 0:d8f2f7d5f31b 154 void onTCPSocketEvent(TCPSocketEvent e);
mr_q 0:d8f2f7d5f31b 155 void onDNSReply(DNSReply r);
mr_q 0:d8f2f7d5f31b 156 void onResult(MySQLResult r); //Called when exchange completed or on failure
mr_q 0:d8f2f7d5f31b 157 void onTimeout(); //Connection has timed out
mr_q 0:d8f2f7d5f31b 158
mr_q 0:d8f2f7d5f31b 159 private:
mr_q 0:d8f2f7d5f31b 160 CDummy* m_pCbItem;
mr_q 0:d8f2f7d5f31b 161 void (CDummy::*m_pCbMeth)(MySQLResult);
mr_q 0:d8f2f7d5f31b 162
mr_q 0:d8f2f7d5f31b 163 void (*m_pCb)(MySQLResult);
mr_q 0:d8f2f7d5f31b 164
mr_q 0:d8f2f7d5f31b 165 TCPSocket* m_pTCPSocket;
mr_q 0:d8f2f7d5f31b 166
mr_q 0:d8f2f7d5f31b 167 Timer m_watchdog;
mr_q 0:d8f2f7d5f31b 168 int m_timeout;
mr_q 0:d8f2f7d5f31b 169
mr_q 0:d8f2f7d5f31b 170 DNSRequest* m_pDnsReq;
mr_q 0:d8f2f7d5f31b 171
mr_q 0:d8f2f7d5f31b 172 bool m_closed;
mr_q 0:d8f2f7d5f31b 173
mr_q 0:d8f2f7d5f31b 174 enum MySQLStep
mr_q 0:d8f2f7d5f31b 175 {
mr_q 0:d8f2f7d5f31b 176 // MYSQL_INIT,
mr_q 0:d8f2f7d5f31b 177 MYSQL_HANDSHAKE,
mr_q 0:d8f2f7d5f31b 178 MYSQL_AUTH,
mr_q 0:d8f2f7d5f31b 179 MYSQL_COMMANDS,
mr_q 0:d8f2f7d5f31b 180 MYSQL_CLOSED
mr_q 0:d8f2f7d5f31b 181 };
mr_q 0:d8f2f7d5f31b 182
mr_q 0:d8f2f7d5f31b 183 //Parameters
mr_q 0:d8f2f7d5f31b 184 Host m_host;
mr_q 0:d8f2f7d5f31b 185
mr_q 0:d8f2f7d5f31b 186 string m_user;
mr_q 0:d8f2f7d5f31b 187 string m_password;
mr_q 0:d8f2f7d5f31b 188 string m_db;
mr_q 0:d8f2f7d5f31b 189
mr_q 0:d8f2f7d5f31b 190 //Low-level buffers & state-machine
mr_q 0:d8f2f7d5f31b 191 MySQLStep m_state;
mr_q 0:d8f2f7d5f31b 192
mr_q 0:d8f2f7d5f31b 193 byte* m_buf;
mr_q 0:d8f2f7d5f31b 194 byte* m_pPos;
mr_q 0:d8f2f7d5f31b 195 int m_len;
mr_q 0:d8f2f7d5f31b 196 int m_size;
mr_q 0:d8f2f7d5f31b 197
mr_q 0:d8f2f7d5f31b 198 int m_packetId;
mr_q 0:d8f2f7d5f31b 199
mr_q 0:d8f2f7d5f31b 200 };
mr_q 0:d8f2f7d5f31b 201
mr_q 0:d8f2f7d5f31b 202 #endif