NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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