xref: /petsc/src/sys/classes/viewer/impls/socket/send.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
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 /*--------------------------------------------------------------*/
609371c9d4SSatish Balay static PetscErrorCode PetscViewerDestroy_Socket(PetscViewer viewer) {
615c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab = (PetscViewer_Socket *)viewer->data;
625c6c1daeSBarry Smith   PetscErrorCode      ierr;
635c6c1daeSBarry Smith 
645c6c1daeSBarry Smith   PetscFunctionBegin;
655c6c1daeSBarry Smith   if (vmatlab->port) {
665c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET)
675c6c1daeSBarry Smith     ierr = closesocket(vmatlab->port);
685c6c1daeSBarry Smith #else
695c6c1daeSBarry Smith     ierr = close(vmatlab->port);
705c6c1daeSBarry Smith #endif
7128b400f6SJacob Faibussowitsch     PetscCheck(!ierr, PETSC_COMM_SELF, PETSC_ERR_SYS, "System error closing socket");
725c6c1daeSBarry Smith   }
739566063dSJacob Faibussowitsch   PetscCall(PetscFree(vmatlab));
742e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerBinarySetSkipHeader_C", NULL));
752e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerBinaryGetSkipHeader_C", NULL));
762e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerBinaryGetFlowControl_C", NULL));
775c6c1daeSBarry Smith   PetscFunctionReturn(0);
785c6c1daeSBarry Smith }
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith /*--------------------------------------------------------------*/
814683183fSBarry Smith /*@C
825c6c1daeSBarry Smith     PetscSocketOpen - handles connected to an open port where someone is waiting.
835c6c1daeSBarry Smith 
844683183fSBarry Smith     Input Parameters:
854683183fSBarry Smith +    url - for example www.mcs.anl.gov
864683183fSBarry Smith -    portnum - for example 80
874683183fSBarry Smith 
8801d2d390SJose E. Roman     Output Parameter:
894683183fSBarry Smith .    t - the socket number
904683183fSBarry Smith 
9195452b02SPatrick Sanan     Notes:
9295452b02SPatrick Sanan     Use close() to close the socket connection
934683183fSBarry Smith 
94811af0c4SBarry Smith     Use read() or `PetscHTTPRequest()` to read from the socket
954683183fSBarry Smith 
964683183fSBarry Smith     Level: advanced
974683183fSBarry Smith 
98db781477SPatrick Sanan .seealso: `PetscSocketListen()`, `PetscSocketEstablish()`, `PetscHTTPRequest()`, `PetscHTTPSConnect()`
994683183fSBarry Smith @*/
1009371c9d4SSatish Balay PetscErrorCode PetscOpenSocket(const char hostname[], int portnum, int *t) {
1015c6c1daeSBarry Smith   struct sockaddr_in sa;
1025c6c1daeSBarry Smith   struct hostent    *hp;
1035c6c1daeSBarry Smith   int                s      = 0;
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: ");
11098921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error open connection to %s", hostname);
1115c6c1daeSBarry Smith   }
1129566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(&sa, sizeof(sa)));
1139566063dSJacob Faibussowitsch   PetscCall(PetscMemcpy(&sa.sin_addr, hp->h_addr_list[0], hp->h_length));
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) {
1199371c9d4SSatish Balay       perror("SEND: error socket");
1209371c9d4SSatish Balay       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error");
1215c6c1daeSBarry Smith     }
1225c6c1daeSBarry Smith     if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
1235c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR)
1245c6c1daeSBarry Smith       ierr = WSAGetLastError();
125a297a907SKarl Rupp       if (ierr == WSAEADDRINUSE) (*PetscErrorPrintf)("SEND: address is in use\n");
126a297a907SKarl Rupp       else if (ierr == WSAEALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
127a297a907SKarl Rupp       else if (ierr == WSAEISCONN) {
1285c6c1daeSBarry Smith         (*PetscErrorPrintf)("SEND: socket already connected\n");
1295c6c1daeSBarry Smith         Sleep((unsigned)1);
1305c6c1daeSBarry Smith       } else if (ierr == WSAECONNREFUSED) {
1315c6c1daeSBarry Smith         /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
1325c6c1daeSBarry Smith         Sleep((unsigned)1);
1335c6c1daeSBarry Smith       } else {
1349371c9d4SSatish Balay         perror(NULL);
1359371c9d4SSatish Balay         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error");
1365c6c1daeSBarry Smith       }
1375c6c1daeSBarry Smith #else
138a297a907SKarl Rupp       if (errno == EADDRINUSE) (*PetscErrorPrintf)("SEND: address is in use\n");
139a297a907SKarl Rupp       else if (errno == EALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
140a297a907SKarl Rupp       else if (errno == EISCONN) {
1415c6c1daeSBarry Smith         (*PetscErrorPrintf)("SEND: socket already connected\n");
1425c6c1daeSBarry Smith         sleep((unsigned)1);
1435c6c1daeSBarry Smith       } else if (errno == ECONNREFUSED) {
1444a285bdaSBarry Smith         refcnt++;
14508401ef6SPierre Jolivet         PetscCheck(refcnt <= 5, PETSC_COMM_SELF, PETSC_ERR_SYS, "Connection refused by remote host %s port %d", hostname, portnum);
1469566063dSJacob Faibussowitsch         PetscCall(PetscInfo(NULL, "Connection refused in attaching socket, trying again\n"));
1475c6c1daeSBarry Smith         sleep((unsigned)1);
1485c6c1daeSBarry Smith       } else {
1499371c9d4SSatish Balay         perror(NULL);
1509371c9d4SSatish Balay         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SYS, "system error");
1515c6c1daeSBarry Smith       }
1525c6c1daeSBarry Smith #endif
1535c6c1daeSBarry Smith       flg = PETSC_TRUE;
1545c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET)
1555c6c1daeSBarry Smith       closesocket(s);
1565c6c1daeSBarry Smith #else
1575c6c1daeSBarry Smith       close(s);
1585c6c1daeSBarry Smith #endif
159a297a907SKarl Rupp     } else flg = PETSC_FALSE;
1605c6c1daeSBarry Smith   }
1615c6c1daeSBarry Smith   *t = s;
1625c6c1daeSBarry Smith   PetscFunctionReturn(0);
1635c6c1daeSBarry Smith }
1645c6c1daeSBarry Smith 
1654683183fSBarry Smith /*@C
1665c6c1daeSBarry Smith    PetscSocketEstablish - starts a listener on a socket
1675c6c1daeSBarry Smith 
1684683183fSBarry Smith    Input Parameters:
1694683183fSBarry Smith .    portnumber - the port to wait at
1704683183fSBarry Smith 
1714683183fSBarry Smith    Output Parameters:
172811af0c4SBarry Smith .     ss - the socket to be used with `PetscSocketListen()`
1734683183fSBarry Smith 
1744683183fSBarry Smith     Level: advanced
1754683183fSBarry Smith 
176db781477SPatrick Sanan .seealso: `PetscSocketListen()`, `PetscOpenSocket()`
1774683183fSBarry Smith @*/
1789371c9d4SSatish Balay PETSC_INTERN PetscErrorCode PetscSocketEstablish(int portnum, int *ss) {
179589a23caSBarry Smith   static size_t      MAXHOSTNAME = 100;
1805c6c1daeSBarry Smith   char               myname[MAXHOSTNAME + 1];
1815c6c1daeSBarry Smith   int                s;
1825c6c1daeSBarry Smith   struct sockaddr_in sa;
1835c6c1daeSBarry Smith   struct hostent    *hp;
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith   PetscFunctionBegin;
1869566063dSJacob Faibussowitsch   PetscCall(PetscGetHostName(myname, sizeof(myname)));
1875c6c1daeSBarry Smith 
1889566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(&sa, sizeof(struct sockaddr_in)));
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith   hp = gethostbyname(myname);
19128b400f6SJacob Faibussowitsch   PetscCheck(hp, 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 
196cc73adaaSBarry Smith   PetscCheck((s = socket(AF_INET, SOCK_STREAM, 0)) >= 0, 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 */
2009566063dSJacob Faibussowitsch     PetscCall(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval)));
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
221811af0c4SBarry Smith    PetscSocketListen - Listens at a socket created with `PetscSocketEstablish()`
2224683183fSBarry Smith 
2234683183fSBarry Smith    Input Parameter:
224811af0c4SBarry 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 
231db781477SPatrick Sanan .seealso: `PetscSocketEstablish()`
2324683183fSBarry Smith @*/
2339371c9d4SSatish Balay PETSC_INTERN PetscErrorCode PetscSocketListen(int listenport, int *t) {
2345c6c1daeSBarry Smith   struct sockaddr_in isa;
2355c6c1daeSBarry Smith #if defined(PETSC_HAVE_ACCEPT_SIZE_T)
2365c6c1daeSBarry Smith   size_t i;
2375c6c1daeSBarry Smith #else
2385c6c1daeSBarry Smith   int i;
2395c6c1daeSBarry Smith #endif
2405c6c1daeSBarry Smith 
2415c6c1daeSBarry Smith   PetscFunctionBegin;
2425c6c1daeSBarry Smith   /* wait for someone to try to connect */
2435c6c1daeSBarry Smith   i = sizeof(struct sockaddr_in);
244cc73adaaSBarry Smith   PetscCheck((*t = accept(listenport, (struct sockaddr *)&isa, (socklen_t *)&i)) >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "error from accept()");
2455c6c1daeSBarry Smith   PetscFunctionReturn(0);
2465c6c1daeSBarry Smith }
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith /*@C
249a30fc760SBarry Smith    PetscViewerSocketOpen - Opens a connection to a MATLAB or other socket based server.
2505c6c1daeSBarry Smith 
251d083f849SBarry Smith    Collective
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith    Input Parameters:
2545c6c1daeSBarry Smith +  comm - the MPI communicator
2550298fd71SBarry Smith .  machine - the machine the server is running on,, use NULL for the local machine, use "server" to passively wait for
2565c6c1daeSBarry Smith              a connection from elsewhere
257811af0c4SBarry Smith -  port - the port to connect to, use `PETSC_DEFAULT` for the default
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith    Output Parameter:
2605c6c1daeSBarry Smith .  lab - a context to use when communicating with the server
2615c6c1daeSBarry Smith 
262811af0c4SBarry Smith    Options Database Keys:
263811af0c4SBarry Smith    For use with  `PETSC_VIEWER_SOCKET_WORLD`, `PETSC_VIEWER_SOCKET_SELF`,
264811af0c4SBarry Smith    `PETSC_VIEWER_SOCKET_()` or if
265811af0c4SBarry Smith     NULL is passed for machine or PETSC_DEFAULT is passed for port
266811af0c4SBarry Smith $    -viewer_socket_machine <machine>
267811af0c4SBarry Smith $    -viewer_socket_port <port>
268811af0c4SBarry Smith 
269811af0c4SBarry Smith    Environmental variables:
270811af0c4SBarry Smith +   `PETSC_VIEWER_SOCKET_PORT` - portnumber
271811af0c4SBarry Smith -   `PETSC_VIEWER_SOCKET_MACHINE` - machine name
272811af0c4SBarry Smith 
2735c6c1daeSBarry Smith    Level: intermediate
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith    Notes:
2765c6c1daeSBarry Smith    Most users should employ the following commands to access the
277811af0c4SBarry Smith    MATLAB `PetscViewer`
2785c6c1daeSBarry Smith $
2795c6c1daeSBarry Smith $    PetscViewerSocketOpen(MPI_Comm comm, char *machine,int port,PetscViewer &viewer)
2805c6c1daeSBarry Smith $    MatView(Mat matrix,PetscViewer viewer)
2815c6c1daeSBarry Smith $
2825c6c1daeSBarry Smith $                or
2835c6c1daeSBarry Smith $
2845c6c1daeSBarry Smith $    PetscViewerSocketOpen(MPI_Comm comm,char *machine,int port,PetscViewer &viewer)
2855c6c1daeSBarry Smith $    VecView(Vec vector,PetscViewer viewer)
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith      Currently the only socket client available is MATLAB. See
288c4762a1bSJed Brown      src/dm/tests/ex12.c and ex12.m for an example of usage.
2895c6c1daeSBarry Smith 
29095452b02SPatrick Sanan     The socket viewer is in some sense a subclass of the binary viewer, to read and write to the socket
291811af0c4SBarry Smith     use `PetscViewerBinaryRead()`, `PetscViewerBinaryWrite()`, `PetscViewerBinarWriteStringArray()`, `PetscViewerBinaryGetDescriptor()`.
292a30fc760SBarry Smith 
293811af0c4SBarry Smith      Use this for communicating with an interactive MATLAB session, see `PETSC_VIEWER_MATLAB_()` for writing output to a
294811af0c4SBarry Smith      .mat file. Use `PetscMatlabEngineCreate()` or `PETSC_MATLAB_ENGINE_()`, `PETSC_MATLAB_ENGINE_SELF`, or `PETSC_MATLAB_ENGINE_WORLD`
295a30fc760SBarry Smith      for communicating with a MATLAB Engine
2965c6c1daeSBarry Smith 
297811af0c4SBarry Smith .seealso: `PETSCVIEWERBINARY`, `PETSCVIEWERSOCKET`, `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerCreate()`, `PetscViewerSetType()`,
298db781477SPatrick Sanan           `PetscViewerSocketSetConnection()`, `PETSC_VIEWER_SOCKET_`, `PETSC_VIEWER_SOCKET_WORLD`,
299db781477SPatrick Sanan           `PETSC_VIEWER_SOCKET_SELF`, `PetscViewerBinaryWrite()`, `PetscViewerBinaryRead()`, `PetscViewerBinaryWriteStringArray()`,
300db781477SPatrick Sanan           `PetscBinaryViewerGetDescriptor()`, `PetscMatlabEngineCreate()`
3015c6c1daeSBarry Smith @*/
3029371c9d4SSatish Balay PetscErrorCode PetscViewerSocketOpen(MPI_Comm comm, const char machine[], int port, PetscViewer *lab) {
3035c6c1daeSBarry Smith   PetscFunctionBegin;
3049566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
3059566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERSOCKET));
3069566063dSJacob Faibussowitsch   PetscCall(PetscViewerSocketSetConnection(*lab, machine, port));
3075c6c1daeSBarry Smith   PetscFunctionReturn(0);
3085c6c1daeSBarry Smith }
3095c6c1daeSBarry Smith 
3109371c9d4SSatish Balay static PetscErrorCode PetscViewerSetFromOptions_Socket(PetscViewer v, PetscOptionItems *PetscOptionsObject) {
3115c6c1daeSBarry Smith   PetscInt  def = -1;
3125c6c1daeSBarry Smith   char      sdef[256];
3135c6c1daeSBarry Smith   PetscBool tflg;
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith   PetscFunctionBegin;
3165c6c1daeSBarry Smith   /*
3175c6c1daeSBarry Smith        These options are not processed here, they are processed in PetscViewerSocketSetConnection(), they
3185c6c1daeSBarry Smith     are listed here for the GUI to display
3195c6c1daeSBarry Smith   */
320d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "Socket PetscViewer Options");
3219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_PORT", sdef, 16, &tflg));
3225c6c1daeSBarry Smith   if (tflg) {
3239566063dSJacob Faibussowitsch     PetscCall(PetscOptionsStringToInt(sdef, &def));
324a297a907SKarl Rupp   } else def = PETSCSOCKETDEFAULTPORT;
3259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsInt("-viewer_socket_port", "Port number to use for socket", "PetscViewerSocketSetConnection", def, NULL, NULL));
3265c6c1daeSBarry Smith 
3279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-viewer_socket_machine", "Machine to use for socket", "PetscViewerSocketSetConnection", sdef, NULL, sizeof(sdef), NULL));
3289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_MACHINE", sdef, sizeof(sdef), &tflg));
32948a46eb9SPierre Jolivet   if (!tflg) PetscCall(PetscGetHostName(sdef, sizeof(sdef)));
330d0609cedSBarry Smith   PetscOptionsHeadEnd();
3315c6c1daeSBarry Smith   PetscFunctionReturn(0);
3325c6c1daeSBarry Smith }
3335c6c1daeSBarry Smith 
3349371c9d4SSatish Balay static PetscErrorCode PetscViewerBinaryGetSkipHeader_Socket(PetscViewer viewer, PetscBool *skip) {
3354c9f2355SSatish Balay   PetscViewer_Socket *vsocket = (PetscViewer_Socket *)viewer->data;
3364c9f2355SSatish Balay 
3374c9f2355SSatish Balay   PetscFunctionBegin;
3384c9f2355SSatish Balay   *skip = vsocket->skipheader;
3394c9f2355SSatish Balay   PetscFunctionReturn(0);
3404c9f2355SSatish Balay }
3414c9f2355SSatish Balay 
3429371c9d4SSatish Balay static PetscErrorCode PetscViewerBinarySetSkipHeader_Socket(PetscViewer viewer, PetscBool skip) {
3434c9f2355SSatish Balay   PetscViewer_Socket *vsocket = (PetscViewer_Socket *)viewer->data;
3444c9f2355SSatish Balay 
3454c9f2355SSatish Balay   PetscFunctionBegin;
3464c9f2355SSatish Balay   vsocket->skipheader = skip;
3474c9f2355SSatish Balay   PetscFunctionReturn(0);
3484c9f2355SSatish Balay }
3494c9f2355SSatish Balay 
3509371c9d4SSatish Balay static PetscErrorCode PetscViewerBinaryGetFlowControl_Socket(PetscViewer viewer, PetscInt *fc) {
3514c9f2355SSatish Balay   PetscFunctionBegin;
3524c9f2355SSatish Balay   *fc = 0;
3534c9f2355SSatish Balay   PetscFunctionReturn(0);
3544c9f2355SSatish Balay }
3554c9f2355SSatish Balay 
3568556b5ebSBarry Smith /*MC
3578556b5ebSBarry Smith    PETSCVIEWERSOCKET - A viewer that writes to a Unix socket
3588556b5ebSBarry Smith 
359811af0c4SBarry Smith   Level: beginner
360811af0c4SBarry Smith 
361811af0c4SBarry Smith .seealso: `PETSC_VIEWERBINARY`, `PetscViewerSocketOpen()`, `PetscViewerDrawOpen()`, `PETSC_VIEWER_DRAW_()`, `PETSC_VIEWER_DRAW_SELF`, `PETSC_VIEWER_DRAW_WORLD`,
362db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PETSCVIEWERBINARY`, `PETSCVIEWERDRAW`,
363db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERASCII`, `PETSCVIEWERMATLAB`,
364db781477SPatrick Sanan           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
3658556b5ebSBarry Smith M*/
3668556b5ebSBarry Smith 
3679371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscViewerCreate_Socket(PetscViewer v) {
3685c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab;
3695c6c1daeSBarry Smith 
3705c6c1daeSBarry Smith   PetscFunctionBegin;
371*4dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&vmatlab));
3725c6c1daeSBarry Smith   vmatlab->port          = 0;
3735c6c1daeSBarry Smith   v->data                = (void *)vmatlab;
3745c6c1daeSBarry Smith   v->ops->destroy        = PetscViewerDestroy_Socket;
37502c9f0b5SLisandro Dalcin   v->ops->flush          = NULL;
3765c6c1daeSBarry Smith   v->ops->setfromoptions = PetscViewerSetFromOptions_Socket;
3775c6c1daeSBarry Smith 
3785c6c1daeSBarry Smith   /* lie and say this is a binary viewer; then all the XXXView_Binary() methods will work correctly on it */
3799566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)v, PETSCVIEWERBINARY));
3809566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerBinarySetSkipHeader_C", PetscViewerBinarySetSkipHeader_Socket));
3819566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerBinaryGetSkipHeader_C", PetscViewerBinaryGetSkipHeader_Socket));
3829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)v, "PetscViewerBinaryGetFlowControl_C", PetscViewerBinaryGetFlowControl_Socket));
3834c9f2355SSatish Balay 
3845c6c1daeSBarry Smith   PetscFunctionReturn(0);
3855c6c1daeSBarry Smith }
3865c6c1daeSBarry Smith 
3875c6c1daeSBarry Smith /*@C
3885c6c1daeSBarry Smith       PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket
3895c6c1daeSBarry Smith              viewer is to use
3905c6c1daeSBarry Smith 
391811af0c4SBarry Smith   Logically Collective on v
3925c6c1daeSBarry Smith 
3935c6c1daeSBarry Smith   Input Parameters:
3945c6c1daeSBarry Smith +   v - viewer to connect
3950298fd71SBarry Smith .   machine - host to connect to, use NULL for the local machine,use "server" to passively wait for
3965c6c1daeSBarry Smith              a connection from elsewhere
397811af0c4SBarry Smith -   port - the port on the machine one is connecting to, use `PETSC_DEFAULT` for default
3985c6c1daeSBarry Smith 
3995c6c1daeSBarry Smith     Level: advanced
4005c6c1daeSBarry Smith 
401811af0c4SBarry Smith .seealso: `PETSCVIEWERMATLAB`, `PETSCVIEWERSOCKET`, `PetscViewerSocketOpen()`
4025c6c1daeSBarry Smith @*/
4039371c9d4SSatish Balay PetscErrorCode PetscViewerSocketSetConnection(PetscViewer v, const char machine[], int port) {
4045c6c1daeSBarry Smith   PetscMPIInt         rank;
4055c6c1daeSBarry Smith   char                mach[256];
4065c6c1daeSBarry Smith   PetscBool           tflg;
4073ca90d2dSJacob Faibussowitsch   PetscViewer_Socket *vmatlab;
4085c6c1daeSBarry Smith 
4095c6c1daeSBarry Smith   PetscFunctionBegin;
4103ca90d2dSJacob Faibussowitsch   PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 1);
4113ca90d2dSJacob Faibussowitsch   if (machine) PetscValidCharPointer(machine, 2);
4123ca90d2dSJacob Faibussowitsch   vmatlab = (PetscViewer_Socket *)v->data;
4135c6c1daeSBarry Smith   /* PetscValidLogicalCollectiveInt(v,port,3); not a PetscInt */
4145c6c1daeSBarry Smith   if (port <= 0) {
4155c6c1daeSBarry Smith     char portn[16];
4169566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_PORT", portn, 16, &tflg));
4175c6c1daeSBarry Smith     if (tflg) {
4185c6c1daeSBarry Smith       PetscInt pport;
4199566063dSJacob Faibussowitsch       PetscCall(PetscOptionsStringToInt(portn, &pport));
4205c6c1daeSBarry Smith       port = (int)pport;
421a297a907SKarl Rupp     } else port = PETSCSOCKETDEFAULTPORT;
4225c6c1daeSBarry Smith   }
4235c6c1daeSBarry Smith   if (!machine) {
4249566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetenv(PetscObjectComm((PetscObject)v), "PETSC_VIEWER_SOCKET_MACHINE", mach, sizeof(mach), &tflg));
42548a46eb9SPierre Jolivet     if (!tflg) PetscCall(PetscGetHostName(mach, sizeof(mach)));
4265c6c1daeSBarry Smith   } else {
4279566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(mach, machine, sizeof(mach)));
4285c6c1daeSBarry Smith   }
4295c6c1daeSBarry Smith 
4309566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank));
431dd400576SPatrick Sanan   if (rank == 0) {
4329566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(mach, "server", &tflg));
4335c6c1daeSBarry Smith     if (tflg) {
4345c6c1daeSBarry Smith       int listenport;
4359566063dSJacob Faibussowitsch       PetscCall(PetscInfo(v, "Waiting for connection from socket process on port %d\n", port));
4369566063dSJacob Faibussowitsch       PetscCall(PetscSocketEstablish(port, &listenport));
4379566063dSJacob Faibussowitsch       PetscCall(PetscSocketListen(listenport, &vmatlab->port));
4385c6c1daeSBarry Smith       close(listenport);
4395c6c1daeSBarry Smith     } else {
4409566063dSJacob Faibussowitsch       PetscCall(PetscInfo(v, "Connecting to socket process on port %d machine %s\n", port, mach));
4419566063dSJacob Faibussowitsch       PetscCall(PetscOpenSocket(mach, port, &vmatlab->port));
4425c6c1daeSBarry Smith     }
4435c6c1daeSBarry Smith   }
4445c6c1daeSBarry Smith   PetscFunctionReturn(0);
4455c6c1daeSBarry Smith }
4465c6c1daeSBarry Smith 
4475c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
4485c6c1daeSBarry Smith /*
4495c6c1daeSBarry Smith     The variable Petsc_Viewer_Socket_keyval is used to indicate an MPI attribute that
4505c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
4515c6c1daeSBarry Smith */
452d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Socket_keyval = MPI_KEYVAL_INVALID;
4535c6c1daeSBarry Smith 
4545c6c1daeSBarry Smith /*@C
4555c6c1daeSBarry Smith      PETSC_VIEWER_SOCKET_ - Creates a socket viewer shared by all processors in a communicator.
4565c6c1daeSBarry Smith 
457d083f849SBarry Smith      Collective
4585c6c1daeSBarry Smith 
4595c6c1daeSBarry Smith      Input Parameter:
460811af0c4SBarry Smith .    comm - the MPI communicator to share the  `PETSCVIEWERSOCKET` `PetscViewer`
4615c6c1daeSBarry Smith 
4625c6c1daeSBarry Smith      Level: intermediate
4635c6c1daeSBarry Smith 
4645c6c1daeSBarry Smith    Options Database Keys:
465811af0c4SBarry Smith    For use with the default `PETSC_VIEWER_SOCKET_WORLD` or if
466811af0c4SBarry Smith     NULL is passed for machine or `PETSC_DEFAULT` is passed for port
4675c6c1daeSBarry Smith $    -viewer_socket_machine <machine>
4685c6c1daeSBarry Smith $    -viewer_socket_port <port>
4695c6c1daeSBarry Smith 
4705c6c1daeSBarry Smith    Environmental variables:
471811af0c4SBarry Smith +   `PETSC_VIEWER_SOCKET_PORT` - portnumber
472811af0c4SBarry Smith -   `PETSC_VIEWER_SOCKET_MACHINE` - machine name
4735c6c1daeSBarry Smith 
4745c6c1daeSBarry Smith      Notes:
475811af0c4SBarry Smith      Unlike almost all other PETSc routines, `PETSC_VIEWER_SOCKET_()` does not return
476811af0c4SBarry Smith      an error code, it returns NULL if it fails. The  `PETSCVIEWERSOCKET`  `PetscViewer` is usually used in the form
4775c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_SOCKET_(comm));
4785c6c1daeSBarry Smith 
4795c6c1daeSBarry Smith      Currently the only socket client available is MATLAB. See
480c4762a1bSJed Brown      src/dm/tests/ex12.c and ex12.m for an example of usage.
4815c6c1daeSBarry Smith 
482811af0c4SBarry Smith      Connects to a waiting socket and stays connected until `PetscViewerDestroy()` is called.
4835c6c1daeSBarry Smith 
484811af0c4SBarry Smith      Use this for communicating with an interactive MATLAB session, see `PETSC_VIEWER_MATLAB_()` for writing output to a
485811af0c4SBarry Smith      .mat file. Use `PetscMatlabEngineCreate()` or `PETSC_MATLAB_ENGINE_()`, `PETSC_MATLAB_ENGINE_SELF`, or `PETSC_MATLAB_ENGINE_WORLD`
486a30fc760SBarry Smith      for communicating with a MATLAB Engine
4875c6c1daeSBarry Smith 
488811af0c4SBarry Smith .seealso: `PETSCVIEWERMATLAB`, `PETSCVIEWERSOCKET`, `PETSC_VIEWER_SOCKET_WORLD`, `PETSC_VIEWER_SOCKET_SELF`, `PetscViewerSocketOpen()`, `PetscViewerCreate()`,
489db781477SPatrick Sanan           `PetscViewerSocketSetConnection()`, `PetscViewerDestroy()`, `PETSC_VIEWER_SOCKET_()`, `PetscViewerBinaryWrite()`, `PetscViewerBinaryRead()`,
490db781477SPatrick Sanan           `PetscViewerBinaryWriteStringArray()`, `PetscViewerBinaryGetDescriptor()`, `PETSC_VIEWER_MATLAB_()`
4915c6c1daeSBarry Smith @*/
4929371c9d4SSatish Balay PetscViewer PETSC_VIEWER_SOCKET_(MPI_Comm comm) {
4935c6c1daeSBarry Smith   PetscErrorCode ierr;
4945c6c1daeSBarry Smith   PetscBool      flg;
4955c6c1daeSBarry Smith   PetscViewer    viewer;
4965c6c1daeSBarry Smith   MPI_Comm       ncomm;
4975c6c1daeSBarry Smith 
4985c6c1daeSBarry Smith   PetscFunctionBegin;
4999371c9d4SSatish Balay   ierr = PetscCommDuplicate(comm, &ncomm, NULL);
5009371c9d4SSatish Balay   if (ierr) {
5019371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
5029371c9d4SSatish Balay     PetscFunctionReturn(NULL);
5039371c9d4SSatish Balay   }
5045c6c1daeSBarry Smith   if (Petsc_Viewer_Socket_keyval == MPI_KEYVAL_INVALID) {
50502c9f0b5SLisandro Dalcin     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Socket_keyval, NULL);
5069371c9d4SSatish Balay     if (ierr) {
5079371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
5089371c9d4SSatish Balay       PetscFunctionReturn(NULL);
5099371c9d4SSatish Balay     }
5105c6c1daeSBarry Smith   }
51147435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm, Petsc_Viewer_Socket_keyval, (void **)&viewer, (int *)&flg);
5129371c9d4SSatish Balay   if (ierr) {
5139371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
5149371c9d4SSatish Balay     PetscFunctionReturn(NULL);
5159371c9d4SSatish Balay   }
5165c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
51702c9f0b5SLisandro Dalcin     ierr = PetscViewerSocketOpen(ncomm, NULL, 0, &viewer);
5189371c9d4SSatish Balay     if (ierr) {
5199371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
5209371c9d4SSatish Balay       PetscFunctionReturn(NULL);
5219371c9d4SSatish Balay     }
5225c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
5239371c9d4SSatish Balay     if (ierr) {
5249371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
5259371c9d4SSatish Balay       PetscFunctionReturn(NULL);
5269371c9d4SSatish Balay     }
52747435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm, Petsc_Viewer_Socket_keyval, (void *)viewer);
5289371c9d4SSatish Balay     if (ierr) {
5299371c9d4SSatish Balay       PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
5309371c9d4SSatish Balay       PetscFunctionReturn(NULL);
5319371c9d4SSatish Balay     }
5325c6c1daeSBarry Smith   }
5335c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
5349371c9d4SSatish Balay   if (ierr) {
5359371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_SOCKET_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_REPEAT, " ");
5369371c9d4SSatish Balay     PetscFunctionReturn(NULL);
5379371c9d4SSatish Balay   }
5385c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
5395c6c1daeSBarry Smith }
540