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