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

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