

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <exec/ports.h>
#include <exec/libraries.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/execbase.h>
#include <exec/devices.h>
#include <intuition/intuition.h>





struct NewWindow nf = {
	20,20,160,100,0,1,
	CLOSEWINDOW,
	WINDOWCLOSE|SMART_REFRESH|WINDOWSIZING|
	WINDOWDRAG|WINDOWDEPTH,NULL,NULL,
	(UBYTE *)"task1",NULL,NULL,
	0,0,0,0,WBENCHSCREEN
		      };	


struct Dato1
           {
            struct Message msgtk1;
            char *passa;
           };

extern int uscita;
struct Window *Fints1;
char *Buffer1[]={"esempio     ","applicazione",
		"multitasking"};	
void task1()
 {
  
  UBYTE ind=0;
  struct MsgPort *mpg,*rp1,*mp1;
  struct Dato1 *dt1,*msg1;
  struct IntuiMessage *msg;
  ULONG sigtk1,caso;  


Fints1 = (struct Window *)OpenWindow(&nf);
	if (Fints1 == 0)
		goto end;

mp1 = (struct MsgPort *)CreatePort("porta1",0);
      if (mp1==0)
         {
   Move(Fints1->RPort,10,10);	
   Text(Fints1->RPort,"niente porta1",13L);
   CloseWindow(Fints1);
   goto end;
         }
sigtk1 =(1 << mp1->mp_SigBit);

rp1=(struct MsgPort *)CreatePort("porta risp1",0);
      if (rp1==0)
         {
         Move(Fints1->RPort,10,10);	
         Text(Fints1->RPort,"niente reply1",13L);
         CloseWindow(Fints1); 
         DeletePort(mp1);
         goto end;
         }


dt1=(struct Dato1 *)AllocMem(sizeof(struct Dato1)
 			,MEMF_CLEAR|MEMF_PUBLIC);

        if (dt1 == 0)
          {
	 Move(Fints1->RPort,10,10);	
         Text(Fints1->RPort,"niente dato1",12L);
         CloseWindow(Fints1); 
         DeletePort(rp1); 
         DeletePort(mp1);
         goto end;
          }


dt1 ->msgtk1.mn_ReplyPort = rp1;



for (;;)
  {
   mpg = (struct MsgPort *)FindPort("porta2");
	if (mpg==0)
		{
   DeletePort(rp1);
   DeletePort(mp1);
   Move(Fints1->RPort,20,20);	
   Text(Fints1->RPort,"porta2 non trovata",18L);
   CloseWindow(Fints1); 
   FreeMem(dt1,sizeof(dt1));
   goto end;
		}

Move(Fints1->RPort,20,30);	
Text(Fints1->RPort,Buffer1[ind],12L);
dt1->passa =(char *)Buffer1[ind++];
PutMsg(mpg,dt1);
Move(Fints1->RPort,20,20);
Text(Fints1->RPort,"invio task2",11L);



      caso=Wait( sigtk1 | 
		1<<Fints1->UserPort->mp_SigBit);
      if (caso == sigtk1)
       { 
        Move(Fints1->RPort,20,40);	
        Text(Fints1->RPort,"ricevo task2",12L); 
        msg1=(struct Dato1 *)GetMsg(mp1);     
        ReplyMsg(msg1);
	Move(Fints1->RPort,30,50);
        Text(Fints1->RPort,msg1->passa,12L);
	  if (msg1->passa == (char *)NULL)
		{
         DeletePort(rp1);
	 DeletePort(mp1);
         Move(Fints1->RPort,20,60);	
         Text(Fints1->RPort,"chiudo task1",12L);
	 CloseWindow(Fints1);
	 FreeMem(dt1,sizeof(dt1));
	 goto end;	
		}
       }

      else
	{
	 msg=(struct IntuiMessage *)
		GetMsg(Fints1->UserPort);
	    if((msg->Class) == CLOSEWINDOW)
		{
    dt1->passa=(char *)NULL;
    mpg = (struct MsgPort *)FindPort("porta2");
    DeletePort(rp1);
    DeletePort(mp1);
    PutMsg(mpg,dt1);
    CloseWindow(Fints1);
    FreeMem(dt1,sizeof(dt1));
    uscita = 1;
    goto end;
                }      
       }

   if(ind==3)
	ind=0;
  }/* fine del for */    

end:

} /* fine del main */

