cadbc.h

Go to the documentation of this file.
00001 #ifndef CADBC_H
00002 #define CADBC_H
00003 
00004 // system specific C library
00005 #include <sys/types.h>
00006 #include <sys/socket.h>
00007 #include <netinet/in.h>
00008 #include <netdb.h>
00009 //#include <openssl/md5.h>
00010 
00011 //#include <string.h>
00012 #include <string>   // in GCC-3.2 you should not use *.h for system headers
00013 #include <cstdio>
00014 //#include <iostream.h>
00015 #include <iostream>
00016 #include <cstdlib>
00017 #include <string>
00018 #include "Aceobj.h"
00019 
00020 //#include "seqrslt.h"
00021 /* after the first hand shake, the Server sends in a header first then
00022  * it sends the body.  You needs to get the body after the header with
00023  * more recv commands.  
00024  * */
00025 
00026 using namespace std;
00027 
00028 class AceException {
00029         public:
00030                 AceException() : err() { }
00031                 AceException(const string &er) : err(er) { }
00032                 AceException(const char er[]) : err(er) { }
00033                 friend ostream& operator<<(ostream &ous, const AceException &ae) { 
00034                         ous << "AceException:\n" << ae.err; return ous; }
00035                 const string& getMessage() const { return err; }
00036 
00037         private:
00038                 string err;
00039 };
00040 class AQLException : public AceException {
00041         public:
00042                 AQLException() : AceException() {}
00043                 AQLException(const string &er) : AceException(er) {}
00044                 friend ostream& operator<<(ostream &ous, const AQLException &e) {
00045                         ous << "AQLException:\n" << e.getMessage(); return ous; }
00046 };
00047 class DNANotFound : public AceException { 
00048         public:
00049                 DNANotFound() : AceException() {}
00050                 DNANotFound(const string &er) : AceException(er) {}
00051 
00052 };
00053 class pepNotFound : public AceException {
00054         public:
00055                 pepNotFound() : AceException() {}
00056                 pepNotFound(const string &er) : AceException(er) {}
00057                 pepNotFound(const char er[]) : AceException(er) {}
00058 };
00059 
00060 class cadbc 
00061 {
00062         public:
00063                 friend class Result;
00068                 //friend class seqrslt;
00069                 // no copy constructor, no assignment operator
00070                 // these should all be made private
00071                 // because a problem on my computer in resolving host names
00072                 // I have to use IP addresses not host names
00073                 // I can use 127.0.0.1 but not localhost for hostname
00074                 // It worked with certain database but not all. Don't know why?
00075 
00087                 cadbc(const string &host, int port, int rcvbfsz=100000);
00088                 ~cadbc() { delete[] msg; shutdown(sock,2); }
00089 
00090                 void login(const char user[], const char passwd[]);
00091 
00092                 /* Get the fasta formated peptide sequence 
00093                  * >seqname
00094                  * PEPSEQFLLW
00095                  *
00096                  * The server returns this format by default.  It is less
00097                  * work to use this format.
00098                  * This method is formated to get even very long sequences.
00099                  * Should not depends on query method.
00100                  * This methods is used by tabaln
00101                  * should use getPeptideFasta
00102                  * This method deprecated
00103                  * */
00104                 //const char* getFasPep(const char pepName[]) throw(pepNotFound) { getPeptideFasta(pepName); return result.c_str(); }
00105                 //const char* getFasPep(const string &pn) {
00106 
00107                 //      return getFasPep(pn.c_str());}
00108 
00109                 // returns a copy of the peptide
00110                 //string& getPeptideFasta(const char pepName[]) throw(pepNotFound);
00111                 //string& getPeptideFasta(const string &pepName) throw(pepNotFound);
00112                 
00118                 const char* getPeptideFasta(const string &pepName) throw(pepNotFound);
00119                 //string& getPeptideString(const char pepName[]) throw(pepNotFound);
00120                 const char* getPeptideString(const string &pepName) throw(pepNotFound);
00121 
00122                 /* The unformated pep string with white space removed 
00123                  * This methods used the getFasPep method.  Then it removes the
00124                  * \n character.  It does more work.
00125                  * */
00126                 //void getPep(char* &pepStr, int &bfsz, const char pepName[]) throw(pepNotFound);
00127                 // getPep() has been removed, all programs depends on this needs to be re-written
00128 
00134                 //string& getDNAFasta(const string& seqname) throw(DNANotFound);
00135                 //string& getDNAString(const string& seqname) throw(DNANotFound);
00144                 const char* getDNAFasta(const string& seqname) throw(DNANotFound);
00145                 const char* getDNAString(const string& seqname) throw(DNANotFound);
00146 
00152                 const char* fas2str(); 
00153                 Aceobj* fetch(const string &objType, const string &objName) throw(AceException);
00161                 void aql(const string &query) throw(AQLException);
00162                 int fields() const { return columns.size(); }
00163                 string fieldName(int i) const { return columns[i]; }
00164                 // field data type is a more correct name
00165                 string fieldType(int i) const { return columns[i]; }
00166 
00177                 bool next();
00181                 string getvalue(int i) { return currRow[i]; }
00182 
00183                 /* potential problem: if a query generate a large result 
00184                  * the server may send the first segment.  The caller needs to 
00185                  * check the message type to decide to receive the next message
00186                  * */
00187                 char* query(const string q) { return query(q.c_str()); }
00188                 char* query(const char q[]) { send_msg("ACESERV_MSGREQ",q); receive(); return getBody(); }
00189 
00190                 /* returns the pointer to the message. 
00191                  * For efficiency (no need to copy result), we can modify this
00192                  * pointer.
00193                  * This is supposed to be the low level method.
00194                  * It does not consult msglen, if truncating from the end, needs to
00195                  * set the char to \0
00196                  * */
00197                 char *getBody() const { return msg+HEADER_LEN; }
00198                 int getBodyLength() const { return msglen-HEADER_LEN; }
00199 
00200                 /* The old message will be overwritten, you need to copy the old
00201                  * message to save them
00202                  * */
00203                 char *getNext() { send_msg("ACESERV_MSGENCORE", "encore"); receive(); return getBody(); }
00204 
00205                 /* return the number of bytes sent including the 50 byte header
00206                  * */
00207                 int send_msg(const char msgType[], const char body[], int body_len);
00208                 int send_msg(const char msgType[], const char body[]) {
00209                         return send_msg(msgType,body,strlen(body)+1); }
00210                 int send_msg(const char msgType[], const string &body) {
00211                         return send_msg(msgType,body.c_str(),body.length()+1); }
00212 
00216                 int receive();
00217 
00218                 /*helper functions */
00219                 /* Depending on the calling context, it could be the size of the
00220                  * out going or the incoming message
00221                  * */
00222                 int getMaxBytes() const { return *((int*)(msg+16)); }
00223                 int getMsgLen() const { return *((int*)(msg+4)); }
00224                 void setMsgLen(int l) { *((int*)(msg+4))=l; }
00225                 const char* getMsgType() const { return msg+20; } 
00226                 void setMsgType(const char mt[]) { 
00227                         if (strlen(mt)>29) { cerr << "msg type longer than 29\n"; exit(1); }
00228                         strcpy(msg+20, mt); }
00229                 int getSockbufSize();
00230                 void setSockbufSize(int sz);
00231 
00232                 void show(); // for debug 
00233 
00234                 static const int HEADER_LEN=50;
00235                 //MESSAGE TYPE, for short cut
00236                 static const char *OK, *REQ, *FAIL, *DATA, *ENCORE, *KILL;
00237                 //enum messageType { MSGOK, MSGREQ, MSGFAIL, MSGDATA, MSGENCORE, MSGKILL};
00238 
00239         private:
00248                 string& completeQuery(const string &query);
00249 
00250                 void showHeader();  // debuging function
00251                 /* msg 
00252                  * is the buffer to hold both incoming and outgoing messages 
00253                  * we are assuming
00254                  * that one object will not be doing both sending and receiving at
00255                  * the same time
00256                  * the whole message to be sent usually smaller
00257                  * No threading assumed here.
00258                  *
00259                  * the allocated size of msg is always msgMaxlen+1
00260                  * */
00261                 char *msg;
00262                 //int msgLen;  // msssage length including the 50 byte header
00271                 int msglen;  // the actual length of msg
00272                 int msgMaxlen;    
00279                 int maxBytes;
00280 
00281                 int sock;            // socket file descriptor
00282                 sockaddr_in svaddr;  // server internet address xxx.com
00283 
00288                 string result;  
00289                 static const int faswidth = 50; // this the aceserver line width for
00290                 // fasta formated sequences, it may changes in the future
00291 
00295                 Aceobj obj;
00296 
00302                 vector<string> columns;
00303                 char *aqlResult;
00304                 char *nextRow; // point to the next row
00309                 vector<string> currRow; 
00310 };
00311 
00390 #endif

Generated on Wed Aug 10 11:56:49 2011 for Softwares from Orpara by  doxygen 1.5.6