xref: /petsc/src/sys/classes/viewer/impls/socket/send.c (revision 242ebda14a69a344bed43bec2e001e417d7cf4e9)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petscsys.h>
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith #if defined(PETSC_NEEDS_UTYPE_TYPEDEFS)
55c6c1daeSBarry Smith /* Some systems have inconsistent include files that use but do not
65c6c1daeSBarry Smith    ensure that the following definitions are made */
75c6c1daeSBarry Smith typedef unsigned char   u_char;
85c6c1daeSBarry Smith typedef unsigned short  u_short;
95c6c1daeSBarry Smith typedef unsigned short  ushort;
105c6c1daeSBarry Smith typedef unsigned int    u_int;
115c6c1daeSBarry Smith typedef unsigned long   u_long;
125c6c1daeSBarry Smith #endif
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith #include <errno.h>
155c6c1daeSBarry Smith #include <ctype.h>
165c6c1daeSBarry Smith #if defined(PETSC_HAVE_MACHINE_ENDIAN_H)
175c6c1daeSBarry Smith #include <machine/endian.h>
185c6c1daeSBarry Smith #endif
195c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
205c6c1daeSBarry Smith #include <unistd.h>
215c6c1daeSBarry Smith #endif
225c6c1daeSBarry Smith #if defined(PETSC_HAVE_SYS_SOCKET_H)
235c6c1daeSBarry Smith #include <sys/socket.h>
245c6c1daeSBarry Smith #endif
255c6c1daeSBarry Smith #if defined(PETSC_HAVE_SYS_WAIT_H)
265c6c1daeSBarry Smith #include <sys/wait.h>
275c6c1daeSBarry Smith #endif
285c6c1daeSBarry Smith #if defined(PETSC_HAVE_NETINET_IN_H)
295c6c1daeSBarry Smith #include <netinet/in.h>
305c6c1daeSBarry Smith #endif
315c6c1daeSBarry Smith #if defined(PETSC_HAVE_NETDB_H)
325c6c1daeSBarry Smith #include <netdb.h>
335c6c1daeSBarry Smith #endif
345c6c1daeSBarry Smith #if defined(PETSC_HAVE_FCNTL_H)
355c6c1daeSBarry Smith #include <fcntl.h>
365c6c1daeSBarry Smith #endif
375c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H)
385c6c1daeSBarry Smith #include <io.h>
395c6c1daeSBarry Smith #endif
405c6c1daeSBarry Smith #if defined(PETSC_HAVE_WINSOCK2_H)
415c6c1daeSBarry Smith #include <Winsock2.h>
425c6c1daeSBarry Smith #endif
435c6c1daeSBarry Smith #include <sys/stat.h>
445c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/socket/socket.h>
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith #if defined(PETSC_NEED_CLOSE_PROTO)
478cc058d9SJed Brown PETSC_EXTERN int close(int);
485c6c1daeSBarry Smith #endif
495c6c1daeSBarry Smith #if defined(PETSC_NEED_SOCKET_PROTO)
508cc058d9SJed Brown PETSC_EXTERN int socket(int,int,int);
515c6c1daeSBarry Smith #endif
525c6c1daeSBarry Smith #if defined(PETSC_NEED_SLEEP_PROTO)
538cc058d9SJed Brown PETSC_EXTERN int sleep(unsigned);
545c6c1daeSBarry Smith #endif
555c6c1daeSBarry Smith #if defined(PETSC_NEED_CONNECT_PROTO)
568cc058d9SJed Brown PETSC_EXTERN int connect(int,struct sockaddr*,int);
575c6c1daeSBarry Smith #endif
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith /*--------------------------------------------------------------*/
605c6c1daeSBarry Smith #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"
293*242ebda1SBarry Smith static 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"
3238cc058d9SJed 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 
478*242ebda1SBarry Smith /* ---------------------------------------------------------------------*/
4795c6c1daeSBarry Smith #if defined(PETSC_USE_SERVER)
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith #include <pthread.h>
4825c6c1daeSBarry Smith #include <time.h>
4835c6c1daeSBarry Smith #define PROTOCOL   "HTTP/1.1"
4845c6c1daeSBarry Smith #define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
4855c6c1daeSBarry Smith 
4865c6c1daeSBarry Smith #undef __FUNCT__
4875c6c1daeSBarry Smith #define __FUNCT__ "PetscWebSendHeader"
4885c6c1daeSBarry Smith PetscErrorCode PetscWebSendHeader(FILE *f, int status, const char *title, const char *extra, const char *mime, int length)
4895c6c1daeSBarry Smith {
4905c6c1daeSBarry Smith   time_t now;
4915c6c1daeSBarry Smith   char   timebuf[128];
4925c6c1daeSBarry Smith 
4935c6c1daeSBarry Smith   PetscFunctionBegin;
4945c6c1daeSBarry Smith   fprintf(f, "%s %d %s\r\n", PROTOCOL, status, title);
4955c6c1daeSBarry Smith   fprintf(f, "Server: %s\r\n", "petscserver/1.0");
4965c6c1daeSBarry Smith   now = time(NULL);
4975c6c1daeSBarry Smith   strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
4985c6c1daeSBarry Smith   fprintf(f, "Date: %s\r\n", timebuf);
4995c6c1daeSBarry Smith   if (extra) fprintf(f, "%s\r\n", extra);
5005c6c1daeSBarry Smith   if (mime) fprintf(f, "Content-Type: %s\r\n", mime);
5015c6c1daeSBarry Smith   if (length >= 0) fprintf(f, "Content-Length: %d\r\n", length);
5025c6c1daeSBarry Smith   fprintf(f, "Connection: close\r\n");
5035c6c1daeSBarry Smith   fprintf(f, "\r\n");
5045c6c1daeSBarry Smith   PetscFunctionReturn(0);
5055c6c1daeSBarry Smith }
5065c6c1daeSBarry Smith 
5075c6c1daeSBarry Smith #undef __FUNCT__
5085c6c1daeSBarry Smith #define __FUNCT__ "PetscWebSendFooter"
5095c6c1daeSBarry Smith PetscErrorCode PetscWebSendFooter(FILE *fd)
5105c6c1daeSBarry Smith {
5115c6c1daeSBarry Smith   PetscFunctionBegin;
5125c6c1daeSBarry Smith   fprintf(fd, "</BODY></HTML>\r\n");
5135c6c1daeSBarry Smith   PetscFunctionReturn(0);
5145c6c1daeSBarry Smith }
5155c6c1daeSBarry Smith 
5165c6c1daeSBarry Smith #undef __FUNCT__
5175c6c1daeSBarry Smith #define __FUNCT__ "PetscWebSendError"
5185c6c1daeSBarry Smith PetscErrorCode PetscWebSendError(FILE *f, int status, const char *title, const char *extra, const char *text)
5195c6c1daeSBarry Smith {
5205c6c1daeSBarry Smith   PetscErrorCode ierr;
5215c6c1daeSBarry Smith 
5225c6c1daeSBarry Smith   PetscFunctionBegin;
5235c6c1daeSBarry Smith   ierr = PetscWebSendHeader(f, status, title, extra, "text/html", -1);CHKERRQ(ierr);
5245c6c1daeSBarry Smith   fprintf(f, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\r\n", status, title);
5255c6c1daeSBarry Smith   fprintf(f, "<BODY><H4>%d %s</H4>\r\n", status, title);
5265c6c1daeSBarry Smith   fprintf(f, "%s\r\n", text);
5275c6c1daeSBarry Smith   ierr = PetscWebSendFooter(f);CHKERRQ(ierr);
5285c6c1daeSBarry Smith   PetscFunctionReturn(0);
5295c6c1daeSBarry Smith }
5305c6c1daeSBarry Smith 
5315c6c1daeSBarry Smith #if defined(PETSC_HAVE_AMS)
5325c6c1daeSBarry Smith #undef __FUNCT__
533*242ebda1SBarry Smith #define __FUNCT__ "PetscAMSObjectsDisplayList"
534*242ebda1SBarry Smith /*
535*242ebda1SBarry Smith     Displays all the PETSc objects published with AMS in a simple HTML list
536*242ebda1SBarry Smith 
537*242ebda1SBarry Smith     Does NOT use Javascript or JSON-RPC
538*242ebda1SBarry Smith */
539*242ebda1SBarry Smith static PetscErrorCode PetscAMSObjectsDisplayList(FILE *fd)
5405c6c1daeSBarry Smith {
5415c6c1daeSBarry Smith   PetscErrorCode     ierr;
5425c6c1daeSBarry Smith   char               host[256],**comm_list,**mem_list,**fld_list;
5435c6c1daeSBarry Smith   AMS_Comm           ams;
5445c6c1daeSBarry Smith   PetscInt           i = 0,j;
5455c6c1daeSBarry Smith   AMS_Memory_type    mtype;
5465c6c1daeSBarry Smith   AMS_Data_type      dtype;
5475c6c1daeSBarry Smith   AMS_Shared_type    stype;
5485c6c1daeSBarry Smith   AMS_Reduction_type rtype;
5495c6c1daeSBarry Smith   AMS_Memory         memory;
5505c6c1daeSBarry Smith   int                len;
5515c6c1daeSBarry Smith   void               *addr;
5525c6c1daeSBarry Smith 
5535c6c1daeSBarry Smith   ierr = PetscGetHostName(host,256);CHKERRQ(ierr);
5545c6c1daeSBarry Smith   ierr = AMS_Connect(host, -1, &comm_list);CHKERRQ(ierr);
5555c6c1daeSBarry Smith   ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "text/html", -1);CHKERRQ(ierr);
556a297a907SKarl Rupp   if (!comm_list || !comm_list[0]) fprintf(fd, "AMS Communicator not running</p>\r\n");
557a297a907SKarl Rupp   else {
5585c6c1daeSBarry Smith     ierr = AMS_Comm_attach(comm_list[0],&ams);CHKERRQ(ierr);
5595c6c1daeSBarry Smith     ierr = AMS_Comm_get_memory_list(ams,&mem_list);CHKERRQ(ierr);
560a297a907SKarl Rupp     if (!mem_list[0]) fprintf(fd, "AMS Communicator %s has no published memories</p>\r\n",comm_list[0]);
561a297a907SKarl Rupp     else {
5625c6c1daeSBarry Smith       fprintf(fd, "<HTML><HEAD><TITLE>Petsc Application Server</TITLE></HEAD>\r\n<BODY>");
5635c6c1daeSBarry Smith       fprintf(fd,"<ul>\r\n");
5645c6c1daeSBarry Smith       while (mem_list[i]) {
5655c6c1daeSBarry Smith         fprintf(fd,"<li> %s</li>\r\n",mem_list[i]);
5665c6c1daeSBarry Smith         ierr = AMS_Memory_attach(ams,mem_list[i],&memory,NULL);CHKERRQ(ierr);
5675c6c1daeSBarry Smith         ierr = AMS_Memory_get_field_list(memory, &fld_list);CHKERRQ(ierr);
5685c6c1daeSBarry Smith         j    = 0;
5695c6c1daeSBarry Smith         fprintf(fd,"<ul>\r\n");
5705c6c1daeSBarry Smith         while (fld_list[j]) {
5715c6c1daeSBarry Smith           fprintf(fd,"<li> %s",fld_list[j]);
5725c6c1daeSBarry Smith           ierr = AMS_Memory_get_field_info(memory, fld_list[j], &addr, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
5735c6c1daeSBarry Smith           if (len == 1) {
5745c6c1daeSBarry Smith             if (dtype == AMS_INT)         fprintf(fd," %d",*(int*)addr);
5755c6c1daeSBarry Smith             else if (dtype == AMS_STRING) fprintf(fd," %s",*(char**)addr);
5765c6c1daeSBarry Smith           }
5775c6c1daeSBarry Smith           fprintf(fd,"</li>\r\n");
5785c6c1daeSBarry Smith           j++;
5795c6c1daeSBarry Smith         }
5805c6c1daeSBarry Smith         fprintf(fd,"</ul>\r\n");
5815c6c1daeSBarry Smith         i++;
5825c6c1daeSBarry Smith       }
5835c6c1daeSBarry Smith       fprintf(fd,"</ul>\r\n");
5845c6c1daeSBarry Smith     }
5855c6c1daeSBarry Smith   }
5865c6c1daeSBarry Smith   ierr = PetscWebSendFooter(fd);CHKERRQ(ierr);
5875c6c1daeSBarry Smith   ierr = AMS_Disconnect();CHKERRQ(ierr);
5885c6c1daeSBarry Smith   PetscFunctionReturn(0);
5895c6c1daeSBarry Smith }
5905c6c1daeSBarry Smith 
5915c6c1daeSBarry Smith #undef __FUNCT__
592*242ebda1SBarry Smith #define __FUNCT__ "PetscAMSObjectsDisplayTree"
593*242ebda1SBarry Smith /*
594*242ebda1SBarry Smith     Displays all the PETSc objects published with AMS in very crude HTML 5 graphics
595*242ebda1SBarry Smith 
596*242ebda1SBarry Smith     Does NOT use Javascript or JSON-RPC
597*242ebda1SBarry Smith */
598*242ebda1SBarry Smith static PetscErrorCode PetscAMSObjectsDisplayTree(FILE *fd)
5995c6c1daeSBarry Smith {
6005c6c1daeSBarry Smith   PetscErrorCode     ierr;
6015c6c1daeSBarry Smith   char               host[256],**comm_list,**mem_list,**fld_list;
6025c6c1daeSBarry Smith   AMS_Comm           ams;
6035c6c1daeSBarry Smith   PetscInt           i = 0,j;
6045c6c1daeSBarry Smith   AMS_Memory_type    mtype;
6055c6c1daeSBarry Smith   AMS_Data_type      dtype;
6065c6c1daeSBarry Smith   AMS_Shared_type    stype;
6075c6c1daeSBarry Smith   AMS_Reduction_type rtype;
6085c6c1daeSBarry Smith   AMS_Memory         memory;
6095c6c1daeSBarry Smith   int                len;
6105c6c1daeSBarry Smith   void               *addr2,*addr3,*addr,*addr4;
6115c6c1daeSBarry Smith 
6125c6c1daeSBarry Smith   ierr = PetscGetHostName(host,256);CHKERRQ(ierr);
6135c6c1daeSBarry Smith   ierr = AMS_Connect(host, -1, &comm_list);CHKERRQ(ierr);
6145c6c1daeSBarry Smith   ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "text/html", -1);CHKERRQ(ierr);
615a297a907SKarl Rupp   if (!comm_list || !comm_list[0]) fprintf(fd, "AMS Communicator not running</p>\r\n");
616a297a907SKarl Rupp   else {
6175c6c1daeSBarry Smith     ierr = AMS_Comm_attach(comm_list[0],&ams);CHKERRQ(ierr);
6185c6c1daeSBarry Smith     ierr = AMS_Comm_get_memory_list(ams,&mem_list);CHKERRQ(ierr);
619a297a907SKarl Rupp     if (!mem_list[0]) fprintf(fd, "AMS Communicator %s has no published memories</p>\r\n",comm_list[0]);
620a297a907SKarl Rupp     else {
6215c6c1daeSBarry Smith       PetscInt  Nlevels,*Level,*Levelcnt,*Idbylevel,*Column,*parentid,*Id,maxId = 0,maxCol = 0,*parentId,id,cnt,Nlevelcnt = 0;
6225c6c1daeSBarry Smith       PetscBool *mask;
6235c6c1daeSBarry Smith       char      **classes,*clas,**subclasses,*sclas;
6245c6c1daeSBarry Smith 
6255c6c1daeSBarry Smith       /* get maximum number of objects */
6265c6c1daeSBarry Smith       while (mem_list[i]) {
6275c6c1daeSBarry Smith         ierr  = AMS_Memory_attach(ams,mem_list[i],&memory,NULL);CHKERRQ(ierr);
6285c6c1daeSBarry Smith         ierr  = AMS_Memory_get_field_list(memory, &fld_list);CHKERRQ(ierr);
6295c6c1daeSBarry Smith         ierr  = AMS_Memory_get_field_info(memory, "Id", &addr2, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6305c6c1daeSBarry Smith         Id    = (int*) addr2;
6315c6c1daeSBarry Smith         maxId = PetscMax(maxId,*Id);
6325c6c1daeSBarry Smith         i++;
6335c6c1daeSBarry Smith       }
6345c6c1daeSBarry Smith       maxId++;
6355c6c1daeSBarry Smith 
6365c6c1daeSBarry Smith       /* Gets everyone's parent ID and which nodes are masked */
6375c6c1daeSBarry Smith       ierr = PetscMalloc4(maxId,PetscInt,&parentid,maxId,PetscBool,&mask,maxId,char**,&classes,maxId,char**,&subclasses);CHKERRQ(ierr);
6385c6c1daeSBarry Smith       ierr = PetscMemzero(classes,maxId*sizeof(char*));CHKERRQ(ierr);
6395c6c1daeSBarry Smith       ierr = PetscMemzero(subclasses,maxId*sizeof(char*));CHKERRQ(ierr);
6405c6c1daeSBarry Smith       for (i=0; i<maxId; i++) mask[i] = PETSC_TRUE;
6415c6c1daeSBarry Smith       i = 0;
6425c6c1daeSBarry Smith       while (mem_list[i]) {
6435c6c1daeSBarry Smith         ierr          = AMS_Memory_attach(ams,mem_list[i],&memory,NULL);CHKERRQ(ierr);
6445c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_list(memory, &fld_list);CHKERRQ(ierr);
6455c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "Id", &addr2, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6465c6c1daeSBarry Smith         Id            = (int*) addr2;
6475c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "ParentId", &addr3, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6485c6c1daeSBarry Smith         parentId      = (int*) addr3;
6495c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "Class", &addr, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6505c6c1daeSBarry Smith         clas          = *(char**)addr;
6515c6c1daeSBarry Smith         ierr          = AMS_Memory_get_field_info(memory, "Type", &addr4, &len, &dtype, &mtype, &stype, &rtype);CHKERRQ(ierr);
6525c6c1daeSBarry Smith         sclas         = *(char**)addr4;
6535c6c1daeSBarry Smith         parentid[*Id] = *parentId;
6545c6c1daeSBarry Smith         mask[*Id]     = PETSC_FALSE;
655a297a907SKarl Rupp 
6565c6c1daeSBarry Smith         ierr = PetscStrallocpy(clas,classes+*Id);CHKERRQ(ierr);
6575c6c1daeSBarry Smith         ierr = PetscStrallocpy(sclas,subclasses+*Id);CHKERRQ(ierr);
6585c6c1daeSBarry Smith         i++;
6595c6c1daeSBarry Smith       }
6605c6c1daeSBarry Smith 
6615c6c1daeSBarry Smith       /* if the parent is masked then relabel the parent as 0 since the true parent was deleted */
6625c6c1daeSBarry Smith       for (i=0; i<maxId; i++) {
6635c6c1daeSBarry Smith         if (!mask[i] && parentid[i] > 0 && mask[parentid[i]]) parentid[i] = 0;
6645c6c1daeSBarry Smith       }
6655c6c1daeSBarry Smith 
6665c6c1daeSBarry Smith       ierr = PetscProcessTree(maxId,mask,parentid,&Nlevels,&Level,&Levelcnt,&Idbylevel,&Column);CHKERRQ(ierr);
6675c6c1daeSBarry Smith 
668a297a907SKarl Rupp       for (i=0; i<Nlevels; i++) maxCol    = PetscMax(maxCol,Levelcnt[i]);
669a297a907SKarl Rupp       for (i=0; i<Nlevels; i++) Nlevelcnt = PetscMax(Nlevelcnt,Levelcnt[i]);
6705c6c1daeSBarry Smith 
6715c6c1daeSBarry Smith       /* print all the top-level objects */
6725c6c1daeSBarry Smith       fprintf(fd, "<HTML><HEAD><TITLE>Petsc Application Server</TITLE>\r\n");
6735c6c1daeSBarry Smith       fprintf(fd, "<canvas width=800 height=600 id=\"tree\"></canvas>\r\n");
6745c6c1daeSBarry Smith       fprintf(fd, "<script type=\"text/javascript\">\r\n");
6755c6c1daeSBarry Smith       fprintf(fd, "  function draw() {\r\n");
6765c6c1daeSBarry Smith       fprintf(fd, "  var example = document.getElementById('tree');\r\n");
6775c6c1daeSBarry Smith       fprintf(fd, "  var context = example.getContext('2d');\r\n");
6785c6c1daeSBarry Smith       /* adjust font size based on how big a tree is printed */
679a297a907SKarl Rupp       if (Nlevels > 5 || Nlevelcnt > 10) fprintf(fd, "  context.font         = \"normal 12px sans-serif\";\r\n");
680a297a907SKarl Rupp       else                               fprintf(fd, "  context.font         = \"normal 24px sans-serif\";\r\n");
6815c6c1daeSBarry Smith       fprintf(fd, "  context.fillStyle = \"rgb(255,0,0)\";\r\n");
6825c6c1daeSBarry Smith       fprintf(fd, "  context.textBaseline = \"top\";\r\n");
6835c6c1daeSBarry Smith       fprintf(fd, "  var xspacep = 0;\r\n");
6845c6c1daeSBarry Smith       fprintf(fd, "  var yspace = example.height/%d;\r\n",(Nlevels+1));
6855c6c1daeSBarry Smith       /* estimate the height of a string as twice the width of a character */
6865c6c1daeSBarry Smith       fprintf(fd, "  var wheight = context.measureText(\"K\");\r\n");
6875c6c1daeSBarry Smith       fprintf(fd, "  var height = 1.6*wheight.width;\r\n");
6885c6c1daeSBarry Smith 
6895c6c1daeSBarry Smith       cnt = 0;
6905c6c1daeSBarry Smith       for (i=0; i<Nlevels; i++) {
6915c6c1daeSBarry Smith         fprintf(fd, "  var xspace = example.width/%d;\r\n",Levelcnt[i]+1);
6925c6c1daeSBarry Smith         for (j=0; j<Levelcnt[i]; j++) {
6935c6c1daeSBarry Smith           id    = Idbylevel[cnt++];
6945c6c1daeSBarry Smith           clas  = classes[id];
6955c6c1daeSBarry Smith           sclas = subclasses[id];
6965c6c1daeSBarry Smith           fprintf(fd, "  var width = context.measureText(\"%s\");\r\n",clas);
6975c6c1daeSBarry Smith           fprintf(fd, "  var swidth = context.measureText(\"%s\");\r\n",sclas);
6985c6c1daeSBarry Smith           fprintf(fd, "  context.fillStyle = \"rgb(255,0,0)\";\r\n");
6995c6c1daeSBarry Smith           fprintf(fd, "  context.fillRect((%d)*xspace-width.width/2, %d*yspace-height/2, width.width, height);\r\n",j+1,i+1);
7005c6c1daeSBarry Smith           fprintf(fd, "  context.fillRect((%d)*xspace-swidth.width/2, %d*yspace+height/2, swidth.width, height);\r\n",j+1,i+1);
7015c6c1daeSBarry Smith           fprintf(fd, "  context.fillStyle = \"rgb(0,0,0)\";\r\n");
7025c6c1daeSBarry Smith           fprintf(fd, "  context.fillText(\"%s\",(%d)*xspace-width.width/2, %d*yspace-height/2);\r\n",clas,j+1,i+1);
7035c6c1daeSBarry Smith           fprintf(fd, "  context.fillText(\"%s\",(%d)*xspace-swidth.width/2, %d*yspace+height/2);\r\n",sclas,j+1,i+1);
7045c6c1daeSBarry Smith           if (parentid[id]) {
7055c6c1daeSBarry Smith             fprintf(fd, "  context.moveTo(%d*xspace,%d*yspace-height/2);\r\n",j+1,i+1);
7065c6c1daeSBarry Smith             fprintf(fd, "  context.lineTo(%d*xspacep,%d*yspace+3*height/2);\r\n",Column[parentid[id]]+1,i);
7075c6c1daeSBarry Smith             fprintf(fd, "  context.stroke();\r\n");
7085c6c1daeSBarry Smith           }
7095c6c1daeSBarry Smith         }
7105c6c1daeSBarry Smith         fprintf(fd, "  xspacep = xspace;\r\n");
7115c6c1daeSBarry Smith       }
7125c6c1daeSBarry Smith       ierr = PetscFree(Level);CHKERRQ(ierr);
7135c6c1daeSBarry Smith       ierr = PetscFree(Levelcnt);CHKERRQ(ierr);
7145c6c1daeSBarry Smith       ierr = PetscFree(Idbylevel);CHKERRQ(ierr);
7155c6c1daeSBarry Smith       ierr = PetscFree(Column);CHKERRQ(ierr);
7165c6c1daeSBarry Smith       for (i=0; i<maxId; i++) {
7175c6c1daeSBarry Smith         ierr = PetscFree(classes[i]);CHKERRQ(ierr);
7185c6c1daeSBarry Smith         ierr = PetscFree(subclasses[i]);CHKERRQ(ierr);
7195c6c1daeSBarry Smith       }
7205c6c1daeSBarry Smith       ierr = PetscFree4(mask,parentid,classes,subclasses);CHKERRQ(ierr);
7215c6c1daeSBarry Smith 
7225c6c1daeSBarry Smith       ierr = AMS_Disconnect();CHKERRQ(ierr);
7235c6c1daeSBarry Smith       fprintf(fd, "}\r\n");
7245c6c1daeSBarry Smith       fprintf(fd, "</script>\r\n");
7255c6c1daeSBarry Smith       fprintf(fd, "<body onload=\"draw();\">\r\n");
7265c6c1daeSBarry Smith       fprintf(fd, "</body></html>\r\n");
7275c6c1daeSBarry Smith     }
7285c6c1daeSBarry Smith   }
7295c6c1daeSBarry Smith   ierr = PetscWebSendFooter(fd);CHKERRQ(ierr);
7305c6c1daeSBarry Smith   PetscFunctionReturn(0);
7315c6c1daeSBarry Smith }
7325c6c1daeSBarry Smith #endif
7335c6c1daeSBarry Smith 
734*242ebda1SBarry Smith #undef __FUNCT__
735*242ebda1SBarry Smith #define __FUNCT__ "PetscWebServeRequestGet"
736*242ebda1SBarry Smith /*@C
737*242ebda1SBarry Smith       PetscWebServeRequestGet - serves a single web Get request
738*242ebda1SBarry Smith 
739*242ebda1SBarry Smith     Not collective
740*242ebda1SBarry Smith 
741*242ebda1SBarry Smith   Input Parameters:
742*242ebda1SBarry Smith +   port - the network file to read and write from
743*242ebda1SBarry Smith -   path - the command from the server
744*242ebda1SBarry Smith 
745*242ebda1SBarry Smith     Level: developer
746*242ebda1SBarry Smith 
747*242ebda1SBarry Smith .seealso: PetscWebServe()
748*242ebda1SBarry Smith @*/
749*242ebda1SBarry Smith static PetscErrorCode  PetscWebServeRequestGet(FILE *fd,const char path[])
750*242ebda1SBarry Smith {
751*242ebda1SBarry Smith   PetscErrorCode ierr;
752*242ebda1SBarry Smith   FILE           *fdo;
753*242ebda1SBarry Smith   char           fullpath[PETSC_MAX_PATH_LEN],truefullpath[PETSC_MAX_PATH_LEN];
754*242ebda1SBarry Smith   const char     *type;
755*242ebda1SBarry Smith   PetscBool      flg;
756*242ebda1SBarry Smith 
757*242ebda1SBarry Smith   PetscFunctionBegin;
758*242ebda1SBarry Smith   fseek(fd, 0, SEEK_CUR); /* Force change of stream direction */
759*242ebda1SBarry Smith 
760*242ebda1SBarry Smith   ierr = PetscStrcmp(path,"/favicon.ico",&flg);CHKERRQ(ierr);
761*242ebda1SBarry Smith   if (flg) {
762*242ebda1SBarry Smith     /* should have cool PETSc icon */;
763*242ebda1SBarry Smith     PetscFunctionReturn(0);
764*242ebda1SBarry Smith   }
765*242ebda1SBarry Smith   ierr = PetscStrcmp(path,"/",&flg);CHKERRQ(ierr);
766*242ebda1SBarry Smith   if (flg) {
767*242ebda1SBarry Smith     char        program[128];
768*242ebda1SBarry Smith     PetscMPIInt size;
769*242ebda1SBarry Smith     PetscViewer viewer;
770*242ebda1SBarry Smith 
771*242ebda1SBarry Smith     ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
772*242ebda1SBarry Smith     ierr = PetscGetProgramName(program,128);CHKERRQ(ierr);
773*242ebda1SBarry Smith     ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "text/html", -1);CHKERRQ(ierr);
774*242ebda1SBarry Smith     fprintf(fd, "<HTML><HEAD><TITLE>Petsc Application Server</TITLE></HEAD>\r\n<BODY>");
775*242ebda1SBarry Smith     fprintf(fd, "<H4>Serving PETSc application code %s </H4>\r\n\n",program);
776*242ebda1SBarry Smith     fprintf(fd, "Number of processes %d\r\n\n",size);
777*242ebda1SBarry Smith     fprintf(fd, "<HR>\r\n");
778*242ebda1SBarry Smith     ierr = PetscViewerASCIIOpenWithFILE(PETSC_COMM_WORLD,fd,&viewer);CHKERRQ(ierr);
779*242ebda1SBarry Smith     ierr = PetscOptionsView(viewer);CHKERRQ(ierr);
780*242ebda1SBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
781*242ebda1SBarry Smith     fprintf(fd, "<HR>\r\n");
782*242ebda1SBarry Smith #if defined(PETSC_HAVE_AMS)
783*242ebda1SBarry Smith     if (PetscAMSPublishAll) {
784*242ebda1SBarry Smith       fprintf(fd, "<a href=\"./ams-tree\">Connect to Memory Snooper--Tree Display</a></p>\r\n\r\n");
785*242ebda1SBarry Smith       fprintf(fd, "<a href=\"./ams-list\">Connect to Memory Snooper--List Display</a></p>\r\n\r\n");
786*242ebda1SBarry Smith     }
787*242ebda1SBarry Smith #endif
788*242ebda1SBarry Smith     fprintf(fd, "<a href=\"./AMSJavascript.html\">Connect to Memory Snooper--Interactive Javascript</a></p>\r\n\r\n");
789*242ebda1SBarry Smith     ierr = PetscWebSendFooter(fd);CHKERRQ(ierr);
790*242ebda1SBarry Smith     PetscFunctionReturn(0);
791*242ebda1SBarry Smith   }
792*242ebda1SBarry Smith 
793*242ebda1SBarry Smith #if defined(PETSC_HAVE_AMS)
794*242ebda1SBarry Smith   ierr = PetscStrcmp(path,"/ams-list",&flg);CHKERRQ(ierr);
795*242ebda1SBarry Smith   if (flg) {
796*242ebda1SBarry Smith     ierr = PetscAMSObjectsDisplayList(fd);CHKERRQ(ierr);
797*242ebda1SBarry Smith     PetscFunctionReturn(0);
798*242ebda1SBarry Smith   }
799*242ebda1SBarry Smith   ierr = PetscStrcmp(path,"/ams-tree",&flg);CHKERRQ(ierr);
800*242ebda1SBarry Smith   if (flg) {
801*242ebda1SBarry Smith     ierr = PetscAMSObjectsDisplayTree(fd);CHKERRQ(ierr);
802*242ebda1SBarry Smith     PetscFunctionReturn(0);
803*242ebda1SBarry Smith   }
804*242ebda1SBarry Smith #endif
805*242ebda1SBarry Smith   ierr = PetscStrcpy(fullpath,"${PETSC_DIR}/include/web");CHKERRQ(ierr);
806*242ebda1SBarry Smith   ierr = PetscStrcat(fullpath,path);CHKERRQ(ierr);
807*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"Checking for file %s\n",fullpath);CHKERRQ(ierr);
808*242ebda1SBarry Smith   ierr = PetscStrreplace(PETSC_COMM_SELF,fullpath,truefullpath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
809*242ebda1SBarry Smith   fdo  = fopen(truefullpath,"r");
810*242ebda1SBarry Smith   if (fdo) {
811*242ebda1SBarry Smith     PetscInt    length,index;
812*242ebda1SBarry Smith     char        data[4096];
813*242ebda1SBarry Smith     struct stat statbuf;
814*242ebda1SBarry Smith     int         n;
815*242ebda1SBarry Smith     const char  *suffixes[] = {".html",".js",".gif",0}, *mimes[] = {"text/html","text/javascript","image/gif","text/unknown"};
816*242ebda1SBarry Smith 
817*242ebda1SBarry Smith     ierr = PetscStrendswithwhich(fullpath,suffixes,&index);CHKERRQ(ierr);
818*242ebda1SBarry Smith     type = mimes[index];
819*242ebda1SBarry Smith     if (!stat(truefullpath, &statbuf)) length = -1;
820*242ebda1SBarry Smith     else length = S_ISREG(statbuf.st_mode) ? statbuf.st_size : -1;
821*242ebda1SBarry Smith     ierr = PetscWebSendHeader(fd, 200, "OK", NULL, type, length);CHKERRQ(ierr);
822*242ebda1SBarry Smith     while ((n = fread(data, 1, sizeof(data), fdo)) > 0) fwrite(data, 1, n, fd);
823*242ebda1SBarry Smith     fclose(fdo);
824*242ebda1SBarry Smith     ierr = PetscInfo2(NULL,"Sent file %s to browser using format %s\n",fullpath,type);CHKERRQ(ierr);
825*242ebda1SBarry Smith     PetscFunctionReturn(0);
826*242ebda1SBarry Smith   }
827*242ebda1SBarry Smith   ierr = PetscWebSendError(fd, 501, "Not supported", NULL, "Unknown request.");CHKERRQ(ierr);
828*242ebda1SBarry Smith   PetscFunctionReturn(0);
829*242ebda1SBarry Smith }
830*242ebda1SBarry Smith 
8315c6c1daeSBarry Smith #if defined(PETSC_HAVE_YAML)
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith /*
834*242ebda1SBarry Smith     Toy YAML/JSON-RPC function that returns all the arguments it is passed
8355c6c1daeSBarry Smith */
8365c6c1daeSBarry Smith #undef __FUNCT__
8375c6c1daeSBarry Smith #define __FUNCT__ "YAML_echo"
838*242ebda1SBarry Smith PETSC_UNUSED static PetscErrorCode YAML_echo(PetscInt argc,char **args,PetscInt *argco,char ***argso)
8395c6c1daeSBarry Smith {
8405c6c1daeSBarry Smith   PetscErrorCode ierr;
8415c6c1daeSBarry Smith   PetscInt       i;
8425c6c1daeSBarry Smith 
8435c6c1daeSBarry Smith   ierr = PetscPrintf(PETSC_COMM_SELF,"Number of arguments to function %d\n",argc);CHKERRQ(ierr);
8445c6c1daeSBarry Smith   for (i=0; i<argc; i++) {
8455c6c1daeSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF,"  %s\n",args[i]);CHKERRQ(ierr);
8465c6c1daeSBarry Smith   }
8475c6c1daeSBarry Smith   *argco = argc;
8485c6c1daeSBarry Smith   ierr   = PetscMalloc(argc*sizeof(char*),argso);CHKERRQ(ierr);
8495c6c1daeSBarry Smith   for (i=0; i<argc; i++) {
8505c6c1daeSBarry Smith     ierr = PetscStrallocpy(args[i],&(*argso)[i]);CHKERRQ(ierr);
8515c6c1daeSBarry Smith   }
8525c6c1daeSBarry Smith   PetscFunctionReturn(0);
8535c6c1daeSBarry Smith }
8545c6c1daeSBarry Smith 
855*242ebda1SBarry Smith /* -------------------------------------------------------------------------------------------
856*242ebda1SBarry Smith      The following set of functions are wrapper functions for AMS functions that
857*242ebda1SBarry Smith 
858*242ebda1SBarry Smith     1)  convert from string arguments to appropriate AMS arguments (int, double, char*, etc)
859*242ebda1SBarry Smith     2)  call the AMS function
860*242ebda1SBarry Smith     3)  convert from the AMS result arguments to string arguments
861*242ebda1SBarry Smith */
862*242ebda1SBarry Smith 
8635c6c1daeSBarry Smith #undef __FUNCT__
8645c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Connect"
8655c6c1daeSBarry Smith /*
8665c6c1daeSBarry Smith       Connects to the local AMS and gets only the first communication name
8675c6c1daeSBarry Smith 
8685c6c1daeSBarry Smith    Input Parameters:
8695c6c1daeSBarry Smith .     none
8705c6c1daeSBarry Smith 
8715c6c1daeSBarry Smith    Output Parameter:
8725c6c1daeSBarry Smith .     oarg1 - the string name of the first communicator
8735c6c1daeSBarry Smith 
8745c6c1daeSBarry Smith */
875*242ebda1SBarry Smith PETSC_EXTERN PetscErrorCode YAML_AMS_Connect(PetscInt argc,char **args,PetscInt *argco,char ***argso)
8765c6c1daeSBarry Smith {
8775c6c1daeSBarry Smith   PetscErrorCode ierr;
8785c6c1daeSBarry Smith   char           **list = 0;
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith   PetscFunctionBegin;
8815c6c1daeSBarry Smith   ierr = AMS_Connect(0,-1,&list);
882a297a907SKarl Rupp   if (ierr) {
8830298fd71SBarry Smith     ierr = PetscInfo1(NULL,"AMS_Connect() error %d\n",ierr);CHKERRQ(ierr);
884a297a907SKarl Rupp   } else if (!list) {
8850298fd71SBarry Smith     ierr = PetscInfo(NULL,"AMS_Connect() list empty, not running AMS server\n");CHKERRQ(ierr);
886a297a907SKarl Rupp   }
8875c6c1daeSBarry Smith   *argco = 1;
8885c6c1daeSBarry Smith   ierr   = PetscMalloc(sizeof(char*),argso);CHKERRQ(ierr);
8895c6c1daeSBarry Smith   if (list) {
8905c6c1daeSBarry Smith     ierr = PetscStrallocpy(list[0],&(*argso)[0]);CHKERRQ(ierr);
8915c6c1daeSBarry Smith   } else {
8925c6c1daeSBarry Smith     ierr = PetscStrallocpy("No AMS publisher running",&(*argso)[0]);CHKERRQ(ierr);
8935c6c1daeSBarry Smith   }
8945c6c1daeSBarry Smith   PetscFunctionReturn(0);
8955c6c1daeSBarry Smith }
8965c6c1daeSBarry Smith 
8975c6c1daeSBarry Smith #undef __FUNCT__
8985c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Comm_attach"
8995c6c1daeSBarry Smith /*
9005c6c1daeSBarry Smith       Attaches to an AMS communicator
9015c6c1daeSBarry Smith 
9025c6c1daeSBarry Smith    Input Parameter:
9035c6c1daeSBarry Smith .     arg1 - string name of the communicator
9045c6c1daeSBarry Smith 
9055c6c1daeSBarry Smith    Output Parameter:
9065c6c1daeSBarry Smith .     oarg1 - the integer name of the communicator
9075c6c1daeSBarry Smith 
9085c6c1daeSBarry Smith */
909*242ebda1SBarry Smith PETSC_EXTERN PetscErrorCode YAML_AMS_Comm_attach(PetscInt argc,char **args,PetscInt *argco,char ***argso)
9105c6c1daeSBarry Smith {
9115c6c1daeSBarry Smith   PetscErrorCode ierr;
9125c6c1daeSBarry Smith   AMS_Comm       comm = -1;
9135c6c1daeSBarry Smith 
9145c6c1daeSBarry Smith   PetscFunctionBegin;
9155c6c1daeSBarry Smith   ierr = AMS_Comm_attach(args[0],&comm);
9160298fd71SBarry Smith   if (ierr) {ierr = PetscInfo1(NULL,"AMS_Comm_attach() error %d\n",ierr);CHKERRQ(ierr);}
9175c6c1daeSBarry Smith   *argco = 1;
9185c6c1daeSBarry Smith   ierr   = PetscMalloc(sizeof(char*),argso);CHKERRQ(ierr);
9195c6c1daeSBarry Smith   ierr   = PetscMalloc(3*sizeof(char*),&argso[0][0]);CHKERRQ(ierr);
9205c6c1daeSBarry Smith   sprintf(argso[0][0],"%d",(int)comm);
9215c6c1daeSBarry Smith   PetscFunctionReturn(0);
9225c6c1daeSBarry Smith }
9235c6c1daeSBarry Smith 
9245c6c1daeSBarry Smith #undef __FUNCT__
9255c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Comm_get_memory_list"
9265c6c1daeSBarry Smith /*
9275c6c1daeSBarry Smith       Gets the list of memories on an AMS Comm
9285c6c1daeSBarry Smith 
9295c6c1daeSBarry Smith    Input Parameter:
9305c6c1daeSBarry Smith .     arg1 - integer name of the communicator
9315c6c1daeSBarry Smith 
9325c6c1daeSBarry Smith    Output Parameter:
9335c6c1daeSBarry Smith .     oarg1 - the list of names
9345c6c1daeSBarry Smith 
9355c6c1daeSBarry Smith */
936*242ebda1SBarry Smith PETSC_EXTERN PetscErrorCode YAML_AMS_Comm_get_memory_list(PetscInt argc,char **args,PetscInt *argco,char ***argso)
9375c6c1daeSBarry Smith {
9385c6c1daeSBarry Smith   PetscErrorCode ierr;
9395c6c1daeSBarry Smith   char           **mem_list;
9405c6c1daeSBarry Smith   AMS_Comm       comm;
9415c6c1daeSBarry Smith   PetscInt       i,iargco = 0;
9425c6c1daeSBarry Smith 
9435c6c1daeSBarry Smith   PetscFunctionBegin;
9445c6c1daeSBarry Smith   sscanf(args[0],"%d",&comm);
9455c6c1daeSBarry Smith   ierr = AMS_Comm_get_memory_list(comm,&mem_list);
9465c6c1daeSBarry Smith   if (ierr) {
9470298fd71SBarry Smith     ierr = PetscInfo1(NULL,"AMS_Comm_get_memory_list() error %d\n",ierr);CHKERRQ(ierr);
9485c6c1daeSBarry Smith   } else {
9495c6c1daeSBarry Smith     while (mem_list[iargco++]) ;
9505c6c1daeSBarry Smith     iargco--;
9515c6c1daeSBarry Smith 
9525c6c1daeSBarry Smith     ierr = PetscMalloc((iargco)*sizeof(char*),argso);CHKERRQ(ierr);
9535c6c1daeSBarry Smith     for (i=0; i<iargco; i++) {
9545c6c1daeSBarry Smith       ierr = PetscStrallocpy(mem_list[i],(*argso)+i);CHKERRQ(ierr);
9555c6c1daeSBarry Smith     }
9565c6c1daeSBarry Smith   }
9575c6c1daeSBarry Smith   *argco = iargco;
9585c6c1daeSBarry Smith   PetscFunctionReturn(0);
9595c6c1daeSBarry Smith }
9605c6c1daeSBarry Smith 
9615c6c1daeSBarry Smith #undef __FUNCT__
9625c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Memory_attach"
9635c6c1daeSBarry Smith /*
9645c6c1daeSBarry Smith       Attaches to an AMS memory in a communicator
9655c6c1daeSBarry Smith 
9665c6c1daeSBarry Smith    Input Parameter:
9675c6c1daeSBarry Smith .     arg1 - communicator
9685c6c1daeSBarry Smith .     arg2 - string name of the memory
9695c6c1daeSBarry Smith 
9705c6c1daeSBarry Smith    Output Parameter:
9715c6c1daeSBarry Smith .     oarg1 - the integer name of the memory
9725c6c1daeSBarry Smith .     oarg2 - the integer step of the memory
9735c6c1daeSBarry Smith 
9745c6c1daeSBarry Smith */
975*242ebda1SBarry Smith PETSC_EXTERN PetscErrorCode YAML_AMS_Memory_attach(PetscInt argc,char **args,PetscInt *argco,char ***argso)
9765c6c1daeSBarry Smith {
9775c6c1daeSBarry Smith   PetscErrorCode ierr;
9785c6c1daeSBarry Smith   AMS_Comm       comm;
9795c6c1daeSBarry Smith   AMS_Memory     mem;
9805c6c1daeSBarry Smith   unsigned int   step;
9815c6c1daeSBarry Smith 
9825c6c1daeSBarry Smith   PetscFunctionBegin;
9835c6c1daeSBarry Smith   sscanf(args[0],"%d",&comm);
9845c6c1daeSBarry Smith   ierr = AMS_Memory_attach(comm,args[1],&mem,&step);
9850298fd71SBarry Smith   if (ierr) {ierr = PetscInfo1(NULL,"AMS_Memory_attach() error %d\n",ierr);CHKERRQ(ierr);}
9865c6c1daeSBarry Smith   *argco = 2;
9875c6c1daeSBarry Smith   ierr   = PetscMalloc(2*sizeof(char*),argso);CHKERRQ(ierr);
9885c6c1daeSBarry Smith   ierr   = PetscMalloc(3*sizeof(char*),&argso[0][0]);CHKERRQ(ierr);
9895c6c1daeSBarry Smith   sprintf(argso[0][0],"%d",(int)mem);
9905c6c1daeSBarry Smith   ierr = PetscMalloc(3*sizeof(char*),&argso[0][1]);CHKERRQ(ierr);
9915c6c1daeSBarry Smith   sprintf(argso[0][1],"%d",(int)step);
9925c6c1daeSBarry Smith   PetscFunctionReturn(0);
9935c6c1daeSBarry Smith }
9945c6c1daeSBarry Smith 
9955c6c1daeSBarry Smith #undef __FUNCT__
9965c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Memory_get_field_list"
9975c6c1daeSBarry Smith /*
9985c6c1daeSBarry Smith       Gets the list of fields on an AMS Memory
9995c6c1daeSBarry Smith 
10005c6c1daeSBarry Smith    Input Parameter:
10015c6c1daeSBarry Smith .     arg1 - integer name of the memory
10025c6c1daeSBarry Smith 
10035c6c1daeSBarry Smith    Output Parameter:
10045c6c1daeSBarry Smith .     oarg1 - the list of names
10055c6c1daeSBarry Smith 
10065c6c1daeSBarry Smith */
1007*242ebda1SBarry Smith PETSC_EXTERN PetscErrorCode YAML_AMS_Memory_get_field_list(PetscInt argc,char **args,PetscInt *argco,char ***argso)
10085c6c1daeSBarry Smith {
10095c6c1daeSBarry Smith   PetscErrorCode ierr;
10105c6c1daeSBarry Smith   char           **field_list;
10115c6c1daeSBarry Smith   AMS_Memory     mem;
10125c6c1daeSBarry Smith   PetscInt       i,iargco = 0;
10135c6c1daeSBarry Smith 
10145c6c1daeSBarry Smith   PetscFunctionBegin;
10155c6c1daeSBarry Smith   sscanf(args[0],"%d",&mem);
10165c6c1daeSBarry Smith   ierr = AMS_Memory_get_field_list(mem,&field_list);
10175c6c1daeSBarry Smith   if (ierr) {
10180298fd71SBarry Smith     ierr = PetscInfo1(NULL,"AMS_Memory_get_field_list() error %d\n",ierr);CHKERRQ(ierr);
10195c6c1daeSBarry Smith   } else {
10205c6c1daeSBarry Smith     while (field_list[iargco++]) ;
10215c6c1daeSBarry Smith     iargco--;
10225c6c1daeSBarry Smith 
10235c6c1daeSBarry Smith     ierr = PetscMalloc((iargco)*sizeof(char*),argso);CHKERRQ(ierr);
10245c6c1daeSBarry Smith     for (i=0; i<iargco; i++) {
10255c6c1daeSBarry Smith       ierr = PetscStrallocpy(field_list[i],(*argso)+i);CHKERRQ(ierr);
10265c6c1daeSBarry Smith     }
10275c6c1daeSBarry Smith   }
10285c6c1daeSBarry Smith   *argco = iargco;
10295c6c1daeSBarry Smith   PetscFunctionReturn(0);
10305c6c1daeSBarry Smith }
10315c6c1daeSBarry Smith 
10325c6c1daeSBarry Smith const char *AMS_Data_types[] = {"AMS_DATA_UNDEF","AMS_BOOLEAN","AMS_INT","AMS_FLOAT","AMS_DOUBLE","AMS_STRING","AMS_Data_type","AMS_",0};
10335c6c1daeSBarry Smith const char *AMS_Memory_types[] = {"AMS_MEMORY_UNDEF","AMS_READ","AMS_WRITE","AMS_Memory_type","AMS_",0};
10345c6c1daeSBarry Smith const char *AMS_Shared_types[] = {"AMS_SHARED_UNDEF","AMS_COMMON","AMS_REDUCED","AMS_DISTRIBUTED","AMS_Shared_type","AMS_",0};
10355c6c1daeSBarry 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};
10365c6c1daeSBarry Smith 
10375c6c1daeSBarry Smith #undef __FUNCT__
10385c6c1daeSBarry Smith #define __FUNCT__ "YAML_AMS_Memory_get_field_info"
10395c6c1daeSBarry Smith /*
10405c6c1daeSBarry Smith       Gets information about a field
10415c6c1daeSBarry Smith 
10425c6c1daeSBarry Smith    Input Parameter:
10435c6c1daeSBarry Smith .     arg1 - memory
10445c6c1daeSBarry Smith .     arg2 - string name of the field
10455c6c1daeSBarry Smith 
10465c6c1daeSBarry Smith    Output Parameter:
10475c6c1daeSBarry Smith 
10485c6c1daeSBarry Smith */
1049*242ebda1SBarry Smith PETSC_EXTERN PetscErrorCode YAML_AMS_Memory_get_field_info(PetscInt argc,char **args,PetscInt *argco,char ***argso)
10505c6c1daeSBarry Smith {
10515c6c1daeSBarry Smith   PetscErrorCode     ierr;
10525c6c1daeSBarry Smith   AMS_Memory         mem;
10535c6c1daeSBarry Smith   void               *addr;
10545c6c1daeSBarry Smith   int                len;
10555c6c1daeSBarry Smith   AMS_Data_type      dtype;
10565c6c1daeSBarry Smith   AMS_Memory_type    mtype;
10575c6c1daeSBarry Smith   AMS_Shared_type    stype;
10585c6c1daeSBarry Smith   AMS_Reduction_type rtype;
10595c6c1daeSBarry Smith   PetscInt           i;
10605c6c1daeSBarry Smith 
10615c6c1daeSBarry Smith   PetscFunctionBegin;
10625c6c1daeSBarry Smith   sscanf(args[0],"%d",&mem);
10635c6c1daeSBarry Smith   ierr = AMS_Memory_get_field_info(mem,args[1],&addr,&len,&dtype,&mtype,&stype,&rtype);
10640298fd71SBarry Smith   if (ierr) {ierr = PetscInfo1(NULL,"AMS_Memory_get_field_info() error %d\n",ierr);CHKERRQ(ierr);}
10655c6c1daeSBarry Smith   *argco = 4 + len;
10665c6c1daeSBarry Smith   ierr   = PetscMalloc((*argco)*sizeof(char*),argso);CHKERRQ(ierr);
10675c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Data_types[dtype],&argso[0][0]);CHKERRQ(ierr);
10685c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Memory_types[mtype],&argso[0][1]);CHKERRQ(ierr);
10695c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Shared_types[stype],&argso[0][2]);CHKERRQ(ierr);
10705c6c1daeSBarry Smith   ierr   = PetscStrallocpy(AMS_Reduction_types[rtype],&argso[0][3]);CHKERRQ(ierr);
10715c6c1daeSBarry Smith   for (i=0; i<len; i++) {
10725c6c1daeSBarry Smith     if (dtype == AMS_STRING) {
10735c6c1daeSBarry Smith       ierr = PetscStrallocpy(*(const char**)addr,&argso[0][4+i]);CHKERRQ(ierr);
10745c6c1daeSBarry Smith     } else if (dtype == AMS_DOUBLE) {
10755c6c1daeSBarry Smith       ierr = PetscMalloc(20*sizeof(char),&argso[0][4+i]);CHKERRQ(ierr);
10765c6c1daeSBarry Smith       sprintf(argso[0][4+i],"%18.16e",*(double*)addr);
10775c6c1daeSBarry Smith     } else if (dtype == AMS_INT) {
10785c6c1daeSBarry Smith       ierr = PetscMalloc(10*sizeof(char),&argso[0][4+i]);CHKERRQ(ierr);
10795c6c1daeSBarry Smith       sprintf(argso[0][4+i],"%d",*(int*)addr);
10805c6c1daeSBarry Smith     } else if (dtype == AMS_BOOLEAN) {
10815c6c1daeSBarry Smith       if (*(int*)addr) {
10825c6c1daeSBarry Smith         ierr = PetscStrallocpy("true",&argso[0][4+i]);CHKERRQ(ierr);
10835c6c1daeSBarry Smith       } else {
10845c6c1daeSBarry Smith         ierr = PetscStrallocpy("false",&argso[0][4+i]);CHKERRQ(ierr);
10855c6c1daeSBarry Smith       }
10865c6c1daeSBarry Smith     } else {
10875c6c1daeSBarry Smith       ierr = PetscStrallocpy("Not yet done",&argso[0][4+i]);CHKERRQ(ierr);
10885c6c1daeSBarry Smith     }
10895c6c1daeSBarry Smith   }
10905c6c1daeSBarry Smith   PetscFunctionReturn(0);
10915c6c1daeSBarry Smith }
10925c6c1daeSBarry Smith 
10935c6c1daeSBarry Smith #include "yaml.h"
10945c6c1daeSBarry Smith #undef __FUNCT__
10955c6c1daeSBarry Smith #define __FUNCT__ "PetscProcessYAMLRPC"
1096*242ebda1SBarry Smith /*
1097*242ebda1SBarry Smith      1) Parses a YAML/JSON-RPC function call generating a function name for an AMS wrapper function and the arguments to the function
1098*242ebda1SBarry Smith      2) loads the function with dlsym(),
1099*242ebda1SBarry Smith      3) calls the wrapper function with the arguments
1100*242ebda1SBarry Smith      4) converts the result arguments back to YAML/JSON.
1101*242ebda1SBarry Smith */
1102*242ebda1SBarry Smith static PetscErrorCode PetscProcessYAMLRPC(const char *request,char **result)
11035c6c1daeSBarry Smith {
11045c6c1daeSBarry Smith   yaml_parser_t  parser;
11055c6c1daeSBarry Smith   yaml_event_t   event;
11065c6c1daeSBarry Smith   int            done  = 0;
11075c6c1daeSBarry Smith   int            count = 0;
11085c6c1daeSBarry Smith   size_t         len;
11095c6c1daeSBarry Smith   PetscErrorCode ierr;
11105c6c1daeSBarry Smith   PetscBool      method,params,id;
11115c6c1daeSBarry Smith   char           *methodname,*idname,**args,**argso = 0;
11125c6c1daeSBarry Smith   PetscInt       argc = 0,argco,i;
11135c6c1daeSBarry Smith   PetscErrorCode (*fun)(PetscInt,char**,PetscInt*,char***);
11145c6c1daeSBarry Smith 
11155c6c1daeSBarry Smith   PetscFunctionBegin;
11165c6c1daeSBarry Smith   ierr = PetscMalloc(sizeof(char*),&args);CHKERRQ(ierr);
11175c6c1daeSBarry Smith   yaml_parser_initialize(&parser);
11185c6c1daeSBarry Smith   PetscStrlen(request,&len);
11195c6c1daeSBarry Smith   yaml_parser_set_input_string(&parser, (unsigned char*)request, len);
11205c6c1daeSBarry Smith 
11215c6c1daeSBarry Smith   /* this is totally bogus; it only handles the simple JSON-RPC messages */
11225c6c1daeSBarry Smith   while (!done) {
11235c6c1daeSBarry Smith     if (!yaml_parser_parse(&parser, &event)) {
11240298fd71SBarry Smith       ierr = PetscInfo(NULL,"Found error in yaml/json\n");CHKERRQ(ierr);
11255c6c1daeSBarry Smith       break;
11265c6c1daeSBarry Smith     }
11275c6c1daeSBarry Smith     done = (event.type == YAML_STREAM_END_EVENT);
11285c6c1daeSBarry Smith     switch (event.type) {
11295c6c1daeSBarry Smith     case YAML_STREAM_START_EVENT:
11300298fd71SBarry Smith       ierr = PetscInfo(NULL,"Stream start\n");CHKERRQ(ierr);
11315c6c1daeSBarry Smith       break;
11325c6c1daeSBarry Smith     case YAML_STREAM_END_EVENT:
11330298fd71SBarry Smith       ierr = PetscInfo(NULL,"Stream end\n");CHKERRQ(ierr);
11345c6c1daeSBarry Smith       break;
11355c6c1daeSBarry Smith     case YAML_DOCUMENT_START_EVENT:
11360298fd71SBarry Smith       ierr = PetscInfo(NULL,"Document start\n");CHKERRQ(ierr);
11375c6c1daeSBarry Smith       break;
11385c6c1daeSBarry Smith     case YAML_DOCUMENT_END_EVENT:
11390298fd71SBarry Smith       ierr = PetscInfo(NULL,"Document end\n");CHKERRQ(ierr);
11405c6c1daeSBarry Smith       break;
11415c6c1daeSBarry Smith     case YAML_MAPPING_START_EVENT:
11420298fd71SBarry Smith       ierr = PetscInfo(NULL,"Mapping start event\n");CHKERRQ(ierr);
11435c6c1daeSBarry Smith       break;
11445c6c1daeSBarry Smith     case YAML_MAPPING_END_EVENT:
11450298fd71SBarry Smith       ierr = PetscInfo(NULL,"Mapping end event \n");CHKERRQ(ierr);
11465c6c1daeSBarry Smith       break;
11475c6c1daeSBarry Smith     case YAML_ALIAS_EVENT:
11480298fd71SBarry Smith       ierr = PetscInfo1(NULL,"Alias event %s\n",event.data.alias.anchor);CHKERRQ(ierr);
11495c6c1daeSBarry Smith       break;
11505c6c1daeSBarry Smith     case YAML_SCALAR_EVENT:
11510298fd71SBarry Smith       ierr = PetscInfo1(NULL,"Scalar event %s\n",event.data.scalar.value);CHKERRQ(ierr);
11525c6c1daeSBarry Smith       ierr = PetscStrcmp((char*)event.data.scalar.value,"method",&method);CHKERRQ(ierr);
11535c6c1daeSBarry Smith       ierr = PetscStrcmp((char*)event.data.scalar.value,"params",&params);CHKERRQ(ierr);
11545c6c1daeSBarry Smith       ierr = PetscStrcmp((char*)event.data.scalar.value,"id",&id);CHKERRQ(ierr);
11555c6c1daeSBarry Smith       if (method) {
11565c6c1daeSBarry Smith         yaml_event_delete(&event);
11575c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
11580298fd71SBarry Smith         ierr = PetscInfo1(NULL,"Method %s\n",event.data.scalar.value);CHKERRQ(ierr);
11595c6c1daeSBarry Smith         ierr = PetscStrallocpy((char*)event.data.scalar.value,&methodname);CHKERRQ(ierr);
11605c6c1daeSBarry Smith       } else if (id) {
11615c6c1daeSBarry Smith         yaml_event_delete(&event);
11625c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
11630298fd71SBarry Smith         ierr = PetscInfo1(NULL,"Id %s\n",event.data.scalar.value);CHKERRQ(ierr);
11645c6c1daeSBarry Smith         ierr = PetscStrallocpy((char*)event.data.scalar.value,&idname);CHKERRQ(ierr);
11655c6c1daeSBarry Smith       } else if (params) {
11665c6c1daeSBarry Smith         yaml_event_delete(&event);
11675c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
11685c6c1daeSBarry Smith         yaml_event_delete(&event);
11695c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
11705c6c1daeSBarry Smith         while (event.type != YAML_SEQUENCE_END_EVENT) {
11710298fd71SBarry Smith           ierr = PetscInfo1(NULL,"  Parameter %s\n",event.data.scalar.value);CHKERRQ(ierr);
11725c6c1daeSBarry Smith           ierr = PetscStrallocpy((char*)event.data.scalar.value,&args[argc++]);CHKERRQ(ierr);
11735c6c1daeSBarry Smith           yaml_event_delete(&event);
11745c6c1daeSBarry Smith           ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
11755c6c1daeSBarry Smith         }
11765c6c1daeSBarry Smith       } else { /* ignore all the other variables in the mapping */
11775c6c1daeSBarry Smith         yaml_event_delete(&event);
11785c6c1daeSBarry Smith         ierr = yaml_parser_parse(&parser, &event);CHKERRQ(!ierr);
11795c6c1daeSBarry Smith       }
11805c6c1daeSBarry Smith       break;
11815c6c1daeSBarry Smith     case YAML_SEQUENCE_START_EVENT:
11820298fd71SBarry Smith       ierr = PetscInfo(NULL,"Sequence start event \n");CHKERRQ(ierr);
11835c6c1daeSBarry Smith       break;
11845c6c1daeSBarry Smith     case YAML_SEQUENCE_END_EVENT:
11850298fd71SBarry Smith       ierr = PetscInfo(NULL,"Sequence end event \n");CHKERRQ(ierr);
11865c6c1daeSBarry Smith       break;
11875c6c1daeSBarry Smith     default:
11885c6c1daeSBarry Smith       /* It couldn't really happen. */
11895c6c1daeSBarry Smith       break;
11905c6c1daeSBarry Smith     }
11915c6c1daeSBarry Smith 
11925c6c1daeSBarry Smith     yaml_event_delete(&event);
11935c6c1daeSBarry Smith     count++;
11945c6c1daeSBarry Smith   }
11955c6c1daeSBarry Smith   yaml_parser_delete(&parser);
11965c6c1daeSBarry Smith 
11970298fd71SBarry Smith   ierr = PetscDLLibrarySym(PETSC_COMM_SELF,NULL,NULL,methodname,(void**)&fun);CHKERRQ(ierr);
11985c6c1daeSBarry Smith   if (fun) {
11990298fd71SBarry Smith     ierr = PetscInfo1(NULL,"Located function %s and running it\n",methodname);CHKERRQ(ierr);
12005c6c1daeSBarry Smith     ierr = (*fun)(argc,args,&argco,&argso);CHKERRQ(ierr);
12015c6c1daeSBarry Smith   } else {
12020298fd71SBarry Smith     ierr = PetscInfo1(NULL,"Did not locate function %s skipping it\n",methodname);CHKERRQ(ierr);
12035c6c1daeSBarry Smith   }
12045c6c1daeSBarry Smith 
12055c6c1daeSBarry Smith   for (i=0; i<argc; i++) {
12065c6c1daeSBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
12075c6c1daeSBarry Smith   }
12085c6c1daeSBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
12095c6c1daeSBarry Smith   ierr = PetscFree(methodname);CHKERRQ(ierr);
12105c6c1daeSBarry Smith 
1211*242ebda1SBarry Smith   /* convert the result back to YAML/JSON; should use YAML/JSON encoder, does not handle zero return arguments */
12125c6c1daeSBarry Smith   ierr = PetscMalloc(1024,result);CHKERRQ(ierr);
12135c6c1daeSBarry Smith   ierr = PetscStrcpy(*result,"{\"error\": null, \"id\": \"");CHKERRQ(ierr);
12145c6c1daeSBarry Smith   ierr = PetscStrcat(*result,idname);CHKERRQ(ierr);
12155c6c1daeSBarry Smith   ierr = PetscStrcat(*result,"\", \"result\" : ");CHKERRQ(ierr);
12165c6c1daeSBarry Smith   if (argco > 1) {ierr = PetscStrcat(*result,"[");CHKERRQ(ierr);}
12175c6c1daeSBarry Smith   for (i=0; i<argco; i++) {
12185c6c1daeSBarry Smith     ierr = PetscStrcat(*result,"\"");CHKERRQ(ierr);
12195c6c1daeSBarry Smith     ierr = PetscStrcat(*result,argso[i]);CHKERRQ(ierr);
12205c6c1daeSBarry Smith     ierr = PetscStrcat(*result,"\"");CHKERRQ(ierr);
12215c6c1daeSBarry Smith     if (i < argco-1) {ierr = PetscStrcat(*result,",");CHKERRQ(ierr);}
12225c6c1daeSBarry Smith   }
12235c6c1daeSBarry Smith   if (argco > 1) {ierr = PetscStrcat(*result,"]");CHKERRQ(ierr);}
12245c6c1daeSBarry Smith   ierr = PetscStrcat(*result,"}");CHKERRQ(ierr);
1225*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"YAML/JSON result of function %s\n",*result);CHKERRQ(ierr);
12265c6c1daeSBarry Smith 
12275c6c1daeSBarry Smith   /* free work space */
12285c6c1daeSBarry Smith   ierr = PetscFree(idname);CHKERRQ(ierr);
12295c6c1daeSBarry Smith   for (i=0; i<argco; i++) {
12305c6c1daeSBarry Smith     ierr = PetscFree(argso[i]);CHKERRQ(ierr);
12315c6c1daeSBarry Smith   }
12325c6c1daeSBarry Smith   ierr = PetscFree(argso);CHKERRQ(ierr);
12335c6c1daeSBarry Smith   PetscFunctionReturn(0);
12345c6c1daeSBarry Smith }
1235*242ebda1SBarry Smith 
1236*242ebda1SBarry Smith #undef __FUNCT__
1237*242ebda1SBarry Smith #define __FUNCT__ "PetscWebServeRequestPostAMSJSONRPC"
1238*242ebda1SBarry Smith /*@C
1239*242ebda1SBarry Smith       PetscWebServeRequestPostAMSJSONRPC - serves a single web POST request based on JSON-RPC
1240*242ebda1SBarry Smith 
1241*242ebda1SBarry Smith        This function allows a Javascript program (running in the browser) to make an AMS function
1242*242ebda1SBarry Smith        call via JSON-RPC
1243*242ebda1SBarry Smith 
1244*242ebda1SBarry Smith        The currently available Javascript programs are in ${PETSC_DIR}/include/web
1245*242ebda1SBarry Smith 
1246*242ebda1SBarry Smith     Not collective
1247*242ebda1SBarry Smith 
1248*242ebda1SBarry Smith   Input Parameters:
1249*242ebda1SBarry Smith .   fd - the network file to read and write from
1250*242ebda1SBarry Smith -   path - the command from the server
1251*242ebda1SBarry Smith 
1252*242ebda1SBarry Smith     Level: developer
1253*242ebda1SBarry Smith 
1254*242ebda1SBarry Smith .seealso: PetscWebServe()
1255*242ebda1SBarry Smith @*/
1256*242ebda1SBarry Smith static PetscErrorCode  PetscWebServeRequestPostAMSJSONRPC(FILE *fd,const char path[])
1257*242ebda1SBarry Smith {
1258*242ebda1SBarry Smith   PetscErrorCode ierr;
1259*242ebda1SBarry Smith   char           buf[4096];
1260*242ebda1SBarry Smith   char           *result;
1261*242ebda1SBarry Smith   PetscInt       cnt = 8;
1262*242ebda1SBarry Smith   int            len;
1263*242ebda1SBarry Smith   size_t         elen;
1264*242ebda1SBarry Smith   char           *fnd;
1265*242ebda1SBarry Smith 
1266*242ebda1SBarry Smith   PetscFunctionBegin;
1267*242ebda1SBarry Smith   while (cnt--) {
1268*242ebda1SBarry Smith 
1269*242ebda1SBarry Smith     if (!fgets(buf, sizeof(buf), fd)) {
1270*242ebda1SBarry Smith       ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
1271*242ebda1SBarry Smith       PetscFunctionReturn(0);
1272*242ebda1SBarry Smith     }
1273*242ebda1SBarry Smith     ierr = PetscInfo1(NULL,"POSTED data %s",buf);CHKERRQ(ierr);
1274*242ebda1SBarry Smith     ierr = PetscStrstr(buf,"Content-Type:",&fnd);CHKERRQ(ierr);
1275*242ebda1SBarry Smith     if (fnd) {
1276*242ebda1SBarry Smith       ierr = PetscStrstr(buf,"application/json-rpc",&fnd);CHKERRQ(ierr);
1277*242ebda1SBarry Smith       if (!fnd) {
1278*242ebda1SBarry Smith         ierr = PetscInfo(NULL,"POST content is not json-rpc, skipping post\n");CHKERRQ(ierr);
1279*242ebda1SBarry Smith         PetscFunctionReturn(0);
1280*242ebda1SBarry Smith       }
1281*242ebda1SBarry Smith     }
1282*242ebda1SBarry Smith   }
1283*242ebda1SBarry Smith   if (!fgets(buf, sizeof(buf), fd)) {
1284*242ebda1SBarry Smith     ierr = PetscInfo(NULL,"Cannot read POST length data, giving up\n");CHKERRQ(ierr);
1285*242ebda1SBarry Smith     PetscFunctionReturn(0);
1286*242ebda1SBarry Smith   }
1287*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"POSTED length data %s",buf);CHKERRQ(ierr);
1288*242ebda1SBarry Smith   sscanf(buf,"Content-Length: %d\n",&len);
1289*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"Length of POSTED data %d\n",len);CHKERRQ(ierr);
1290*242ebda1SBarry Smith   if (!fgets(buf, sizeof(buf), fd)) {
1291*242ebda1SBarry Smith     ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
1292*242ebda1SBarry Smith     PetscFunctionReturn(0);
1293*242ebda1SBarry Smith   }
1294*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"POSTED data %s",buf);CHKERRQ(ierr);
1295*242ebda1SBarry Smith   if (!fgets(buf, sizeof(buf), fd)) {
1296*242ebda1SBarry Smith     ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
1297*242ebda1SBarry Smith     PetscFunctionReturn(0);
1298*242ebda1SBarry Smith   }
1299*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"POSTED data %s",buf);CHKERRQ(ierr);
1300*242ebda1SBarry Smith   if (!fgets(buf, len+1, fd)) { /* why is this len + 1? */
1301*242ebda1SBarry Smith     ierr = PetscInfo(NULL,"Cannot read POST data, giving up\n");CHKERRQ(ierr);
1302*242ebda1SBarry Smith     PetscFunctionReturn(0);
1303*242ebda1SBarry Smith   }
1304*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"POSTED data %s\n",buf);CHKERRQ(ierr);
1305*242ebda1SBarry Smith   fseek(fd, 0, SEEK_CUR); /* Force change of stream direction */
1306*242ebda1SBarry Smith   ierr = PetscProcessYAMLRPC(buf,&result);CHKERRQ(ierr);
1307*242ebda1SBarry Smith   ierr = PetscStrlen(result,&elen);CHKERRQ(ierr);
1308*242ebda1SBarry Smith   ierr = PetscWebSendHeader(fd, 200, "OK", NULL, "application/json-rpc",(int)elen);CHKERRQ(ierr);
1309*242ebda1SBarry Smith   fprintf(fd, "%s",result);
1310*242ebda1SBarry Smith   PetscFunctionReturn(0);
1311*242ebda1SBarry Smith }
13125c6c1daeSBarry Smith #endif
13135c6c1daeSBarry Smith 
13145c6c1daeSBarry Smith #undef __FUNCT__
13155c6c1daeSBarry Smith #define __FUNCT__ "PetscWebServeRequest"
13165c6c1daeSBarry Smith /*@C
13175c6c1daeSBarry Smith       PetscWebServeRequest - serves a single web request
13185c6c1daeSBarry Smith 
13195c6c1daeSBarry Smith     Not collective
13205c6c1daeSBarry Smith 
13215c6c1daeSBarry Smith   Input Parameters:
13225c6c1daeSBarry Smith .   port - the port
13235c6c1daeSBarry Smith 
13245c6c1daeSBarry Smith     Level: developer
13255c6c1daeSBarry Smith 
13265c6c1daeSBarry Smith .seealso: PetscWebServe()
13275c6c1daeSBarry Smith @*/
1328*242ebda1SBarry Smith static PetscErrorCode  PetscWebServeRequest(int port)
13295c6c1daeSBarry Smith {
13305c6c1daeSBarry Smith   PetscErrorCode ierr;
1331*242ebda1SBarry Smith   FILE           *fd;
1332*242ebda1SBarry Smith   char           buf[4096];
1333*242ebda1SBarry Smith   char           *method, *path, *protocol;
13345c6c1daeSBarry Smith   PetscBool      flg;
13355c6c1daeSBarry Smith   PetscToken     tok;
13365c6c1daeSBarry Smith 
13375c6c1daeSBarry Smith   PetscFunctionBegin;
13385c6c1daeSBarry Smith   fd = fdopen(port, "r+");
13395c6c1daeSBarry Smith 
13400298fd71SBarry Smith   ierr = PetscInfo(NULL,"Processing web request\n");CHKERRQ(ierr);
13415c6c1daeSBarry Smith   if (!fgets(buf, sizeof(buf), fd)) {
13420298fd71SBarry Smith     ierr = PetscInfo(NULL,"Cannot read web request, giving up\n");CHKERRQ(ierr);
13435c6c1daeSBarry Smith     goto theend;
13445c6c1daeSBarry Smith   }
13450298fd71SBarry Smith   ierr = PetscInfo1(NULL,"Processing web request %s",buf);CHKERRQ(ierr);
13465c6c1daeSBarry Smith 
13475c6c1daeSBarry Smith   ierr = PetscTokenCreate(buf,' ',&tok);CHKERRQ(ierr);
13485c6c1daeSBarry Smith   ierr = PetscTokenFind(tok,&method);CHKERRQ(ierr);
13495c6c1daeSBarry Smith   ierr = PetscTokenFind(tok,&path);CHKERRQ(ierr);
13505c6c1daeSBarry Smith   ierr = PetscTokenFind(tok,&protocol);CHKERRQ(ierr);
1351*242ebda1SBarry Smith   ierr = PetscInfo3(NULL,"Browser method %s path %s protocol %s\n",method,path,protocol);
13525c6c1daeSBarry Smith 
13535c6c1daeSBarry Smith   if (!method || !path || !protocol) {
13540298fd71SBarry Smith     ierr = PetscInfo(NULL,"Web request not well formatted, giving up\n");CHKERRQ(ierr);
13555c6c1daeSBarry Smith     goto theend;
13565c6c1daeSBarry Smith   }
13575c6c1daeSBarry Smith 
13585c6c1daeSBarry Smith   ierr = PetscStrcmp(method,"GET",&flg);
1359*242ebda1SBarry Smith   if (flg) {
1360*242ebda1SBarry Smith       ierr = PetscWebServeRequestGet(fd,path);CHKERRQ(ierr);
1361*242ebda1SBarry Smith   } else {
13625c6c1daeSBarry Smith #if defined(PETSC_HAVE_YAML)
13635c6c1daeSBarry Smith     ierr = PetscStrcmp(method,"POST",&flg);
13645c6c1daeSBarry Smith     if (flg) {
1365*242ebda1SBarry Smith       ierr = PetscWebServeRequestPostAMSJSONRPC(fd,path);CHKERRQ(ierr);
13665c6c1daeSBarry Smith     } else {
1367*242ebda1SBarry Smith #else
1368*242ebda1SBarry Smith     {
13695c6c1daeSBarry Smith #endif
13705c6c1daeSBarry Smith       ierr = PetscWebSendError(fd, 501, "Not supported", NULL, "Method is not supported.");CHKERRQ(ierr);
13710298fd71SBarry Smith       ierr = PetscInfo(NULL,"Web request not a GET or POST, giving up\n");CHKERRQ(ierr);
13725c6c1daeSBarry Smith     }
13735c6c1daeSBarry Smith   }
13745c6c1daeSBarry Smith theend:
13755c6c1daeSBarry Smith   ierr = PetscTokenDestroy(&tok);CHKERRQ(ierr);
13765c6c1daeSBarry Smith   fclose(fd);
1377*242ebda1SBarry Smith   ierr = PetscInfo1(NULL,"Finished processing request %s\n",method);CHKERRQ(ierr);
13785c6c1daeSBarry Smith   PetscFunctionReturn(0);
13795c6c1daeSBarry Smith }
13805c6c1daeSBarry Smith 
13815c6c1daeSBarry Smith #undef __FUNCT__
13825c6c1daeSBarry Smith #define __FUNCT__ "PetscWebServeWait"
13835c6c1daeSBarry Smith /*@C
13845c6c1daeSBarry Smith       PetscWebServeWait - waits for requests on a thread
13855c6c1daeSBarry Smith 
13865c6c1daeSBarry Smith     Not collective
13875c6c1daeSBarry Smith 
13885c6c1daeSBarry Smith   Input Parameter:
13895c6c1daeSBarry Smith .   port - port to listen on
13905c6c1daeSBarry Smith 
13915c6c1daeSBarry Smith     Level: developer
13925c6c1daeSBarry Smith 
13935c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen(), PetscWebServe()
13945c6c1daeSBarry Smith @*/
13955c6c1daeSBarry Smith void *PetscWebServeWait(int *port)
13965c6c1daeSBarry Smith {
13975c6c1daeSBarry Smith   PetscErrorCode ierr;
13985c6c1daeSBarry Smith   int            iport,listenport,tport = *port;
13995c6c1daeSBarry Smith 
14000298fd71SBarry Smith   ierr = PetscInfo1(NULL,"Starting webserver at port %d\n",tport);if (ierr) return 0;
14015c6c1daeSBarry Smith   ierr = PetscFree(port);if (ierr) return 0;
14025c6c1daeSBarry Smith   ierr = PetscSocketEstablish(tport,&listenport);if (ierr) return 0;
14035c6c1daeSBarry Smith   while (1) {
14045c6c1daeSBarry Smith     ierr = PetscSocketListen(listenport,&iport);if (ierr) return 0;
14055c6c1daeSBarry Smith     ierr = PetscWebServeRequest(iport);if (ierr) return 0;
14065c6c1daeSBarry Smith     close(iport);
14075c6c1daeSBarry Smith   }
14085c6c1daeSBarry Smith   close(listenport);
14095c6c1daeSBarry Smith   return 0;
14105c6c1daeSBarry Smith }
14115c6c1daeSBarry Smith 
14125c6c1daeSBarry Smith #undef __FUNCT__
14135c6c1daeSBarry Smith #define __FUNCT__ "PetscWebServe"
1414*242ebda1SBarry Smith /*@
14155c6c1daeSBarry Smith       PetscWebServe - start up the PETSc web server and respond to requests
14165c6c1daeSBarry Smith 
14175c6c1daeSBarry Smith     Not collective - only does something on process zero of the communicator
14185c6c1daeSBarry Smith 
14195c6c1daeSBarry Smith   Input Parameters:
14205c6c1daeSBarry Smith +   comm - the MPI communicator
14215c6c1daeSBarry Smith -   port - port to listen on
14225c6c1daeSBarry Smith 
14235c6c1daeSBarry Smith   Options Database Key:
14245c6c1daeSBarry Smith +  -server <port> - start PETSc webserver (default port is 8080)
14255c6c1daeSBarry Smith -  -ams_publish_objects
14265c6c1daeSBarry Smith 
14275c6c1daeSBarry Smith 
14285c6c1daeSBarry Smith    Notes: Point your browser to http://hostname:8080   to access the PETSc web server, where hostname is the name of your machine.
14295c6c1daeSBarry Smith       If you are running PETSc on your local machine you can use http://localhost:8080
14305c6c1daeSBarry Smith 
14315c6c1daeSBarry Smith       If the PETSc program completes before you connect with the browser you will not be able to connect to the PETSc webserver.
14325c6c1daeSBarry Smith 
14335c6c1daeSBarry Smith       Read the top of $PETSC_DIR/include/web/AMSJavascript.py before running.
14345c6c1daeSBarry Smith 
1435*242ebda1SBarry Smith     Level: intermediate
14365c6c1daeSBarry Smith 
14375c6c1daeSBarry Smith .seealso: PetscViewerSocketOpen()
14385c6c1daeSBarry Smith @*/
14395c6c1daeSBarry Smith PetscErrorCode  PetscWebServe(MPI_Comm comm,int port)
14405c6c1daeSBarry Smith {
14415c6c1daeSBarry Smith   PetscErrorCode ierr;
14425c6c1daeSBarry Smith   PetscMPIInt    rank;
14435c6c1daeSBarry Smith   pthread_t      thread;
14445c6c1daeSBarry Smith   int            *trueport;
14455c6c1daeSBarry Smith 
14465c6c1daeSBarry Smith   PetscFunctionBegin;
14475c6c1daeSBarry 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);
14485c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
14495c6c1daeSBarry Smith   if (rank) PetscFunctionReturn(0);
14505c6c1daeSBarry Smith 
14515c6c1daeSBarry Smith   if (port == PETSC_DECIDE || port == PETSC_DEFAULT) port = 8080;
14525c6c1daeSBarry Smith   ierr = PetscMalloc(1*sizeof(int),&trueport);CHKERRQ(ierr); /* malloc this so it still exists in thread */
14535c6c1daeSBarry Smith   *trueport = port;
14545c6c1daeSBarry Smith   ierr = pthread_create(&thread, NULL, (void *(*)(void*))PetscWebServeWait, trueport);CHKERRQ(ierr);
14555c6c1daeSBarry Smith   PetscFunctionReturn(0);
14565c6c1daeSBarry Smith }
14575c6c1daeSBarry Smith #endif
14585c6c1daeSBarry Smith 
14595c6c1daeSBarry Smith 
14605c6c1daeSBarry Smith 
14615c6c1daeSBarry Smith 
14625c6c1daeSBarry Smith 
14635c6c1daeSBarry Smith 
14645c6c1daeSBarry Smith 
14655c6c1daeSBarry Smith 
14665c6c1daeSBarry Smith 
14675c6c1daeSBarry Smith 
14685c6c1daeSBarry Smith 
14695c6c1daeSBarry Smith 
14705c6c1daeSBarry Smith 
14715c6c1daeSBarry Smith 
1472