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
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
00052
00053
00054
00055 TCP_Socket::TCP_Socket()
00056 :myPort(0)
00057 {
00058
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
00066 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00067 cerr << "TCP_Socket::TCP_Socket - could not open socket\n";
00068 }
00069
00070
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
00076 INET_getsockname(sockfd, &my_Addr, &addrLength);
00077 myPort = ntohs(my_Addr.sin_port);
00078
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088 TCP_Socket::TCP_Socket(unsigned int port)
00089 :myPort(0)
00090 {
00091
00092
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
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
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
00118 INET_getsockname(sockfd, &my_Addr, &addrLength);
00119 myPort = ntohs(my_Addr.sin_port);
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 TCP_Socket::TCP_Socket(unsigned int other_Port, char *other_InetAddr)
00130 :myPort(0)
00131 {
00132
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
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
00146 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00147 cerr << "TCP_Socket::TCP_Socket - could not open socket\n";
00148 }
00149
00150
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
00159
00160
00161 TCP_Socket::~TCP_Socket()
00162 {
00163 close(sockfd);
00164 }
00165
00166
00167
00168 int
00169 TCP_Socket::setUpActor(void)
00170 {
00171
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
00179 INET_getsockname(sockfd, &my_Addr, &addrLength);
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 return 0;
00201
00202 }
00203
00204
00205 int
00206 TCP_Socket::setUpShadow(void)
00207 {
00208
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
00219 close(sockfd);
00220
00221 sockfd = newsockfd;
00222
00223
00224 INET_getsockname(sockfd, &my_Addr, &addrLength);
00225 myPort = ntohs(my_Addr.sin_port);
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
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
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
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
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
00337
00338
00339 int
00340 TCP_Socket::recvMsg(int dbTag, int commitTag,
00341 Message &msg, ChannelAddress *theAddress)
00342 {
00343
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
00364
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
00380
00381
00382 int
00383 TCP_Socket::sendMsg(int dbTag, int commitTag,
00384 const Message &msg, ChannelAddress *theAddress)
00385 {
00386
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
00407
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
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
00450
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
00466
00467
00468 int
00469 TCP_Socket::sendMatrix(int dbTag, int commitTag,
00470 const Matrix &theMatrix, ChannelAddress *theAddress)
00471 {
00472
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
00494
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
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
00542
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
00558
00559
00560 int
00561 TCP_Socket::sendVector(int dbTag, int commitTag,
00562 const Vector &theVector, ChannelAddress *theAddress)
00563 {
00564
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
00585
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
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
00628
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
00644
00645
00646 int
00647 TCP_Socket::sendID(int dbTag, int commitTag,
00648 const ID &theID, ChannelAddress *theAddress)
00649 {
00650
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
00671
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
00708
00709
00710
00711
00712
00713
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
00737
00738
00739
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
00761
00762
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