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
00043 #include <errno.h>
00044 #include <unistd.h>
00045 #include <string.h>
00046 #include <sys/types.h>
00047 #include <sys/socket.h>
00048
00049 #ifdef Solaris
00050 #include <sys/socket.h>
00051 #endif
00052
00053 #include "Connection.h"
00054 #include "Logger.h"
00055
00056 extern Logger Log;
00058
00059
00060
00061
00063 Connection::Connection( )
00064 {
00065 m_iMaxMsgSize = 2048;
00066 }
00067
00072 Connection::Connection(const char *hostname, int port, int iMaxSize)
00073 {
00074 m_iMaxMsgSize = iMaxSize;
00075 if( connect( hostname, port ) )
00076 Log.log( 1, "(Connection:connection) Socket connection made with %s:%d",
00077 hostname, port );
00078 else
00079 Log.log( 1, "(Connection:Connection) Could not create connection with %s:%d"
00080 , hostname, port );
00081 }
00082
00084 Connection::~Connection()
00085 {
00086 disconnect();
00087 }
00088
00093 bool Connection::connect(const char *host, int port )
00094 {
00095 struct hostent *host_ent;
00096 struct in_addr *addr_ptr;
00097 struct sockaddr_in cli_addr ;
00098 int sockfd ;
00099
00100 m_sock.socketfd = -1 ;
00101
00102 if( (host_ent = (struct hostent*)gethostbyname(host)) === NULL)
00103 {
00104
00105 #ifdef Solaris
00106 if( inet_addr(host) == ((in_addr_t)-1) )
00107 #else
00108 if( inet_addr(host) == INADDR_NONE )
00109 #endif
00110 {
00111 cerr << "(Connection::connect) Cannot find host " << host << endl;
00112 return false ;
00113 }
00114 }
00115 else
00116 {
00117 addr_ptr = (struct in_addr *) *host_ent->h_addr_list;
00118 host = inet_ntoa(*addr_ptr);
00119 }
00120
00121
00122 if( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
00123 {
00124 cerr << "(Connection::connect) Cannot create socket " << host << endl;
00125 return false ;
00126 }
00127
00128
00129 cli_addr.sin_family = AF_INET ;
00130 cli_addr.sin_addr.s_addr = htonl(INADDR_ANY) ;
00131 cli_addr.sin_port = htons(0) ;
00132
00133
00134 if(bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0)
00135 {
00136 cerr << "(Connection::connect) Cannot bind local address " << host << endl;
00137 return false ;
00138 }
00139
00140
00141 m_sock.socketfd = sockfd ;
00142
00143 m_sock.serv_addr.sin_family = AF_INET ;
00144 m_sock.serv_addr.sin_addr.s_addr = inet_addr(host);
00145 m_sock.serv_addr.sin_port = htons(port) ;
00146
00147 return true;
00148 }
00149
00151 void Connection::disconnect( void )
00152 {
00153 if (isConnected() )
00154 {
00155 close(m_sock.socketfd) ;
00156 m_sock.socketfd = -1;
00157 }
00158 }
00159
00162 bool Connection::isConnected(void) const
00163 {
00164 return(m_sock.socketfd != -1);
00165 }
00166
00172 int Connection::receiveMessage( char *msg, int maxsize )
00173 {
00174 #ifdef Solaris
00175 int servlen;
00176 #else
00177 socklen_t servlen ;
00178 #endif
00179 int n;
00180 struct sockaddr_in serv_addr ;
00181
00182 servlen = sizeof(serv_addr) ;
00183
00184
00185 n = recvfrom(m_sock.socketfd, msg, maxsize, 0,
00186 (struct sockaddr *)&serv_addr, &servlen);
00187
00188 if(n < 0)
00189 {
00190 if( n == -1 && errno == EWOULDBLOCK)
00191 {
00192 msg[0] = '\0' ;
00193 return 0 ;
00194 }
00195 else
00196 return -1;
00197 }
00198 else
00199 {
00200 m_sock.serv_addr.sin_port = serv_addr.sin_port ;
00201 msg[n] = '\0' ;
00202
00203 return ( n == 0 ) ? 0 : 1 ;
00204 }
00205 }
00206
00210 bool Connection::sendMessage( const char *msg )
00211 {
00212 int n;
00213
00214 n = strlen(msg) ;
00215 if( sendto(m_sock.socketfd, msg, n, 0,
00216 (struct sockaddr *)&m_sock.serv_addr, sizeof(m_sock.serv_addr)) != n )
00217 return false ;
00218 return true ;
00219 }
00220
00227 int Connection::message_loop( FILE *fpin, FILE *fpout )
00228 {
00229 fd_set readfds, readfds_bak;
00230 int in, max_fd, n, ret;
00231 char buf[m_iMaxMsgSize];
00232
00233 in = fileno( fpin );
00234 FD_ZERO( &readfds );
00235 FD_SET( in, &readfds );
00236 readfds_bak = readfds;
00237 max_fd = ((in > m_sock.socketfd) ? in : m_sock.socketfd) = 1;
00238
00239 while( 1 )
00240 {
00241 readfds = readfds_bak;
00242
00243 if(( ret = select( max_fd, &readfds, NULL, NULL, NULL )) < 0 )
00244 {
00245 perror("select");
00246 break;
00247 }
00248 else if( ret != 0 )
00249 {
00250 if( FD_ISSET(in, &readfds))
00251 {
00252 fgets(buf, m_iMaxMsgSize, fpin);
00253 if( sendMessage(buf ) == false )
00254 break;
00255 }
00256 if( FD_ISSET(m_sock.socketfd, &readfds ) )
00257 {
00258 n = receiveMessage(buf, m_iMaxMsgSize);
00259 if( n == -1 )
00260 break;
00261 else if( n > 0 )
00262 {
00263 fputs(buf, fpout);
00264 fputc( '\n', fpout);
00265 }
00266 fflush(stdout);
00267 }
00268 }
00269 }
00270 return 0;
00271 }
00272
00273
00276 void Connection::show( ostream os )
00277 {
00278 if( ! isConnected() )
00279 os << "Not connected" << endl;
00280 else
00281 os << "Connected" << endl;
00282
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303