4 #include "SALOME_Comm_i.hxx"
6 #include "omnithread.h"
7 #include "Utils_SINGLETON.hxx"
8 #include "Utils_ORB_INIT.hxx"
11 #include "SenderFactory.hxx"
15 CORBA::ORB_var &getGlobalORB(){
16 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
17 CORBA::ORB_var &orb = init(0,0);
22 /*! Return the C++ data associated to the array to transmit.
23 Used when sender and receiver are collocalized.
25 const void *SALOME_Sender_i::getData(long &size) const{
30 /*! Return the sizeof() of each component of the generic array
32 int SALOME_Sender_i::getSizeOf() const {
36 /*! Unique constructor */
37 SALOME_Sender_i::SALOME_Sender_i(const void *tabToSend,long lgrTabToSend,int sizeOf,bool ownTabToSend):_tabToSend(tabToSend),_lgrTabToSend(lgrTabToSend),_sizeOf(sizeOf),_ownTabToSend(ownTabToSend){
40 /*! To force ownerShip status */
41 void SALOME_Sender_i::setOwnerShip(bool own)
46 /*! 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.
48 void SALOME_Sender_i::release()
50 PortableServer::ObjectId_var oid = _default_POA()->servant_to_id(this);
51 _default_POA()->deactivate_object(oid);
55 SALOME_SenderDouble_i::SALOME_SenderDouble_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
61 SALOME_SenderDouble_i::~SALOME_SenderDouble_i()
64 delete [] (double *)_tabToSend;
67 /*! Return a new sender of the same array but with an another protocol and delegates to the returned sender the ownership of array.
69 SALOME::SenderDouble_ptr SALOME_SenderDouble_i::buildOtherWithProtocol(SALOME::TypeOfCommunication type)
71 return SenderFactory::buildSender(type,this);
74 /*! Method to establish if the CORBA object refered by pCorba is collocalised.\n
75 If it is, the pointer to the servant that incarnates the CORBA object is returned.
77 SALOME_SenderDouble_i *SALOME_SenderDouble_i::find(SALOME::SenderDouble_ptr pCorba){
78 PortableServer::ServantBase *ret;
80 ret=PortableServer::POA::_the_root_poa()->reference_to_servant(pCorba);
86 return dynamic_cast<SALOME_SenderDouble_i *>(ret);
89 SALOME_SenderInt_i::SALOME_SenderInt_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
95 SALOME_SenderInt_i::~SALOME_SenderInt_i()
98 delete [] (int *)_tabToSend;
101 /*! Return a new sender of the same array but with an another protocol.
103 SALOME::SenderInt_ptr SALOME_SenderInt_i::buildOtherWithProtocol(SALOME::TypeOfCommunication type)
105 return SenderFactory::buildSender(type,this);
108 /*! Method to establish if the CORBA object refered by pCorba is collocalised.\n
109 If it is, the pointer to the servant that incarnates the CORBA object is returned.
111 SALOME_SenderInt_i *SALOME_SenderInt_i::find(SALOME::SenderInt_ptr pCorba){
112 PortableServer::ServantBase *ret;
114 ret=PortableServer::POA::_the_root_poa()->reference_to_servant(pCorba);
120 return dynamic_cast<SALOME_SenderInt_i *>(ret);
123 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){
126 SALOME_CorbaDoubleNCSender_i::~SALOME_CorbaDoubleNCSender_i(){
129 CORBA::ULong SALOME_CorbaDoubleNCSender_i::getSize(){
130 CORBA::ULong ret=_lgrTabToSend;
134 SALOME::vectorOfDouble* SALOME_CorbaDoubleNCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
135 SALOME::vectorOfDouble_var c1 = new SALOME::vectorOfDouble(length,length,(CORBA::Double *)((double *)_tabToSend+(long)offset),0);
139 SALOME::vectorOfDouble* SALOME_CorbaDoubleNCSender_i::send(){
140 SALOME::vectorOfDouble_var c1 = new SALOME::vectorOfDouble(_lgrTabToSend,_lgrTabToSend,(CORBA::Double *)_tabToSend,0);
144 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){
147 SALOME_CorbaDoubleCSender_i::~SALOME_CorbaDoubleCSender_i(){
150 CORBA::ULong SALOME_CorbaDoubleCSender_i::getSize(){
151 CORBA::ULong ret=_lgrTabToSend;
155 SALOME::vectorOfDouble* SALOME_CorbaDoubleCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
156 SALOME::vectorOfDouble_var c1 = new SALOME::vectorOfDouble;
158 for (long i=0; i<length; i++)
159 c1[i] = ((double *)_tabToSend)[i+offset];
163 ////////////////////////
165 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){
168 SALOME_CorbaLongNCSender_i::~SALOME_CorbaLongNCSender_i(){
171 CORBA::ULong SALOME_CorbaLongNCSender_i::getSize(){
172 CORBA::ULong ret=_lgrTabToSend;
176 SALOME::vectorOfLong* SALOME_CorbaLongNCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
177 SALOME::vectorOfLong_var c1 = new SALOME::vectorOfLong(length,length,(CORBA::Long *)((long *)_tabToSend+(long)offset),0);
181 SALOME::vectorOfLong* SALOME_CorbaLongNCSender_i::send(){
182 SALOME::vectorOfLong_var c1 = new SALOME::vectorOfLong(_lgrTabToSend,_lgrTabToSend,(CORBA::Long *)_tabToSend,0);
186 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){
189 SALOME_CorbaLongCSender_i::~SALOME_CorbaLongCSender_i(){
192 CORBA::ULong SALOME_CorbaLongCSender_i::getSize(){
193 CORBA::ULong ret=_lgrTabToSend;
197 SALOME::vectorOfLong* SALOME_CorbaLongCSender_i::sendPart(CORBA::ULong offset, CORBA::ULong length){
198 SALOME::vectorOfLong_var c1 = new SALOME::vectorOfLong;
200 for (long i=0; i<length; i++)
201 c1[i] = ((long *)_tabToSend)[i+offset];
207 unsigned long SALOME_MPISender_i::_tag1=0;
209 unsigned long SALOME_MPISender_i::_tag2=1;
211 SALOME_MPISender_i::SALOME_MPISender_i(const void *tabToSend,long lgrTabToSend,int sizeOf,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeOf,ownTabToSend){
212 _portName=new char[MPI_MAX_PORT_NAME];
215 SALOME_MPISender_i::~SALOME_MPISender_i(){
219 SALOME::MPISender::param* SALOME_MPISender_i::getParam()
224 SALOME::MPISender::param_var p = new SALOME::MPISender::param;
225 MPI_Comm_rank(MPI_COMM_WORLD,&_cproc);
231 std::string service("toto_");
232 sprintf(stag,"%d_",_tag1);
234 sprintf(stag,"%d_",p->tag2);
236 p->service = CORBA::string_dup(service.c_str());
237 MPI_Open_port(MPI_INFO_NULL, _portName);
238 MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN);
239 while ( i != TIMEOUT && MPI_Publish_name((char*)service.c_str(),MPI_INFO_NULL,_portName) != MPI_SUCCESS) {
242 MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_ARE_FATAL);
243 if ( i == TIMEOUT ) {
244 MPI_Close_port(_portName);
253 void SALOME_MPISender_i::send()
255 _type=getTypeOfDataTransmitted();
256 _argsForThr=new (void *)[8];
257 _argsForThr[0]=_portName;
258 _argsForThr[1]=&_lgrTabToSend;
259 _argsForThr[2]=(void *)_tabToSend;
260 _argsForThr[3]=&_cproc;
261 _argsForThr[4]=&_tag1Inst;
262 _argsForThr[5]=&_tag2Inst;
263 _argsForThr[6]=&_com;
264 _argsForThr[7]=&_type;
266 _newThr=new omni_thread(SALOME_MPISender_i::myThread,_argsForThr);
270 void* SALOME_MPISender_i::myThread(void *args)
272 void **argsTab=(void **)args;
273 long *lgrTabToSend=(long *)argsTab[1];
274 int *cproc=(int *)argsTab[3];
275 int *tag1=(int *)argsTab[4];
276 int *tag2=(int *)argsTab[5];
277 MPI_Comm *com=(MPI_Comm *)argsTab[6];
278 SALOME::TypeOfDataTransmitted *type=(SALOME::TypeOfDataTransmitted *)argsTab[7];
280 MPI_Comm_accept((char *)argsTab[0],MPI_INFO_NULL,0,MPI_COMM_SELF,com);
281 MPI_Send(lgrTabToSend,1,MPI_LONG,*cproc,*tag1,*com);
284 case SALOME::DOUBLE_:
285 MPI_Send(argsTab[2],*lgrTabToSend,MPI_DOUBLE,*cproc,*tag2,*com);
288 MPI_Send(argsTab[2],*lgrTabToSend,MPI_INT,*cproc,*tag2,*com);
294 void SALOME_MPISender_i::close(const SALOME::MPISender::param& p)
296 std::string service(p.service);
297 const char *st=p.service;
300 MPI_Comm_free(&_com);
301 MPI_Unpublish_name((char *)service.c_str(),MPI_INFO_NULL,_portName);
302 MPI_Close_port(_portName);
303 delete [] _argsForThr;
306 SALOME_MPISenderDouble_i::SALOME_MPISenderDouble_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend)
307 :SALOME_SenderDouble_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_MPISender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
308 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
312 SALOME_MPISenderInt_i::SALOME_MPISenderInt_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend)
313 :SALOME_SenderInt_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_MPISender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
314 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
323 #define _POSIX_PII_SOCKET
324 #define _LIBC_POLLUTION_H_
326 #include <sys/types.h>
327 #include <sys/socket.h>
328 #include <netinet/in.h>
329 #include <arpa/inet.h>
333 SALOME_SocketSender_i::SALOME_SocketSender_i(const void *tabToSend,long lgrTabToSend,int sizeOf,bool ownTabToSend):SALOME_Sender_i(tabToSend,lgrTabToSend,sizeOf,ownTabToSend){
334 _IPAddress = inetAddress();
339 SALOME_SocketSender_i::~SALOME_SocketSender_i(){
342 std::string SALOME_SocketSender_i::inetAddress()
345 char t[INET_ADDRSTRLEN+1];
346 struct hostent *host;
347 struct in_addr saddr;
353 saddr.s_addr = inet_addr(s);
354 if (saddr.s_addr != -1)
355 inet_ntop(AF_INET, &saddr, t, INET_ADDRSTRLEN);
357 host = gethostbyname(s);
359 inet_ntop(AF_INET, (struct in_addr *) *host->h_addr_list,
362 return std::string(t);
365 SALOME::SocketSender::param * SALOME_SocketSender_i::getParam()
368 SALOME::SocketSender::param_var p = new SALOME::SocketSender::param;
371 p->lend = _lgrTabToSend - 1;
373 p->internet_address = CORBA::string_dup(_IPAddress.c_str());
378 void SALOME_SocketSender_i::send()
380 _type=getTypeOfDataTransmitted();
381 _argsForThr=new void *[6];
382 _argsForThr[0]=&_serverSockfd;
383 _argsForThr[1]=&_clientSockfd;
384 _argsForThr[2]=&_lgrTabToSend;
385 _argsForThr[3]=(void *)_tabToSend;
386 _argsForThr[4]=&_errorFlag;
387 _argsForThr[5]=&_type;
389 _newThr=new omni_thread(SALOME_SocketSender_i::myThread,_argsForThr);
393 void* SALOME_SocketSender_i::myThread(void *args)
396 void **argsTab=(void **)args;
397 int *serverSockfd=(int *)argsTab[0];
398 int *clientSockfd=(int *)argsTab[1];
399 long *lgrTabToSend=(long *)argsTab[2];
400 void *tabToSend=argsTab[3];
401 bool *errorFlag=(bool*)argsTab[4];
402 SALOME::TypeOfDataTransmitted *type=(SALOME::TypeOfDataTransmitted *)argsTab[5];
404 XDR xp; /* pointeur sur le decodeur XDR */
408 case SALOME::DOUBLE_:
409 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(double),XDR_ENCODE );
410 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(double), (xdrproc_t)xdr_double );
413 while( n < *lgrTabToSend*sizeof(double) ){
414 m = write(*clientSockfd, (char*)tabToSend+n, *lgrTabToSend*sizeof(double)-n);
416 if( *clientSockfd >= 0 ){
417 ::close(*clientSockfd);
420 if( *serverSockfd >= 0 ){
421 ::close(*serverSockfd);
430 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(double),XDR_DECODE );
431 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(double), (xdrproc_t)xdr_double );
435 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(int),XDR_ENCODE );
436 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(int), (xdrproc_t)xdr_int );
439 while( n < *lgrTabToSend*sizeof(int) ){
440 m = write(*clientSockfd, (char*)tabToSend+n, *lgrTabToSend*sizeof(int)-n);
442 if( *clientSockfd >= 0 ){
443 ::close(*clientSockfd);
446 if( *serverSockfd >= 0 ){
447 ::close(*serverSockfd);
456 xdrmem_create(&xp,(char*)tabToSend,(*lgrTabToSend)*sizeof(int),XDR_DECODE );
457 xdr_vector( &xp, (char*)tabToSend, *lgrTabToSend, sizeof(int), (xdrproc_t)xdr_int );
463 void SALOME_SocketSender_i::initCom() throw(SALOME::SALOME_Exception)
465 struct sockaddr_in serv_addr;
467 SALOME::ExceptionStruct es;
469 /* Ouverture de la socket */
470 _serverSockfd = socket(AF_INET , SOCK_STREAM , 0);
471 if(_serverSockfd < 0) {
472 es.type = SALOME::COMM;
473 es.text = "error Socket exception";
474 throw SALOME::SALOME_Exception(es);
476 /* Socket structure initialisation*/
477 bzero((char*)&serv_addr,sizeof(serv_addr));
478 serv_addr.sin_family = AF_INET;
479 serv_addr.sin_port = 0; /* asking for a free port */
480 serv_addr.sin_addr.s_addr = INADDR_ANY;
482 /* Association of socket with a port */
483 if( ::bind(_serverSockfd, (struct sockaddr *) & serv_addr,
484 sizeof(struct sockaddr)) < 0 ) {
486 es.type = SALOME::COMM;
487 es.text = "error bind Socket exception";
488 throw SALOME::SALOME_Exception(es);
490 /* Listening to the allocated port */
491 if( listen(_serverSockfd, 10) < 0 ) {
493 es.type = SALOME::COMM;
494 es.text = "error listen Socket exception";
495 throw SALOME::SALOME_Exception(es);
497 /* Retrieving port number*/
498 if( getsockname(_serverSockfd, (struct sockaddr *) & serv_addr, &n) < 0 ){
500 es.type = SALOME::COMM;
501 es.text = "error getName Socket exception";
502 throw SALOME::SALOME_Exception(es);
504 _port = htons(serv_addr.sin_port);
508 void SALOME_SocketSender_i::acceptCom() throw(SALOME::SALOME_Exception)
511 struct sockaddr_in client_addr;
512 SALOME::ExceptionStruct es;
514 sin_size = sizeof(struct sockaddr_in);
516 _clientSockfd = accept(_serverSockfd, (struct sockaddr *)&client_addr, &sin_size);
517 if( _clientSockfd < 0 ){
519 es.type = SALOME::COMM;
520 es.text = "error accept Socket exception";
521 throw SALOME::SALOME_Exception(es);
525 void SALOME_SocketSender_i::closeCom()
527 if( _clientSockfd >= 0 ){
528 ::close(_clientSockfd);
531 if( _serverSockfd >= 0 ){
532 ::close(_serverSockfd);
538 void SALOME_SocketSender_i::endOfCom()
544 SALOME::ExceptionStruct es;
545 es.type = SALOME::COMM;
546 es.text = "error write Socket exception";
547 throw SALOME::SALOME_Exception(es);
549 delete [] _argsForThr;
552 SALOME_SocketSenderDouble_i::SALOME_SocketSenderDouble_i(const double *tabToSend,long lgrTabToSend,bool ownTabToSend)
553 :SALOME_SenderDouble_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_SocketSender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
554 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(double),ownTabToSend)
558 SALOME_SocketSenderInt_i::SALOME_SocketSenderInt_i(const int *tabToSend,long lgrTabToSend,bool ownTabToSend)
559 :SALOME_SenderInt_i(tabToSend,lgrTabToSend,ownTabToSend),SALOME_SocketSender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
560 ,SALOME_Sender_i(tabToSend,lgrTabToSend,sizeof(int),ownTabToSend)
565 #undef _LIBC_POLLUTION_H_
566 #undef _POSIX_PII_SOCKET