1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "SALOME_Comm_i.hxx"
26 #include "omniORB4/poa.h"
27 #include "omnithread.h"
28 #include "Utils_SINGLETON.hxx"
29 #include "Utils_ORB_INIT.hxx"
30 #include "utilities.h"
32 #include "SenderFactory.hxx"
36 CORBA::ORB_var &getGlobalORB(){
37 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
38 CORBA::ORB_var &orb = init(0,0);
43 /*! Return the C++ data associated to the array to transmit.
44 Used when sender and receiver are collocalized.
46 const void *SALOME_Sender_i::getData(long &size) const{
51 /*! Return the sizeof() of each component of the generic array
53 int SALOME_Sender_i::getSizeOf() const {
57 /*! Unique constructor */
58 SALOME_Sender_i::SALOME_Sender_i(const void *tabToSend,long lgrTabToSend,int sizeOf,bool ownTabToSend):_tabToSend(tabToSend),_lgrTabToSend(lgrTabToSend),_sizeOf(sizeOf),_ownTabToSend(ownTabToSend){
61 /*! To force ownerShip status */
62 void SALOME_Sender_i::setOwnerShip(bool own)
67 /*! Method for the remote destroy of the current servant. This method is used by the receiver to destroy the sender when the transfert is complete.
69 void SALOME_Sender_i::release()
71 PortableServer::ObjectId_var oid = _default_POA()->servant_to_id(this);
72 _default_POA()->deactivate_object(oid);
76 SALOME_SenderDouble_i::SALOME_SenderDouble_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
82 SALOME_SenderDouble_i::~SALOME_SenderDouble_i()
85 delete [] (double *)_tabToSend;
88 /*! Return a new sender of the same array but with an another protocol and delegates to the returned sender the ownership of array.
90 SALOME::SenderDouble_ptr SALOME_SenderDouble_i::buildOtherWithProtocol(SALOME::TypeOfCommunication type)
92 return SenderFactory::buildSender(type,this);
95 /*! Method to establish if the CORBA object refered by pCorba is collocalised.\n
96 If it is, the pointer to the servant that incarnates the CORBA object is returned.
98 SALOME_SenderDouble_i *SALOME_SenderDouble_i::find(SALOME::SenderDouble_ptr pCorba){
99 PortableServer::ServantBase *ret;
101 ret=PortableServer::POA::_the_root_poa()->reference_to_servant(pCorba);
107 return dynamic_cast<SALOME_SenderDouble_i *>(ret);
110 SALOME_SenderInt_i::SALOME_SenderInt_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
116 SALOME_SenderInt_i::~SALOME_SenderInt_i()
119 delete [] (int *)_tabToSend;
122 /*! Return a new sender of the same array but with an another protocol.
124 SALOME::SenderInt_ptr SALOME_SenderInt_i::buildOtherWithProtocol(SALOME::TypeOfCommunication type)
126 return SenderFactory::buildSender(type,this);
129 /*! Method to establish if the CORBA object refered by pCorba is collocalised.\n
130 If it is, the pointer to the servant that incarnates the CORBA object is returned.
132 SALOME_SenderInt_i *SALOME_SenderInt_i::find(SALOME::SenderInt_ptr pCorba){
133 PortableServer::ServantBase *ret;
135 ret=PortableServer::POA::_the_root_poa()->reference_to_servant(pCorba);
141 return dynamic_cast<SALOME_SenderInt_i *>(ret);
144 SALOME_CorbaDoubleNCSender_i::SALOME_CorbaDoubleNCSender_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_SenderDouble_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend){
147 SALOME_CorbaDoubleNCSender_i::~SALOME_CorbaDoubleNCSender_i(){
150 CORBA::ULong SALOME_CorbaDoubleNCSender_i::getSize(){
151 CORBA::ULong ret=_lgrTabToSend;
155 SALOME::vectorOfDouble* SALOME_CorbaDoubleNCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
156 SALOME::vectorOfDouble_var c1 = new SALOME::vectorOfDouble(length,length,(CORBA::Double *)((double *)_tabToSend+(long)offset),0);
160 SALOME::vectorOfDouble* SALOME_CorbaDoubleNCSender_i::send(){
161 SALOME::vectorOfDouble_var c1 = new SALOME::vectorOfDouble(_lgrTabToSend,_lgrTabToSend,(CORBA::Double *)_tabToSend,0);
165 SALOME_CorbaDoubleCSender_i::SALOME_CorbaDoubleCSender_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_SenderDouble_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend){
168 SALOME_CorbaDoubleCSender_i::~SALOME_CorbaDoubleCSender_i(){
171 CORBA::ULong SALOME_CorbaDoubleCSender_i::getSize(){
172 CORBA::ULong ret=_lgrTabToSend;
176 SALOME::vectorOfDouble* SALOME_CorbaDoubleCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
177 SALOME::vectorOfDouble_var c1 = new SALOME::vectorOfDouble;
179 for (long i=0; i<length; i++)
180 c1[i] = ((double *)_tabToSend)[i+offset];
184 ////////////////////////
186 SALOME_CorbaLongNCSender_i::SALOME_CorbaLongNCSender_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_SenderInt_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend){
189 SALOME_CorbaLongNCSender_i::~SALOME_CorbaLongNCSender_i(){
192 CORBA::ULong SALOME_CorbaLongNCSender_i::getSize(){
193 CORBA::ULong ret=_lgrTabToSend;
197 SALOME::vectorOfLong* SALOME_CorbaLongNCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
198 SALOME::vectorOfLong_var c1 = new SALOME::vectorOfLong(length,length,(CORBA::Long *)((long *)_tabToSend+(long)offset),0);
202 SALOME::vectorOfLong* SALOME_CorbaLongNCSender_i::send(){
203 SALOME::vectorOfLong_var c1 = new SALOME::vectorOfLong(_lgrTabToSend,_lgrTabToSend,(CORBA::Long *)_tabToSend,0);
207 SALOME_CorbaLongCSender_i::SALOME_CorbaLongCSender_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_SenderInt_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend){
210 SALOME_CorbaLongCSender_i::~SALOME_CorbaLongCSender_i(){
213 CORBA::ULong SALOME_CorbaLongCSender_i::getSize(){
214 CORBA::ULong ret=_lgrTabToSend;
218 SALOME::vectorOfLong* SALOME_CorbaLongCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
219 SALOME::vectorOfLong_var c1 = new SALOME::vectorOfLong;
221 for (long i=0; i<length; i++)
222 c1[i] = ((long *)_tabToSend)[i+offset];
228 unsigned long SALOME_MPISender_i::_tag1=0;
230 unsigned long SALOME_MPISender_i::_tag2=1;
232 SALOME_MPISender_i::SALOME_MPISender_i(const void *tabToSend,long lgrTabToSend,int sizeOf,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeOf,ownTabToSend){
233 _portName=new char[MPI_MAX_PORT_NAME];
236 SALOME_MPISender_i::~SALOME_MPISender_i(){
240 SALOME::MPISender::param* SALOME_MPISender_i::getParam()
245 SALOME::MPISender::param_var p = new SALOME::MPISender::param;
246 MPI_Comm_rank(MPI_COMM_WORLD,&_cproc);
252 std::string service("toto_");
253 sprintf(stag,"%d_",_tag1);
255 sprintf(stag,"%d_",p->tag2);
257 p->service = CORBA::string_dup(service.c_str());
258 MPI_Open_port(MPI_INFO_NULL, _portName);
259 MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN);
260 while ( i != TIMEOUT && MPI_Publish_name((char*)service.c_str(),MPI_INFO_NULL,_portName) != MPI_SUCCESS) {
263 MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_ARE_FATAL);
264 if ( i == TIMEOUT ) {
265 MPI_Close_port(_portName);
274 void SALOME_MPISender_i::send()
276 _type=getTypeOfDataTransmitted();
277 _argsForThr=new void *[8];
278 _argsForThr[0]=_portName;
279 _argsForThr[1]=&_lgrTabToSend;
280 _argsForThr[2]=(void *)_tabToSend;
281 _argsForThr[3]=&_cproc;
282 _argsForThr[4]=&_tag1Inst;
283 _argsForThr[5]=&_tag2Inst;
284 _argsForThr[6]=&_com;
285 _argsForThr[7]=&_type;
287 _newThr=new omni_thread(SALOME_MPISender_i::myThread,_argsForThr);
291 void* SALOME_MPISender_i::myThread(void *args)
293 void **argsTab=(void **)args;
294 long *lgrTabToSend=(long *)argsTab[1];
295 int *cproc=(int *)argsTab[3];
296 int *tag1=(int *)argsTab[4];
297 int *tag2=(int *)argsTab[5];
298 MPI_Comm *com=(MPI_Comm *)argsTab[6];
299 SALOME::TypeOfDataTransmitted *type=(SALOME::TypeOfDataTransmitted *)argsTab[7];
301 MPI_Comm_accept((char *)argsTab[0],MPI_INFO_NULL,0,MPI_COMM_SELF,com);
302 MPI_Send(lgrTabToSend,1,MPI_LONG,*cproc,*tag1,*com);
305 case SALOME::DOUBLE_:
306 MPI_Send(argsTab[2],*lgrTabToSend,MPI_DOUBLE,*cproc,*tag2,*com);
309 MPI_Send(argsTab[2],*lgrTabToSend,MPI_INT,*cproc,*tag2,*com);
315 void SALOME_MPISender_i::close(const SALOME::MPISender::param& p)
317 std::string service(p.service);
318 const char *st=p.service;
321 MPI_Comm_free(&_com);
322 MPI_Unpublish_name((char *)service.c_str(),MPI_INFO_NULL,_portName);
323 MPI_Close_port(_portName);
324 delete [] _argsForThr;
327 SALOME_MPISenderDouble_i::SALOME_MPISenderDouble_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend)
328 :SALOME_SenderDouble_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_MPISender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
329 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
333 SALOME_MPISenderInt_i::SALOME_MPISenderInt_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend)
334 :SALOME_SenderInt_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_MPISender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
335 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
344 #define _POSIX_PII_SOCKET
345 #define _LIBC_POLLUTION_H_
347 #include <sys/types.h>
348 #include <sys/socket.h>
349 #include <netinet/in.h>
350 #include <arpa/inet.h>
354 SALOME_SocketSender_i::SALOME_SocketSender_i(const void *tabToSend,long lgrTabToSend,int sizeOf,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeOf,ownTabToSend){
355 _IPAddress = inetAddress();
360 SALOME_SocketSender_i::~SALOME_SocketSender_i(){
363 std::string SALOME_SocketSender_i::inetAddress()
366 char t[INET_ADDRSTRLEN+1];
367 struct hostent *host;
368 struct in_addr saddr;
374 saddr.s_addr = inet_addr(s);
375 if (saddr.s_addr != -1)
376 inet_ntop(AF_INET, &saddr, t, INET_ADDRSTRLEN);
378 host = gethostbyname(s);
380 inet_ntop(AF_INET, (struct in_addr *) *host->h_addr_list,
383 return std::string(t);
386 SALOME::SocketSender::param * SALOME_SocketSender_i::getParam()
389 SALOME::SocketSender::param_var p = new SALOME::SocketSender::param;
392 p->lend = _lgrTabToSend - 1;
394 p->internet_address = CORBA::string_dup(_IPAddress.c_str());
399 void SALOME_SocketSender_i::send()
401 _type=getTypeOfDataTransmitted();
402 _argsForThr=new void *[6];
403 _argsForThr[0]=&_serverSockfd;
404 _argsForThr[1]=&_clientSockfd;
405 _argsForThr[2]=&_lgrTabToSend;
406 _argsForThr[3]=(void *)_tabToSend;
407 _argsForThr[4]=&_errorFlag;
408 _argsForThr[5]=&_type;
410 _newThr=new omni_thread(SALOME_SocketSender_i::myThread,_argsForThr);
414 void* SALOME_SocketSender_i::myThread(void *args)
417 void **argsTab=(void **)args;
418 int *serverSockfd=(int *)argsTab[0];
419 int *clientSockfd=(int *)argsTab[1];
420 long *lgrTabToSend=(long *)argsTab[2];
421 void *tabToSend=argsTab[3];
422 bool *errorFlag=(bool*)argsTab[4];
423 SALOME::TypeOfDataTransmitted *type=(SALOME::TypeOfDataTransmitted *)argsTab[5];
425 XDR xp; /* pointeur sur le decodeur XDR */
429 case SALOME::DOUBLE_:
430 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(double),XDR_ENCODE );
431 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(double), (xdrproc_t)xdr_double );
434 while( n < *lgrTabToSend*sizeof(double) ){
435 m = write(*clientSockfd, (char*)tabToSend+n, *lgrTabToSend*sizeof(double)-n);
437 if( *clientSockfd >= 0 ){
438 ::close(*clientSockfd);
441 if( *serverSockfd >= 0 ){
442 ::close(*serverSockfd);
451 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(double),XDR_DECODE );
452 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(double), (xdrproc_t)xdr_double );
456 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(int),XDR_ENCODE );
457 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(int), (xdrproc_t)xdr_int );
460 while( n < *lgrTabToSend*sizeof(int) ){
461 m = write(*clientSockfd, (char*)tabToSend+n, *lgrTabToSend*sizeof(int)-n);
463 if( *clientSockfd >= 0 ){
464 ::close(*clientSockfd);
467 if( *serverSockfd >= 0 ){
468 ::close(*serverSockfd);
477 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(int),XDR_DECODE );
478 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(int), (xdrproc_t)xdr_int );
484 void SALOME_SocketSender_i::initCom() throw(SALOME::SALOME_Exception)
486 struct sockaddr_in serv_addr;
488 SALOME::ExceptionStruct es;
490 /* Ouverture de la socket */
491 _serverSockfd = socket(AF_INET , SOCK_STREAM , 0);
492 if(_serverSockfd < 0) {
493 es.type = SALOME::COMM;
494 es.text = "error Socket exception";
495 throw SALOME::SALOME_Exception(es);
497 /* Socket structure initialisation*/
498 bzero((char*)&serv_addr,sizeof(serv_addr));
499 serv_addr.sin_family = AF_INET;
500 serv_addr.sin_port = 0; /* asking for a free port */
501 serv_addr.sin_addr.s_addr = INADDR_ANY;
503 /* Association of socket with a port */
504 if( ::bind(_serverSockfd, (struct sockaddr *) & serv_addr,
505 sizeof(struct sockaddr)) < 0 ) {
507 es.type = SALOME::COMM;
508 es.text = "error bind Socket exception";
509 throw SALOME::SALOME_Exception(es);
511 /* Listening to the allocated port */
512 if( listen(_serverSockfd, 10) < 0 ) {
514 es.type = SALOME::COMM;
515 es.text = "error listen Socket exception";
516 throw SALOME::SALOME_Exception(es);
518 /* Retrieving port number*/
519 if( getsockname(_serverSockfd, (struct sockaddr *) & serv_addr, &n) < 0 ){
521 es.type = SALOME::COMM;
522 es.text = "error getName Socket exception";
523 throw SALOME::SALOME_Exception(es);
525 _port = htons(serv_addr.sin_port);
529 void SALOME_SocketSender_i::acceptCom() throw(SALOME::SALOME_Exception)
532 struct sockaddr_in client_addr;
533 SALOME::ExceptionStruct es;
535 sin_size = sizeof(struct sockaddr_in);
537 _clientSockfd = accept(_serverSockfd, (struct sockaddr *)&client_addr, &sin_size);
538 if( _clientSockfd < 0 ){
540 es.type = SALOME::COMM;
541 es.text = "error accept Socket exception";
542 throw SALOME::SALOME_Exception(es);
546 void SALOME_SocketSender_i::closeCom()
548 if( _clientSockfd >= 0 ){
549 ::close(_clientSockfd);
552 if( _serverSockfd >= 0 ){
553 ::close(_serverSockfd);
559 void SALOME_SocketSender_i::endOfCom()
565 SALOME::ExceptionStruct es;
566 es.type = SALOME::COMM;
567 es.text = "error write Socket exception";
568 throw SALOME::SALOME_Exception(es);
570 delete [] _argsForThr;
573 SALOME_SocketSenderDouble_i::SALOME_SocketSenderDouble_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend)
574 :SALOME_SenderDouble_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_SocketSender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
575 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
579 SALOME_SocketSenderInt_i::SALOME_SocketSenderInt_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend)
580 :SALOME_SenderInt_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_SocketSender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
581 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
586 #undef _LIBC_POLLUTION_H_
587 #undef _POSIX_PII_SOCKET