xref: /petsc/src/sys/classes/viewer/impls/socket/send.c (revision 8cc058d9cd56c1ccb3be12a47760ddfc446aaffc)
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)
47*8cc058d9SJed Brown PETSC_EXTERN int close(int);
485c6c1daeSBarry Smith #endif
495c6c1daeSBarry Smith #if defined(PETSC_NEED_SOCKET_PROTO)
50*8cc058d9SJed Brown PETSC_EXTERN int socket(int,int,int);
515c6c1daeSBarry Smith #endif
525c6c1daeSBarry Smith #if defined(PETSC_NEED_SLEEP_PROTO)
53*8cc058d9SJed Brown PETSC_EXTERN int sleep(unsigned);
545c6c1daeSBarry Smith #endif
555c6c1daeSBarry Smith #if defined(PETSC_NEED_CONNECT_PROTO)
56*8cc058d9SJed Brown PETSC_EXTERN int connect(int,struct sockaddr*,int);
575c6c1daeSBarry Smith #endif
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith /*--------------------------------------------------------------*/
605c6c1daeSBarry Smith #undef __FUNCT__
615c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Socket"
625c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_Socket(PetscViewer viewer)
635c6c1daeSBarry Smith {
645c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)viewer->data;
655c6c1daeSBarry Smith   PetscErrorCode     ierr;
665c6c1daeSBarry Smith 
675c6c1daeSBarry Smith   PetscFunctionBegin;
685c6c1daeSBarry Smith   if (vmatlab->port) {
695c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET)
705c6c1daeSBarry Smith     ierr = closesocket(vmatlab->port);
715c6c1daeSBarry Smith #else
725c6c1daeSBarry Smith     ierr = close(vmatlab->port);
735c6c1daeSBarry Smith #endif
745c6c1daeSBarry Smith     if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"System error closing socket");
755c6c1daeSBarry Smith   }
765c6c1daeSBarry Smith   ierr = PetscFree(vmatlab);CHKERRQ(ierr);
775c6c1daeSBarry Smith   PetscFunctionReturn(0);
785c6c1daeSBarry Smith }
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith /*--------------------------------------------------------------*/
815c6c1daeSBarry Smith #undef __FUNCT__
825c6c1daeSBarry Smith #define __FUNCT__ "PetscOpenSocket"
835c6c1daeSBarry Smith /*
845c6c1daeSBarry Smith     PetscSocketOpen - handles connected to an open port where someone is waiting.
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith .seealso:   PetscSocketListen(), PetscSocketEstablish()
875c6c1daeSBarry Smith */
885c6c1daeSBarry Smith PetscErrorCode  PetscOpenSocket(char *hostname,int portnum,int *t)
895c6c1daeSBarry Smith {
905c6c1daeSBarry Smith   struct sockaddr_in sa;
915c6c1daeSBarry Smith   struct hostent     *hp;
925c6c1daeSBarry Smith   int                s = 0;
935c6c1daeSBarry Smith   PetscErrorCode     ierr;
945c6c1daeSBarry Smith   PetscBool          flg = PETSC_TRUE;
955c6c1daeSBarry Smith 
965c6c1daeSBarry Smith   PetscFunctionBegin;
975c6c1daeSBarry Smith   if (!(hp=gethostbyname(hostname))) {
985c6c1daeSBarry Smith     perror("SEND: error gethostbyname: ");
995c6c1daeSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error open connection to %s",hostname);
1005c6c1daeSBarry Smith   }
1015c6c1daeSBarry Smith   ierr = PetscMemzero(&sa,sizeof(sa));CHKERRQ(ierr);
1025c6c1daeSBarry Smith   ierr = PetscMemcpy(&sa.sin_addr,hp->h_addr_list[0],hp->h_length);CHKERRQ(ierr);
1035c6c1daeSBarry Smith 
1045c6c1daeSBarry Smith   sa.sin_family = hp->h_addrtype;
1055c6c1daeSBarry Smith   sa.sin_port   = htons((u_short) portnum);
1065c6c1daeSBarry Smith   while (flg) {
1075c6c1daeSBarry Smith     if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) {
1085c6c1daeSBarry Smith       perror("SEND: error socket");  SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
1095c6c1daeSBarry Smith     }
1105c6c1daeSBarry Smith     if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
1115c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR)
1125c6c1daeSBarry Smith       ierr = WSAGetLastError();
113a297a907SKarl Rupp       if (ierr == WSAEADDRINUSE)    (*PetscErrorPrintf)("SEND: address is in use\n");
114a297a907SKarl Rupp       else if (ierr == WSAEALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
115a297a907SKarl Rupp       else if (ierr == WSAEISCONN) {
1165c6c1daeSBarry Smith         (*PetscErrorPrintf)("SEND: socket already connected\n");
1175c6c1daeSBarry Smith         Sleep((unsigned) 1);
1185c6c1daeSBarry Smith       } else if (ierr == WSAECONNREFUSED) {
1195c6c1daeSBarry Smith         /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
1205c6c1daeSBarry Smith         Sleep((unsigned) 1);
1215c6c1daeSBarry Smith       } else {
1225c6c1daeSBarry Smith         perror(NULL); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
1235c6c1daeSBarry Smith       }
1245c6c1daeSBarry Smith #else
125a297a907SKarl Rupp       if (errno == EADDRINUSE)    (*PetscErrorPrintf)("SEND: address is in use\n");
126a297a907SKarl Rupp       else if (errno == EALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
127a297a907SKarl Rupp       else if (errno == EISCONN) {
1285c6c1daeSBarry Smith         (*PetscErrorPrintf)("SEND: socket already connected\n");
1295c6c1daeSBarry Smith         sleep((unsigned) 1);
1305c6c1daeSBarry Smith       } else if (errno == ECONNREFUSED) {
1315c6c1daeSBarry Smith         /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
1325c6c1daeSBarry Smith         ierr = PetscInfo(0,"Connection refused in attaching socket, trying again");CHKERRQ(ierr);
1335c6c1daeSBarry Smith         sleep((unsigned) 1);
1345c6c1daeSBarry Smith       } else {
1355c6c1daeSBarry Smith         perror(NULL); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
1365c6c1daeSBarry Smith       }
1375c6c1daeSBarry Smith #endif
1385c6c1daeSBarry Smith       flg = PETSC_TRUE;
1395c6c1daeSBarry Smith #if defined(PETSC_HAVE_CLOSESOCKET)
1405c6c1daeSBarry Smith       closesocket(s);
1415c6c1daeSBarry Smith #else
1425c6c1daeSBarry Smith       close(s);
1435c6c1daeSBarry Smith #endif
144a297a907SKarl Rupp     } else flg = PETSC_FALSE;
1455c6c1daeSBarry Smith   }
1465c6c1daeSBarry Smith   *t = s;
1475c6c1daeSBarry Smith   PetscFunctionReturn(0);
1485c6c1daeSBarry Smith }
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith #define MAXHOSTNAME 100
1515c6c1daeSBarry Smith #undef __FUNCT__
1525c6c1daeSBarry Smith #define __FUNCT__ "PetscSocketEstablish"
1535c6c1daeSBarry Smith /*
1545c6c1daeSBarry Smith    PetscSocketEstablish - starts a listener on a socket
1555c6c1daeSBarry Smith 
1565c6c1daeSBarry Smith .seealso:   PetscSocketListen()
1575c6c1daeSBarry Smith */
1585c6c1daeSBarry Smith PetscErrorCode PetscSocketEstablish(int portnum,int *ss)
1595c6c1daeSBarry Smith {
1605c6c1daeSBarry Smith   char               myname[MAXHOSTNAME+1];
1615c6c1daeSBarry Smith   int                s;
1625c6c1daeSBarry Smith   PetscErrorCode     ierr;
1635c6c1daeSBarry Smith   struct sockaddr_in sa;
1645c6c1daeSBarry Smith   struct hostent     *hp;
1655c6c1daeSBarry Smith 
1665c6c1daeSBarry Smith   PetscFunctionBegin;
1675c6c1daeSBarry Smith   ierr = PetscGetHostName(myname,MAXHOSTNAME);CHKERRQ(ierr);
1685c6c1daeSBarry Smith 
1695c6c1daeSBarry Smith   ierr = PetscMemzero(&sa,sizeof(struct sockaddr_in));CHKERRQ(ierr);
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith   hp = gethostbyname(myname);
1725c6c1daeSBarry Smith   if (!hp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Unable to get hostent information from system");
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith   sa.sin_family = hp->h_addrtype;
1755c6c1daeSBarry Smith   sa.sin_port   = htons((u_short)portnum);
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith   if ((s = socket(AF_INET,SOCK_STREAM,0)) < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Error running socket() command");
1785c6c1daeSBarry Smith #if defined(PETSC_HAVE_SO_REUSEADDR)
1795c6c1daeSBarry Smith   {
1805c6c1daeSBarry Smith     int optval = 1; /* Turn on the option */
1815c6c1daeSBarry Smith     ierr = setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&optval,sizeof(optval));CHKERRQ(ierr);
1825c6c1daeSBarry Smith   }
1835c6c1daeSBarry Smith #endif
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith   while (bind(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
1865c6c1daeSBarry Smith #if defined(PETSC_HAVE_WSAGETLASTERROR)
1875c6c1daeSBarry Smith     ierr = WSAGetLastError();
1885c6c1daeSBarry Smith     if (ierr != WSAEADDRINUSE) {
1895c6c1daeSBarry Smith #else
1905c6c1daeSBarry Smith     if (errno != EADDRINUSE) {
1915c6c1daeSBarry Smith #endif
1925c6c1daeSBarry Smith       close(s);
1935c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Error from bind()");
1945c6c1daeSBarry Smith     }
1955c6c1daeSBarry Smith   }
1965c6c1daeSBarry Smith   listen(s,0);
1975c6c1daeSBarry Smith   *ss = s;
1985c6c1daeSBarry Smith   return(0);
1995c6c1daeSBarry Smith }
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith #undef __FUNCT__
2025c6c1daeSBarry Smith #define __FUNCT__ "PetscSocketListen"
2035c6c1daeSBarry Smith /*
2045c6c1daeSBarry Smith    PetscSocketListens - Listens at a socket created with PetscSocketEstablish()
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith .seealso:   PetscSocketEstablish()
2075c6c1daeSBarry Smith */
2085c6c1daeSBarry Smith PetscErrorCode PetscSocketListen(int listenport,int *t)
2095c6c1daeSBarry Smith {
2105c6c1daeSBarry Smith   struct sockaddr_in isa;
2115c6c1daeSBarry Smith #if defined(PETSC_HAVE_ACCEPT_SIZE_T)
2125c6c1daeSBarry Smith   size_t             i;
2135c6c1daeSBarry Smith #else
2145c6c1daeSBarry Smith   int                i;
2155c6c1daeSBarry Smith #endif
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith   PetscFunctionBegin;
2185c6c1daeSBarry Smith   /* wait for someone to try to connect */
2195c6c1daeSBarry Smith   i = sizeof(struct sockaddr_in);
2205c6c1daeSBarry Smith   if ((*t = accept(listenport,(struct sockaddr*)&isa,(socklen_t*)&i)) < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"error from accept()\n");
2215c6c1daeSBarry Smith   PetscFunctionReturn(0);
2225c6c1daeSBarry Smith }
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith #undef __FUNCT__
2255c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSocketOpen"
2265c6c1daeSBarry Smith /*@C
2275c6c1daeSBarry Smith    PetscViewerSocketOpen - Opens a connection to a MATLAB or other socket
2285c6c1daeSBarry Smith         based server.
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith    Collective on MPI_Comm
2315c6c1daeSBarry Smith 
2325c6c1daeSBarry Smith    Input Parameters:
2335c6c1daeSBarry Smith +  comm - the MPI communicator
2340298fd71SBarry Smith .  machine - the machine the server is running on,, use NULL for the local machine, use "server" to passively wait for
2355c6c1daeSBarry Smith              a connection from elsewhere
2365c6c1daeSBarry Smith -  port - the port to connect to, use PETSC_DEFAULT for the default
2375c6c1daeSBarry Smith 
2385c6c1daeSBarry Smith    Output Parameter:
2395c6c1daeSBarry Smith .  lab - a context to use when communicating with the server
2405c6c1daeSBarry Smith 
2415c6c1daeSBarry Smith    Level: intermediate
2425c6c1daeSBarry Smith 
2435c6c1daeSBarry Smith    Notes:
2445c6c1daeSBarry Smith    Most users should employ the following commands to access the
2455c6c1daeSBarry Smith    MATLAB PetscViewers
2465c6c1daeSBarry Smith $
2475c6c1daeSBarry Smith $    PetscViewerSocketOpen(MPI_Comm comm, char *machine,int port,PetscViewer &viewer)
2485c6c1daeSBarry Smith $    MatView(Mat matrix,PetscViewer viewer)
2495c6c1daeSBarry Smith $
2505c6c1daeSBarry Smith $                or
2515c6c1daeSBarry Smith $
2525c6c1daeSBarry Smith $    PetscViewerSocketOpen(MPI_Comm comm,char *machine,int port,PetscViewer &viewer)
2535c6c1daeSBarry Smith $    VecView(Vec vector,PetscViewer viewer)
2545c6c1daeSBarry Smith 
2555c6c1daeSBarry Smith    Options Database Keys:
2565c6c1daeSBarry Smith    For use with  PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF,
2575c6c1daeSBarry Smith    PETSC_VIEWER_SOCKET_() or if
2580298fd71SBarry Smith     NULL is passed for machine or PETSC_DEFAULT is passed for port
2595c6c1daeSBarry Smith $    -viewer_socket_machine <machine>
2605c6c1daeSBarry Smith $    -viewer_socket_port <port>
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith    Environmental variables:
2635c6c1daeSBarry Smith +   PETSC_VIEWER_SOCKET_PORT portnumber
2645c6c1daeSBarry Smith -   PETSC_VIEWER_SOCKET_MACHINE machine name
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith      Currently the only socket client available is MATLAB. See
2675c6c1daeSBarry Smith      src/dm/da/examples/tests/ex12.c and ex12.m for an example of usage.
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith    Notes: The socket viewer is in some sense a subclass of the binary viewer, to read and write to the socket
2705c6c1daeSBarry Smith           use PetscViewerBinaryRead/Write/GetDescriptor().
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith    Concepts: MATLAB^sending data
2735c6c1daeSBarry Smith    Concepts: sockets^sending data
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerCreate(), PetscViewerSetType(),
2765c6c1daeSBarry Smith           PetscViewerSocketSetConnection(), PETSC_VIEWER_SOCKET_, PETSC_VIEWER_SOCKET_WORLD,
2775c6c1daeSBarry Smith           PETSC_VIEWER_SOCKET_SELF, PetscViewerBinaryWrite(), PetscViewerBinaryRead(), PetscViewerBinaryWriteStringArray(),
2785c6c1daeSBarry Smith           PetscBinaryViewerGetDescriptor()
2795c6c1daeSBarry Smith @*/
2805c6c1daeSBarry Smith PetscErrorCode  PetscViewerSocketOpen(MPI_Comm comm,const char machine[],int port,PetscViewer *lab)
2815c6c1daeSBarry Smith {
2825c6c1daeSBarry Smith   PetscErrorCode ierr;
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith   PetscFunctionBegin;
2855c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
2865c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERSOCKET);CHKERRQ(ierr);
2875c6c1daeSBarry Smith   ierr = PetscViewerSocketSetConnection(*lab,machine,port);CHKERRQ(ierr);
2885c6c1daeSBarry Smith   PetscFunctionReturn(0);
2895c6c1daeSBarry Smith }
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith #undef __FUNCT__
2925c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetFromOptions_Socket"
2935c6c1daeSBarry Smith PetscErrorCode PetscViewerSetFromOptions_Socket(PetscViewer v)
2945c6c1daeSBarry Smith {
2955c6c1daeSBarry Smith   PetscErrorCode ierr;
2965c6c1daeSBarry Smith   PetscInt       def = -1;
2975c6c1daeSBarry Smith   char           sdef[256];
2985c6c1daeSBarry Smith   PetscBool      tflg;
2995c6c1daeSBarry Smith 
3005c6c1daeSBarry Smith   PetscFunctionBegin;
3015c6c1daeSBarry Smith   /*
3025c6c1daeSBarry Smith        These options are not processed here, they are processed in PetscViewerSocketSetConnection(), they
3035c6c1daeSBarry Smith     are listed here for the GUI to display
3045c6c1daeSBarry Smith   */
3055c6c1daeSBarry Smith   ierr = PetscOptionsHead("Socket PetscViewer Options");CHKERRQ(ierr);
306ce94432eSBarry Smith   ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",sdef,16,&tflg);CHKERRQ(ierr);
3075c6c1daeSBarry Smith   if (tflg) {
3085c6c1daeSBarry Smith     ierr = PetscOptionsStringToInt(sdef,&def);CHKERRQ(ierr);
309a297a907SKarl Rupp   } else def = PETSCSOCKETDEFAULTPORT;
3105c6c1daeSBarry Smith   ierr = PetscOptionsInt("-viewer_socket_port","Port number to use for socket","PetscViewerSocketSetConnection",def,0,0);CHKERRQ(ierr);
3115c6c1daeSBarry Smith 
3125c6c1daeSBarry Smith   ierr = PetscOptionsString("-viewer_socket_machine","Machine to use for socket","PetscViewerSocketSetConnection",sdef,0,0,0);CHKERRQ(ierr);
313ce94432eSBarry Smith   ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",sdef,256,&tflg);CHKERRQ(ierr);
3145c6c1daeSBarry Smith   if (!tflg) {
3155c6c1daeSBarry Smith     ierr = PetscGetHostName(sdef,256);CHKERRQ(ierr);
3165c6c1daeSBarry Smith   }
3175c6c1daeSBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
3185c6c1daeSBarry Smith   PetscFunctionReturn(0);
3195c6c1daeSBarry Smith }
3205c6c1daeSBarry Smith 
3215c6c1daeSBarry Smith #undef __FUNCT__
3225c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Socket"
323*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Socket(PetscViewer v)
3245c6c1daeSBarry Smith {
3255c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab;
3265c6c1daeSBarry Smith   PetscErrorCode     ierr;
3275c6c1daeSBarry Smith 
3285c6c1daeSBarry Smith   PetscFunctionBegin;
3295c6c1daeSBarry Smith   ierr                   = PetscNewLog(v,PetscViewer_Socket,&vmatlab);CHKERRQ(ierr);
3305c6c1daeSBarry Smith   vmatlab->port          = 0;
3315c6c1daeSBarry Smith   v->data                = (void*)vmatlab;
3325c6c1daeSBarry Smith   v->ops->destroy        = PetscViewerDestroy_Socket;
3335c6c1daeSBarry Smith   v->ops->flush          = 0;
3345c6c1daeSBarry Smith   v->ops->setfromoptions = PetscViewerSetFromOptions_Socket;
3355c6c1daeSBarry Smith 
3365c6c1daeSBarry Smith   /* lie and say this is a binary viewer; then all the XXXView_Binary() methods will work correctly on it */
3375c6c1daeSBarry Smith   ierr                   = PetscObjectChangeTypeName((PetscObject)v,PETSCVIEWERBINARY);CHKERRQ(ierr);
3385c6c1daeSBarry Smith   PetscFunctionReturn(0);
3395c6c1daeSBarry Smith }
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith #undef __FUNCT__
3425c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSocketSetConnection"
3435c6c1daeSBarry Smith /*@C
3445c6c1daeSBarry Smith       PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket
3455c6c1daeSBarry Smith              viewer is to use
3465c6c1daeSBarry Smith 
3475c6c1daeSBarry Smith   Logically Collective on PetscViewer
3485c6c1daeSBarry Smith 
3495c6c1daeSBarry Smith   Input Parameters:
3505c6c1daeSBarry Smith +   v - viewer to connect
3510298fd71SBarry Smith .   machine - host to connect to, use NULL for the local machine,use "server" to passively wait for
3525c6c1daeSBarry Smith              a connection from elsewhere
3535c6c1daeSBarry Smith -   port - the port on the machine one is connecting to, use PETSC_DEFAULT for default
3545c6c1daeSBarry Smith 
3555c6c1daeSBarry Smith     Level: advanced
3565c6c1daeSBarry Smith 
3575c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen()
3585c6c1daeSBarry Smith @*/
3595c6c1daeSBarry Smith PetscErrorCode  PetscViewerSocketSetConnection(PetscViewer v,const char machine[],int port)
3605c6c1daeSBarry Smith {
3615c6c1daeSBarry Smith   PetscErrorCode     ierr;
3625c6c1daeSBarry Smith   PetscMPIInt        rank;
3635c6c1daeSBarry Smith   char               mach[256];
3645c6c1daeSBarry Smith   PetscBool          tflg;
3655c6c1daeSBarry Smith   PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)v->data;
3665c6c1daeSBarry Smith 
3675c6c1daeSBarry Smith   PetscFunctionBegin;
3685c6c1daeSBarry Smith   /* PetscValidLogicalCollectiveInt(v,port,3); not a PetscInt */
3695c6c1daeSBarry Smith   if (port <= 0) {
3705c6c1daeSBarry Smith     char portn[16];
371ce94432eSBarry Smith     ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",portn,16,&tflg);CHKERRQ(ierr);
3725c6c1daeSBarry Smith     if (tflg) {
3735c6c1daeSBarry Smith       PetscInt pport;
3745c6c1daeSBarry Smith       ierr = PetscOptionsStringToInt(portn,&pport);CHKERRQ(ierr);
3755c6c1daeSBarry Smith       port = (int)pport;
376a297a907SKarl Rupp     } else port = PETSCSOCKETDEFAULTPORT;
3775c6c1daeSBarry Smith   }
3785c6c1daeSBarry Smith   if (!machine) {
379ce94432eSBarry Smith     ierr = PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",mach,256,&tflg);CHKERRQ(ierr);
3805c6c1daeSBarry Smith     if (!tflg) {
3815c6c1daeSBarry Smith       ierr = PetscGetHostName(mach,256);CHKERRQ(ierr);
3825c6c1daeSBarry Smith     }
3835c6c1daeSBarry Smith   } else {
3845c6c1daeSBarry Smith     ierr = PetscStrncpy(mach,machine,256);CHKERRQ(ierr);
3855c6c1daeSBarry Smith   }
3865c6c1daeSBarry Smith 
387ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr);
3885c6c1daeSBarry Smith   if (!rank) {
3895c6c1daeSBarry Smith     ierr = PetscStrcmp(mach,"server",&tflg);CHKERRQ(ierr);
3905c6c1daeSBarry Smith     if (tflg) {
3915c6c1daeSBarry Smith       int listenport;
3925c6c1daeSBarry Smith       ierr = PetscInfo1(v,"Waiting for connection from socket process on port %D\n",port);CHKERRQ(ierr);
3935c6c1daeSBarry Smith       ierr = PetscSocketEstablish(port,&listenport);CHKERRQ(ierr);
3945c6c1daeSBarry Smith       ierr = PetscSocketListen(listenport,&vmatlab->port);CHKERRQ(ierr);
3955c6c1daeSBarry Smith       close(listenport);
3965c6c1daeSBarry Smith     } else {
3975c6c1daeSBarry Smith       ierr = PetscInfo2(v,"Connecting to socket process on port %D machine %s\n",port,mach);CHKERRQ(ierr);
3985c6c1daeSBarry Smith       ierr = PetscOpenSocket(mach,port,&vmatlab->port);CHKERRQ(ierr);
3995c6c1daeSBarry Smith     }
4005c6c1daeSBarry Smith   }
4015c6c1daeSBarry Smith   PetscFunctionReturn(0);
4025c6c1daeSBarry Smith }
4035c6c1daeSBarry Smith 
4045c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
4055c6c1daeSBarry Smith /*
4065c6c1daeSBarry Smith     The variable Petsc_Viewer_Socket_keyval is used to indicate an MPI attribute that
4075c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
4085c6c1daeSBarry Smith */
4095c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Socket_keyval = MPI_KEYVAL_INVALID;
4105c6c1daeSBarry Smith 
4115c6c1daeSBarry Smith 
4125c6c1daeSBarry Smith #undef __FUNCT__
4135c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_SOCKET_"
4145c6c1daeSBarry Smith /*@C
4155c6c1daeSBarry Smith      PETSC_VIEWER_SOCKET_ - Creates a socket viewer shared by all processors in a communicator.
4165c6c1daeSBarry Smith 
4175c6c1daeSBarry Smith      Collective on MPI_Comm
4185c6c1daeSBarry Smith 
4195c6c1daeSBarry Smith      Input Parameter:
4205c6c1daeSBarry Smith .    comm - the MPI communicator to share the socket PetscViewer
4215c6c1daeSBarry Smith 
4225c6c1daeSBarry Smith      Level: intermediate
4235c6c1daeSBarry Smith 
4245c6c1daeSBarry Smith    Options Database Keys:
4255c6c1daeSBarry Smith    For use with the default PETSC_VIEWER_SOCKET_WORLD or if
4260298fd71SBarry Smith     NULL is passed for machine or PETSC_DEFAULT is passed for port
4275c6c1daeSBarry Smith $    -viewer_socket_machine <machine>
4285c6c1daeSBarry Smith $    -viewer_socket_port <port>
4295c6c1daeSBarry Smith 
4305c6c1daeSBarry Smith    Environmental variables:
4315c6c1daeSBarry Smith +   PETSC_VIEWER_SOCKET_PORT portnumber
4325c6c1daeSBarry Smith -   PETSC_VIEWER_SOCKET_MACHINE machine name
4335c6c1daeSBarry Smith 
4345c6c1daeSBarry Smith      Notes:
4355c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PetscViewer_SOCKET_ does not return
4365c6c1daeSBarry Smith      an error code.  The socket PetscViewer is usually used in the form
4375c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_SOCKET_(comm));
4385c6c1daeSBarry Smith 
4395c6c1daeSBarry Smith      Currently the only socket client available is MATLAB. See
4405c6c1daeSBarry Smith      src/dm/da/examples/tests/ex12.c and ex12.m for an example of usage.
4415c6c1daeSBarry Smith 
4425c6c1daeSBarry Smith      Connects to a waiting socket and stays connected until PetscViewerDestroy() is called.
4435c6c1daeSBarry Smith 
4445c6c1daeSBarry Smith      Use this for communicating with an interactive MATLAB session, see PETSC_VIEWER_MATLAB_() for communicating with the MATLAB engine.
4455c6c1daeSBarry Smith 
4465c6c1daeSBarry Smith .seealso: PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF, PetscViewerSocketOpen(), PetscViewerCreate(),
4475c6c1daeSBarry Smith           PetscViewerSocketSetConnection(), PetscViewerDestroy(), PETSC_VIEWER_SOCKET_(), PetscViewerBinaryWrite(), PetscViewerBinaryRead(),
4485c6c1daeSBarry Smith           PetscViewerBinaryWriteStringArray(), PetscBinaryViewerGetDescriptor(), PETSC_VIEWER_MATLAB_()
4495c6c1daeSBarry Smith @*/
4505c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_SOCKET_(MPI_Comm comm)
4515c6c1daeSBarry Smith {
4525c6c1daeSBarry Smith   PetscErrorCode ierr;
4535c6c1daeSBarry Smith   PetscBool      flg;
4545c6c1daeSBarry Smith   PetscViewer    viewer;
4555c6c1daeSBarry Smith   MPI_Comm       ncomm;
4565c6c1daeSBarry Smith 
4575c6c1daeSBarry Smith   PetscFunctionBegin;
4580298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4595c6c1daeSBarry Smith   if (Petsc_Viewer_Socket_keyval == MPI_KEYVAL_INVALID) {
4605c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Socket_keyval,0);
4615c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4625c6c1daeSBarry Smith   }
4635c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Socket_keyval,(void**)&viewer,(int*)&flg);
4645c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4655c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
4665c6c1daeSBarry Smith     ierr = PetscViewerSocketOpen(ncomm,0,0,&viewer);
4675c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4685c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
4695c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4705c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Socket_keyval,(void*)viewer);
4715c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4725c6c1daeSBarry Smith   }
4735c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
4745c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
4755c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
4765c6c1daeSBarry Smith }
4775c6c1daeSBarry Smith 
4785c6c1daeSBarry Smith #if defined(PETSC_USE_SERVER)
4795c6c1daeSBarry Smith 
4805c6c1daeSBarry Smith #include <pthread.h>
4815c6c1daeSBarry Smith #include <time.h>
4825c6c1daeSBarry Smith #define PROTOCOL   "HTTP/1.1"
4835c6c1daeSBarry Smith #define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith #undef __FUNCT__
4865c6c1daeSBarry Smith #define __FUNCT__ "PetscWebSendHeader"
4875c6c1daeSBarry Smith PetscErrorCode PetscWebSendHeader(FILE *f, int status, const char *title, const char *extra, const char *mime, int length)
4885c6c1daeSBarry Smith {
4895c6c1daeSBarry Smith   time_t now;
4905c6c1daeSBarry Smith   char   timebuf[128];
4915c6c1daeSBarry Smith 
4925c6c1daeSBarry Smith   PetscFunctionBegin;
4935c6c1daeSBarry Smith   fprintf(f, "%s %d %s\r\n", PROTOCOL, status, title);
4945c6c1daeSBarry Smith   fprintf(f, "Server: %s\r\n", "petscserver/1.0");
4955c6c1daeSBarry Smith   now = time(NULL);
4965c6c1daeSBarry Smith   strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
4975c6c1daeSBarry Smith   fprintf(f, "Date: %s\r\n", timebuf);
4985c6c1daeSBarry Smith   if (extra) fprintf(f, "%s\r\n", extra);
4995c6c1daeSBarry Smith   if (mime) fprintf(f, "Content-Type: %s\r\n", mime);
5005c6c1daeSBarry Smith   if (length >= 0) fprintf(f, "Content-Length: %d\r\n", length);
5015c6c1daeSBarry Smith   fprintf(f, "Connection: close\r\n");
5025c6c1daeSBarry Smith   fprintf(f, "\r\n");
5035c6c1daeSBarry Smith   PetscFunctionReturn(0);
5045c6c1daeSBarry Smith }
5055c6c1daeSBarry Smith 
5065c6c1daeSBarry Smith #undef __FUNCT__
5075c6c1daeSBarry Smith #define __FUNCT__ "PetscWebSendFooter"
5085c6c1daeSBarry Smith PetscErrorCode PetscWebSendFooter(FILE *fd)
5095c6c1daeSBarry Smith {
5105c6c1daeSBarry Smith   PetscFunctionBegin;
5115c6c1daeSBarry Smith   fprintf(fd, "</BODY></HTML>\r\n");
5125c6c1daeSBarry Smith   PetscFunctionReturn(0);
5135c6c1daeSBarry Smith }
5145c6c1daeSBarry Smith 
5155c6c1daeSBarry Smith #undef __FUNCT__
5165c6c1daeSBarry Smith #define __FUNCT__ "PetscWebSendError"
5175c6c1daeSBarry Smith PetscErrorCode PetscWebSendError(FILE *f, int status, const char *title, const char *extra, const char *text)
5185c6c1daeSBarry Smith {
5195c6c1daeSBarry Smith   PetscErrorCode ierr;
5205c6c1daeSBarry Smith 
5215c6c1daeSBarry Smith   PetscFunctionBegin;
5225c6c1daeSBarry Smith   ierr = PetscWebSendHeader(f, status, title, extra, "text/html", -1);CHKERRQ(ierr);
5235c6c1daeSBarry Smith   fprintf(f, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\r\n", status, title);
5245c6c1daeSBarry Smith   fprintf(f, "<BODY><H4>%d %s</H4>\r\n", status, title);
5255c6c1daeSBarry Smith   fprintf(f, "%s\r\n", text);
5265c6c1daeSBarry Smith   ierr = PetscWebSendFooter(f);CHKERRQ(ierr);
5275c6c1daeSBarry Smith   PetscFunctionReturn(0);
5285c6c1daeSBarry Smith }
5295c6c1daeSBarry Smith 
5305c6c1daeSBarry Smith #if defined(PETSC_HAVE_AMS)
5315c6c1daeSBarry Smith #undef __FUNCT__
5325c6c1daeSBarry Smith #define __FUNCT__ "PetscAMSDisplayList"
5335c6c1daeSBarry Smith PetscErrorCode PetscAMSDisplayList(FILE *fd)
5345c6c1daeSBarry Smith {
5355c6c1daeSBarry Smith   PetscErrorCode     ierr;
5365c6c1daeSBarry Smith   char               host[256],**comm_list,**mem_list,**fld_list;
5375c6c1daeSBarry Smith   AMS_Comm           ams;
5385c6c1daeSBarry Smith   PetscInt           i = 0,j;
5395c6c1daeSBarry Smith   AMS_Memory_type    mtype;
5405c6c1daeSBarry Smith   AMS_Data_type      dtype;
5415c6c1daeSBarry Smith   AMS_Shared_type    stype;
5425c6c1daeSBarry Smith   AMS_Reduction_type rtype;
5435c6c1daeSBarry Smith   AMS_Memory         memory;
5445c6c1daeSBarry Smith   int                len;
5455c6c1daeSBarry Smith   void               *addr;
5465c6c1daeSBarry Smith 
5475c6c1daeSBarry Smith   ierr = PetscGetHostName(host,256);CHKERRQ(ierr);
5485c6c1daeSBarry Smith   ierr = AMS_Connect(host, -1, &comm_list);CHKERRQ(ierr);
5495c6c1daeSBarry Smith   ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "text/html", -1);CHKERRQ(ierr);
550a297a907SKarl Rupp   if (!comm_list || !comm_list[0]) fprintf(fd, "AMS Communicator not running</p>\r\n");
551a297a907SKarl Rupp   else {
5525c6c1daeSBarry Smith     ierr = AMS_Comm_attach(comm_list[0],&ams);CHKERRQ(ierr);
5535c6c1daeSBarry Smith     ierr = AMS_Comm_get_memory_list(ams,&mem_list);CHKERRQ(ierr);
554a297a907SKarl Rupp     if (!mem_list[0]) fprintf(fd, "AMS Communicator %s has no published memories</p>\r\n",comm_list[0]);
555a297a907SKarl Rupp     else {
5565c6c1daeSBarry Smith       fprintf(fd, "<HTML><HEAD><TITLE>Petsc Application Server</TITLE></HEAD>\r\n<BODY>");
5575c6c1daeSBarry Smith       fprintf(fd,"<ul>\r\n");
5585c6c1daeSBarry Smith       while (mem_list[i]) {
5595c6c1daeSBarry Smith         fprintf(fd,"<li> %s</li>\r\n",mem_list[i]);
5605c6c1daeSBarry Smith         ierr = AMS_Memory_attach(ams,mem_list[i],&memory,NULL);CHKERRQ(ierr);
5615c6c1daeSBarry Smith         ierr = AMS_Memory_get_field_list(memory, &fld_list);CHKERRQ(ierr);
5625c6c1daeSBarry Smith         j    = 0;
5635c6c1daeSBarry Smith         fprintf(fd,"<ul>\r\n");
5645c6c1daeSBarry Smith         while (fld_list[j]) {
5655c6c1daeSBarry Smith           fprintf(fd,"<li> %s",fld_list[j]);
5665c6c1daeSBarry Smith           ierr = AMS_Memory_get_field_info(memory, fld_list[j], &addr, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
5675c6c1daeSBarry Smith           if (len == 1) {
5685c6c1daeSBarry Smith             if (dtype == AMS_INT)         fprintf(fd," %d",*(int*)addr);
5695c6c1daeSBarry Smith             else if (dtype == AMS_STRING) fprintf(fd," %s",*(char**)addr);
5705c6c1daeSBarry Smith           }
5715c6c1daeSBarry Smith           fprintf(fd,"</li>\r\n");
5725c6c1daeSBarry Smith           j++;
5735c6c1daeSBarry Smith         }
5745c6c1daeSBarry Smith         fprintf(fd,"</ul>\r\n");
5755c6c1daeSBarry Smith         i++;
5765c6c1daeSBarry Smith       }
5775c6c1daeSBarry Smith       fprintf(fd,"</ul>\r\n");
5785c6c1daeSBarry Smith     }
5795c6c1daeSBarry Smith   }
5805c6c1daeSBarry Smith   ierr = PetscWebSendFooter(fd);CHKERRQ(ierr);
5815c6c1daeSBarry Smith   ierr = AMS_Disconnect();CHKERRQ(ierr);
5825c6c1daeSBarry Smith   PetscFunctionReturn(0);
5835c6c1daeSBarry Smith }
5845c6c1daeSBarry Smith 
5855c6c1daeSBarry Smith #undef __FUNCT__
5865c6c1daeSBarry Smith #define __FUNCT__ "PetscAMSDisplayTree"
5875c6c1daeSBarry Smith PetscErrorCode PetscAMSDisplayTree(FILE *fd)
5885c6c1daeSBarry Smith {
5895c6c1daeSBarry Smith   PetscErrorCode     ierr;
5905c6c1daeSBarry Smith   char               host[256],**comm_list,**mem_list,**fld_list;
5915c6c1daeSBarry Smith   AMS_Comm           ams;
5925c6c1daeSBarry Smith   PetscInt           i = 0,j;
5935c6c1daeSBarry Smith   AMS_Memory_type    mtype;
5945c6c1daeSBarry Smith   AMS_Data_type      dtype;
5955c6c1daeSBarry Smith   AMS_Shared_type    stype;
5965c6c1daeSBarry Smith   AMS_Reduction_type rtype;
5975c6c1daeSBarry Smith   AMS_Memory         memory;
5985c6c1daeSBarry Smith   int                len;
5995c6c1daeSBarry Smith   void               *addr2,*addr3,*addr,*addr4;
6005c6c1daeSBarry Smith 
6015c6c1daeSBarry Smith   ierr = PetscGetHostName(host,256);CHKERRQ(ierr);
6025c6c1daeSBarry Smith   ierr = AMS_Connect(host, -1, &comm_list);CHKERRQ(ierr);
6035c6c1daeSBarry Smith   ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "text/html", -1);CHKERRQ(ierr);
604a297a907SKarl Rupp   if (!comm_list || !comm_list[0]) fprintf(fd, "AMS Communicator not running</p>\r\n");
605a297a907SKarl Rupp   else {
6065c6c1daeSBarry Smith     ierr = AMS_Comm_attach(comm_list[0],&ams);CHKERRQ(ierr);
6075c6c1daeSBarry Smith     ierr = AMS_Comm_get_memory_list(ams,&mem_list);CHKERRQ(ierr);
608a297a907SKarl Rupp     if (!mem_list[0]) fprintf(fd, "AMS Communicator %s has no published memories</p>\r\n",comm_list[0]);
609a297a907SKarl Rupp     else {
6105c6c1daeSBarry Smith       PetscInt  Nlevels,*Level,*Levelcnt,*Idbylevel,*Column,*parentid,*Id,maxId = 0,maxCol = 0,*parentId,id,cnt,Nlevelcnt = 0;
6115c6c1daeSBarry Smith       PetscBool *mask;
6125c6c1daeSBarry Smith       char      **classes,*clas,**subclasses,*sclas;
6135c6c1daeSBarry Smith 
6145c6c1daeSBarry Smith       /* get maximum number of objects */
6155c6c1daeSBarry Smith       while (mem_list[i]) {
6165c6c1daeSBarry Smith         ierr  = AMS_Memory_attach(ams,mem_list[i],&memory,NULL);CHKERRQ(ierr);
6175c6c1daeSBarry Smith         ierr  = AMS_Memory_get_field_list(memory, &fld_list);CHKERRQ(ierr);
6185c6c1daeSBarry Smith         ierr  = AMS_Memory_get_field_info(memory, "Id", &addr2, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6195c6c1daeSBarry Smith         Id    = (int*) addr2;
6205c6c1daeSBarry Smith         maxId = PetscMax(maxId,*Id);
6215c6c1daeSBarry Smith         i++;
6225c6c1daeSBarry Smith       }
6235c6c1daeSBarry Smith       maxId++;
6245c6c1daeSBarry Smith 
6255c6c1daeSBarry Smith       /* Gets everyone's parent ID and which nodes are masked */
6265c6c1daeSBarry Smith       ierr = PetscMalloc4(maxId,PetscInt,&parentid,maxId,PetscBool,&mask,maxId,char**,&classes,maxId,char**,&subclasses);CHKERRQ(ierr);
6275c6c1daeSBarry Smith       ierr = PetscMemzero(classes,maxId*sizeof(char*));CHKERRQ(ierr);
6285c6c1daeSBarry Smith       ierr = PetscMemzero(subclasses,maxId*sizeof(char*));CHKERRQ(ierr);
6295c6c1daeSBarry Smith       for (i=0; i<maxId; i++) mask[i] = PETSC_TRUE;
6305c6c1daeSBarry Smith       i = 0;
6315c6c1daeSBarry Smith       while (mem_list[i]) {
6325c6c1daeSBarry Smith         ierr          = AMS_Memory_attach(ams,mem_list[i],&memory,NULL);CHKERRQ(ierr);
6335c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_list(memory, &fld_list);CHKERRQ(ierr);
6345c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "Id", &addr2, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6355c6c1daeSBarry Smith         Id            = (int*) addr2;
6365c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "ParentId", &addr3, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6375c6c1daeSBarry Smith         parentId      = (int*) addr3;
6385c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "Class", &addr, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6395c6c1daeSBarry Smith         clas          = *(char**)addr;
6405c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "Type", &addr4, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6415c6c1daeSBarry Smith         sclas         = *(char**)addr4;
6425c6c1daeSBarry Smith         parentid[*Id] = *parentId;
6435c6c1daeSBarry Smith         mask[*Id]     = PETSC_FALSE;
644a297a907SKarl Rupp 
6455c6c1daeSBarry Smith         ierr = PetscStrallocpy(clas,classes+*Id);CHKERRQ(ierr);
6465c6c1daeSBarry Smith         ierr = PetscStrallocpy(sclas,subclasses+*Id);CHKERRQ(ierr);
6475c6c1daeSBarry Smith         i++;
6485c6c1daeSBarry Smith       }
6495c6c1daeSBarry Smith 
6505c6c1daeSBarry Smith       /* if the parent is masked then relabel the parent as 0 since the true parent was deleted */
6515c6c1daeSBarry Smith       for (i=0; i<maxId; i++) {
6525c6c1daeSBarry Smith         if (!mask[i] && parentid[i] > 0 && mask[parentid[i]]) parentid[i] = 0;
6535c6c1daeSBarry Smith       }
6545c6c1daeSBarry Smith 
6555c6c1daeSBarry Smith       ierr = PetscProcessTree(maxId,mask,parentid,&Nlevels,&Level,&Levelcnt,&Idbylevel,&Column);CHKERRQ(ierr);
6565c6c1daeSBarry Smith 
657a297a907SKarl Rupp       for (i=0; i<Nlevels; i++) maxCol    = PetscMax(maxCol,Levelcnt[i]);
658a297a907SKarl Rupp       for (i=0; i<Nlevels; i++) Nlevelcnt = PetscMax(Nlevelcnt,Levelcnt[i]);
6595c6c1daeSBarry Smith 
6605c6c1daeSBarry Smith       /* print all the top-level objects */
6615c6c1daeSBarry Smith       fprintf(fd, "<HTML><HEAD><TITLE>Petsc Application Server</TITLE>\r\n");
6625c6c1daeSBarry Smith       fprintf(fd, "<canvas width=800 height=600 id=\"tree\"></canvas>\r\n");
6635c6c1daeSBarry Smith       fprintf(fd, "<script type=\"text/javascript\">\r\n");
6645c6c1daeSBarry Smith       fprintf(fd, "  function draw() {\r\n");
6655c6c1daeSBarry Smith       fprintf(fd, "  var example = document.getElementById('tree');\r\n");
6665c6c1daeSBarry Smith       fprintf(fd, "  var context = example.getContext('2d');\r\n");
6675c6c1daeSBarry Smith       /* adjust font size based on how big a tree is printed */
668a297a907SKarl Rupp       if (Nlevels > 5 || Nlevelcnt > 10) fprintf(fd, "  context.font         = \"normal 12px sans-serif\";\r\n");
669a297a907SKarl Rupp       else                               fprintf(fd, "  context.font         = \"normal 24px sans-serif\";\r\n");
6705c6c1daeSBarry Smith       fprintf(fd, "  context.fillStyle = \"rgb(255,0,0)\";\r\n");
6715c6c1daeSBarry Smith       fprintf(fd, "  context.textBaseline = \"top\";\r\n");
6725c6c1daeSBarry Smith       fprintf(fd, "  var xspacep = 0;\r\n");
6735c6c1daeSBarry Smith       fprintf(fd, "  var yspace = example.height/%d;\r\n",(Nlevels+1));
6745c6c1daeSBarry Smith       /* estimate the height of a string as twice the width of a character */
6755c6c1daeSBarry Smith       fprintf(fd, "  var wheight = context.measureText(\"K\");\r\n");
6765c6c1daeSBarry Smith       fprintf(fd, "  var height = 1.6*wheight.width;\r\n");
6775c6c1daeSBarry Smith 
6785c6c1daeSBarry Smith       cnt = 0;
6795c6c1daeSBarry Smith       for (i=0; i<Nlevels; i++) {
6805c6c1daeSBarry Smith         fprintf(fd, "  var xspace = example.width/%d;\r\n",Levelcnt[i]+1);
6815c6c1daeSBarry Smith         for (j=0; j<Levelcnt[i]; j++) {
6825c6c1daeSBarry Smith           id    = Idbylevel[cnt++];
6835c6c1daeSBarry Smith           clas  = classes[id];
6845c6c1daeSBarry Smith           sclas = subclasses[id];
6855c6c1daeSBarry Smith           fprintf(fd, "  var width = context.measureText(\"%s\");\r\n",clas);
6865c6c1daeSBarry Smith           fprintf(fd, "  var swidth = context.measureText(\"%s\");\r\n",sclas);
6875c6c1daeSBarry Smith           fprintf(fd, "  context.fillStyle = \"rgb(255,0,0)\";\r\n");
6885c6c1daeSBarry Smith           fprintf(fd, "  context.fillRect((%d)*xspace-width.width/2, %d*yspace-height/2, width.width, height);\r\n",j+1,i+1);
6895c6c1daeSBarry Smith           fprintf(fd, "  context.fillRect((%d)*xspace-swidth.width/2, %d*yspace+height/2, swidth.width, height);\r\n",j+1,i+1);
6905c6c1daeSBarry Smith           fprintf(fd, "  context.fillStyle = \"rgb(0,0,0)\";\r\n");
6915c6c1daeSBarry Smith           fprintf(fd, "  context.fillText(\"%s\",(%d)*xspace-width.width/2, %d*yspace-height/2);\r\n",clas,j+1,i+1);
6925c6c1daeSBarry Smith           fprintf(fd, "  context.fillText(\"%s\",(%d)*xspace-swidth.width/2, %d*yspace+height/2);\r\n",sclas,j+1,i+1);
6935c6c1daeSBarry Smith           if (parentid[id]) {
6945c6c1daeSBarry Smith             fprintf(fd, "  context.moveTo(%d*xspace,%d*yspace-height/2);\r\n",j+1,i+1);
6955c6c1daeSBarry Smith             fprintf(fd, "  context.lineTo(%d*xspacep,%d*yspace+3*height/2);\r\n",Column[parentid[id]]+1,i);
6965c6c1daeSBarry Smith             fprintf(fd, "  context.stroke();\r\n");
6975c6c1daeSBarry Smith           }
6985c6c1daeSBarry Smith         }
6995c6c1daeSBarry Smith         fprintf(fd, "  xspacep = xspace;\r\n");
7005c6c1daeSBarry Smith       }
7015c6c1daeSBarry Smith       ierr = PetscFree(Level);CHKERRQ(ierr);
7025c6c1daeSBarry Smith       ierr = PetscFree(Levelcnt);CHKERRQ(ierr);
7035c6c1daeSBarry Smith       ierr = PetscFree(Idbylevel);CHKERRQ(ierr);
7045c6c1daeSBarry Smith       ierr = PetscFree(Column);CHKERRQ(ierr);
7055c6c1daeSBarry Smith       for (i=0; i<maxId; i++) {
7065c6c1daeSBarry Smith         ierr = PetscFree(classes[i]);CHKERRQ(ierr);
7075c6c1daeSBarry Smith         ierr = PetscFree(subclasses[i]);CHKERRQ(ierr);
7085c6c1daeSBarry Smith       }
7095c6c1daeSBarry Smith       ierr = PetscFree4(mask,parentid,classes,subclasses);CHKERRQ(ierr);
7105c6c1daeSBarry Smith 
7115c6c1daeSBarry Smith       ierr = AMS_Disconnect();CHKERRQ(ierr);
7125c6c1daeSBarry Smith       fprintf(fd, "}\r\n");
7135c6c1daeSBarry Smith       fprintf(fd, "</script>\r\n");
7145c6c1daeSBarry Smith       fprintf(fd, "<body onload=\"draw();\">\r\n");
7155c6c1daeSBarry Smith       fprintf(fd, "</body></html>\r\n");
7165c6c1daeSBarry Smith     }
7175c6c1daeSBarry Smith   }
7185c6c1daeSBarry Smith   ierr = PetscWebSendFooter(fd);CHKERRQ(ierr);
7195c6c1daeSBarry Smith   PetscFunctionReturn(0);
7205c6c1daeSBarry Smith }
7215c6c1daeSBarry Smith #endif
7225c6c1daeSBarry Smith 
7235c6c1daeSBarry Smith #if defined(PETSC_HAVE_YAML)
7245c6c1daeSBarry Smith 
7255c6c1daeSBarry Smith /*
7265c6c1daeSBarry Smith     Toy function that returns all the arguments it is passed
7275c6c1daeSBarry Smith */
7285c6c1daeSBarry Smith #undef __FUNCT__
7295c6c1daeSBarry Smith #define __FUNCT__ "YAML_echo"
7305c6c1daeSBarry Smith PetscErrorCode YAML_echo(PetscInt argc,char **args,PetscInt *argco,char ***argso)
7315c6c1daeSBarry Smith {
7325c6c1daeSBarry Smith   PetscErrorCode ierr;
7335c6c1daeSBarry Smith   PetscInt       i;
7345c6c1daeSBarry Smith 
7355c6c1daeSBarry Smith   ierr = PetscPrintf(PETSC_COMM_SELF,"Number of arguments to function %d\n",argc);CHKERRQ(ierr);
7365c6c1daeSBarry Smith   for (i=0; i<argc; i++) {
7375c6c1daeSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF,"  %s\n",args[i]);CHKERRQ(ierr);
7385c6c1daeSBarry Smith   }
7395c6c1daeSBarry Smith   *argco = argc;
7405c6c1daeSBarry Smith   ierr   = PetscMalloc(argc*sizeof(char*),argso);CHKERRQ(ierr);
7415c6c1daeSBarry Smith   for (i=0; i<argc; i++) {
7425c6c1daeSBarry Smith     ierr = PetscStrallocpy(args[i],&(*argso)[i]);CHKERRQ(ierr);
7435c6c1daeSBarry Smith   }
7445c6c1daeSBarry Smith   PetscFunctionReturn(0);
7455c6c1daeSBarry Smith }
7465c6c1daeSBarry Smith 
7475c6c1daeSBarry Smith #undef __FUNCT__
7485c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Connect"
7495c6c1daeSBarry Smith /*
7505c6c1daeSBarry Smith       Connects to the local AMS and gets only the first communication name
7515c6c1daeSBarry Smith 
7525c6c1daeSBarry Smith    Input Parameters:
7535c6c1daeSBarry Smith .     none
7545c6c1daeSBarry Smith 
7555c6c1daeSBarry Smith    Output Parameter:
7565c6c1daeSBarry Smith .     oarg1 - the string name of the first communicator
7575c6c1daeSBarry Smith 
7585c6c1daeSBarry Smith */
7595c6c1daeSBarry Smith PetscErrorCode YAML_AMS_Connect(PetscInt argc,char **args,PetscInt *argco,char ***argso)
7605c6c1daeSBarry Smith {
7615c6c1daeSBarry Smith   PetscErrorCode ierr;
7625c6c1daeSBarry Smith   char           **list = 0;
7635c6c1daeSBarry Smith 
7645c6c1daeSBarry Smith   PetscFunctionBegin;
7655c6c1daeSBarry Smith   ierr = AMS_Connect(0,-1,&list);
766a297a907SKarl Rupp   if (ierr) {
7670298fd71SBarry Smith     ierr = PetscInfo1(NULL,"AMS_Connect() error %d\n",ierr);CHKERRQ(ierr);
768a297a907SKarl Rupp   } else if (!list) {
7690298fd71SBarry Smith     ierr = PetscInfo(NULL,"AMS_Connect() list empty, not running AMS server\n");CHKERRQ(ierr);
770a297a907SKarl Rupp   }
7715c6c1daeSBarry Smith   *argco = 1;
7725c6c1daeSBarry Smith   ierr   = PetscMalloc(sizeof(char*),argso);CHKERRQ(ierr);
7735c6c1daeSBarry Smith   if (list) {
7745c6c1daeSBarry Smith     ierr = PetscStrallocpy(list[0],&(*argso)[0]);CHKERRQ(ierr);
7755c6c1daeSBarry Smith   } else {
7765c6c1daeSBarry Smith     ierr = PetscStrallocpy("No AMS publisher running",&(*argso)[0]);CHKERRQ(ierr);
7775c6c1daeSBarry Smith   }
7785c6c1daeSBarry Smith   PetscFunctionReturn(0);
7795c6c1daeSBarry Smith }
7805c6c1daeSBarry Smith 
7815c6c1daeSBarry Smith #undef __FUNCT__
7825c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Comm_attach"
7835c6c1daeSBarry Smith /*
7845c6c1daeSBarry Smith       Attaches to an AMS communicator
7855c6c1daeSBarry Smith 
7865c6c1daeSBarry Smith    Input Parameter:
7875c6c1daeSBarry Smith .     arg1 - string name of the communicator
7885c6c1daeSBarry Smith 
7895c6c1daeSBarry Smith    Output Parameter:
7905c6c1daeSBarry Smith .     oarg1 - the integer name of the communicator
7915c6c1daeSBarry Smith 
7925c6c1daeSBarry Smith */
7935c6c1daeSBarry Smith PetscErrorCode YAML_AMS_Comm_attach(PetscInt argc,char **args,PetscInt *argco,char ***argso)
7945c6c1daeSBarry Smith {
7955c6c1daeSBarry Smith   PetscErrorCode ierr;
7965c6c1daeSBarry Smith   AMS_Comm       comm = -1;
7975c6c1daeSBarry Smith 
7985c6c1daeSBarry Smith   PetscFunctionBegin;
7995c6c1daeSBarry Smith   ierr = AMS_Comm_attach(args[0],&comm);
8000298fd71SBarry Smith   if (ierr) {ierr = PetscInfo1(NULL,"AMS_Comm_attach() error %d\n",ierr);CHKERRQ(ierr);}
8015c6c1daeSBarry Smith   *argco = 1;
8025c6c1daeSBarry Smith   ierr   = PetscMalloc(sizeof(char*),argso);CHKERRQ(ierr);
8035c6c1daeSBarry Smith   ierr   = PetscMalloc(3*sizeof(char*),&argso[0][0]);CHKERRQ(ierr);
8045c6c1daeSBarry Smith   sprintf(argso[0][0],"%d",(int)comm);
8055c6c1daeSBarry Smith   PetscFunctionReturn(0);
8065c6c1daeSBarry Smith }
8075c6c1daeSBarry Smith 
8085c6c1daeSBarry Smith #undef __FUNCT__
8095c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Comm_get_memory_list"
8105c6c1daeSBarry Smith /*
8115c6c1daeSBarry Smith       Gets the list of memories on an AMS Comm
8125c6c1daeSBarry Smith 
8135c6c1daeSBarry Smith    Input Parameter:
8145c6c1daeSBarry Smith .     arg1 - integer name of the communicator
8155c6c1daeSBarry Smith 
8165c6c1daeSBarry Smith    Output Parameter:
8175c6c1daeSBarry Smith .     oarg1 - the list of names
8185c6c1daeSBarry Smith 
8195c6c1daeSBarry Smith */
8205c6c1daeSBarry Smith PetscErrorCode YAML_AMS_Comm_get_memory_list(PetscInt argc,char **args,PetscInt *argco,char ***argso)
8215c6c1daeSBarry Smith {
8225c6c1daeSBarry Smith   PetscErrorCode ierr;
8235c6c1daeSBarry Smith   char           **mem_list;
8245c6c1daeSBarry Smith   AMS_Comm       comm;
8255c6c1daeSBarry Smith   PetscInt       i,iargco = 0;
8265c6c1daeSBarry Smith 
8275c6c1daeSBarry Smith   PetscFunctionBegin;
8285c6c1daeSBarry Smith   sscanf(args[0],"%d",&comm);
8295c6c1daeSBarry Smith   ierr = AMS_Comm_get_memory_list(comm,&mem_list);
8305c6c1daeSBarry Smith   if (ierr) {
8310298fd71SBarry Smith     ierr = PetscInfo1(NULL,"AMS_Comm_get_memory_list() error %d\n",ierr);CHKERRQ(ierr);
8325c6c1daeSBarry Smith   } else {
8335c6c1daeSBarry Smith     while (mem_list[iargco++]) ;
8345c6c1daeSBarry Smith     iargco--;
8355c6c1daeSBarry Smith 
8365c6c1daeSBarry Smith     ierr = PetscMalloc((iargco)*sizeof(char*),argso);CHKERRQ(ierr);
8375c6c1daeSBarry Smith     for (i=0; i<iargco; i++) {
8385c6c1daeSBarry Smith       ierr = PetscStrallocpy(mem_list[i],(*argso)+i);CHKERRQ(ierr);
8395c6c1daeSBarry Smith     }
8405c6c1daeSBarry Smith   }
8415c6c1daeSBarry Smith   *argco = iargco;
8425c6c1daeSBarry Smith   PetscFunctionReturn(0);
8435c6c1daeSBarry Smith }
8445c6c1daeSBarry Smith 
8455c6c1daeSBarry Smith #undef __FUNCT__
8465c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Memory_attach"
8475c6c1daeSBarry Smith /*
8485c6c1daeSBarry Smith       Attaches to an AMS memory in a communicator
8495c6c1daeSBarry Smith 
8505c6c1daeSBarry Smith    Input Parameter:
8515c6c1daeSBarry Smith .     arg1 - communicator
8525c6c1daeSBarry Smith .     arg2 - string name of the memory
8535c6c1daeSBarry Smith 
8545c6c1daeSBarry Smith    Output Parameter:
8555c6c1daeSBarry Smith .     oarg1 - the integer name of the memory
8565c6c1daeSBarry Smith .     oarg2 - the integer step of the memory
8575c6c1daeSBarry Smith 
8585c6c1daeSBarry Smith */
8595c6c1daeSBarry Smith PetscErrorCode YAML_AMS_Memory_attach(PetscInt argc,char **args,PetscInt *argco,char ***argso)
8605c6c1daeSBarry Smith {
8615c6c1daeSBarry Smith   PetscErrorCode ierr;
8625c6c1daeSBarry Smith   AMS_Comm       comm;
8635c6c1daeSBarry Smith   AMS_Memory     mem;
8645c6c1daeSBarry Smith   unsigned int   step;
8655c6c1daeSBarry Smith 
8665c6c1daeSBarry Smith   PetscFunctionBegin;
8675c6c1daeSBarry Smith   sscanf(args[0],"%d",&comm);
8685c6c1daeSBarry Smith   ierr = AMS_Memory_attach(comm,args[1],&mem,&step);
8690298fd71SBarry Smith   if (ierr) {ierr = PetscInfo1(NULL,"AMS_Memory_attach() error %d\n",ierr);CHKERRQ(ierr);}
8705c6c1daeSBarry Smith   *argco = 2;
8715c6c1daeSBarry Smith   ierr   = PetscMalloc(2*sizeof(char*),argso);CHKERRQ(ierr);
8725c6c1daeSBarry Smith   ierr   = PetscMalloc(3*sizeof(char*),&argso[0][0]);CHKERRQ(ierr);
8735c6c1daeSBarry Smith   sprintf(argso[0][0],"%d",(int)mem);
8745c6c1daeSBarry Smith   ierr = PetscMalloc(3*sizeof(char*),&argso[0][1]);CHKERRQ(ierr);
8755c6c1daeSBarry Smith   sprintf(argso[0][1],"%d",(int)step);
8765c6c1daeSBarry Smith   PetscFunctionReturn(0);
8775c6c1daeSBarry Smith }
8785c6c1daeSBarry Smith 
8795c6c1daeSBarry Smith #undef __FUNCT__
8805c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Memory_get_field_list"
8815c6c1daeSBarry Smith /*
8825c6c1daeSBarry Smith       Gets the list of fields on an AMS Memory
8835c6c1daeSBarry Smith 
8845c6c1daeSBarry Smith    Input Parameter:
8855c6c1daeSBarry Smith .     arg1 - integer name of the memory
8865c6c1daeSBarry Smith 
8875c6c1daeSBarry Smith    Output Parameter:
8885c6c1daeSBarry Smith .     oarg1 - the list of names
8895c6c1daeSBarry Smith 
8905c6c1daeSBarry Smith */
8915c6c1daeSBarry Smith PetscErrorCode YAML_AMS_Memory_get_field_list(PetscInt argc,char **args,PetscInt *argco,char ***argso)
8925c6c1daeSBarry Smith {
8935c6c1daeSBarry Smith   PetscErrorCode ierr;
8945c6c1daeSBarry Smith   char           **field_list;
8955c6c1daeSBarry Smith   AMS_Memory     mem;
8965c6c1daeSBarry Smith   PetscInt       i,iargco = 0;
8975c6c1daeSBarry Smith 
8985c6c1daeSBarry Smith   PetscFunctionBegin;
8995c6c1daeSBarry Smith   sscanf(args[0],"%d",&mem);
9005c6c1daeSBarry Smith   ierr = AMS_Memory_get_field_list(mem,&field_list);
9015c6c1daeSBarry Smith   if (ierr) {
9020298fd71SBarry Smith     ierr = PetscInfo1(NULL,"AMS_Memory_get_field_list() error %d\n",ierr);CHKERRQ(ierr);
9035c6c1daeSBarry Smith   } else {
9045c6c1daeSBarry Smith     while (field_list[iargco++]) ;
9055c6c1daeSBarry Smith     iargco--;
9065c6c1daeSBarry Smith 
9075c6c1daeSBarry Smith     ierr = PetscMalloc((iargco)*sizeof(char*),argso);CHKERRQ(ierr);
9085c6c1daeSBarry Smith     for (i=0; i<iargco; i++) {
9095c6c1daeSBarry Smith       ierr = PetscStrallocpy(field_list[i],(*argso)+i);CHKERRQ(ierr);
9105c6c1daeSBarry Smith     }
9115c6c1daeSBarry Smith   }
9125c6c1daeSBarry Smith   *argco = iargco;
9135c6c1daeSBarry Smith   PetscFunctionReturn(0);
9145c6c1daeSBarry Smith }
9155c6c1daeSBarry Smith 
9165c6c1daeSBarry Smith const char *AMS_Data_types[] = {"AMS_DATA_UNDEF","AMS_BOOLEAN","AMS_INT","AMS_FLOAT","AMS_DOUBLE","AMS_STRING","AMS_Data_type","AMS_",0};
9175c6c1daeSBarry Smith const char *AMS_Memory_types[] = {"AMS_MEMORY_UNDEF","AMS_READ","AMS_WRITE","AMS_Memory_type","AMS_",0};
9185c6c1daeSBarry Smith const char *AMS_Shared_types[] = {"AMS_SHARED_UNDEF","AMS_COMMON","AMS_REDUCED","AMS_DISTRIBUTED","AMS_Shared_type","AMS_",0};
9195c6c1daeSBarry Smith const char *AMS_Reduction_types[] = {"AMS_REDUCTION_WHY_NOT_UNDEF?","AMS_SUM","AMS_MAX","AMS_MIN","AMS_REDUCTION_UNDEF","AMS_Reduction_type","AMS_",0};
9205c6c1daeSBarry Smith 
9215c6c1daeSBarry Smith #undef __FUNCT__
9225c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Memory_get_field_info"
9235c6c1daeSBarry Smith /*
9245c6c1daeSBarry Smith       Gets information about a field
9255c6c1daeSBarry Smith 
9265c6c1daeSBarry Smith    Input Parameter:
9275c6c1daeSBarry Smith .     arg1 - memory
9285c6c1daeSBarry Smith .     arg2 - string name of the field
9295c6c1daeSBarry Smith 
9305c6c1daeSBarry Smith    Output Parameter:
9315c6c1daeSBarry Smith 
9325c6c1daeSBarry Smith */
9335c6c1daeSBarry Smith PetscErrorCode YAML_AMS_Memory_get_field_info(PetscInt argc,char **args,PetscInt *argco,char ***argso)
9345c6c1daeSBarry Smith {
9355c6c1daeSBarry Smith   PetscErrorCode     ierr;
9365c6c1daeSBarry Smith   AMS_Memory         mem;
9375c6c1daeSBarry Smith   void               *addr;
9385c6c1daeSBarry Smith   int                len;
9395c6c1daeSBarry Smith   AMS_Data_type      dtype;
9405c6c1daeSBarry Smith   AMS_Memory_type    mtype;
9415c6c1daeSBarry Smith   AMS_Shared_type    stype;
9425c6c1daeSBarry Smith   AMS_Reduction_type rtype;
9435c6c1daeSBarry Smith   PetscInt           i;
9445c6c1daeSBarry Smith 
9455c6c1daeSBarry Smith   PetscFunctionBegin;
9465c6c1daeSBarry Smith   sscanf(args[0],"%d",&mem);
9475c6c1daeSBarry Smith   ierr = AMS_Memory_get_field_info(mem,args[1],&addr,&len,&dtype,&mtype,&stype,&rtype);
9480298fd71SBarry Smith   if (ierr) {ierr = PetscInfo1(NULL,"AMS_Memory_get_field_info() error %d\n",ierr);CHKERRQ(ierr);}
9495c6c1daeSBarry Smith   *argco = 4 + len;
9505c6c1daeSBarry Smith   ierr   = PetscMalloc((*argco)*sizeof(char*),argso);CHKERRQ(ierr);
9515c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Data_types[dtype],&argso[0][0]);CHKERRQ(ierr);
9525c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Memory_types[mtype],&argso[0][1]);CHKERRQ(ierr);
9535c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Shared_types[stype],&argso[0][2]);CHKERRQ(ierr);
9545c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Reduction_types[rtype],&argso[0][3]);CHKERRQ(ierr);
9555c6c1daeSBarry Smith   for (i=0; i<len; i++) {
9565c6c1daeSBarry Smith     if (dtype == AMS_STRING) {
9575c6c1daeSBarry Smith       ierr = PetscStrallocpy(*(const char**)addr,&argso[0][4+i]);CHKERRQ(ierr);
9585c6c1daeSBarry Smith     } else if (dtype == AMS_DOUBLE) {
9595c6c1daeSBarry Smith       ierr = PetscMalloc(20*sizeof(char),&argso[0][4+i]);CHKERRQ(ierr);
9605c6c1daeSBarry Smith       sprintf(argso[0][4+i],"%18.16e",*(double*)addr);
9615c6c1daeSBarry Smith     } else if (dtype == AMS_INT) {
9625c6c1daeSBarry Smith       ierr = PetscMalloc(10*sizeof(char),&argso[0][4+i]);CHKERRQ(ierr);
9635c6c1daeSBarry Smith       sprintf(argso[0][4+i],"%d",*(int*)addr);
9645c6c1daeSBarry Smith     } else if (dtype == AMS_BOOLEAN) {
9655c6c1daeSBarry Smith       if (*(int*)addr) {
9665c6c1daeSBarry Smith         ierr = PetscStrallocpy("true",&argso[0][4+i]);CHKERRQ(ierr);
9675c6c1daeSBarry Smith       } else {
9685c6c1daeSBarry Smith         ierr = PetscStrallocpy("false",&argso[0][4+i]);CHKERRQ(ierr);
9695c6c1daeSBarry Smith       }
9705c6c1daeSBarry Smith     } else {
9715c6c1daeSBarry Smith       ierr = PetscStrallocpy("Not yet done",&argso[0][4+i]);CHKERRQ(ierr);
9725c6c1daeSBarry Smith     }
9735c6c1daeSBarry Smith   }
9745c6c1daeSBarry Smith   PetscFunctionReturn(0);
9755c6c1daeSBarry Smith }
9765c6c1daeSBarry Smith 
9775c6c1daeSBarry Smith #include "yaml.h"
9785c6c1daeSBarry Smith #undef __FUNCT__
9795c6c1daeSBarry Smith #define __FUNCT__ "PetscProcessYAMLRPC"
9805c6c1daeSBarry Smith PetscErrorCode PetscProcessYAMLRPC(const char *request,char **result)
9815c6c1daeSBarry Smith {
9825c6c1daeSBarry Smith   yaml_parser_t  parser;
9835c6c1daeSBarry Smith   yaml_event_t   event;
9845c6c1daeSBarry Smith   int            done  = 0;
9855c6c1daeSBarry Smith   int            count = 0;
9865c6c1daeSBarry Smith   size_t         len;
9875c6c1daeSBarry Smith   PetscErrorCode ierr;
9885c6c1daeSBarry Smith   PetscBool      method,params,id;
9895c6c1daeSBarry Smith   char           *methodname,*idname,**args,**argso = 0;
9905c6c1daeSBarry Smith   PetscInt       argc = 0,argco,i;
9915c6c1daeSBarry Smith   PetscErrorCode (*fun)(PetscInt,char**,PetscInt*,char***);
9925c6c1daeSBarry Smith 
9935c6c1daeSBarry Smith   PetscFunctionBegin;
9945c6c1daeSBarry Smith   ierr = PetscMalloc(sizeof(char*),&args);CHKERRQ(ierr);
9955c6c1daeSBarry Smith   yaml_parser_initialize(&parser);
9965c6c1daeSBarry Smith   PetscStrlen(request,&len);
9975c6c1daeSBarry Smith   yaml_parser_set_input_string(&parser, (unsigned char*)request, len);
9985c6c1daeSBarry Smith 
9995c6c1daeSBarry Smith   /* this is totally bogus; it only handles the simple JSON-RPC messages */
10005c6c1daeSBarry Smith   while (!done) {
10015c6c1daeSBarry Smith     if (!yaml_parser_parse(&parser, &event)) {
10020298fd71SBarry Smith       ierr = PetscInfo(NULL,"Found error in yaml/json\n");CHKERRQ(ierr);
10035c6c1daeSBarry Smith       break;
10045c6c1daeSBarry Smith     }
10055c6c1daeSBarry Smith     done = (event.type == YAML_STREAM_END_EVENT);
10065c6c1daeSBarry Smith     switch (event.type) {
10075c6c1daeSBarry Smith     case YAML_STREAM_START_EVENT:
10080298fd71SBarry Smith       ierr = PetscInfo(NULL,"Stream start\n");CHKERRQ(ierr);
10095c6c1daeSBarry Smith       break;
10105c6c1daeSBarry Smith     case YAML_STREAM_END_EVENT:
10110298fd71SBarry Smith       ierr = PetscInfo(NULL,"Stream end\n");CHKERRQ(ierr);
10125c6c1daeSBarry Smith       break;
10135c6c1daeSBarry Smith     case YAML_DOCUMENT_START_EVENT:
10140298fd71SBarry Smith       ierr = PetscInfo(NULL,"Document start\n");CHKERRQ(ierr);
10155c6c1daeSBarry Smith       break;
10165c6c1daeSBarry Smith     case YAML_DOCUMENT_END_EVENT:
10170298fd71SBarry Smith       ierr = PetscInfo(NULL,"Document end\n");CHKERRQ(ierr);
10185c6c1daeSBarry Smith       break;
10195c6c1daeSBarry Smith     case YAML_MAPPING_START_EVENT:
10200298fd71SBarry Smith       ierr = PetscInfo(NULL,"Mapping start event\n");CHKERRQ(ierr);
10215c6c1daeSBarry Smith       break;
10225c6c1daeSBarry Smith     case YAML_MAPPING_END_EVENT:
10230298fd71SBarry Smith       ierr = PetscInfo(NULL,"Mapping end event \n");CHKERRQ(ierr);
10245c6c1daeSBarry Smith       break;
10255c6c1daeSBarry Smith     case YAML_ALIAS_EVENT:
10260298fd71SBarry Smith       ierr = PetscInfo1(NULL,"Alias event %s\n",event.data.alias.anchor);CHKERRQ(ierr);
10275c6c1daeSBarry Smith       break;
10285c6c1daeSBarry Smith     case YAML_SCALAR_EVENT:
10290298fd71SBarry Smith       ierr = PetscInfo1(NULL,"Scalar event %s\n",event.data.scalar.value);CHKERRQ(ierr);
10305c6c1daeSBarry Smith       ierr = PetscStrcmp((char*)event.data.scalar.value,"method",&method);CHKERRQ(ierr);
10315c6c1daeSBarry Smith       ierr = PetscStrcmp((char*)event.data.scalar.value,"params",&params);CHKERRQ(ierr);
10325c6c1daeSBarry Smith       ierr = PetscStrcmp((char*)event.data.scalar.value,"id",&id);CHKERRQ(ierr);
10335c6c1daeSBarry Smith       if (method) {
10345c6c1daeSBarry Smith         yaml_event_delete(&event);
10355c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
10360298fd71SBarry Smith         ierr = PetscInfo1(NULL,"Method %s\n",event.data.scalar.value);CHKERRQ(ierr);
10375c6c1daeSBarry Smith         ierr = PetscStrallocpy((char*)event.data.scalar.value,&methodname);CHKERRQ(ierr);
10385c6c1daeSBarry Smith       } else if (id) {
10395c6c1daeSBarry Smith         yaml_event_delete(&event);
10405c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
10410298fd71SBarry Smith         ierr = PetscInfo1(NULL,"Id %s\n",event.data.scalar.value);CHKERRQ(ierr);
10425c6c1daeSBarry Smith         ierr = PetscStrallocpy((char*)event.data.scalar.value,&idname);CHKERRQ(ierr);
10435c6c1daeSBarry Smith       } else if (params) {
10445c6c1daeSBarry Smith         yaml_event_delete(&event);
10455c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
10465c6c1daeSBarry Smith         yaml_event_delete(&event);
10475c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
10485c6c1daeSBarry Smith         while (event.type != YAML_SEQUENCE_END_EVENT) {
10490298fd71SBarry Smith           ierr = PetscInfo1(NULL,"  Parameter %s\n",event.data.scalar.value);CHKERRQ(ierr);
10505c6c1daeSBarry Smith           ierr = PetscStrallocpy((char*)event.data.scalar.value,&args[argc++]);CHKERRQ(ierr);
10515c6c1daeSBarry Smith           yaml_event_delete(&event);
10525c6c1daeSBarry Smith           ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
10535c6c1daeSBarry Smith         }
10545c6c1daeSBarry Smith       } else { /* ignore all the other variables in the mapping */
10555c6c1daeSBarry Smith         yaml_event_delete(&event);
10565c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
10575c6c1daeSBarry Smith       }
10585c6c1daeSBarry Smith       break;
10595c6c1daeSBarry Smith     case YAML_SEQUENCE_START_EVENT:
10600298fd71SBarry Smith       ierr = PetscInfo(NULL,"Sequence start event \n");CHKERRQ(ierr);
10615c6c1daeSBarry Smith       break;
10625c6c1daeSBarry Smith     case YAML_SEQUENCE_END_EVENT:
10630298fd71SBarry Smith       ierr = PetscInfo(NULL,"Sequence end event \n");CHKERRQ(ierr);
10645c6c1daeSBarry Smith       break;
10655c6c1daeSBarry Smith     default:
10665c6c1daeSBarry Smith       /* It couldn't really happen. */
10675c6c1daeSBarry Smith       break;
10685c6c1daeSBarry Smith     }
10695c6c1daeSBarry Smith 
10705c6c1daeSBarry Smith     yaml_event_delete(&event);
10715c6c1daeSBarry Smith     count++;
10725c6c1daeSBarry Smith   }
10735c6c1daeSBarry Smith   yaml_parser_delete(&parser);
10745c6c1daeSBarry Smith 
10750298fd71SBarry Smith   ierr = PetscDLLibrarySym(PETSC_COMM_SELF,NULL,NULL,methodname,(void**)&fun);CHKERRQ(ierr);
10765c6c1daeSBarry Smith   if (fun) {
10770298fd71SBarry Smith     ierr = PetscInfo1(NULL,"Located function %s and running it\n",methodname);CHKERRQ(ierr);
10785c6c1daeSBarry Smith     ierr = (*fun)(argc,args,&argco,&argso);CHKERRQ(ierr);
10795c6c1daeSBarry Smith   } else {
10800298fd71SBarry Smith     ierr = PetscInfo1(NULL,"Did not locate function %s skipping it\n",methodname);CHKERRQ(ierr);
10815c6c1daeSBarry Smith   }
10825c6c1daeSBarry Smith 
10835c6c1daeSBarry Smith   for (i=0; i<argc; i++) {
10845c6c1daeSBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
10855c6c1daeSBarry Smith   }
10865c6c1daeSBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
10875c6c1daeSBarry Smith   ierr = PetscFree(methodname);CHKERRQ(ierr);
10885c6c1daeSBarry Smith 
10895c6c1daeSBarry Smith   /* convert the result back to YAML; should use YAML encoder, does not handle zero return arguments */
10905c6c1daeSBarry Smith   ierr = PetscMalloc(1024,result);CHKERRQ(ierr);
10915c6c1daeSBarry Smith   ierr = PetscStrcpy(*result,"{\"error\": null, \"id\": \"");CHKERRQ(ierr);
10925c6c1daeSBarry Smith   ierr = PetscStrcat(*result,idname);CHKERRQ(ierr);
10935c6c1daeSBarry Smith   ierr = PetscStrcat(*result,"\", \"result\" : ");CHKERRQ(ierr);
10945c6c1daeSBarry Smith   if (argco > 1) {ierr = PetscStrcat(*result,"[");CHKERRQ(ierr);}
10955c6c1daeSBarry Smith   for (i=0; i<argco; i++) {
10965c6c1daeSBarry Smith     ierr = PetscStrcat(*result,"\"");CHKERRQ(ierr);
10975c6c1daeSBarry Smith     ierr = PetscStrcat(*result,argso[i]);CHKERRQ(ierr);
10985c6c1daeSBarry Smith     ierr = PetscStrcat(*result,"\"");CHKERRQ(ierr);
10995c6c1daeSBarry Smith     if (i < argco-1) {ierr = PetscStrcat(*result,",");CHKERRQ(ierr);}
11005c6c1daeSBarry Smith   }
11015c6c1daeSBarry Smith   if (argco > 1) {ierr = PetscStrcat(*result,"]");CHKERRQ(ierr);}
11025c6c1daeSBarry Smith   ierr = PetscStrcat(*result,"}");CHKERRQ(ierr);
11030298fd71SBarry Smith   ierr = PetscInfo1(NULL,"YAML result of function %s\n",*result);CHKERRQ(ierr);
11045c6c1daeSBarry Smith 
11055c6c1daeSBarry Smith   /* free work space */
11065c6c1daeSBarry Smith   ierr = PetscFree(idname);CHKERRQ(ierr);
11075c6c1daeSBarry Smith   for (i=0; i<argco; i++) {
11085c6c1daeSBarry Smith     ierr = PetscFree(argso[i]);CHKERRQ(ierr);
11095c6c1daeSBarry Smith   }
11105c6c1daeSBarry Smith   ierr = PetscFree(argso);CHKERRQ(ierr);
11115c6c1daeSBarry Smith   PetscFunctionReturn(0);
11125c6c1daeSBarry Smith }
11135c6c1daeSBarry Smith #endif
11145c6c1daeSBarry Smith 
11155c6c1daeSBarry Smith #undef __FUNCT__
11165c6c1daeSBarry Smith #define __FUNCT__ "PetscWebServeRequest"
11175c6c1daeSBarry Smith /*@C
11185c6c1daeSBarry Smith       PetscWebServeRequest - serves a single web request
11195c6c1daeSBarry Smith 
11205c6c1daeSBarry Smith     Not collective
11215c6c1daeSBarry Smith 
11225c6c1daeSBarry Smith   Input Parameters:
11235c6c1daeSBarry Smith .   port - the port
11245c6c1daeSBarry Smith 
11255c6c1daeSBarry Smith     Level: developer
11265c6c1daeSBarry Smith 
11275c6c1daeSBarry Smith .seealso: PetscWebServe()
11285c6c1daeSBarry Smith @*/
11295c6c1daeSBarry Smith PetscErrorCode  PetscWebServeRequest(int port)
11305c6c1daeSBarry Smith {
11315c6c1daeSBarry Smith   PetscErrorCode ierr;
11325c6c1daeSBarry Smith   FILE           *fd,*fdo;
11335c6c1daeSBarry Smith   char           buf[4096],fullpath[PETSC_MAX_PATH_LEN],truefullpath[PETSC_MAX_PATH_LEN];
11345c6c1daeSBarry Smith   char           *method, *path, *protocol,*result;
11355c6c1daeSBarry Smith   const char     *type;
11365c6c1daeSBarry Smith   PetscBool      flg;
11375c6c1daeSBarry Smith   PetscToken     tok;
11385c6c1daeSBarry Smith   PetscInt       cnt = 8;
11395c6c1daeSBarry Smith 
11405c6c1daeSBarry Smith   PetscFunctionBegin;
11415c6c1daeSBarry Smith   fd = fdopen(port, "r+");
11425c6c1daeSBarry Smith 
11430298fd71SBarry Smith   ierr = PetscInfo(NULL,"Processing web request\n");CHKERRQ(ierr);
11445c6c1daeSBarry Smith   if (!fgets(buf, sizeof(buf), fd)) {
11450298fd71SBarry Smith     ierr = PetscInfo(NULL,"Cannot read web request, giving up\n");CHKERRQ(ierr);
11465c6c1daeSBarry Smith     goto theend;
11475c6c1daeSBarry Smith   }
11480298fd71SBarry Smith   ierr = PetscInfo1(NULL,"Processing web request %s",buf);CHKERRQ(ierr);
11495c6c1daeSBarry Smith 
11505c6c1daeSBarry Smith   ierr = PetscTokenCreate(buf,' ',&tok);CHKERRQ(ierr);
11515c6c1daeSBarry Smith   ierr = PetscTokenFind(tok,&method);CHKERRQ(ierr);
11525c6c1daeSBarry Smith   ierr = PetscTokenFind(tok,&path);CHKERRQ(ierr);
11535c6c1daeSBarry Smith   ierr = PetscTokenFind(tok,&protocol);CHKERRQ(ierr);
11545c6c1daeSBarry Smith 
11555c6c1daeSBarry Smith   if (!method || !path || !protocol) {
11560298fd71SBarry Smith     ierr = PetscInfo(NULL,"Web request not well formatted, giving up\n");CHKERRQ(ierr);
11575c6c1daeSBarry Smith     goto theend;
11585c6c1daeSBarry Smith   }
11595c6c1daeSBarry Smith 
11605c6c1daeSBarry Smith   ierr = PetscStrcmp(method,"GET",&flg);
11615c6c1daeSBarry Smith   if (!flg) {
11625c6c1daeSBarry Smith #if defined(PETSC_HAVE_YAML)
11635c6c1daeSBarry Smith     ierr = PetscStrcmp(method,"POST",&flg);
11645c6c1daeSBarry Smith     /*
11655c6c1daeSBarry Smith           Start to handle support for POSTs based on json-rpc
11665c6c1daeSBarry Smith     */
11675c6c1daeSBarry Smith     if (flg) {
11685c6c1daeSBarry Smith       int    len;
11695c6c1daeSBarry Smith       size_t elen;
11705c6c1daeSBarry Smith       char   *fnd;
11715c6c1daeSBarry Smith       while (cnt--) {
11725c6c1daeSBarry Smith 
11735c6c1daeSBarry Smith         if (!fgets(buf, sizeof(buf), fd)) {
11740298fd71SBarry Smith           ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
11755c6c1daeSBarry Smith           goto theend;
11765c6c1daeSBarry Smith         }
11770298fd71SBarry Smith         ierr = PetscInfo1(NULL,"POSTED data %s",buf);CHKERRQ(ierr);
11785c6c1daeSBarry Smith         ierr = PetscStrstr(buf,"Content-Type:",&fnd);CHKERRQ(ierr);
11795c6c1daeSBarry Smith         if (fnd) {
11805c6c1daeSBarry Smith           ierr = PetscStrstr(buf,"application/json-rpc",&fnd);CHKERRQ(ierr);
11815c6c1daeSBarry Smith           if (!fnd) {
11820298fd71SBarry Smith             ierr = PetscInfo(NULL,"POST content is not json-rpc, skipping post\n");CHKERRQ(ierr);
11835c6c1daeSBarry Smith             goto theend;
11845c6c1daeSBarry Smith           }
11855c6c1daeSBarry Smith         }
11865c6c1daeSBarry Smith       }
11875c6c1daeSBarry Smith       if (!fgets(buf, sizeof(buf), fd)) {
11880298fd71SBarry Smith         ierr = PetscInfo(NULL,"Cannot read POST length data, giving up\n");CHKERRQ(ierr);
11895c6c1daeSBarry Smith         goto theend;
11905c6c1daeSBarry Smith       }
11910298fd71SBarry Smith       ierr = PetscInfo1(NULL,"POSTED length data %s",buf);CHKERRQ(ierr);
11925c6c1daeSBarry Smith       sscanf(buf,"Content-Length: %d\n",&len);
11930298fd71SBarry Smith       ierr = PetscInfo1(NULL,"Length of POSTED data %d\n",len);CHKERRQ(ierr);
11945c6c1daeSBarry Smith       if (!fgets(buf, sizeof(buf), fd)) {
11950298fd71SBarry Smith         ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
11965c6c1daeSBarry Smith         goto theend;
11975c6c1daeSBarry Smith       }
11980298fd71SBarry Smith       ierr = PetscInfo1(NULL,"POSTED data %s",buf);CHKERRQ(ierr);
11995c6c1daeSBarry Smith       if (!fgets(buf, sizeof(buf), fd)) {
12000298fd71SBarry Smith         ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
12015c6c1daeSBarry Smith         goto theend;
12025c6c1daeSBarry Smith       }
12030298fd71SBarry Smith       ierr = PetscInfo1(NULL,"POSTED data %s",buf);CHKERRQ(ierr);
12045c6c1daeSBarry Smith       if (!fgets(buf, len+1, fd)) { /* why is this len + 1? */
12050298fd71SBarry Smith         ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
12065c6c1daeSBarry Smith         goto theend;
12075c6c1daeSBarry Smith       }
12080298fd71SBarry Smith       ierr = PetscInfo1(NULL,"POSTED data %s\n",buf);CHKERRQ(ierr);
12095c6c1daeSBarry Smith       fseek(fd, 0, SEEK_CUR); /* Force change of stream direction */
12105c6c1daeSBarry Smith       ierr = PetscProcessYAMLRPC(buf,&result);CHKERRQ(ierr);
12115c6c1daeSBarry Smith       ierr = PetscStrlen(result,&elen);CHKERRQ(ierr);
12125c6c1daeSBarry Smith       ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "application/json-rpc",(int)elen);CHKERRQ(ierr);
12135c6c1daeSBarry Smith       fprintf(fd, "%s",result);
12145c6c1daeSBarry Smith       goto theend;
12155c6c1daeSBarry Smith     } else {
12165c6c1daeSBarry Smith #endif
12175c6c1daeSBarry Smith       ierr = PetscWebSendError(fd, 501, "Not supported", NULL, "Method is not supported.");CHKERRQ(ierr);
12180298fd71SBarry Smith       ierr = PetscInfo(NULL,"Web request not a GET or POST, giving up\n");CHKERRQ(ierr);
12195c6c1daeSBarry Smith #if defined(PETSC_HAVE_YAML)
12205c6c1daeSBarry Smith     }
12215c6c1daeSBarry Smith #endif
12225c6c1daeSBarry Smith   } else {
12235c6c1daeSBarry Smith     fseek(fd, 0, SEEK_CUR); /* Force change of stream direction */
12245c6c1daeSBarry Smith 
12255c6c1daeSBarry Smith     ierr = PetscStrcmp(path,"/favicon.ico",&flg);CHKERRQ(ierr);
12265c6c1daeSBarry Smith     if (flg) {
12275c6c1daeSBarry Smith       /* should have cool PETSc icon */;
12285c6c1daeSBarry Smith       goto theend;
12295c6c1daeSBarry Smith     }
12305c6c1daeSBarry Smith     ierr = PetscStrcmp(path,"/",&flg);CHKERRQ(ierr);
12315c6c1daeSBarry Smith     if (flg) {
12325c6c1daeSBarry Smith       char        program[128];
12335c6c1daeSBarry Smith       PetscMPIInt size;
12345c6c1daeSBarry Smith       PetscViewer viewer;
12355c6c1daeSBarry Smith 
12365c6c1daeSBarry Smith       ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
12375c6c1daeSBarry Smith       ierr = PetscGetProgramName(program,128);CHKERRQ(ierr);
12385c6c1daeSBarry Smith       ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "text/html", -1);CHKERRQ(ierr);
12395c6c1daeSBarry Smith       fprintf(fd, "<HTML><HEAD><TITLE>Petsc Application Server</TITLE></HEAD>\r\n<BODY>");
12405c6c1daeSBarry Smith       fprintf(fd, "<H4>Serving PETSc application code %s </H4>\r\n\n",program);
12415c6c1daeSBarry Smith       fprintf(fd, "Number of processes %d\r\n\n",size);
12425c6c1daeSBarry Smith       fprintf(fd, "<HR>\r\n");
12435c6c1daeSBarry Smith       ierr = PetscViewerASCIIOpenWithFILE(PETSC_COMM_WORLD,fd,&viewer);CHKERRQ(ierr);
12445c6c1daeSBarry Smith       ierr = PetscOptionsView(viewer);CHKERRQ(ierr);
12455c6c1daeSBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
12465c6c1daeSBarry Smith       fprintf(fd, "<HR>\r\n");
12475c6c1daeSBarry Smith #if defined(PETSC_HAVE_AMS)
12485c6c1daeSBarry Smith       if (PetscAMSPublishAll) {
12495c6c1daeSBarry Smith         fprintf(fd, "<a href=\"./ams-tree\">Connect to Memory Snooper--Tree Display</a></p>\r\n\r\n");
12505c6c1daeSBarry Smith         fprintf(fd, "<a href=\"./ams-list\">Connect to Memory Snooper--List Display</a></p>\r\n\r\n");
12515c6c1daeSBarry Smith       }
12525c6c1daeSBarry Smith #endif
12535c6c1daeSBarry Smith       fprintf(fd, "<a href=\"./AMSJavascript.html\">Connect to Memory Snooper--Interactive Javascript</a></p>\r\n\r\n");
12545c6c1daeSBarry Smith       ierr = PetscWebSendFooter(fd);CHKERRQ(ierr);
12555c6c1daeSBarry Smith       goto theend;
12565c6c1daeSBarry Smith     }
12575c6c1daeSBarry Smith 
12585c6c1daeSBarry Smith #if defined(PETSC_HAVE_AMS)
12595c6c1daeSBarry Smith     ierr = PetscStrcmp(path,"/ams-list",&flg);CHKERRQ(ierr);
12605c6c1daeSBarry Smith     if (flg) {
12615c6c1daeSBarry Smith       ierr = PetscAMSDisplayList(fd);CHKERRQ(ierr);
12625c6c1daeSBarry Smith       goto theend;
12635c6c1daeSBarry Smith     }
12640298fd71SBarry Smith     ierr = PetscInfo1(NULL,"Browser path %s\n",path);
12655c6c1daeSBarry Smith     ierr = PetscStrcmp(path,"/ams-tree",&flg);CHKERRQ(ierr);
12665c6c1daeSBarry Smith     if (flg) {
12675c6c1daeSBarry Smith       ierr = PetscAMSDisplayTree(fd);CHKERRQ(ierr);
12685c6c1daeSBarry Smith       goto theend;
12695c6c1daeSBarry Smith     }
12705c6c1daeSBarry Smith #endif
12715c6c1daeSBarry Smith     ierr = PetscStrcpy(fullpath,"${PETSC_DIR}/include/web");CHKERRQ(ierr);
12725c6c1daeSBarry Smith     ierr = PetscStrcat(fullpath,path);CHKERRQ(ierr);
12730298fd71SBarry Smith     ierr = PetscInfo1(NULL,"Checking for file %s\n",fullpath);CHKERRQ(ierr);
12745c6c1daeSBarry Smith     ierr = PetscStrreplace(PETSC_COMM_SELF,fullpath,truefullpath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
12755c6c1daeSBarry Smith     fdo  = fopen(truefullpath,"r");
12765c6c1daeSBarry Smith     if (fdo) {
12775c6c1daeSBarry Smith       PetscInt    length,index;
12785c6c1daeSBarry Smith       char        data[4096];
12795c6c1daeSBarry Smith       struct stat statbuf;
12805c6c1daeSBarry Smith       int         n;
12815c6c1daeSBarry Smith       const char  *suffixes[] = {".html",".js",".gif",0}, *mimes[] = {"text/html","text/javascript","image/gif","text/unknown"};
12825c6c1daeSBarry Smith 
12835c6c1daeSBarry Smith       ierr = PetscStrendswithwhich(fullpath,suffixes,&index);CHKERRQ(ierr);
12845c6c1daeSBarry Smith       type = mimes[index];
12855c6c1daeSBarry Smith       if (!stat(truefullpath, &statbuf)) length = -1;
12865c6c1daeSBarry Smith       else length = S_ISREG(statbuf.st_mode) ? statbuf.st_size : -1;
12875c6c1daeSBarry Smith       ierr = PetscWebSendHeader(fd, 200, "OK", NULL, type, length);CHKERRQ(ierr);
12885c6c1daeSBarry Smith       while ((n = fread(data, 1, sizeof(data), fdo)) > 0) fwrite(data, 1, n, fd);
12895c6c1daeSBarry Smith       fclose(fdo);
12900298fd71SBarry Smith       ierr = PetscInfo2(NULL,"Sent file %s to browser using format %s\n",fullpath,type);CHKERRQ(ierr);
12915c6c1daeSBarry Smith       goto theend;
12925c6c1daeSBarry Smith     }
12935c6c1daeSBarry Smith     ierr = PetscWebSendError(fd, 501, "Not supported", NULL, "Unknown request.");CHKERRQ(ierr);
12945c6c1daeSBarry Smith   }
12955c6c1daeSBarry Smith theend:
12965c6c1daeSBarry Smith   ierr = PetscTokenDestroy(&tok);CHKERRQ(ierr);
12975c6c1daeSBarry Smith   fclose(fd);
12980298fd71SBarry Smith   ierr = PetscInfo(NULL,"Finished processing request\n");CHKERRQ(ierr);
12995c6c1daeSBarry Smith   PetscFunctionReturn(0);
13005c6c1daeSBarry Smith }
13015c6c1daeSBarry Smith 
13025c6c1daeSBarry Smith #undef __FUNCT__
13035c6c1daeSBarry Smith #define __FUNCT__ "PetscWebServeWait"
13045c6c1daeSBarry Smith /*@C
13055c6c1daeSBarry Smith       PetscWebServeWait - waits for requests on a thread
13065c6c1daeSBarry Smith 
13075c6c1daeSBarry Smith     Not collective
13085c6c1daeSBarry Smith 
13095c6c1daeSBarry Smith   Input Parameter:
13105c6c1daeSBarry Smith .   port - port to listen on
13115c6c1daeSBarry Smith 
13125c6c1daeSBarry Smith     Level: developer
13135c6c1daeSBarry Smith 
13145c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscWebServe()
13155c6c1daeSBarry Smith @*/
13165c6c1daeSBarry Smith void *PetscWebServeWait(int *port)
13175c6c1daeSBarry Smith {
13185c6c1daeSBarry Smith   PetscErrorCode ierr;
13195c6c1daeSBarry Smith   int            iport,listenport,tport = *port;
13205c6c1daeSBarry Smith 
13210298fd71SBarry Smith   ierr = PetscInfo1(NULL,"Starting webserver at port %d\n",tport);if (ierr) return 0;
13225c6c1daeSBarry Smith   ierr = PetscFree(port);if (ierr) return 0;
13235c6c1daeSBarry Smith   ierr = PetscSocketEstablish(tport,&listenport);if (ierr) return 0;
13245c6c1daeSBarry Smith   while (1) {
13255c6c1daeSBarry Smith     ierr = PetscSocketListen(listenport,&iport);if (ierr) return 0;
13265c6c1daeSBarry Smith     ierr = PetscWebServeRequest(iport);if (ierr) return 0;
13275c6c1daeSBarry Smith     close(iport);
13285c6c1daeSBarry Smith   }
13295c6c1daeSBarry Smith   close(listenport);
13305c6c1daeSBarry Smith   return 0;
13315c6c1daeSBarry Smith }
13325c6c1daeSBarry Smith 
13335c6c1daeSBarry Smith #undef __FUNCT__
13345c6c1daeSBarry Smith #define __FUNCT__ "PetscWebServe"
13355c6c1daeSBarry Smith /*@C
13365c6c1daeSBarry Smith       PetscWebServe - start up the PETSc web server and respond to requests
13375c6c1daeSBarry Smith 
13385c6c1daeSBarry Smith     Not collective - only does something on process zero of the communicator
13395c6c1daeSBarry Smith 
13405c6c1daeSBarry Smith   Input Parameters:
13415c6c1daeSBarry Smith +   comm - the MPI communicator
13425c6c1daeSBarry Smith -   port - port to listen on
13435c6c1daeSBarry Smith 
13445c6c1daeSBarry Smith   Options Database Key:
13455c6c1daeSBarry Smith +  -server <port> - start PETSc webserver (default port is 8080)
13465c6c1daeSBarry Smith -  -ams_publish_objects
13475c6c1daeSBarry Smith 
13485c6c1daeSBarry Smith 
13495c6c1daeSBarry Smith    Notes: Point your browser to http://hostname:8080   to access the PETSc web server, where hostname is the name of your machine.
13505c6c1daeSBarry Smith       If you are running PETSc on your local machine you can use http://localhost:8080
13515c6c1daeSBarry Smith 
13525c6c1daeSBarry Smith       If the PETSc program completes before you connect with the browser you will not be able to connect to the PETSc webserver.
13535c6c1daeSBarry Smith 
13545c6c1daeSBarry Smith       Read the top of $PETSC_DIR/include/web/AMSJavascript.py before running.
13555c6c1daeSBarry Smith 
13565c6c1daeSBarry Smith     Level: developer
13575c6c1daeSBarry Smith 
13585c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen()
13595c6c1daeSBarry Smith @*/
13605c6c1daeSBarry Smith PetscErrorCode  PetscWebServe(MPI_Comm comm,int port)
13615c6c1daeSBarry Smith {
13625c6c1daeSBarry Smith   PetscErrorCode ierr;
13635c6c1daeSBarry Smith   PetscMPIInt    rank;
13645c6c1daeSBarry Smith   pthread_t      thread;
13655c6c1daeSBarry Smith   int            *trueport;
13665c6c1daeSBarry Smith 
13675c6c1daeSBarry Smith   PetscFunctionBegin;
13685c6c1daeSBarry Smith   if (port < 1 && port != PETSC_DEFAULT && port != PETSC_DECIDE) SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_ARG_WRONG,"Cannot use negative port number %d",port);
13695c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
13705c6c1daeSBarry Smith   if (rank) PetscFunctionReturn(0);
13715c6c1daeSBarry Smith 
13725c6c1daeSBarry Smith   if (port == PETSC_DECIDE || port == PETSC_DEFAULT) port = 8080;
13735c6c1daeSBarry Smith   ierr = PetscMalloc(1*sizeof(int),&trueport);CHKERRQ(ierr); /* malloc this so it still exists in thread */
13745c6c1daeSBarry Smith   *trueport = port;
13755c6c1daeSBarry Smith   ierr = pthread_create(&thread, NULL, (void *(*)(void*))PetscWebServeWait, trueport);CHKERRQ(ierr);
13765c6c1daeSBarry Smith   PetscFunctionReturn(0);
13775c6c1daeSBarry Smith }
13785c6c1daeSBarry Smith #endif
13795c6c1daeSBarry Smith 
13805c6c1daeSBarry Smith 
13815c6c1daeSBarry Smith 
13825c6c1daeSBarry Smith 
13835c6c1daeSBarry Smith 
13845c6c1daeSBarry Smith 
13855c6c1daeSBarry Smith 
13865c6c1daeSBarry Smith 
13875c6c1daeSBarry Smith 
13885c6c1daeSBarry Smith 
13895c6c1daeSBarry Smith 
13905c6c1daeSBarry Smith 
13915c6c1daeSBarry Smith 
13925c6c1daeSBarry Smith 
1393