/* 
		
		include	"baselibs.i"
		include	"baselibs1.i"
		include	"baseio.i"
		include	"baseexec.i"
		include	"exec/nodes.i"
		include	"exec/lists.i"
		include "exec/ports.i"
		include	"libraries/dos.i"
		

	#define	LF,10
	#define	MEMF_PUBLIC,1
	#define	OLD_MODE,1005
	
    	STRUCTURE	XYMessage,MN_SIZE
    	UWORD          	xy_X
    	UWORD          	xy_Y
	LABEL		XYMessage_SIZE


	variables

	 main()
	
    	CPTR	xymp
	CPTR	xyreplymp		;Structure Messageport
    	CPTR	xymsg
	CPTR	msg		;Structure XYMessage
	CPTR	conhandle
    	int	portsig,usersig,signal
    	int	foundport		;ABORT = FALSE;
	int	flag
	int	msgxy_X,msgxy_Y
	int	ABORT
	int	signalbit
	byte	sigbit

	{
	calc	ABORT,=,0
	func	conhandle,=,fopen,<"CON:0/0/640/150/Message_Window">,OLD_MODE
	if	conhandle,==,0,0
		{
		print	<"no open CON:",LF>
		exit	0
		}
		ifend	0

	calc	_stdout,=,conhandle
    		func	xymsg,=,AllocMem,XYMessage_SIZE,MEMF_PUBLIC
    		if xymsg,==,0,1
		{
        		printf	<"Not enough memory for message structure",LF>
        	exit	30
    		}
		ifend	1
   	
	;/* In this example we set up the public XY port ourselves */

   		func	xymp,=,CreatePort,"xyport",0	
   		if xymp,==,0,2
		{
	       	printf	<"Couldn't create xyport",LF>	   /* so much for the example */
       		FreeMem	xymsg,XYMessage_SIZE
       		exit	30
   		}
		ifend	2
    	;/* The replyport we'll use to get response */

    		func	xyreplymp,=,CreatePort,"xyreplyport",0
    		if xyreplymp,==,0,3
		{
        	printf	<"Couldn't create xyreplyport",LF>
        	DeletePort	xymp
        	FreeMem	xymsg,XYMessage_SIZE
		exit	31 
   		}
		ifend	3

    		structput	xymsg,->,LN_TYPE,=,NT_MESSAGE
    		structput	xymsg,->,MN_LENGTH,=,XYMessage_SIZE
    		structput	xymsg,->,MN_REPLYPORT,=,xyreplymp
    		structput	xymsg,->,xy_X,=,10
    		structput	xymsg,->,xy_Y,=,20

    	ROUTINE		foundport,=,SafePutToPort,xymsg
	printf	<"port has found %lx",LF>,foundport
    	if foundport,==,0,4
		 {             /* couldn't find port */
        	DeletePort	xyreplymp
        	DeletePort	xymp
        	FreeMem		xymsg,XYMessage_SIZE
		exit	32
    		}
		ifend	4
    ;/* we just found the port we created ourselves and put a message to it! WOW*/
    ;/* let's pretend we're the other guy now */

    	printf	<"Exit with CTRL-F",LF>		 /* User can abort with CTRL-F */

    	func	msg,=,WaitPort,xymp
                    ;/* doesn't wait since there HAS to be a message */
    	func	msg,=,GetMsg,xymp
    	while msg,!=,0,5
	{

		structput	msgxy_X,=,msg,->,xy_X
		structput	msgxy_Y,=,msg,->,xy_Y
        	printf		<"x = %ld y = %ld",LF>,msgxy_X,msgxy_Y
		calc		msgxy_X,=,msgxy_X,+,50
		calc		msgxy_Y,=,msgxy_Y,+,50
        	structput	msg,->,xy_X,=,msgxy_X
		structput	msg,->,xy_Y,=,msgxy_Y
       
        	ReplyMsg	msg     /* reply to the sender */
		func	msg,=,GetMsg,xymp    		
		
	}	5
	printf	<"message %lx ",LF>,msg
    ;/* We put on the other hat again & wait for the reply to our message sent */
    ;/* We'll use Wait() for this one */    
	structput	sigbit,=,xyreplymp,->,MP_SIGBIT
    	calc	portsig,=,1,«,sigbit
    	calc	usersig,=,SIGBREAKF_CTRL_F	;          /* User can break with CTRL-F */
    	
	calc	flag,=,0
    	while	flag,==,0,7
	 {
	calc	signalbit,=,portsig,|,usersig
        func	signal,=,Wait,signalbit	     /* sleep 'till someone signals */
  	printf	<"signal %lx "LF>,signal
	calc	signalbit,=,signal,&,portsig
	printf	<"signalbit %lx = ",LF>,signalbit
        if signalbit,!=,0,8
	 	{               /* got a signal at the msgport */
		func	msg,=,GetMsg,xyreplymp
            	while	msg,!=,0,9
			{
		 
			structput	msgxy_X,=,msg,->,xy_X
			structput	msgxy_Y,=,msg,->,xy_Y
                	printf	<"X = %ld Y = %ld",LF>,msgxy_X,msgxy_Y
               ; /* no need to reply. WE have sent the message */
			func	msg,=,GetMsg,xyreplymp
        		}	9 
	 	}	
		ifend	8


	calc	signalbit,=,signal,&,usersig
        if signalbit,!=,0,11
		{
			func	msg,=,GetMsg,xyreplymp
	            	while	msg,!=,0,12
			{
			 func	msg,=,GetMsg,xyreplymp	  /* just in case */
			}	12
        	    	calc	ABORT,=,1
			
        	ifend	11

        if ABORT,==,1,14 
		{
	           ; /* this about sums it up. let's clean up */
            	DeletePort	xyreplymp	
            	DeletePort	xymp	      /* the `other guy's' port */
            	FreeMem		xymsg,XYMessage_SIZE
		fclose	conhandle
            	exit	0
        	}
		ifend	14
	}	7
	}

	VOID SafePutToPort,message
	CPTR	message
	CPTR	port
	{
    	Forbid()
	func	port,=,FindPort,"xyport"
	if port,!=,0,15
		{
	    	PutMsg	port,message
		}
		ifend	15
    	Permit()
    	return	port		/* If zero, the port has gone away */

   ; /* Once we do a Permit(), the port might go away and leave us with
    ; * an invalid port pointer. So we return just a BOOL to indicate whether
    ; * the message has been sent or not. 
    ; */
	}
	end