xref: /petsc/src/sys/classes/viewer/impls/socket/send.c (revision 3ca90d2d9fe4d5ec7086bd4aee14f89370d16392)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petscsys.h>
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith #if defined(PETSC_NEEDS_UTYPE_TYPEDEFS)
55c6c1daeSBarry Smith /* Some systems have inconsistent include files that use but do not
65c6c1daeSBarry Smith    ensure that the following definitions are made */
75c6c1daeSBarry Smith typedef unsigned char   u_char;
85c6c1daeSBarry Smith typedef unsigned short  u_short;
95c6c1daeSBarry Smith typedef unsigned short  ushort;
105c6c1daeSBarry Smith typedef unsigned int    u_int;
115c6c1daeSBarry Smith typedef unsigned long   u_long;
125c6c1daeSBarry Smith #endif
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith #include <errno.h>
155c6c1daeSBarry Smith #include <ctype.h>
165c6c1daeSBarry Smith #if defined(PETSC_HAVE_MACHINE_ENDIAN_H)
175c6c1daeSBarry Smith #include <machine/endian.h>
185c6c1daeSBarry Smith #endif
195c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
205c6c1daeSBarry Smith #include <unistd.h>
215c6c1daeSBarry Smith #endif
225c6c1daeSBarry Smith #if defined(PETSC_HAVE_SYS_SOCKET_H)
235c6c1daeSBarry Smith #include <sys/socket.h>
245c6c1daeSBarry Smith #endif
255c6c1daeSBarry Smith #if defined(PETSC_HAVE_SYS_WAIT_H)
265c6c1daeSBarry Smith #include <sys/wait.h>
275c6c1daeSBarry Smith #endif
285c6c1daeSBarry Smith #if defined(PETSC_HAVE_NETINET_IN_H)
295c6c1daeSBarry Smith #include <netinet/in.h>
305c6c1daeSBarry Smith #endif
315c6c1daeSBarry Smith #if defined(PETSC_HAVE_NETDB_H)
325c6c1daeSBarry Smith #include <netdb.h>
335c6c1daeSBarry Smith #endif
345c6c1daeSBarry Smith #if defined(PETSC_HAVE_FCNTL_H)
355c6c1daeSBarry Smith #include <fcntl.h>
365c6c1daeSBarry Smith #endif
375c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H)
385c6c1daeSBarry Smith #include <io.h>
395c6c1daeSBarry Smith #endif
405c6c1daeSBarry Smith #if defined(PETSC_HAVE_WINSOCK2_H)
415c6c1daeSBarry Smith #include <Winsock2.h>
425c6c1daeSBarry Smith #endif
435c6c1daeSBarry Smith #include <sys/stat.h>
445c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/socket/socket.h>
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith #if defined(PETSC_NEED_CLOSE_PROTO)
478cc058d9SJed Brown PETSC_EXTERN int close(int);
485c6c1daeSBarry Smith #endif
495c6c1daeSBarry Smith #if defined(PETSC_NEED_SOCKET_PROTO)
508cc058d9SJed Brown PETSC_EXTERN int socket(int,int,int);
515c6c1daeSBarry Smith #endif
525c6c1daeSBarry Smith #if defined(PETSC_NEED_SLEEP_PROTO)
538cc058d9SJed Brown PETSC_EXTERN int sleep(unsigned);
545c6c1daeSBarry Smith #endif
555c6c1daeSBarry Smith #if defined(PETSC_NEED_CONNECT_PROTO)
568cc058d9SJed Brown PETSC_EXTERN int connect(int,struct sockaddr*,int);
575c6c1daeSBarry Smith #endif
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith /*--------------------------------------------------------------*/
605c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_Socket(PetscViewer viewer)
615c6c1daeSBarry Smith {
625c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)viewer->data;
635c6c1daeSBarry Smith   PetscErrorCode     ierr;
645c6c1daeSBarry Smith 
655c6c1daeSBarry Smith   PetscFunctionBegin;
665c6c1daeSBarry Smith   if (vmatlab->port) {
675c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET)
685c6c1daeSBarry Smith     ierr = closesocket(vmatlab->port);
695c6c1daeSBarry Smith #else
705c6c1daeSBarry Smith     ierr = close(vmatlab->port);
715c6c1daeSBarry Smith #endif
725c6c1daeSBarry Smith     if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"System error closing socket");
735c6c1daeSBarry Smith   }
745c6c1daeSBarry Smith   ierr = PetscFree(vmatlab);CHKERRQ(ierr);
755c6c1daeSBarry Smith   PetscFunctionReturn(0);
765c6c1daeSBarry Smith }
775c6c1daeSBarry Smith 
785c6c1daeSBarry Smith /*--------------------------------------------------------------*/
794683183fSBarry Smith /*@C
805c6c1daeSBarry Smith     PetscSocketOpen - handles connected to an open port where someone is waiting.
815c6c1daeSBarry Smith 
824683183fSBarry Smith     Input Parameters:
834683183fSBarry Smith +    url - for example www.mcs.anl.gov
844683183fSBarry Smith -    portnum - for example 80
854683183fSBarry Smith 
8601d2d390SJose E. Roman     Output Parameter:
874683183fSBarry Smith .    t - the socket number
884683183fSBarry Smith 
8995452b02SPatrick Sanan     Notes:
9095452b02SPatrick Sanan     Use close() to close the socket connection
914683183fSBarry Smith 
924683183fSBarry Smith     Use read() or PetscHTTPRequest() to read from the socket
934683183fSBarry Smith 
944683183fSBarry Smith     Level: advanced
954683183fSBarry Smith 
964683183fSBarry Smith .seealso:   PetscSocketListen(), PetscSocketEstablish(), PetscHTTPRequest(), PetscHTTPSConnect()
974683183fSBarry Smith @*/
98e2fc02c1SBarry Smith PetscErrorCode  PetscOpenSocket(const char hostname[],int portnum,int *t)
995c6c1daeSBarry Smith {
1005c6c1daeSBarry Smith   struct sockaddr_in sa;
1015c6c1daeSBarry Smith   struct hostent     *hp;
1025c6c1daeSBarry Smith   int                s = 0;
1035c6c1daeSBarry Smith   PetscErrorCode     ierr;
1045c6c1daeSBarry Smith   PetscBool          flg = PETSC_TRUE;
1054a285bdaSBarry Smith   static int         refcnt = 0;
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith   PetscFunctionBegin;
1085c6c1daeSBarry Smith   if (!(hp=gethostbyname(hostname))) {
1095c6c1daeSBarry Smith     perror("SEND: error gethostbyname: ");
1105c6c1daeSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error open connection to %s",hostname);
1115c6c1daeSBarry Smith   }
1125c6c1daeSBarry Smith   ierr = PetscMemzero(&sa,sizeof(sa));CHKERRQ(ierr);
1135c6c1daeSBarry Smith   ierr = PetscMemcpy(&sa.sin_addr,hp->h_addr_list[0],hp->h_length);CHKERRQ(ierr);
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith   sa.sin_family = hp->h_addrtype;
1165c6c1daeSBarry Smith   sa.sin_port   = htons((u_short) portnum);
1175c6c1daeSBarry Smith   while (flg) {
1185c6c1daeSBarry Smith     if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) {
1195c6c1daeSBarry Smith       perror("SEND: error socket");  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
1205c6c1daeSBarry Smith     }
1215c6c1daeSBarry Smith     if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
1225c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR)
1235c6c1daeSBarry Smith       ierr = WSAGetLastError();
124a297a907SKarl Rupp       if (ierr == WSAEADDRINUSE)    (*PetscErrorPrintf)("SEND: address is in use\n");
125a297a907SKarl Rupp       else if (ierr == WSAEALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
126a297a907SKarl Rupp       else if (ierr == WSAEISCONN) {
1275c6c1daeSBarry Smith         (*PetscErrorPrintf)("SEND: socket already connected\n");
1285c6c1daeSBarry Smith         Sleep((unsigned) 1);
1295c6c1daeSBarry Smith       } else if (ierr == WSAECONNREFUSED) {
1305c6c1daeSBarry Smith         /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
1315c6c1daeSBarry Smith         Sleep((unsigned) 1);
1325c6c1daeSBarry Smith       } else {
1335c6c1daeSBarry Smith         perror(NULL); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
1345c6c1daeSBarry Smith       }
1355c6c1daeSBarry Smith #else
136a297a907SKarl Rupp       if (errno == EADDRINUSE)    (*PetscErrorPrintf)("SEND: address is in use\n");
137a297a907SKarl Rupp       else if (errno == EALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
138a297a907SKarl Rupp       else if (errno == EISCONN) {
1395c6c1daeSBarry Smith         (*PetscErrorPrintf)("SEND: socket already connected\n");
1405c6c1daeSBarry Smith         sleep((unsigned) 1);
1415c6c1daeSBarry Smith       } else if (errno == ECONNREFUSED) {
1424a285bdaSBarry Smith         refcnt++;
1434a285bdaSBarry Smith         if (refcnt > 5) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SYS,"Connection refused by remote host %s port %d",hostname,portnum);
14402c9f0b5SLisandro Dalcin         ierr = PetscInfo(NULL,"Connection refused in attaching socket, trying again\n");CHKERRQ(ierr);
1455c6c1daeSBarry Smith         sleep((unsigned) 1);
1465c6c1daeSBarry Smith       } else {
1475c6c1daeSBarry Smith         perror(NULL); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
1485c6c1daeSBarry Smith       }
1495c6c1daeSBarry Smith #endif
1505c6c1daeSBarry Smith       flg = PETSC_TRUE;
1515c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET)
1525c6c1daeSBarry Smith       closesocket(s);
1535c6c1daeSBarry Smith #else
1545c6c1daeSBarry Smith       close(s);
1555c6c1daeSBarry Smith #endif
156a297a907SKarl Rupp     } else flg = PETSC_FALSE;
1575c6c1daeSBarry Smith   }
1585c6c1daeSBarry Smith   *t = s;
1595c6c1daeSBarry Smith   PetscFunctionReturn(0);
1605c6c1daeSBarry Smith }
1615c6c1daeSBarry Smith 
1624683183fSBarry Smith /*@C
1635c6c1daeSBarry Smith    PetscSocketEstablish - starts a listener on a socket
1645c6c1daeSBarry Smith 
1654683183fSBarry Smith    Input Parameters:
1664683183fSBarry Smith .    portnumber - the port to wait at
1674683183fSBarry Smith 
1684683183fSBarry Smith    Output Parameters:
1694683183fSBarry Smith .     ss - the socket to be used with PetscSocketListen()
1704683183fSBarry Smith 
1714683183fSBarry Smith     Level: advanced
1724683183fSBarry Smith 
1734683183fSBarry Smith .seealso:   PetscSocketListen(), PetscOpenSocket()
1744683183fSBarry Smith 
1754683183fSBarry Smith @*/
1765847ff97SBarry Smith PETSC_INTERN PetscErrorCode PetscSocketEstablish(int portnum,int *ss)
1775c6c1daeSBarry Smith {
178589a23caSBarry Smith   static size_t      MAXHOSTNAME = 100;
1795c6c1daeSBarry Smith   char               myname[MAXHOSTNAME+1];
1805c6c1daeSBarry Smith   int                s;
1815c6c1daeSBarry Smith   PetscErrorCode     ierr;
1825c6c1daeSBarry Smith   struct sockaddr_in sa;
1835c6c1daeSBarry Smith   struct hostent     *hp;
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith   PetscFunctionBegin;
186589a23caSBarry Smith   ierr = PetscGetHostName(myname,sizeof(myname));CHKERRQ(ierr);
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith   ierr = PetscMemzero(&sa,sizeof(struct sockaddr_in));CHKERRQ(ierr);
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith   hp = gethostbyname(myname);
1915c6c1daeSBarry Smith   if (!hp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Unable to get hostent information from system");
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith   sa.sin_family = hp->h_addrtype;
1945c6c1daeSBarry Smith   sa.sin_port   = htons((u_short)portnum);
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith   if ((s = socket(AF_INET,SOCK_STREAM,0)) < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Error running socket() command");
1975c6c1daeSBarry Smith #if defined(PETSC_HAVE_SO_REUSEADDR)
1985c6c1daeSBarry Smith   {
1995c6c1daeSBarry Smith     int optval = 1; /* Turn on the option */
2005c6c1daeSBarry Smith     ierr = setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&optval,sizeof(optval));CHKERRQ(ierr);
2015c6c1daeSBarry Smith   }
2025c6c1daeSBarry Smith #endif
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith   while (bind(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
2055c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR)
2065c6c1daeSBarry Smith     ierr = WSAGetLastError();
2075c6c1daeSBarry Smith     if (ierr != WSAEADDRINUSE) {
2085c6c1daeSBarry Smith #else
2095c6c1daeSBarry Smith     if (errno != EADDRINUSE) {
2105c6c1daeSBarry Smith #endif
2115c6c1daeSBarry Smith       close(s);
2125c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Error from bind()");
2135c6c1daeSBarry Smith     }
2145c6c1daeSBarry Smith   }
2155c6c1daeSBarry Smith   listen(s,0);
2165c6c1daeSBarry Smith   *ss = s;
2175c6c1daeSBarry Smith   return(0);
2185c6c1daeSBarry Smith }
2195c6c1daeSBarry Smith 
2204683183fSBarry Smith /*@C
2214683183fSBarry Smith    PetscSocketListen - Listens at a socket created with PetscSocketEstablish()
2224683183fSBarry Smith 
2234683183fSBarry Smith    Input Parameter:
2244683183fSBarry Smith .    listenport - obtained with PetscSocketEstablish()
2254683183fSBarry Smith 
2264683183fSBarry Smith    Output Parameter:
2274683183fSBarry Smith .     t - pass this to read() to read what is passed to this connection
2284683183fSBarry Smith 
2294683183fSBarry Smith     Level: advanced
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith .seealso:   PetscSocketEstablish()
2324683183fSBarry Smith @*/
2335847ff97SBarry Smith PETSC_INTERN PetscErrorCode PetscSocketListen(int listenport,int *t)
2345c6c1daeSBarry Smith {
2355c6c1daeSBarry Smith   struct sockaddr_in isa;
2365c6c1daeSBarry Smith #if defined(PETSC_HAVE_ACCEPT_SIZE_T)
2375c6c1daeSBarry Smith   size_t             i;
2385c6c1daeSBarry Smith #else
2395c6c1daeSBarry Smith   int                i;
2405c6c1daeSBarry Smith #endif
2415c6c1daeSBarry Smith 
2425c6c1daeSBarry Smith   PetscFunctionBegin;
2435c6c1daeSBarry Smith   /* wait for someone to try to connect */
2445c6c1daeSBarry Smith   i = sizeof(struct sockaddr_in);
2455c6c1daeSBarry Smith   if ((*t = accept(listenport,(struct sockaddr*)&isa,(socklen_t*)&i)) < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"error from accept()\n");
2465c6c1daeSBarry Smith   PetscFunctionReturn(0);
2475c6c1daeSBarry Smith }
2485c6c1daeSBarry Smith 
2495c6c1daeSBarry Smith /*@C
250a30fc760SBarry Smith    PetscViewerSocketOpen - Opens a connection to a MATLAB or other socket based server.
2515c6c1daeSBarry Smith 
252d083f849SBarry Smith    Collective
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith    Input Parameters:
2555c6c1daeSBarry Smith +  comm - the MPI communicator
2560298fd71SBarry Smith .  machine - the machine the server is running on,, use NULL for the local machine, use "server" to passively wait for
2575c6c1daeSBarry Smith              a connection from elsewhere
2585c6c1daeSBarry Smith -  port - the port to connect to, use PETSC_DEFAULT for the default
2595c6c1daeSBarry Smith 
2605c6c1daeSBarry Smith    Output Parameter:
2615c6c1daeSBarry Smith .  lab - a context to use when communicating with the server
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith    Level: intermediate
2645c6c1daeSBarry Smith 
2655c6c1daeSBarry Smith    Notes:
2665c6c1daeSBarry Smith    Most users should employ the following commands to access the
2675c6c1daeSBarry Smith    MATLAB PetscViewers
2685c6c1daeSBarry Smith $
2695c6c1daeSBarry Smith $    PetscViewerSocketOpen(MPI_Comm comm, char *machine,int port,PetscViewer &viewer)
2705c6c1daeSBarry Smith $    MatView(Mat matrix,PetscViewer viewer)
2715c6c1daeSBarry Smith $
2725c6c1daeSBarry Smith $                or
2735c6c1daeSBarry Smith $
2745c6c1daeSBarry Smith $    PetscViewerSocketOpen(MPI_Comm comm,char *machine,int port,PetscViewer &viewer)
2755c6c1daeSBarry Smith $    VecView(Vec vector,PetscViewer viewer)
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith    Options Database Keys:
2785c6c1daeSBarry Smith    For use with  PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF,
2795c6c1daeSBarry Smith    PETSC_VIEWER_SOCKET_() or if
2800298fd71SBarry Smith     NULL is passed for machine or PETSC_DEFAULT is passed for port
2815c6c1daeSBarry Smith $    -viewer_socket_machine <machine>
2825c6c1daeSBarry Smith $    -viewer_socket_port <port>
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith    Environmental variables:
285a2b725a8SWilliam Gropp +   PETSC_VIEWER_SOCKET_PORT - portnumber
286a2b725a8SWilliam Gropp -   PETSC_VIEWER_SOCKET_MACHINE - machine name
2875c6c1daeSBarry Smith 
2885c6c1daeSBarry Smith      Currently the only socket client available is MATLAB. See
289c4762a1bSJed Brown      src/dm/tests/ex12.c and ex12.m for an example of usage.
2905c6c1daeSBarry Smith 
29195452b02SPatrick Sanan    Notes:
29295452b02SPatrick Sanan     The socket viewer is in some sense a subclass of the binary viewer, to read and write to the socket
293a30fc760SBarry Smith           use PetscViewerBinaryRead(), PetscViewerBinaryWrite(), PetscViewerBinarWriteStringArray(), PetscViewerBinaryGetDescriptor().
294a30fc760SBarry Smith 
295a30fc760SBarry Smith      Use this for communicating with an interactive MATLAB session, see PETSC_VIEWER_MATLAB_() for writing output to a
296a30fc760SBarry Smith      .mat file. Use PetscMatlabEngineCreate() or PETSC_MATLAB_ENGINE_(), PETSC_MATLAB_ENGINE_SELF, or PETSC_MATLAB_ENGINE_WORLD
297a30fc760SBarry Smith      for communicating with a MATLAB Engine
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerCreate(), PetscViewerSetType(),
3005c6c1daeSBarry Smith           PetscViewerSocketSetConnection(), PETSC_VIEWER_SOCKET_, PETSC_VIEWER_SOCKET_WORLD,
3015c6c1daeSBarry Smith           PETSC_VIEWER_SOCKET_SELF, PetscViewerBinaryWrite(), PetscViewerBinaryRead(), PetscViewerBinaryWriteStringArray(),
302a30fc760SBarry Smith           PetscBinaryViewerGetDescriptor(), PetscMatlabEngineCreate()
3035c6c1daeSBarry Smith @*/
3045c6c1daeSBarry Smith PetscErrorCode  PetscViewerSocketOpen(MPI_Comm comm,const char machine[],int port,PetscViewer *lab)
3055c6c1daeSBarry Smith {
3065c6c1daeSBarry Smith   PetscErrorCode ierr;
3075c6c1daeSBarry Smith 
3085c6c1daeSBarry Smith   PetscFunctionBegin;
3095c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
3105c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERSOCKET);CHKERRQ(ierr);
3115c6c1daeSBarry Smith   ierr = PetscViewerSocketSetConnection(*lab,machine,port);CHKERRQ(ierr);
3125c6c1daeSBarry Smith   PetscFunctionReturn(0);
3135c6c1daeSBarry Smith }
3145c6c1daeSBarry Smith 
3154416b707SBarry Smith static PetscErrorCode PetscViewerSetFromOptions_Socket(PetscOptionItems *PetscOptionsObject,PetscViewer v)
3165c6c1daeSBarry Smith {
3175c6c1daeSBarry Smith   PetscErrorCode ierr;
3185c6c1daeSBarry Smith   PetscInt       def = -1;
3195c6c1daeSBarry Smith   char           sdef[256];
3205c6c1daeSBarry Smith   PetscBool      tflg;
3215c6c1daeSBarry Smith 
3225c6c1daeSBarry Smith   PetscFunctionBegin;
3235c6c1daeSBarry Smith   /*
3245c6c1daeSBarry Smith        These options are not processed here, they are processed in PetscViewerSocketSetConnection(), they
3255c6c1daeSBarry Smith     are listed here for the GUI to display
3265c6c1daeSBarry Smith   */
327e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"Socket PetscViewer Options");CHKERRQ(ierr);
328ce94432eSBarry Smith   ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",sdef,16,&tflg);CHKERRQ(ierr);
3295c6c1daeSBarry Smith   if (tflg) {
3305c6c1daeSBarry Smith     ierr = PetscOptionsStringToInt(sdef,&def);CHKERRQ(ierr);
331a297a907SKarl Rupp   } else def = PETSCSOCKETDEFAULTPORT;
33202c9f0b5SLisandro Dalcin   ierr = PetscOptionsInt("-viewer_socket_port","Port number to use for socket","PetscViewerSocketSetConnection",def,NULL,NULL);CHKERRQ(ierr);
3335c6c1daeSBarry Smith 
334589a23caSBarry Smith   ierr = PetscOptionsString("-viewer_socket_machine","Machine to use for socket","PetscViewerSocketSetConnection",sdef,NULL,sizeof(sdef),NULL);CHKERRQ(ierr);
335589a23caSBarry Smith   ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",sdef,sizeof(sdef),&tflg);CHKERRQ(ierr);
3365c6c1daeSBarry Smith   if (!tflg) {
337589a23caSBarry Smith     ierr = PetscGetHostName(sdef,sizeof(sdef));CHKERRQ(ierr);
3385c6c1daeSBarry Smith   }
3395c6c1daeSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
3405c6c1daeSBarry Smith   PetscFunctionReturn(0);
3415c6c1daeSBarry Smith }
3425c6c1daeSBarry Smith 
3434c9f2355SSatish Balay static PetscErrorCode PetscViewerBinaryGetSkipHeader_Socket(PetscViewer viewer,PetscBool  *skip)
3444c9f2355SSatish Balay {
3454c9f2355SSatish Balay   PetscViewer_Socket *vsocket = (PetscViewer_Socket*)viewer->data;
3464c9f2355SSatish Balay 
3474c9f2355SSatish Balay   PetscFunctionBegin;
3484c9f2355SSatish Balay   *skip = vsocket->skipheader;
3494c9f2355SSatish Balay   PetscFunctionReturn(0);
3504c9f2355SSatish Balay }
3514c9f2355SSatish Balay 
3524c9f2355SSatish Balay static PetscErrorCode PetscViewerBinarySetSkipHeader_Socket(PetscViewer viewer,PetscBool skip)
3534c9f2355SSatish Balay {
3544c9f2355SSatish Balay   PetscViewer_Socket *vsocket = (PetscViewer_Socket*)viewer->data;
3554c9f2355SSatish Balay 
3564c9f2355SSatish Balay   PetscFunctionBegin;
3574c9f2355SSatish Balay   vsocket->skipheader = skip;
3584c9f2355SSatish Balay   PetscFunctionReturn(0);
3594c9f2355SSatish Balay }
3604c9f2355SSatish Balay 
3614c9f2355SSatish Balay static PetscErrorCode  PetscViewerBinaryGetFlowControl_Socket(PetscViewer viewer,PetscInt *fc)
3624c9f2355SSatish Balay {
3634c9f2355SSatish Balay   PetscFunctionBegin;
3644c9f2355SSatish Balay   *fc = 0;
3654c9f2355SSatish Balay   PetscFunctionReturn(0);
3664c9f2355SSatish Balay }
3674c9f2355SSatish Balay 
3688556b5ebSBarry Smith /*MC
3698556b5ebSBarry Smith    PETSCVIEWERSOCKET - A viewer that writes to a Unix socket
3708556b5ebSBarry Smith 
3718556b5ebSBarry Smith .seealso:  PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSC_VIEWER_DRAW_(),PETSC_VIEWER_DRAW_SELF, PETSC_VIEWER_DRAW_WORLD,
3728556b5ebSBarry Smith            PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
3738556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
3748556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
3758556b5ebSBarry Smith 
3761b266c99SBarry Smith   Level: beginner
3778556b5ebSBarry Smith M*/
3788556b5ebSBarry Smith 
3798cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Socket(PetscViewer v)
3805c6c1daeSBarry Smith {
3815c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab;
3825c6c1daeSBarry Smith   PetscErrorCode     ierr;
3835c6c1daeSBarry Smith 
3845c6c1daeSBarry Smith   PetscFunctionBegin;
385b00a9115SJed Brown   ierr                   = PetscNewLog(v,&vmatlab);CHKERRQ(ierr);
3865c6c1daeSBarry Smith   vmatlab->port          = 0;
3875c6c1daeSBarry Smith   v->data                = (void*)vmatlab;
3885c6c1daeSBarry Smith   v->ops->destroy        = PetscViewerDestroy_Socket;
38902c9f0b5SLisandro Dalcin   v->ops->flush          = NULL;
3905c6c1daeSBarry Smith   v->ops->setfromoptions = PetscViewerSetFromOptions_Socket;
3915c6c1daeSBarry Smith 
3925c6c1daeSBarry Smith   /* lie and say this is a binary viewer; then all the XXXView_Binary() methods will work correctly on it */
3935c6c1daeSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)v,PETSCVIEWERBINARY);CHKERRQ(ierr);
3944c9f2355SSatish Balay   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Socket);CHKERRQ(ierr);
3954c9f2355SSatish Balay   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Socket);CHKERRQ(ierr);
3964c9f2355SSatish Balay   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Socket);CHKERRQ(ierr);
3974c9f2355SSatish Balay 
3985c6c1daeSBarry Smith   PetscFunctionReturn(0);
3995c6c1daeSBarry Smith }
4005c6c1daeSBarry Smith 
4015c6c1daeSBarry Smith /*@C
4025c6c1daeSBarry Smith       PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket
4035c6c1daeSBarry Smith              viewer is to use
4045c6c1daeSBarry Smith 
4055c6c1daeSBarry Smith   Logically Collective on PetscViewer
4065c6c1daeSBarry Smith 
4075c6c1daeSBarry Smith   Input Parameters:
4085c6c1daeSBarry Smith +   v - viewer to connect
4090298fd71SBarry Smith .   machine - host to connect to, use NULL for the local machine,use "server" to passively wait for
4105c6c1daeSBarry Smith              a connection from elsewhere
4115c6c1daeSBarry Smith -   port - the port on the machine one is connecting to, use PETSC_DEFAULT for default
4125c6c1daeSBarry Smith 
4135c6c1daeSBarry Smith     Level: advanced
4145c6c1daeSBarry Smith 
4155c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen()
4165c6c1daeSBarry Smith @*/
4175c6c1daeSBarry Smith PetscErrorCode  PetscViewerSocketSetConnection(PetscViewer v,const char machine[],int port)
4185c6c1daeSBarry Smith {
4195c6c1daeSBarry Smith   PetscErrorCode     ierr;
4205c6c1daeSBarry Smith   PetscMPIInt        rank;
4215c6c1daeSBarry Smith   char               mach[256];
4225c6c1daeSBarry Smith   PetscBool          tflg;
423*3ca90d2dSJacob Faibussowitsch   PetscViewer_Socket *vmatlab;
4245c6c1daeSBarry Smith 
4255c6c1daeSBarry Smith   PetscFunctionBegin;
426*3ca90d2dSJacob Faibussowitsch   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
427*3ca90d2dSJacob Faibussowitsch   if (machine) PetscValidCharPointer(machine,2);
428*3ca90d2dSJacob Faibussowitsch   vmatlab = (PetscViewer_Socket*)v->data;
4295c6c1daeSBarry Smith   /* PetscValidLogicalCollectiveInt(v,port,3); not a PetscInt */
4305c6c1daeSBarry Smith   if (port <= 0) {
4315c6c1daeSBarry Smith     char portn[16];
432ce94432eSBarry Smith     ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",portn,16,&tflg);CHKERRQ(ierr);
4335c6c1daeSBarry Smith     if (tflg) {
4345c6c1daeSBarry Smith       PetscInt pport;
4355c6c1daeSBarry Smith       ierr = PetscOptionsStringToInt(portn,&pport);CHKERRQ(ierr);
4365c6c1daeSBarry Smith       port = (int)pport;
437a297a907SKarl Rupp     } else port = PETSCSOCKETDEFAULTPORT;
4385c6c1daeSBarry Smith   }
4395c6c1daeSBarry Smith   if (!machine) {
440589a23caSBarry Smith     ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",mach,sizeof(mach),&tflg);CHKERRQ(ierr);
4415c6c1daeSBarry Smith     if (!tflg) {
442589a23caSBarry Smith       ierr = PetscGetHostName(mach,sizeof(mach));CHKERRQ(ierr);
4435c6c1daeSBarry Smith     }
4445c6c1daeSBarry Smith   } else {
445589a23caSBarry Smith     ierr = PetscStrncpy(mach,machine,sizeof(mach));CHKERRQ(ierr);
4465c6c1daeSBarry Smith   }
4475c6c1daeSBarry Smith 
448ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRMPI(ierr);
449dd400576SPatrick Sanan   if (rank == 0) {
4505c6c1daeSBarry Smith     ierr = PetscStrcmp(mach,"server",&tflg);CHKERRQ(ierr);
4515c6c1daeSBarry Smith     if (tflg) {
4525c6c1daeSBarry Smith       int listenport;
453*3ca90d2dSJacob Faibussowitsch       ierr = PetscInfo1(v,"Waiting for connection from socket process on port %d\n",port);CHKERRQ(ierr);
4545c6c1daeSBarry Smith       ierr = PetscSocketEstablish(port,&listenport);CHKERRQ(ierr);
4555c6c1daeSBarry Smith       ierr = PetscSocketListen(listenport,&vmatlab->port);CHKERRQ(ierr);
4565c6c1daeSBarry Smith       close(listenport);
4575c6c1daeSBarry Smith     } else {
458*3ca90d2dSJacob Faibussowitsch       ierr = PetscInfo2(v,"Connecting to socket process on port %d machine %s\n",port,mach);CHKERRQ(ierr);
4595c6c1daeSBarry Smith       ierr = PetscOpenSocket(mach,port,&vmatlab->port);CHKERRQ(ierr);
4605c6c1daeSBarry Smith     }
4615c6c1daeSBarry Smith   }
4625c6c1daeSBarry Smith   PetscFunctionReturn(0);
4635c6c1daeSBarry Smith }
4645c6c1daeSBarry Smith 
4655c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
4665c6c1daeSBarry Smith /*
4675c6c1daeSBarry Smith     The variable Petsc_Viewer_Socket_keyval is used to indicate an MPI attribute that
4685c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
4695c6c1daeSBarry Smith */
470d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Socket_keyval = MPI_KEYVAL_INVALID;
4715c6c1daeSBarry Smith 
4725c6c1daeSBarry Smith /*@C
4735c6c1daeSBarry Smith      PETSC_VIEWER_SOCKET_ - Creates a socket viewer shared by all processors in a communicator.
4745c6c1daeSBarry Smith 
475d083f849SBarry Smith      Collective
4765c6c1daeSBarry Smith 
4775c6c1daeSBarry Smith      Input Parameter:
4785c6c1daeSBarry Smith .    comm - the MPI communicator to share the socket PetscViewer
4795c6c1daeSBarry Smith 
4805c6c1daeSBarry Smith      Level: intermediate
4815c6c1daeSBarry Smith 
4825c6c1daeSBarry Smith    Options Database Keys:
4835c6c1daeSBarry Smith    For use with the default PETSC_VIEWER_SOCKET_WORLD or if
4840298fd71SBarry Smith     NULL is passed for machine or PETSC_DEFAULT is passed for port
4855c6c1daeSBarry Smith $    -viewer_socket_machine <machine>
4865c6c1daeSBarry Smith $    -viewer_socket_port <port>
4875c6c1daeSBarry Smith 
4885c6c1daeSBarry Smith    Environmental variables:
489a2b725a8SWilliam Gropp +   PETSC_VIEWER_SOCKET_PORT - portnumber
490a2b725a8SWilliam Gropp -   PETSC_VIEWER_SOCKET_MACHINE - machine name
4915c6c1daeSBarry Smith 
4925c6c1daeSBarry Smith      Notes:
493fa131761SBarry Smith      Unlike almost all other PETSc routines, Petsc_VIEWER_SOCKET_ does not return
494fa131761SBarry Smith      an error code, it returns NULL if it fails. The socket PetscViewer is usually used in the form
4955c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_SOCKET_(comm));
4965c6c1daeSBarry Smith 
4975c6c1daeSBarry Smith      Currently the only socket client available is MATLAB. See
498c4762a1bSJed Brown      src/dm/tests/ex12.c and ex12.m for an example of usage.
4995c6c1daeSBarry Smith 
5005c6c1daeSBarry Smith      Connects to a waiting socket and stays connected until PetscViewerDestroy() is called.
5015c6c1daeSBarry Smith 
502a30fc760SBarry Smith      Use this for communicating with an interactive MATLAB session, see PETSC_VIEWER_MATLAB_() for writing output to a
503a30fc760SBarry Smith      .mat file. Use PetscMatlabEngineCreate() or PETSC_MATLAB_ENGINE_(), PETSC_MATLAB_ENGINE_SELF, or PETSC_MATLAB_ENGINE_WORLD
504a30fc760SBarry Smith      for communicating with a MATLAB Engine
5055c6c1daeSBarry Smith 
5065c6c1daeSBarry Smith .seealso: PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF, PetscViewerSocketOpen(), PetscViewerCreate(),
5075c6c1daeSBarry Smith           PetscViewerSocketSetConnection(), PetscViewerDestroy(), PETSC_VIEWER_SOCKET_(), PetscViewerBinaryWrite(), PetscViewerBinaryRead(),
508a30fc760SBarry Smith           PetscViewerBinaryWriteStringArray(), PetscViewerBinaryGetDescriptor(), PETSC_VIEWER_MATLAB_()
5095c6c1daeSBarry Smith @*/
5105c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_SOCKET_(MPI_Comm comm)
5115c6c1daeSBarry Smith {
5125c6c1daeSBarry Smith   PetscErrorCode ierr;
5135c6c1daeSBarry Smith   PetscBool      flg;
5145c6c1daeSBarry Smith   PetscViewer    viewer;
5155c6c1daeSBarry Smith   MPI_Comm       ncomm;
5165c6c1daeSBarry Smith 
5175c6c1daeSBarry Smith   PetscFunctionBegin;
51802c9f0b5SLisandro Dalcin   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
5195c6c1daeSBarry Smith   if (Petsc_Viewer_Socket_keyval == MPI_KEYVAL_INVALID) {
52002c9f0b5SLisandro Dalcin     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Socket_keyval,NULL);
52102c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
5225c6c1daeSBarry Smith   }
52347435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Socket_keyval,(void**)&viewer,(int*)&flg);
52402c9f0b5SLisandro Dalcin   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
5255c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
52602c9f0b5SLisandro Dalcin     ierr = PetscViewerSocketOpen(ncomm,NULL,0,&viewer);
5272cb5e1ccSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");PetscFunctionReturn(NULL);}
5285c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
5292cb5e1ccSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");PetscFunctionReturn(NULL);}
53047435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Socket_keyval,(void*)viewer);
53102c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
5325c6c1daeSBarry Smith   }
5335c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
5342cb5e1ccSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");PetscFunctionReturn(NULL);}
5355c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
5365c6c1daeSBarry Smith }
537