Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

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