EnsemblServer/ClientServerUtils.h

Go to the documentation of this file.
00001 /*  Last edited: May 27 14:31 2002 (ac2) */
00002 
00003 // #######################################################################
00004 
00005 // SSAHA : Sequence Search and Alignment by Hashing Algorithm
00006 // Version 3.2, released 1st March 2004
00007 // Copyright (c) Genome Research 2002
00008 
00009 // SSAHA is free software; you can redistribute it and/or modify 
00010 // it under the terms of version 2 of the GNU General Public Licence
00011 // as published by the Free Software Foundation.
00012  
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public Licence for more details.
00017  
00018 // You should have received a copy of the GNU General Public Licence
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 // or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
00022 
00023 // #######################################################################
00024 
00025 // Module Name  : ClientServerUtils
00026 // File Name    : ClientServerUtils.h
00027 // Language     : C++
00028 // Module Author: Anthony J. Cox (ac2@sanger.ac.uk)
00029 
00030 // Include guard:
00031 #ifndef INCLUDED_ClientServerUtils
00032 #define INCLUDED_ClientServerUtils
00033 
00034 // Description:
00035 
00036 // Includes:
00037 #include <sys/types.h>
00038 #include <sys/wait.h>
00039 #include <sys/socket.h>
00040 #include <sys/time.h>
00041 #include <signal.h>
00042 #include <errno.h>
00043 #include <unistd.h>
00044 #include <netinet/in.h>
00045 #include <netdb.h>
00046 
00047 #include <arpa/inet.h>
00048 #include <pthread.h>
00049 #include <stdio.h>
00050 #include <cstdlib>
00051 #include <string>
00052 #include <deque>
00053 typedef   void    Sigfunc(int);
00054 #define SERV_PORT 9877
00055 #define SERV_VERSION 4
00056 #define SA      struct sockaddr
00057 #define LISTENQ         1024
00058 #define MAXLINE         4096    /* max text line length */
00059 #include<exception>
00060 #include "GlobalDefinitions.h"
00061 class Match;
00062 
00063 // next line needed to get server to compile on ecs1h and hcs2f
00064 #if OHNOITSADEC
00065 typedef int socklen_t; 
00066 #endif
00067 
00068 /* If we're not using GNU C, elide __attribute__ */
00069 #ifndef __GNUC__
00070 #  define  __attribute__(x)  /*NOTHING*/
00071 #endif
00072 
00073 // ### Class Declarations ###
00074 
00075 // Client/server interface protocol is as follows
00076 // i) Server sends Handshake to client, to tell client the word size w it is
00077 // expecting
00078 // ii) Client reads in queries and converts them to bases using w bases
00079 // per Word (TBD need to change slightly for protein)
00080 // iii) Client sends QueryHeader to server, to tell server how many sequences 
00081 // to expect. Also sends total number of words as a checksum, and parameters to
00082 // use for the search.
00083 // iv) For each query sequence, client sends a SequenceHeader and then the 
00084 // Words of the query sequence
00085 // v) Server does the match
00086 // vi) Server sends a MatchHeader, to report whether the search was successful
00087 // and, if so, how many MatchStructs to expect
00088 // vii) Client reads a MatchStruct for each Match, and does what the hell it
00089 // likes with 'em.
00090 
00091 
00092 // TBD also include table type (DNA/protein)
00093 // The `attribute' gibberish ensures sizeof(struct)= sum of sizeof what's in it
00094 // (i.e no weird packing)
00095 
00096 enum TableType
00097 {
00098   e2bitDNA = 0,
00099   e5bitProtein = 1,
00100   e5bitTranslatedDNA = 2 
00101 };
00102 
00103 
00104 
00105 struct Handshake
00106 {
00107   unsigned int ssahaversion;
00108   unsigned int wordLength;
00109   TableType tableType;
00110   unsigned int maxBufferSize;
00111 } __attribute__ ((packed));
00112 
00113 enum SortModeType
00114 {
00115   eNoSort = 0,
00116   eSortByMatchLength = 1,
00117   eSortByPercentMatch = 2,
00118   eSortAndReturnSequence = 3 // return matched ASCII seq to client
00119 };
00120 
00121 struct QueryHeader
00122 {
00123   unsigned int numQuerySeqs;
00124   unsigned int numQueryWords;
00125   unsigned int bitsPerSymbol;
00126   unsigned int minPrint;
00127   unsigned int maxGap;
00128   unsigned int maxInsert;
00129   unsigned int numRepeats;
00130   unsigned int clipThreshold;
00131   unsigned int maxMatches;
00132   unsigned int substituteThreshold;
00133   unsigned int bandExtension;
00134   SortModeType sortMode;
00135   friend ostream& operator<<( ostream& os, QueryHeader& q )
00136   {
00137     os << q.numQuerySeqs << " seqs " << q.numQueryWords << " words "
00138        << q.minPrint << "-" << q.maxGap << "-" << q.maxInsert << "-"
00139        << q.numRepeats << "-" << q.clipThreshold << "-" 
00140        << q.maxMatches << "-" << q.sortMode << "-"
00141        << q.substituteThreshold << "-" << q.bandExtension << endl;
00142   }
00143 } __attribute__ ((packed));
00144 
00145 struct SequenceHeader
00146 {
00147   unsigned int size;
00148   unsigned int basesInLast;
00149 } __attribute__ ((packed));
00150 
00151 struct MatchHeader
00152 {
00153   bool wasSuccessful; // TBD enum with more descriptive stuff
00154   int numSubjectNames; 
00155   int numMatches;
00156 } __attribute__ ((packed));
00157 // then expects numSubjectNames int/string pairs followed by numMatches 
00158 // 
00159 
00160 struct MatchInfo
00161 {
00162   SequenceNumber subjectNum;
00163   SequenceOffset subjectStart;
00164   SequenceOffset subjectEnd;
00165   SequenceNumber queryNum;
00166   SequenceOffset queryStart;
00167   SequenceOffset queryEnd;
00168   int  numBases;
00169   bool isQueryForward;
00170   bool isSubjectForward;
00171   // simple constructors
00172   MatchInfo( void ) :
00173   subjectNum(0),
00174   subjectStart(0),
00175   subjectEnd(0),
00176   queryNum(0),
00177   queryStart(0),
00178   queryEnd(0),
00179   numBases(0),
00180   isQueryForward(true),
00181   isSubjectForward(true) {}
00182   MatchInfo( const Match& m );
00183 } __attribute__ ((packed));
00184 
00185 
00186 
00187 
00188 class NetworkException : public SSAHAException
00189 {
00190     public:
00191     NetworkException( const string& message ) :
00192     SSAHAException(message) {}
00193 };
00194 
00195 class BrokenSocketException : public NetworkException
00196 {
00197  public:
00198   BrokenSocketException( void ) :
00199     NetworkException("broken socket exception") {}
00200 };
00201 
00202 
00203 // ### Function Declarations ###
00204 
00205 void err_sys(const char *fmt, ...);
00206 void sig_chld(int signo);
00207 struct sockaddr;
00208 int Accept(int fd, sockaddr *sa, socklen_t *salenptr);
00209 void Bind(int fd, const sockaddr* sa, socklen_t salen);
00210 void Listen(int fd, int backlog);
00211 Sigfunc * signal(int signo, Sigfunc *func);
00212 Sigfunc * Signal(int signo, Sigfunc *func);
00213 void Close(int fd);
00214 pid_t Fork(void);
00215 int Socket(int family, int type, int protocol);
00216 ssize_t readn(int fd, void *vptr, size_t n);
00217 ssize_t Readn(int fd, void *ptr, size_t nbytes);
00218 ssize_t writen(int fd, const void *vptr, size_t n); 
00219 void Writen(int fd, void *ptr, size_t nbytes);
00220 char * Fgets(char *ptr, int n, FILE *stream);
00221 void Fputs(const char *ptr, FILE *stream);
00222 void Connect(int fd, const sockaddr *sa, socklen_t salen);
00223 
00224 
00225 class SocketInterface : public deque<char>
00226 {
00227  public:
00228   SocketInterface( int portNum, int timeOutSeconds=10000 ) : 
00229     portNum_( portNum ), bytesRead_(0), bytesSent_(0) 
00230     {
00231       timeOut_.tv_sec  = timeOutSeconds;
00232       timeOut_.tv_usec = 0;
00233     }
00234   enum { MaxChunkSize=MAXLINE };
00235   
00236   void setTimeOut( int timeOutSeconds ) {
00237     timeOut_.tv_sec = timeOutSeconds;
00238   }
00239 
00240   void getAtLeast( int numBytes );
00241 
00242   template <typename T> void sendStruct( T* ptr )
00243   {
00244     bytesSent_+= sizeof(T);
00245     Writen(portNum_, (void*) ptr, sizeof(T) );
00246   }
00247   
00248   void copy( char* target, int numBytes);
00249 
00250   template <typename T> void receiveStruct( T* ptr )
00251   {
00252     copy( (char*)ptr, sizeof(T));
00253   }
00254 
00255 
00256   void checkSocketEmpty( void ); // else throws;
00257 
00258   void sendSequence( const WordSequence& seq );
00259   void receiveSequence( WordSequence& seq );
00260 
00261   void sendString( const string& s );
00262   void sendChars( const char* pChar, int numChars );
00263 
00264   void receiveString( string& s );
00265   void receiveChars( vector<char>& v );
00266 
00267  protected:
00268   int portNum_;
00269   int bytesRead_;
00270   int bytesSent_;
00271   // timeOut_ used in getAtLeast
00272   timeval timeOut_;
00273   char buffer_[MAXLINE];
00274 };
00275 
00276 #endif
00277 
00278 // End of file ClientServerUtils.h

Generated on Fri Dec 21 13:12:15 2007 for ssaha by  doxygen 1.5.2