TCP_Socket.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.4 $
00022 // $Date: 2005/11/23 23:43:47 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/actor/channel/TCP_Socket.cpp,v $
00024                                                                         
00025                                                                         
00026 // Written: fmk
00027 // Created: 11/96
00028 // Revision: A
00029 //
00030 // Purpose: This file contains the implementation of the methods needed
00031 // to define the TCP_Socket class interface.
00032 //
00033 // What: "@(#) TCP_Socket.C, revA"
00034 
00035 #include "TCP_Socket.h"
00036 #include <string.h>
00037 #include <Matrix.h>
00038 #include <Vector.h>
00039 #include <ID.h>
00040 #include <Message.h>
00041 #include <ChannelAddress.h>
00042 #include <MovableObject.h>
00043 
00044 static int GetHostAddr(char *host, char *IntAddr);
00045 static void inttoa(unsigned int no, char *string, int *cnt);
00046 
00047 // TCP_Socket(unsigned int other_Port, char *other_InetAddr): 
00048 //      constructor to open a socket with my inet_addr and with a port number 
00049 //      given by the OS. 
00050 
00051 TCP_Socket::TCP_Socket()
00052   :myPort(0), connectType(0)
00053 {
00054     // set up my_Addr 
00055     bzero((char *) &my_Addr, sizeof(my_Addr));    
00056     my_Addr.addr_in.sin_family = AF_INET;
00057     my_Addr.addr_in.sin_port = htons(0);
00058 
00059     #ifdef _WIN32
00060       my_Addr.addr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
00061     #else
00062       my_Addr.addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
00063     #endif
00064 
00065     addrLength = sizeof(my_Addr.addr_in);
00066     
00067     // open a socket
00068     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00069         opserr << "TCP_Socket::TCP_Socket - could not open socket\n";
00070     }
00071 
00072     // bind local address to it
00073     if (bind(sockfd, (struct sockaddr *) &my_Addr.addr_in, sizeof(my_Addr.addr_in)) < 0) {
00074         opserr << "TCP_Socket::TCP_Socket - could not bind local address\n";
00075     }
00076     
00077     // get my_address info
00078     getsockname(sockfd, &my_Addr.addr, &addrLength);
00079     myPort = ntohs(my_Addr.addr_in.sin_port);
00080 }    
00081 
00082 
00083 
00084 
00085 
00086 // TCP_Socket(unsigned int port): 
00087 //      constructor to open a socket with my inet_addr and with a port number port.
00088 
00089 TCP_Socket::TCP_Socket(unsigned int port) 
00090   :myPort(0), connectType(0)
00091 {
00092     // set up my_Addr.addr_in with address given by port and internet address of
00093     // machine on which the process that uses this routine is running.
00094 
00095     // set up my_Addr 
00096     bzero((char *) &my_Addr.addr_in, sizeof(my_Addr.addr_in));
00097     my_Addr.addr_in.sin_family = AF_INET;
00098     my_Addr.addr_in.sin_port = htons(port);
00099 
00100     #ifdef _WIN32
00101       my_Addr.addr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
00102     #else
00103       my_Addr.addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
00104     #endif
00105 
00106     addrLength = sizeof(my_Addr.addr_in);
00107 
00108     // open a socket
00109     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00110         opserr << "TCP_Socket::TCP_Socket - could not open socket\n";
00111     }
00112     
00113     // bind local address to it
00114     if (bind(sockfd,&my_Addr.addr, sizeof(my_Addr.addr_in)) < 0) {
00115         opserr << "TCP_Socket::TCP_Socket - could not bind local address\n";
00116     }    
00117 
00118     // get my_address info
00119     getsockname(sockfd, &my_Addr.addr, &addrLength);
00120     myPort = ntohs(my_Addr.addr_in.sin_port);    
00121 }
00122 
00123 
00124 
00125 // TCP_Socket(unsigned int other_Port, char *other_InetAddr): 
00126 //      constructor to open a socket with my inet_addr and with a port number 
00127 //      given by the OS. Then to connect with a TCP_Socket whose address is
00128 //      given by other_Port and other_InetAddr. 
00129 
00130 TCP_Socket::TCP_Socket(unsigned int other_Port, char *other_InetAddr)
00131   :myPort(0), connectType(1)
00132 {
00133     // set up remote address
00134     bzero((char *) &other_Addr.addr_in, sizeof(other_Addr.addr_in));
00135     other_Addr.addr_in.sin_family      = AF_INET;
00136     other_Addr.addr_in.sin_port        = htons(other_Port);
00137 
00138     #ifdef _WIN32
00139       other_Addr.addr_in.sin_addr.S_un.S_addr = inet_addr(other_InetAddr);
00140     #else
00141       other_Addr.addr_in.sin_addr.s_addr = inet_addr(other_InetAddr);
00142     #endif
00143 
00144     // set up my_Addr.addr_in 
00145     bzero((char *) &my_Addr.addr_in, sizeof(my_Addr.addr_in));    
00146     my_Addr.addr_in.sin_family = AF_INET;
00147     my_Addr.addr_in.sin_port = htons(0);
00148 
00149     #ifdef _WIN32
00150       my_Addr.addr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
00151     #else
00152       my_Addr.addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
00153     #endif
00154 
00155     addrLength = sizeof(my_Addr.addr_in);
00156     
00157     // open a socket
00158     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00159         opserr << "TCP_Socket::TCP_Socket - could not open socket\n";
00160     }
00161 
00162     // bind local address to it
00163     if (bind(sockfd, (struct sockaddr *) &my_Addr.addr_in,sizeof(my_Addr.addr_in)) < 0) {
00164         opserr << "TCP_Socket::TCP_Socket - could not bind local address\n";
00165     }
00166     myPort = ntohs(my_Addr.addr_in.sin_port);    
00167 }    
00168 
00169 
00170 // ~TCP_Socket():
00171 //      destructor
00172 
00173 TCP_Socket::~TCP_Socket()
00174 {
00175   #ifdef _WIN32
00176     closesocket(sockfd);
00177   #else
00178     close(sockfd);
00179   #endif
00180 }
00181 
00182 
00183 
00184 int 
00185 TCP_Socket::setUpConnection(void)
00186 {
00187   if (connectType == 1) {
00188 
00189     // now try to connect to socket with remote address.
00190     if (connect(sockfd, (struct sockaddr *) &other_Addr.addr_in, 
00191                 sizeof(other_Addr.addr_in))< 0) {
00192       
00193         opserr << "TCP_Socket::TCP_Socket - could not connect\n";
00194         return -1;
00195     }
00196     // get my_address info
00197     getsockname(sockfd, &my_Addr.addr, &addrLength);
00198     
00199 
00200   } else {
00201     
00202     // wait for other process to contact me & set up connection
00203     socket_type newsockfd;
00204     listen(sockfd, 1);    
00205     newsockfd = accept(sockfd, (struct sockaddr *) &other_Addr.addr_in, &addrLength);
00206     
00207     if (newsockfd < 0) {
00208       opserr << "TCP_Socket::TCP_Socket - could not accept connection\n";
00209       return -1;
00210     }    
00211     
00212     // close old socket & reset sockfd
00213     // we can close as we are not going to wait for others to connect
00214     #ifdef _WIN32
00215       closesocket(sockfd);
00216     #else
00217       close(sockfd);
00218     #endif
00219 
00220     sockfd = newsockfd;
00221     
00222     // get my_address info
00223     getsockname(sockfd, &my_Addr.addr, &addrLength);
00224     myPort = ntohs(my_Addr.addr_in.sin_port);    
00225   }    
00226 
00227   // set socket so no delay    
00228   /*
00229     int optlen;
00230     optlen = 1;
00231     if ((setsockopt(sockfd,IPPROTO_TCP, TCP_NODELAY, 
00232     (char *) &optlen, sizeof(int))) < 0) { 
00233     opserr << "TCP_Socket::TCP_Socket - could not set TCP_NODELAY\n";
00234     }     
00235   */
00236   /*
00237     int flag=sizeof(int);
00238     if ((getsockopt(sockfd,IPPROTO_TCP, TCP_NODELAY, 
00239     (char *) &optlen, &flag)) < 0) { 
00240     opserr << "TCP_Socket::TCP_Socket - could not set TCP_NODELAY\n";
00241     }           
00242     opserr << "TCP_Socket::TCP_Socket - " << optlen << " flag " << flag <<  endln;
00243   */
00244     
00245 
00246   return 0;
00247 }    
00248 
00249 int
00250 TCP_Socket::setNextAddress(const ChannelAddress &theAddress)
00251 {       
00252     SocketAddress *theSocketAddress = 0;
00253     if (theAddress.getType() == SOCKET_TYPE) {
00254             theSocketAddress = (SocketAddress *)(&theAddress);    
00255             // check address is the only address a TCP_socket can send to
00256             if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr, 
00257                      theSocketAddress->addrLength) != 0) {
00258                 
00259                 opserr << "TCP_Socket::recvMsg() - a TCP_Socket ";
00260                 opserr << "can only communicate with one other TCP_Socket\n"; 
00261                 return -1;
00262             }
00263         }
00264     else {
00265         opserr << "TCP_Socket::setNextAddress() - a TCP_Socket ";
00266         opserr << "can only communicate with a TCP_Socket";
00267         opserr << " address given is not of type SocketAddress\n"; 
00268         return -1;          
00269     }                   
00270         
00271     return 0;
00272 }
00273 
00274 
00275 
00276 int 
00277 TCP_Socket::sendObj(int commitTag,
00278                     MovableObject &theObject, 
00279                     ChannelAddress *theAddress) 
00280 {
00281     // first check address is the only address a TCP_socket can send to
00282     SocketAddress *theSocketAddress = 0;
00283     if (theAddress != 0) {
00284         if (theAddress->getType() == SOCKET_TYPE) 
00285             theSocketAddress = (SocketAddress *)theAddress;
00286         else {
00287             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00288             opserr << "can only communicate with a TCP_Socket";
00289             opserr << " address given is not of type SocketAddress\n"; 
00290             return -1;      
00291         }                   
00292             
00293         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr, 
00294                  theSocketAddress->addrLength) != 0) {
00295 
00296             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00297             opserr << "can only communicate with one other TCP_Socket";
00298             opserr << " address given is not that address\n"; 
00299             return -1;      
00300         }       
00301     }    
00302     return theObject.sendSelf(commitTag, *this);
00303 }
00304 
00305 int 
00306 TCP_Socket::recvObj(int commitTag,
00307                     MovableObject &theObject, 
00308                     FEM_ObjectBroker &theBroker, 
00309                     ChannelAddress *theAddress)
00310 {
00311     // first check address is the only address a TCP_socket can send to
00312     SocketAddress *theSocketAddress = 0;
00313     if (theAddress != 0) {
00314         if (theAddress->getType() == SOCKET_TYPE) 
00315             theSocketAddress = (SocketAddress *)theAddress;
00316         else {
00317             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00318             opserr << "can only communicate with a TCP_Socket";
00319             opserr << " address given is not of type SocketAddress\n"; 
00320             return -1;      
00321         }                   
00322         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr, 
00323              theSocketAddress->addrLength) != 0) {
00324 
00325             opserr << "TCP_Socket::recvMsg() - a TCP_Socket ";
00326             opserr << "can only communicate with one other TCP_Socket\n"; 
00327             return -1;
00328         }
00329     }
00330     return theObject.recvSelf(commitTag, *this, theBroker);
00331 }
00332 
00333 
00334 // void Recv(Message &):
00335 //      Method to receive a message, also sets other_Addr.addr_in to that of sender
00336 
00337 int 
00338 TCP_Socket::recvMsg(int dbTag, int commitTag,
00339                     Message &msg, ChannelAddress *theAddress)
00340 {       
00341     // first check address is the only address a TCP_socket can send to
00342     SocketAddress *theSocketAddress = 0;
00343     if (theAddress != 0) {
00344         if (theAddress->getType() == SOCKET_TYPE) 
00345             theSocketAddress = (SocketAddress *)theAddress;
00346         else {
00347             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00348             opserr << "can only communicate with a TCP_Socket";
00349             opserr << " address given is not of type SocketAddress\n"; 
00350             return -1;      
00351         }                   
00352         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00353              theSocketAddress->addrLength) != 0) {
00354 
00355             opserr << "TCP_Socket::recvMsg() - a TCP_Socket ";
00356             opserr << "can only communicate with one other TCP_Socket\n"; 
00357             return -1;
00358         }
00359     }
00360 
00361     // if o.k. get a ponter to the data in the message and 
00362     // place the incoming data there
00363     int nleft,nread;
00364     char *gMsg;
00365     gMsg = msg.data;
00366     nleft = msg.length;
00367 
00368     while (nleft > 0) {
00369         nread = recv(sockfd,gMsg,nleft,0);
00370         nleft -= nread;
00371         gMsg +=  nread;
00372     }
00373     return 0;
00374 }
00375 
00376 
00377 // void Send(Message &):
00378 //      Method to send a message to an address given by other_Addr.addr_in.
00379 
00380 int 
00381 TCP_Socket::sendMsg(int dbTag, int commitTag,
00382                     const Message &msg, ChannelAddress *theAddress)
00383 {       
00384     // first check address is the only address a TCP_socket can send to
00385     SocketAddress *theSocketAddress = 0;
00386     if (theAddress != 0) {
00387         if (theAddress->getType() == SOCKET_TYPE) 
00388             theSocketAddress = (SocketAddress *)theAddress;
00389         else {
00390             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00391             opserr << "can only communicate with a TCP_Socket";
00392             opserr << " address given is not of type SocketAddress\n"; 
00393             return -1;      
00394         }                   
00395         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00396              theSocketAddress->addrLength) != 0) {
00397 
00398             opserr << "TCP_Socket::recvMsg() - a TCP_Socket ";
00399             opserr << "can only communicate with one other TCP_Socket\n"; 
00400             return -1;
00401         }
00402     }
00403 
00404     // if o.k. get a ponter to the data in the message and 
00405     // place the incoming data there
00406     int nwrite, nleft;    
00407     char *gMsg;
00408     gMsg = msg.data;
00409     nleft = msg.length;
00410 
00411     while (nleft > 0) {
00412         nwrite = send(sockfd,gMsg,nleft,0);
00413         nleft -= nwrite;
00414 
00415         gMsg +=  nwrite;
00416     }
00417     return 0;
00418 }
00419 
00420 
00421 
00422 int 
00423 TCP_Socket::recvMatrix(int dbTag, int commitTag,
00424                        Matrix &theMatrix, ChannelAddress *theAddress)
00425                     
00426 {       
00427     // first check address is the only address a TCP_socket can send to
00428     SocketAddress *theSocketAddress = 0;
00429     if (theAddress != 0) {
00430         if (theAddress->getType() == SOCKET_TYPE) 
00431             theSocketAddress = (SocketAddress *)theAddress;
00432         else {
00433             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00434             opserr << "can only communicate with a TCP_Socket";
00435             opserr << " address given is not of type SocketAddress\n"; 
00436             return -1;      
00437         }                   
00438         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00439              theSocketAddress->addrLength) != 0) {
00440 
00441             opserr << "TCP_Socket::recvMatrix() - a TCP_Socket ";
00442             opserr << "can only communicate with one other TCP_Socket\n"; 
00443             return -1;
00444         }
00445     }
00446 
00447     // if o.k. get a ponter to the data in the Matrix and 
00448     // place the incoming data there
00449     int nleft,nread;
00450     double *data = theMatrix.data;
00451     char *gMsg = (char *)data;;
00452     nleft =  theMatrix.dataSize * sizeof(double);
00453 
00454     while (nleft > 0) {
00455         nread = recv(sockfd,gMsg,nleft,0);
00456         nleft -= nread;
00457         gMsg +=  nread;
00458     }
00459     return 0;
00460 }
00461 
00462 
00463 // void Send(Matrix &):
00464 //      Method to send a Matrix to an address given by other_Addr.addr_in.
00465 
00466 int 
00467 TCP_Socket::sendMatrix(int dbTag, int commitTag,
00468                        const Matrix &theMatrix, ChannelAddress *theAddress)
00469 {       
00470     // first check address is the only address a TCP_socket can send to
00471     SocketAddress *theSocketAddress = 0;
00472     if (theAddress != 0) {
00473         if (theAddress->getType() == SOCKET_TYPE) 
00474             theSocketAddress = (SocketAddress *)theAddress;
00475         else {
00476             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00477             opserr << "can only communicate with a TCP_Socket";
00478             opserr << " address given is not of type SocketAddress\n"; 
00479             return -1;      
00480         }                       SocketAddress *theSocketAddress = 0;
00481 
00482         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00483              theSocketAddress->addrLength) != 0) {
00484 
00485             opserr << "TCP_Socket::recvMatrix() - a TCP_Socket ";
00486             opserr << "can only communicate with one other TCP_Socket\n"; 
00487             return -1;
00488         }
00489     }
00490 
00491     // if o.k. get a ponter to the data in the Matrix and 
00492     // place the incoming data there
00493     int nwrite, nleft;    
00494     double *data = theMatrix.data;
00495     char *gMsg = (char *)data;
00496     nleft =  theMatrix.dataSize * sizeof(double);
00497 
00498     while (nleft > 0) {
00499         nwrite = send(sockfd,gMsg,nleft,0);
00500         nleft -= nwrite;
00501         
00502         gMsg +=  nwrite;
00503     }
00504     return 0;
00505 }
00506 
00507 
00508 
00509 
00510 
00511 
00512 
00513 
00514 int 
00515 TCP_Socket::recvVector(int dbTag, int commitTag,
00516                        Vector &theVector, ChannelAddress *theAddress)
00517                     
00518 {       
00519     // first check address is the only address a TCP_socket can send to
00520     SocketAddress *theSocketAddress = 0;
00521     if (theAddress != 0) {
00522         if (theAddress->getType() == SOCKET_TYPE) 
00523             theSocketAddress = (SocketAddress *)theAddress;
00524         else {
00525             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00526             opserr << "can only communicate with a TCP_Socket";
00527             opserr << " address given is not of type SocketAddress\n"; 
00528             return -1;      
00529         }               
00530         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00531              theSocketAddress->addrLength) != 0) {
00532 
00533             opserr << "TCP_Socket::recvVector() - a TCP_Socket ";
00534             opserr << "can only communicate with one other TCP_Socket\n"; 
00535             return -1;
00536         }
00537     }
00538 
00539     // if o.k. get a ponter to the data in the Vector and 
00540     // place the incoming data there
00541     int nleft,nread;
00542     double *data = theVector.theData;
00543     char *gMsg = (char *)data;;
00544     nleft =  theVector.sz * sizeof(double);
00545 
00546     while (nleft > 0) {
00547         nread = recv(sockfd,gMsg,nleft,0);
00548         nleft -= nread;
00549         gMsg +=  nread;
00550     }
00551     return 0;
00552 }
00553 
00554 
00555 // void Send(Vector &):
00556 //      Method to send a Vector to an address given by other_Addr.addr_in.
00557 
00558 int 
00559 TCP_Socket::sendVector(int dbTag, int commitTag,
00560                        const Vector &theVector, ChannelAddress *theAddress)
00561 {       
00562     // first check address is the only address a TCP_socket can send to
00563     SocketAddress *theSocketAddress = 0;
00564     if (theAddress != 0) {
00565         if (theAddress->getType() == SOCKET_TYPE) 
00566             theSocketAddress = (SocketAddress *)theAddress;
00567         else {
00568             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00569             opserr << "can only communicate with a TCP_Socket";
00570             opserr << " address given is not of type SocketAddress\n"; 
00571             return -1;      
00572         }               
00573         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00574              theSocketAddress->addrLength) != 0) {
00575 
00576             opserr << "TCP_Socket::recvVector() - a TCP_Socket ";
00577             opserr << "can only communicate with one other TCP_Socket\n"; 
00578             return -1;
00579         }
00580     }
00581 
00582     // if o.k. get a ponter to the data in the Vector and 
00583     // place the incoming data there
00584     int nwrite, nleft;    
00585     double *data = theVector.theData;
00586     char *gMsg = (char *)data;
00587     nleft =  theVector.sz * sizeof(double);
00588     
00589     while (nleft > 0) {
00590         nwrite = send(sockfd,gMsg,nleft,0);
00591         nleft -= nwrite;
00592         gMsg +=  nwrite;
00593     }
00594     return 0;
00595 }
00596 
00597 
00598 
00599 
00600 int 
00601 TCP_Socket::recvID(int dbTag, int commitTag,
00602                    ID &theID, ChannelAddress *theAddress)
00603                     
00604 {       
00605     // first check address is the only address a TCP_socket can send to
00606     SocketAddress *theSocketAddress = 0;
00607     if (theAddress != 0) {
00608         if (theAddress->getType() == SOCKET_TYPE) 
00609             theSocketAddress = (SocketAddress *)theAddress;
00610         else {
00611             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00612             opserr << "can only communicate with a TCP_Socket";
00613             opserr << " address given is not of type SocketAddress\n"; 
00614             return -1;      
00615         }               
00616         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00617              theSocketAddress->addrLength) != 0) {
00618 
00619             opserr << "TCP_Socket::recvID() - a TCP_Socket ";
00620             opserr << "can only communicate with one other TCP_Socket\n"; 
00621             return -1;
00622         }
00623     }
00624 
00625     // if o.k. get a ponter to the data in the ID and 
00626     // place the incoming data there
00627     int nleft,nread;
00628     int *data = theID.data;
00629     char *gMsg = (char *)data;;
00630     nleft =  theID.sz * sizeof(int);
00631 
00632     while (nleft > 0) {
00633         nread = recv(sockfd,gMsg,nleft,0);
00634         nleft -= nread;
00635         gMsg +=  nread;
00636     }
00637     return 0;
00638 }
00639 
00640 
00641 // void Send(ID &):
00642 //      Method to send a ID to an address given by other_Addr.addr_in.
00643 
00644 int 
00645 TCP_Socket::sendID(int dbTag, int commitTag,
00646                    const ID &theID, ChannelAddress *theAddress)
00647 {       
00648     // first check address is the only address a TCP_socket can send to
00649     SocketAddress *theSocketAddress = 0;
00650     if (theAddress != 0) {
00651         if (theAddress->getType() == SOCKET_TYPE) 
00652             theSocketAddress = (SocketAddress *)theAddress;
00653         else {
00654             opserr << "TCP_Socket::sendObj() - a TCP_Socket ";
00655             opserr << "can only communicate with a TCP_Socket";
00656             opserr << " address given is not of type SocketAddress\n"; 
00657             return -1;      
00658         }               
00659         if (bcmp((char *) &other_Addr.addr_in, (char *) &theSocketAddress->address.addr_in, 
00660              theSocketAddress->addrLength) != 0) {
00661 
00662             opserr << "TCP_Socket::recvID() - a TCP_Socket ";
00663             opserr << "can only communicate with one other TCP_Socket\n"; 
00664             return -1;
00665         }
00666     }
00667 
00668     // if o.k. get a ponter to the data in the ID and 
00669     // place the incoming data there
00670     int nwrite, nleft;    
00671     int *data = theID.data;
00672     char *gMsg = (char *)data;
00673     nleft =  theID.sz * sizeof(int);
00674     
00675     while (nleft > 0) {
00676         nwrite = send(sockfd,gMsg,nleft,0);
00677         nleft -= nwrite;
00678         gMsg +=  nwrite;
00679     }
00680     return 0;
00681 }
00682 
00683 
00684 
00685 
00686 
00687 
00688 unsigned int 
00689 TCP_Socket::getPortNumber(void) const
00690 {
00691     return myPort;
00692 }
00693 
00694 
00695 char *
00696 TCP_Socket::addToProgram(void)
00697 {
00698     char *tcp = " 1 ";
00699 
00700     char  my_InetAddr[MAX_INET_ADDR];
00701     char  myPortNum[8];
00702     char  me[30];
00703     unsigned int thePort = this->getPortNumber();
00704    /*
00705     char *me =(char *)malloc(30*sizeof(char));
00706     char *my_InetAddr=(char *)malloc(30*sizeof(char));
00707     char *myPortNum = (char *)malloc(30*sizeof(char));
00708     for (int i=0; i<30; i++) {
00709         me[i] = ' ';
00710         my_InetAddr[i] = ' ';
00711         myPortNum[i] = ' ';
00712     }
00713     */
00714     int start = 0;
00715     inttoa(thePort,myPortNum,&start);
00716     gethostname(me,MAX_INET_ADDR);
00717     GetHostAddr(me,my_InetAddr);
00718 
00719     char *newStuff =(char *)malloc(100*sizeof(char));
00720     for (int i=0; i<100; i++) 
00721         newStuff[i] = ' ';
00722     
00723     strcpy(newStuff,tcp);
00724     strcat(newStuff," ");          
00725     strcat(newStuff,my_InetAddr);
00726     strcat(newStuff," ");
00727     strcat(newStuff,myPortNum);
00728     strcat(newStuff," ");    
00729 
00730     return newStuff;
00731 }
00732 
00733 
00734 // G e t H o s t A d d r
00735 //      GetHostAddr is a function to get the internet address of a host
00736 //      Takes machine name host & Returns 0 if o.k,  -1 if gethostbyname 
00737 //      error, -2 otherwise. The internet address is returned in IntAddr
00738 
00739 static int GetHostAddr(char *host, char *IntAddr)
00740 {
00741     register struct hostent *hostptr;
00742 
00743     if ( (hostptr = gethostbyname(host)) == NULL) 
00744         return (-1);
00745 
00746     switch(hostptr->h_addrtype) {
00747       case AF_INET:
00748         strcpy(IntAddr,inet_ntoa(*(struct in_addr *)*hostptr->h_addr_list));
00749         return (0);
00750         
00751       default:
00752         return (-2);
00753     }
00754 }
00755 
00756     
00757 /*
00758  *  i n t t o a
00759  *
00760  *  Function to convert int to ascii
00761  *  
00762  */
00763 
00764 static void inttoa(unsigned int no, char *string, int *cnt) {
00765     if (no /10) {
00766         inttoa(no/10, string, cnt);
00767         *cnt = *cnt+1;
00768     }
00769     string[*cnt] = no % 10 + '0';
00770 }
00771 

Generated on Mon Oct 23 15:04:56 2006 for OpenSees by doxygen 1.5.0