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

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