00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef INCLUDED_ClientServerUtils
00032 #define INCLUDED_ClientServerUtils
00033
00034
00035
00036
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
00059 #include<exception>
00060 #include "GlobalDefinitions.h"
00061 class Match;
00062
00063
00064 #if OHNOITSADEC
00065 typedef int socklen_t;
00066 #endif
00067
00068
00069 #ifndef __GNUC__
00070 # define __attribute__(x)
00071 #endif
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
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
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;
00154 int numSubjectNames;
00155 int numMatches;
00156 } __attribute__ ((packed));
00157
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
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
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 );
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
00272 timeval timeOut_;
00273 char buffer_[MAXLINE];
00274 };
00275
00276 #endif
00277
00278