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_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
00050
00051
00052
00053 TCP_SocketNoDelay::TCP_SocketNoDelay()
00054 :myPort(0)
00055 {
00056
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
00064 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00065 opserr << "TCP_SocketNoDelay::TCP_SocketNoDelay - could not open socket\n";
00066 }
00067
00068
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
00074 INET_getsockname(sockfd, &my_Addr, &addrLength);
00075 myPort = ntohs(my_Addr.sin_port);
00076
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086 TCP_SocketNoDelay::TCP_SocketNoDelay(unsigned int port)
00087 :myPort(0)
00088 {
00089
00090
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
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
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
00116 INET_getsockname(sockfd, &my_Addr, &addrLength);
00117 myPort = ntohs(my_Addr.sin_port);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127 TCP_SocketNoDelay::TCP_SocketNoDelay(unsigned int other_Port, char *other_InetAddr)
00128 :myPort(0)
00129 {
00130
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
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
00144 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00145 opserr << "TCP_SocketNoDelay::TCP_SocketNoDelay - could not open socket\n";
00146 }
00147
00148
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
00157
00158
00159 TCP_SocketNoDelay::~TCP_SocketNoDelay()
00160 {
00161 close(sockfd);
00162 }
00163
00164
00165
00166 int
00167 TCP_SocketNoDelay::setUpActor(void)
00168 {
00169
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
00177 INET_getsockname(sockfd, &my_Addr, &addrLength);
00178
00179
00180
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
00191
00192
00193
00194
00195
00196
00197
00198 return 0;
00199
00200 }
00201
00202
00203 int
00204 TCP_SocketNoDelay::setUpShadow(void)
00205 {
00206
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
00217 close(sockfd);
00218
00219 sockfd = newsockfd;
00220
00221
00222 INET_getsockname(sockfd, &my_Addr, &addrLength);
00223 myPort = ntohs(my_Addr.sin_port);
00224
00225
00226
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
00236
00237
00238
00239
00240
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
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
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
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
00333
00334
00335 int
00336 TCP_SocketNoDelay::recvMsg(Message &msg, ChannelAddress *theAddress)
00337 {
00338
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
00359
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
00375
00376
00377 int
00378 TCP_SocketNoDelay::sendMsg(const Message &msg, ChannelAddress *theAddress)
00379 {
00380
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
00401
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
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
00443
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
00459
00460
00461 int
00462 TCP_SocketNoDelay::sendMatrix(const Matrix &theMatrix, ChannelAddress *theAddress)
00463 {
00464
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
00486
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
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
00533
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
00549
00550
00551 int
00552 TCP_SocketNoDelay::sendVector(const Vector &theVector, ChannelAddress *theAddress)
00553 {
00554
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
00575
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
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
00617
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
00633
00634
00635 int
00636 TCP_SocketNoDelay::sendID(const ID &theID, ChannelAddress *theAddress)
00637 {
00638
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
00659
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
00697
00698
00699
00700
00701
00702
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
00727
00728
00729
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
00751
00752
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