Actual source code: send.c
petsc-3.10.5 2019-03-28
2: #include <petscsys.h>
4: #if defined(PETSC_NEEDS_UTYPE_TYPEDEFS)
5: /* Some systems have inconsistent include files that use but do not
6: ensure that the following definitions are made */
7: typedef unsigned char u_char;
8: typedef unsigned short u_short;
9: typedef unsigned short ushort;
10: typedef unsigned int u_int;
11: typedef unsigned long u_long;
12: #endif
14: #include <errno.h>
15: #include <ctype.h>
16: #if defined(PETSC_HAVE_MACHINE_ENDIAN_H)
17: #include <machine/endian.h>
18: #endif
19: #if defined(PETSC_HAVE_UNISTD_H)
20: #include <unistd.h>
21: #endif
22: #if defined(PETSC_HAVE_SYS_SOCKET_H)
23: #include <sys/socket.h>
24: #endif
25: #if defined(PETSC_HAVE_SYS_WAIT_H)
26: #include <sys/wait.h>
27: #endif
28: #if defined(PETSC_HAVE_NETINET_IN_H)
29: #include <netinet/in.h>
30: #endif
31: #if defined(PETSC_HAVE_NETDB_H)
32: #include <netdb.h>
33: #endif
34: #if defined(PETSC_HAVE_FCNTL_H)
35: #include <fcntl.h>
36: #endif
37: #if defined(PETSC_HAVE_IO_H)
38: #include <io.h>
39: #endif
40: #if defined(PETSC_HAVE_WINSOCK2_H)
41: #include <Winsock2.h>
42: #endif
43: #include <sys/stat.h>
44: #include <../src/sys/classes/viewer/impls/socket/socket.h>
46: #if defined(PETSC_NEED_CLOSE_PROTO)
47: PETSC_EXTERN int close(int);
48: #endif
49: #if defined(PETSC_NEED_SOCKET_PROTO)
50: PETSC_EXTERN int socket(int,int,int);
51: #endif
52: #if defined(PETSC_NEED_SLEEP_PROTO)
53: PETSC_EXTERN int sleep(unsigned);
54: #endif
55: #if defined(PETSC_NEED_CONNECT_PROTO)
56: PETSC_EXTERN int connect(int,struct sockaddr*,int);
57: #endif
59: /*--------------------------------------------------------------*/
60: static PetscErrorCode PetscViewerDestroy_Socket(PetscViewer viewer)
61: {
62: PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)viewer->data;
63: PetscErrorCode ierr;
66: if (vmatlab->port) {
67: #if defined(PETSC_HAVE_CLOSESOCKET)
68: closesocket(vmatlab->port);
69: #else
70: close(vmatlab->port);
71: #endif
72: if (ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"System error closing socket");
73: }
74: PetscFree(vmatlab);
75: return(0);
76: }
78: /*--------------------------------------------------------------*/
79: /*@C
80: PetscSocketOpen - handles connected to an open port where someone is waiting.
82: Input Parameters:
83: + url - for example www.mcs.anl.gov
84: - portnum - for example 80
86: Output Paramater:
87: . t - the socket number
89: Notes:
90: Use close() to close the socket connection
92: Use read() or PetscHTTPRequest() to read from the socket
94: Level: advanced
96: .seealso: PetscSocketListen(), PetscSocketEstablish(), PetscHTTPRequest(), PetscHTTPSConnect()
97: @*/
98: PetscErrorCode PetscOpenSocket(const char hostname[],int portnum,int *t)
99: {
100: struct sockaddr_in sa;
101: struct hostent *hp;
102: int s = 0;
103: PetscErrorCode ierr;
104: PetscBool flg = PETSC_TRUE;
105: static int refcnt = 0;
108: if (!(hp=gethostbyname(hostname))) {
109: perror("SEND: error gethostbyname: ");
110: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error open connection to %s",hostname);
111: }
112: PetscMemzero(&sa,sizeof(sa));
113: PetscMemcpy(&sa.sin_addr,hp->h_addr_list[0],hp->h_length);
115: sa.sin_family = hp->h_addrtype;
116: sa.sin_port = htons((u_short) portnum);
117: while (flg) {
118: if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) {
119: perror("SEND: error socket"); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
120: }
121: if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
122: #if defined(PETSC_HAVE_WSAGETLASTERROR)
123: WSAGetLastError();
124: if (ierr == WSAEADDRINUSE) (*PetscErrorPrintf)("SEND: address is in use\n");
125: else if (ierr == WSAEALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
126: else if (ierr == WSAEISCONN) {
127: (*PetscErrorPrintf)("SEND: socket already connected\n");
128: Sleep((unsigned) 1);
129: } else if (ierr == WSAECONNREFUSED) {
130: /* (*PetscErrorPrintf)("SEND: forcefully rejected\n"); */
131: Sleep((unsigned) 1);
132: } else {
133: perror(NULL); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
134: }
135: #else
136: if (errno == EADDRINUSE) (*PetscErrorPrintf)("SEND: address is in use\n");
137: else if (errno == EALREADY) (*PetscErrorPrintf)("SEND: socket is non-blocking \n");
138: else if (errno == EISCONN) {
139: (*PetscErrorPrintf)("SEND: socket already connected\n");
140: sleep((unsigned) 1);
141: } else if (errno == ECONNREFUSED) {
142: refcnt++;
143: if (refcnt > 5) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SYS,"Connection refused by remote host %s port %d",hostname,portnum);
144: PetscInfo(0,"Connection refused in attaching socket, trying again\n");
145: sleep((unsigned) 1);
146: } else {
147: perror(NULL); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"system error");
148: }
149: #endif
150: flg = PETSC_TRUE;
151: #if defined(PETSC_HAVE_CLOSESOCKET)
152: closesocket(s);
153: #else
154: close(s);
155: #endif
156: } else flg = PETSC_FALSE;
157: }
158: *t = s;
159: return(0);
160: }
162: #define MAXHOSTNAME 100
163: /*@C
164: PetscSocketEstablish - starts a listener on a socket
166: Input Parameters:
167: . portnumber - the port to wait at
169: Output Parameters:
170: . ss - the socket to be used with PetscSocketListen()
172: Level: advanced
174: .seealso: PetscSocketListen(), PetscOpenSocket()
176: @*/
177: PETSC_INTERN PetscErrorCode PetscSocketEstablish(int portnum,int *ss)
178: {
179: char myname[MAXHOSTNAME+1];
180: int s;
181: PetscErrorCode ierr;
182: struct sockaddr_in sa;
183: struct hostent *hp;
186: PetscGetHostName(myname,MAXHOSTNAME);
188: PetscMemzero(&sa,sizeof(struct sockaddr_in));
190: hp = gethostbyname(myname);
191: if (!hp) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Unable to get hostent information from system");
193: sa.sin_family = hp->h_addrtype;
194: sa.sin_port = htons((u_short)portnum);
196: if ((s = socket(AF_INET,SOCK_STREAM,0)) < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Error running socket() command");
197: #if defined(PETSC_HAVE_SO_REUSEADDR)
198: {
199: int optval = 1; /* Turn on the option */
200: setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char*)&optval,sizeof(optval));
201: }
202: #endif
204: while (bind(s,(struct sockaddr*)&sa,sizeof(sa)) < 0) {
205: #if defined(PETSC_HAVE_WSAGETLASTERROR)
206: WSAGetLastError();
207: if (ierr != WSAEADDRINUSE) {
208: #else
209: if (errno != EADDRINUSE) {
210: #endif
211: close(s);
212: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"Error from bind()");
213: }
214: }
215: listen(s,0);
216: *ss = s;
217: return(0);
218: }
220: /*@C
221: PetscSocketListen - Listens at a socket created with PetscSocketEstablish()
223: Input Parameter:
224: . listenport - obtained with PetscSocketEstablish()
226: Output Parameter:
227: . t - pass this to read() to read what is passed to this connection
229: Level: advanced
231: .seealso: PetscSocketEstablish()
232: @*/
233: PETSC_INTERN PetscErrorCode PetscSocketListen(int listenport,int *t)
234: {
235: struct sockaddr_in isa;
236: #if defined(PETSC_HAVE_ACCEPT_SIZE_T)
237: size_t i;
238: #else
239: int i;
240: #endif
243: /* wait for someone to try to connect */
244: i = sizeof(struct sockaddr_in);
245: if ((*t = accept(listenport,(struct sockaddr*)&isa,(socklen_t*)&i)) < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"error from accept()\n");
246: return(0);
247: }
249: /*@C
250: PetscViewerSocketOpen - Opens a connection to a MATLAB or other socket based server.
252: Collective on MPI_Comm
254: Input Parameters:
255: + comm - the MPI communicator
256: . machine - the machine the server is running on,, use NULL for the local machine, use "server" to passively wait for
257: a connection from elsewhere
258: - port - the port to connect to, use PETSC_DEFAULT for the default
260: Output Parameter:
261: . lab - a context to use when communicating with the server
263: Level: intermediate
265: Notes:
266: Most users should employ the following commands to access the
267: MATLAB PetscViewers
268: $
269: $ PetscViewerSocketOpen(MPI_Comm comm, char *machine,int port,PetscViewer &viewer)
270: $ MatView(Mat matrix,PetscViewer viewer)
271: $
272: $ or
273: $
274: $ PetscViewerSocketOpen(MPI_Comm comm,char *machine,int port,PetscViewer &viewer)
275: $ VecView(Vec vector,PetscViewer viewer)
277: Options Database Keys:
278: For use with PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF,
279: PETSC_VIEWER_SOCKET_() or if
280: NULL is passed for machine or PETSC_DEFAULT is passed for port
281: $ -viewer_socket_machine <machine>
282: $ -viewer_socket_port <port>
284: Environmental variables:
285: + PETSC_VIEWER_SOCKET_PORT portnumber
286: - PETSC_VIEWER_SOCKET_MACHINE machine name
288: Currently the only socket client available is MATLAB. See
289: src/dm/examples/tests/ex12.c and ex12.m for an example of usage.
291: Notes:
292: The socket viewer is in some sense a subclass of the binary viewer, to read and write to the socket
293: use PetscViewerBinaryRead(), PetscViewerBinaryWrite(), PetscViewerBinarWriteStringArray(), PetscViewerBinaryGetDescriptor().
295: Use this for communicating with an interactive MATLAB session, see PETSC_VIEWER_MATLAB_() for writing output to a
296: .mat file. Use PetscMatlabEngineCreate() or PETSC_MATLAB_ENGINE_(), PETSC_MATLAB_ENGINE_SELF, or PETSC_MATLAB_ENGINE_WORLD
297: for communicating with a MATLAB Engine
299: Concepts: MATLAB^sending data
300: Concepts: sockets^sending data
302: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerCreate(), PetscViewerSetType(),
303: PetscViewerSocketSetConnection(), PETSC_VIEWER_SOCKET_, PETSC_VIEWER_SOCKET_WORLD,
304: PETSC_VIEWER_SOCKET_SELF, PetscViewerBinaryWrite(), PetscViewerBinaryRead(), PetscViewerBinaryWriteStringArray(),
305: PetscBinaryViewerGetDescriptor(), PetscMatlabEngineCreate()
306: @*/
307: PetscErrorCode PetscViewerSocketOpen(MPI_Comm comm,const char machine[],int port,PetscViewer *lab)
308: {
312: PetscViewerCreate(comm,lab);
313: PetscViewerSetType(*lab,PETSCVIEWERSOCKET);
314: PetscViewerSocketSetConnection(*lab,machine,port);
315: return(0);
316: }
318: static PetscErrorCode PetscViewerSetFromOptions_Socket(PetscOptionItems *PetscOptionsObject,PetscViewer v)
319: {
321: PetscInt def = -1;
322: char sdef[256];
323: PetscBool tflg;
326: /*
327: These options are not processed here, they are processed in PetscViewerSocketSetConnection(), they
328: are listed here for the GUI to display
329: */
330: PetscOptionsHead(PetscOptionsObject,"Socket PetscViewer Options");
331: PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",sdef,16,&tflg);
332: if (tflg) {
333: PetscOptionsStringToInt(sdef,&def);
334: } else def = PETSCSOCKETDEFAULTPORT;
335: PetscOptionsInt("-viewer_socket_port","Port number to use for socket","PetscViewerSocketSetConnection",def,0,0);
337: PetscOptionsString("-viewer_socket_machine","Machine to use for socket","PetscViewerSocketSetConnection",sdef,0,0,0);
338: PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",sdef,256,&tflg);
339: if (!tflg) {
340: PetscGetHostName(sdef,256);
341: }
342: PetscOptionsTail();
343: return(0);
344: }
346: static PetscErrorCode PetscViewerBinaryGetSkipHeader_Socket(PetscViewer viewer,PetscBool *skip)
347: {
348: PetscViewer_Socket *vsocket = (PetscViewer_Socket*)viewer->data;
351: *skip = vsocket->skipheader;
352: return(0);
353: }
355: static PetscErrorCode PetscViewerBinarySetSkipHeader_Socket(PetscViewer viewer,PetscBool skip)
356: {
357: PetscViewer_Socket *vsocket = (PetscViewer_Socket*)viewer->data;
360: vsocket->skipheader = skip;
361: return(0);
362: }
364: static PetscErrorCode PetscViewerBinaryGetFlowControl_Socket(PetscViewer viewer,PetscInt *fc)
365: {
367: *fc = 0;
368: return(0);
369: }
371: /*MC
372: PETSCVIEWERSOCKET - A viewer that writes to a Unix socket
375: .seealso: PetscViewerSocketOpen(), PetscViewerDrawOpen(), PETSC_VIEWER_DRAW_(),PETSC_VIEWER_DRAW_SELF, PETSC_VIEWER_DRAW_WORLD,
376: PetscViewerCreate(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PETSCVIEWERBINARY, PETSCVIEWERDRAW,
377: PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB,
378: PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
380: Level: beginner
381: M*/
383: PETSC_EXTERN PetscErrorCode PetscViewerCreate_Socket(PetscViewer v)
384: {
385: PetscViewer_Socket *vmatlab;
386: PetscErrorCode ierr;
389: PetscNewLog(v,&vmatlab);
390: vmatlab->port = 0;
391: v->data = (void*)vmatlab;
392: v->ops->destroy = PetscViewerDestroy_Socket;
393: v->ops->flush = 0;
394: v->ops->setfromoptions = PetscViewerSetFromOptions_Socket;
396: /* lie and say this is a binary viewer; then all the XXXView_Binary() methods will work correctly on it */
397: PetscObjectChangeTypeName((PetscObject)v,PETSCVIEWERBINARY);
398: PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Socket);
399: PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Socket);
400: PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Socket);
402: return(0);
403: }
405: /*@C
406: PetscViewerSocketSetConnection - Sets the machine and port that a PETSc socket
407: viewer is to use
409: Logically Collective on PetscViewer
411: Input Parameters:
412: + v - viewer to connect
413: . machine - host to connect to, use NULL for the local machine,use "server" to passively wait for
414: a connection from elsewhere
415: - port - the port on the machine one is connecting to, use PETSC_DEFAULT for default
417: Level: advanced
419: .seealso: PetscViewerSocketOpen()
420: @*/
421: PetscErrorCode PetscViewerSocketSetConnection(PetscViewer v,const char machine[],int port)
422: {
423: PetscErrorCode ierr;
424: PetscMPIInt rank;
425: char mach[256];
426: PetscBool tflg;
427: PetscViewer_Socket *vmatlab = (PetscViewer_Socket*)v->data;
431: if (port <= 0) {
432: char portn[16];
433: PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_PORT",portn,16,&tflg);
434: if (tflg) {
435: PetscInt pport;
436: PetscOptionsStringToInt(portn,&pport);
437: port = (int)pport;
438: } else port = PETSCSOCKETDEFAULTPORT;
439: }
440: if (!machine) {
441: PetscOptionsGetenv(PetscObjectComm((PetscObject)v),"PETSC_VIEWER_SOCKET_MACHINE",mach,256,&tflg);
442: if (!tflg) {
443: PetscGetHostName(mach,256);
444: }
445: } else {
446: PetscStrncpy(mach,machine,256);
447: }
449: MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);
450: if (!rank) {
451: PetscStrcmp(mach,"server",&tflg);
452: if (tflg) {
453: int listenport;
454: PetscInfo1(v,"Waiting for connection from socket process on port %D\n",port);
455: PetscSocketEstablish(port,&listenport);
456: PetscSocketListen(listenport,&vmatlab->port);
457: close(listenport);
458: } else {
459: PetscInfo2(v,"Connecting to socket process on port %D machine %s\n",port,mach);
460: PetscOpenSocket(mach,port,&vmatlab->port);
461: }
462: }
463: return(0);
464: }
466: /* ---------------------------------------------------------------------*/
467: /*
468: The variable Petsc_Viewer_Socket_keyval is used to indicate an MPI attribute that
469: is attached to a communicator, in this case the attribute is a PetscViewer.
470: */
471: PetscMPIInt Petsc_Viewer_Socket_keyval = MPI_KEYVAL_INVALID;
474: /*@C
475: PETSC_VIEWER_SOCKET_ - Creates a socket viewer shared by all processors in a communicator.
477: Collective on MPI_Comm
479: Input Parameter:
480: . comm - the MPI communicator to share the socket PetscViewer
482: Level: intermediate
484: Options Database Keys:
485: For use with the default PETSC_VIEWER_SOCKET_WORLD or if
486: NULL is passed for machine or PETSC_DEFAULT is passed for port
487: $ -viewer_socket_machine <machine>
488: $ -viewer_socket_port <port>
490: Environmental variables:
491: + PETSC_VIEWER_SOCKET_PORT portnumber
492: - PETSC_VIEWER_SOCKET_MACHINE machine name
494: Notes:
495: Unlike almost all other PETSc routines, PetscViewer_SOCKET_ does not return
496: an error code. The socket PetscViewer is usually used in the form
497: $ XXXView(XXX object,PETSC_VIEWER_SOCKET_(comm));
499: Currently the only socket client available is MATLAB. See
500: src/dm/examples/tests/ex12.c and ex12.m for an example of usage.
502: Connects to a waiting socket and stays connected until PetscViewerDestroy() is called.
504: Use this for communicating with an interactive MATLAB session, see PETSC_VIEWER_MATLAB_() for writing output to a
505: .mat file. Use PetscMatlabEngineCreate() or PETSC_MATLAB_ENGINE_(), PETSC_MATLAB_ENGINE_SELF, or PETSC_MATLAB_ENGINE_WORLD
506: for communicating with a MATLAB Engine
508: .seealso: PETSC_VIEWER_SOCKET_WORLD, PETSC_VIEWER_SOCKET_SELF, PetscViewerSocketOpen(), PetscViewerCreate(),
509: PetscViewerSocketSetConnection(), PetscViewerDestroy(), PETSC_VIEWER_SOCKET_(), PetscViewerBinaryWrite(), PetscViewerBinaryRead(),
510: PetscViewerBinaryWriteStringArray(), PetscViewerBinaryGetDescriptor(), PETSC_VIEWER_MATLAB_()
511: @*/
512: PetscViewer PETSC_VIEWER_SOCKET_(MPI_Comm comm)
513: {
515: PetscBool flg;
516: PetscViewer viewer;
517: MPI_Comm ncomm;
520: PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
521: if (Petsc_Viewer_Socket_keyval == MPI_KEYVAL_INVALID) {
522: MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Socket_keyval,0);
523: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
524: }
525: MPI_Comm_get_attr(ncomm,Petsc_Viewer_Socket_keyval,(void**)&viewer,(int*)&flg);
526: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
527: if (!flg) { /* PetscViewer not yet created */
528: PetscViewerSocketOpen(ncomm,0,0,&viewer);
529: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
530: PetscObjectRegisterDestroy((PetscObject)viewer);
531: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
532: MPI_Comm_set_attr(ncomm,Petsc_Viewer_Socket_keyval,(void*)viewer);
533: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
534: }
535: PetscCommDestroy(&ncomm);
536: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SOCKET_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
537: PetscFunctionReturn(viewer);
538: }