EXTERN_C_END #undef __FUNCT__ #define __FUNCT__ "PetscViewerSocketSetConnection" /*@C PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket viewer is to use Collective on PetscViewer Input Parameters: + v - viewer to connect . machine - host to connect to, use PETSC_NULL for the local machine,use "server" to passively wait for a connection from elsewhere - port - the port on the machine one is connecting to, use PETSC_DEFAULT for default Level: advanced .seealso: PetscViewerSocketOpen() @*/ PetscErrorCode PETSC_DLLEXPORT PetscViewerSocketSetConnection(PetscViewer v,const char machine[],PetscInt port) { PetscErrorCode ierr; PetscMPIInt rank; char mach[256]; PetscTruth tflg; PetscViewer_Socket *vmatlab = (PetscViewer_Socket *)v->data; PetscFunctionBegin; if (port <= 0) { char portn[16]; ierr = PetscOptionsGetenv(((PetscObject)v)->comm,"PETSC_VIEWER_SOCKET_PORT",portn,16,&tflg);CHKERRQ(ierr); if (tflg) { ierr = PetscOptionsAtoi(portn,&port);CHKERRQ(ierr); } else { port = PETSCSOCKETDEFAULTPORT; } } if (!machine) { ierr = PetscOptionsGetenv(((PetscObject)v)->comm,"PETSC_VIEWER_SOCKET_MACHINE",mach,256,&tflg);CHKERRQ(ierr); if (!tflg) { ierr = PetscGetHostName(mach,256);CHKERRQ(ierr); } } else { ierr = PetscStrncpy(mach,machine,256);CHKERRQ(ierr); } ierr = MPI_Comm_rank(((PetscObject)v)->comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscStrcmp(mach,"server",&tflg);CHKERRQ(ierr); if (tflg) { ierr = PetscInfo1(v,"Waiting for connection from socket process on port %D\n",port);CHKERRQ(ierr); ierr = SOCKAnswer_Private((int)port,&vmatlab->port);CHKERRQ(ierr); } else { ierr = PetscInfo2(v,"Connecting to socket process on port %D machine %s\n",port,mach);CHKERRQ(ierr); ierr = PetscOpenSocket(mach,(int)port,&vmatlab->port);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
/*@C PetscHTTPSConnect - connect to a HTTPS server Input Parameters: + host - the name of the machine hosting the HTTPS server . port - the port number where the server is hosting, usually 443 - ctx - value obtained with PetscSSLInitializeContext() Output Parameters: + sock - socket to connect - ssl - the argument passed to PetscHTTPSRequest() Level: advanced .seealso: PetscOpenSocket(), PetscHTTPSRequest(), PetscSSLInitializeContext() @*/ PetscErrorCode PetscHTTPSConnect(const char host[],int port,SSL_CTX *ctx,int *sock,SSL **ssl) { BIO *sbio; PetscErrorCode ierr; PetscFunctionBegin; /* Connect the TCP socket*/ ierr = PetscOpenSocket(host,port,sock);CHKERRQ(ierr); /* Connect the SSL socket */ *ssl = SSL_new(ctx); sbio = BIO_new_socket(*sock,BIO_NOCLOSE); SSL_set_bio(*ssl,sbio,sbio); if (SSL_connect(*ssl) <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SSL connect error"); PetscFunctionReturn(0); }
/*@C PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket viewer is to use Logically Collective on PetscViewer Input Parameters: + v - viewer to connect . machine - host to connect to, use NULL for the local machine,use "server" to passively wait for a connection from elsewhere - port - the port on the machine one is connecting to, use PETSC_DEFAULT for default Level: advanced .seealso: PetscViewerSocketOpen() @*/ PetscErrorCode PetscViewerSocketSetConnection(PetscViewer v,const char machine[],int port) { PetscErrorCode ierr; PetscMPIInt rank; char mach[256]; PetscBool tflg; PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)v->data; PetscFunctionBegin; /* PetscValidLogicalCollectiveInt(v,port,3); not a PetscInt */ if (port <= 0) { char portn[16]; ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",portn,16,&tflg);CHKERRQ(ierr); if (tflg) { PetscInt pport; ierr = PetscOptionsStringToInt(portn,&pport);CHKERRQ(ierr); port = (int)pport; } else port = PETSCSOCKETDEFAULTPORT; } if (!machine) { ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",mach,256,&tflg);CHKERRQ(ierr); if (!tflg) { ierr = PetscGetHostName(mach,256);CHKERRQ(ierr); } } else { ierr = PetscStrncpy(mach,machine,256);CHKERRQ(ierr); } ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscStrcmp(mach,"server",&tflg);CHKERRQ(ierr); if (tflg) { int listenport; ierr = PetscInfo1(v,"Waiting for connection from socket process on port %D\n",port);CHKERRQ(ierr); ierr = PetscSocketEstablish(port,&listenport);CHKERRQ(ierr); ierr = PetscSocketListen(listenport,&vmatlab->port);CHKERRQ(ierr); close(listenport); } else { ierr = PetscInfo2(v,"Connecting to socket process on port %D machine %s\n",port,mach);CHKERRQ(ierr); ierr = PetscOpenSocket(mach,port,&vmatlab->port);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
/*@C PetscTextBelt - Sends an SMS to an American/Canadian phone number Not collective, only the first process in MPI_Comm does anything Input Parameters: + comm - the MPI communicator . number - the 10 digit telephone number - message - the message Output Parameter: . flg - PETSC_TRUE if the text was sent Level: intermediate Notes: TextBelt is run for testing purposes only, please do not use this feature often Developer Notes: I do not know how to make the buff[] long enough to receive the "success" string but short enough that the code does not hang waiting for part of the message to arrive that does not exist, hence the success flg may be improperly set to false even though the message was delivered. .seealso: PetscOpenSocket(), PetscHTTPRequest() @*/ PetscErrorCode PetscTextBelt(MPI_Comm comm,const char number[],const char message[],PetscBool *flg) { PetscErrorCode ierr; size_t nlen,mlen,blen; PetscMPIInt rank; PetscFunctionBegin; ierr = PetscStrlen(number,&nlen);CHKERRQ(ierr); if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number); ierr = PetscStrlen(message,&mlen);CHKERRQ(ierr); if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message %s is too long",message); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { int sock; char buff[474],*body; PetscInt i; ierr = PetscMalloc1(mlen+nlen+100,&body);CHKERRQ(ierr); ierr = PetscStrcpy(body,"number=");CHKERRQ(ierr); ierr = PetscStrcat(body,number);CHKERRQ(ierr); ierr = PetscStrcat(body,"&");CHKERRQ(ierr); ierr = PetscStrcat(body,"message=");CHKERRQ(ierr); ierr = PetscStrcat(body,message);CHKERRQ(ierr); ierr = PetscStrlen(body,&blen);CHKERRQ(ierr); for (i=0; i<(int)blen; i++) { if (body[i] == ' ') body[i] = '+'; } ierr = PetscOpenSocket("textbelt.com",80,&sock);CHKERRQ(ierr); ierr = PetscHTTPRequest("POST","textbelt.com/text",NULL,"application/x-www-form-urlencoded",body,sock,buff,sizeof(buff));CHKERRQ(ierr); close(sock); ierr = PetscFree(body);CHKERRQ(ierr); if (flg) { char *found; ierr = PetscStrstr(buff,"\"success\":tr",&found);CHKERRQ(ierr); *flg = found ? PETSC_TRUE : PETSC_FALSE; } } PetscFunctionReturn(0); }