Dirk-Willem van Gulik (NXP/mbed)
/
Bonjour
Bonjour/Zerconf library
services/mysql/MySQLClient.h@5:8e53abda9900, 2010-08-14 (annotated)
- Committer:
- dirkx
- Date:
- Sat Aug 14 15:54:31 2010 +0000
- Revision:
- 5:8e53abda9900
- Parent:
- 0:355018f44c9f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dirkx | 0:355018f44c9f | 1 | |
dirkx | 0:355018f44c9f | 2 | /* |
dirkx | 0:355018f44c9f | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
dirkx | 0:355018f44c9f | 4 | |
dirkx | 0:355018f44c9f | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
dirkx | 0:355018f44c9f | 6 | of this software and associated documentation files (the "Software"), to deal |
dirkx | 0:355018f44c9f | 7 | in the Software without restriction, including without limitation the rights |
dirkx | 0:355018f44c9f | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
dirkx | 0:355018f44c9f | 9 | copies of the Software, and to permit persons to whom the Software is |
dirkx | 0:355018f44c9f | 10 | furnished to do so, subject to the following conditions: |
dirkx | 0:355018f44c9f | 11 | |
dirkx | 0:355018f44c9f | 12 | The above copyright notice and this permission notice shall be included in |
dirkx | 0:355018f44c9f | 13 | all copies or substantial portions of the Software. |
dirkx | 0:355018f44c9f | 14 | |
dirkx | 0:355018f44c9f | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dirkx | 0:355018f44c9f | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dirkx | 0:355018f44c9f | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dirkx | 0:355018f44c9f | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dirkx | 0:355018f44c9f | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dirkx | 0:355018f44c9f | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dirkx | 0:355018f44c9f | 21 | THE SOFTWARE. |
dirkx | 0:355018f44c9f | 22 | */ |
dirkx | 0:355018f44c9f | 23 | |
dirkx | 0:355018f44c9f | 24 | #ifndef MYSQL_CLIENT_H |
dirkx | 0:355018f44c9f | 25 | #define MYSQL_CLIENT_H |
dirkx | 0:355018f44c9f | 26 | |
dirkx | 0:355018f44c9f | 27 | #include "if/net/net.h" |
dirkx | 0:355018f44c9f | 28 | #include "api/TCPSocket.h" |
dirkx | 0:355018f44c9f | 29 | #include "api/DNSRequest.h" |
dirkx | 0:355018f44c9f | 30 | #include "mbed.h" |
dirkx | 0:355018f44c9f | 31 | |
dirkx | 0:355018f44c9f | 32 | #include <string> |
dirkx | 0:355018f44c9f | 33 | using std::string; |
dirkx | 0:355018f44c9f | 34 | |
dirkx | 0:355018f44c9f | 35 | #include <map> |
dirkx | 0:355018f44c9f | 36 | using std::map; |
dirkx | 0:355018f44c9f | 37 | |
dirkx | 0:355018f44c9f | 38 | typedef unsigned char byte; |
dirkx | 0:355018f44c9f | 39 | |
dirkx | 0:355018f44c9f | 40 | enum MySQLResult |
dirkx | 0:355018f44c9f | 41 | { |
dirkx | 0:355018f44c9f | 42 | MYSQL_OK, |
dirkx | 0:355018f44c9f | 43 | MYSQL_PROCESSING, |
dirkx | 0:355018f44c9f | 44 | MYSQL_PRTCL, |
dirkx | 0:355018f44c9f | 45 | MYSQL_SETUP, //Not properly configured |
dirkx | 0:355018f44c9f | 46 | MYSQL_DNS, //Could not resolve name |
dirkx | 0:355018f44c9f | 47 | MYSQL_AUTHFAILED, //Auth failure |
dirkx | 0:355018f44c9f | 48 | MYSQL_READY, //Ready to send commands |
dirkx | 0:355018f44c9f | 49 | MYSQL_SQL, //SQL Error |
dirkx | 0:355018f44c9f | 50 | MYSQL_TIMEOUT, //Connection timeout |
dirkx | 0:355018f44c9f | 51 | MYSQL_CONN //Connection error |
dirkx | 0:355018f44c9f | 52 | }; |
dirkx | 0:355018f44c9f | 53 | |
dirkx | 0:355018f44c9f | 54 | class MySQLClient : protected NetService |
dirkx | 0:355018f44c9f | 55 | { |
dirkx | 0:355018f44c9f | 56 | public: |
dirkx | 0:355018f44c9f | 57 | MySQLClient(); |
dirkx | 0:355018f44c9f | 58 | virtual ~MySQLClient(); |
dirkx | 0:355018f44c9f | 59 | |
dirkx | 0:355018f44c9f | 60 | //High Level setup functions |
dirkx | 0:355018f44c9f | 61 | MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking |
dirkx | 0:355018f44c9f | 62 | template<class T> |
dirkx | 0:355018f44c9f | 63 | MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking |
dirkx | 0:355018f44c9f | 64 | { |
dirkx | 0:355018f44c9f | 65 | setOnResult(pItem, pMethod); |
dirkx | 0:355018f44c9f | 66 | setup(host, user, password, db); |
dirkx | 0:355018f44c9f | 67 | return MYSQL_PROCESSING; |
dirkx | 0:355018f44c9f | 68 | } |
dirkx | 0:355018f44c9f | 69 | |
dirkx | 0:355018f44c9f | 70 | MySQLResult sql(string& sqlCommand); |
dirkx | 0:355018f44c9f | 71 | |
dirkx | 0:355018f44c9f | 72 | MySQLResult exit(); |
dirkx | 0:355018f44c9f | 73 | |
dirkx | 0:355018f44c9f | 74 | void setOnResult( void (*pMethod)(MySQLResult) ); |
dirkx | 0:355018f44c9f | 75 | class CDummy; |
dirkx | 0:355018f44c9f | 76 | template<class T> |
dirkx | 0:355018f44c9f | 77 | void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) ) |
dirkx | 0:355018f44c9f | 78 | { |
dirkx | 0:355018f44c9f | 79 | m_pCb = NULL; |
dirkx | 0:355018f44c9f | 80 | m_pCbItem = (CDummy*) pItem; |
dirkx | 0:355018f44c9f | 81 | m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod; |
dirkx | 0:355018f44c9f | 82 | } |
dirkx | 0:355018f44c9f | 83 | |
dirkx | 0:355018f44c9f | 84 | void setTimeout(int ms); |
dirkx | 0:355018f44c9f | 85 | |
dirkx | 0:355018f44c9f | 86 | virtual void poll(); //Called by NetServices |
dirkx | 0:355018f44c9f | 87 | |
dirkx | 0:355018f44c9f | 88 | protected: |
dirkx | 0:355018f44c9f | 89 | void resetTimeout(); |
dirkx | 0:355018f44c9f | 90 | |
dirkx | 0:355018f44c9f | 91 | void init(); |
dirkx | 0:355018f44c9f | 92 | void close(); |
dirkx | 0:355018f44c9f | 93 | |
dirkx | 0:355018f44c9f | 94 | void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary |
dirkx | 0:355018f44c9f | 95 | void connect(); //Start Connection |
dirkx | 0:355018f44c9f | 96 | |
dirkx | 0:355018f44c9f | 97 | void handleHandshake(); |
dirkx | 0:355018f44c9f | 98 | void sendAuth(); |
dirkx | 0:355018f44c9f | 99 | |
dirkx | 0:355018f44c9f | 100 | void handleAuthResult(); |
dirkx | 0:355018f44c9f | 101 | void sendAuth323(); |
dirkx | 0:355018f44c9f | 102 | |
dirkx | 0:355018f44c9f | 103 | void sendCommand(byte command, byte* arg, int len); |
dirkx | 0:355018f44c9f | 104 | void handleCommandResult(); |
dirkx | 0:355018f44c9f | 105 | |
dirkx | 0:355018f44c9f | 106 | void readData(); //Copy to buf |
dirkx | 0:355018f44c9f | 107 | void writeData(); //Copy from buf |
dirkx | 0:355018f44c9f | 108 | |
dirkx | 0:355018f44c9f | 109 | void onTCPSocketEvent(TCPSocketEvent e); |
dirkx | 0:355018f44c9f | 110 | void onDNSReply(DNSReply r); |
dirkx | 0:355018f44c9f | 111 | void onResult(MySQLResult r); //Called when exchange completed or on failure |
dirkx | 0:355018f44c9f | 112 | void onTimeout(); //Connection has timed out |
dirkx | 0:355018f44c9f | 113 | |
dirkx | 0:355018f44c9f | 114 | private: |
dirkx | 0:355018f44c9f | 115 | CDummy* m_pCbItem; |
dirkx | 0:355018f44c9f | 116 | void (CDummy::*m_pCbMeth)(MySQLResult); |
dirkx | 0:355018f44c9f | 117 | |
dirkx | 0:355018f44c9f | 118 | void (*m_pCb)(MySQLResult); |
dirkx | 0:355018f44c9f | 119 | |
dirkx | 0:355018f44c9f | 120 | TCPSocket* m_pTCPSocket; |
dirkx | 0:355018f44c9f | 121 | |
dirkx | 0:355018f44c9f | 122 | Timer m_watchdog; |
dirkx | 0:355018f44c9f | 123 | int m_timeout; |
dirkx | 0:355018f44c9f | 124 | |
dirkx | 0:355018f44c9f | 125 | DNSRequest* m_pDnsReq; |
dirkx | 0:355018f44c9f | 126 | |
dirkx | 0:355018f44c9f | 127 | bool m_closed; |
dirkx | 0:355018f44c9f | 128 | |
dirkx | 0:355018f44c9f | 129 | enum MySQLStep |
dirkx | 0:355018f44c9f | 130 | { |
dirkx | 0:355018f44c9f | 131 | // MYSQL_INIT, |
dirkx | 0:355018f44c9f | 132 | MYSQL_HANDSHAKE, |
dirkx | 0:355018f44c9f | 133 | MYSQL_AUTH, |
dirkx | 0:355018f44c9f | 134 | MYSQL_COMMANDS, |
dirkx | 0:355018f44c9f | 135 | MYSQL_CLOSED |
dirkx | 0:355018f44c9f | 136 | }; |
dirkx | 0:355018f44c9f | 137 | |
dirkx | 0:355018f44c9f | 138 | //Parameters |
dirkx | 0:355018f44c9f | 139 | Host m_host; |
dirkx | 0:355018f44c9f | 140 | |
dirkx | 0:355018f44c9f | 141 | string m_user; |
dirkx | 0:355018f44c9f | 142 | string m_password; |
dirkx | 0:355018f44c9f | 143 | string m_db; |
dirkx | 0:355018f44c9f | 144 | |
dirkx | 0:355018f44c9f | 145 | //Low-level buffers & state-machine |
dirkx | 0:355018f44c9f | 146 | MySQLStep m_state; |
dirkx | 0:355018f44c9f | 147 | |
dirkx | 0:355018f44c9f | 148 | byte* m_buf; |
dirkx | 0:355018f44c9f | 149 | byte* m_pPos; |
dirkx | 0:355018f44c9f | 150 | int m_len; |
dirkx | 0:355018f44c9f | 151 | int m_size; |
dirkx | 0:355018f44c9f | 152 | |
dirkx | 0:355018f44c9f | 153 | int m_packetId; |
dirkx | 0:355018f44c9f | 154 | |
dirkx | 0:355018f44c9f | 155 | }; |
dirkx | 0:355018f44c9f | 156 | |
dirkx | 0:355018f44c9f | 157 | #endif |