

#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>
#include <stdio.h>


extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;


struct NewWindow nf2 =
{
 340,
 50,
 180,
 100,
 0,
 1,
 CLOSEWINDOW,
 WINDOWCLOSE|ACTIVATE|WINDOWSIZING|WINDOWDRAG|
 SMART_REFRESH|WINDOWDEPTH,
 NULL,
 NULL,
 (UBYTE *)"Task2",
 NULL,
 NULL,
 0,
 0,
 0,
 0,
 WBENCHSCREEN
};



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

char *Buffer2[]={"esempio     ","applicazione",
		"multitasking"};

extern int uscita;
struct Window *Fints2;
struct IntuiMessage *msg;
APTR   OpenLibrary();

void task2()
 {
  
  UBYTE ind=0;
  struct MsgPort *mpg,*rp2,*mp2;
  struct Dato1 *dt1,*msg1;
  ULONG sigtk2,caso;  


GfxBase = (struct GfxBase *)
           OpenLibrary("graphics.library",0L);
	if (GfxBase==0)
		goto end;

IntuitionBase = (struct IntuitionBase *)
	   OpenLibrary("intuition.library",0L);
	if (IntuitionBase==0)
		goto end;

Fints2 = (struct Window *)OpenWindow(&nf2);
	if (Fints2 == NULL)
		goto end;

mp2 = (struct MsgPort *)CreatePort("porta2",0);
      if (mp2==0)
         {
          Move(Fints2->RPort,20,20);
          Text(Fints2->RPort,"niente porta2",13L);
          CloseWindow(Fints2);
          goto end;
         }
sigtk2 =(1 << mp2->mp_SigBit);

rp2=(struct MsgPort *)CreatePort("porta risp2",0);
      if (rp2==0)
         {
	  Move(Fints2->RPort,20,20);
          Text(Fints2->RPort,"niente reply2",13L);
          CloseWindow(Fints2);
          DeletePort(mp2);
          goto end;
         }


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

        if (dt1 == 0)
          {
	  Move(Fints2->RPort,20,20);
          Text(Fints2->RPort,"niente dato1",12L);
          CloseWindow(Fints2);
          DeletePort(rp2); 
          DeletePort(mp2);
          goto end;
          }


dt1 ->msgtk1.mn_ReplyPort = rp2;



for (;;)
  {
  	Move(Fints2->RPort,20,20);
        Text(Fints2->RPort,"wait prima",10L); 
        caso = Wait( sigtk2 
	     | 1<<Fints2->UserPort->mp_SigBit);
	Move(Fints2->RPort,20,40);
        Text(Fints2->RPort,"wait dopo",9L); 
      if (caso == sigtk2)
       { 
	Move(Fints2->RPort,20,20);
        Text(Fints2->RPort,"ricevo task1",12L); 
        msg1=(struct Dato1 *)GetMsg(mp2);     
        ReplyMsg(msg1);
        dt1->passa =Buffer2[ind++];        
	 if (msg1->passa == (char *)NULL)	
		{
                  DeletePort(rp2);
		  DeletePort(mp2);
		  CloseWindow(Fints2);
		  FreeMem(dt1,sizeof(dt1));
		  goto end;
		}      
      mpg = (struct MsgPort *)FindPort("porta1");
	if (mpg==0)
		{
  Move(Fints2->RPort,20,20);	
  Text(Fints2->RPort,"porta1 non trovata",18L);
  DeletePort(rp2);
  DeletePort(mp2);
  CloseWindow(Fints2); 
  FreeMem(dt1,sizeof(dt1));
  goto end;
		}

        PutMsg(mpg,dt1);
	Move(Fints2->RPort,20,60);	
        Text(Fints2->RPort,"invio task1",11L);
       }

      else
	{
	Move(Fints2->RPort,20,60);		
        Text(Fints2->RPort,"else",4L); 
        msg=(struct IntuiMessage *)
             GetMsg(Fints2->UserPort);
	    if((msg->Class) == CLOSEWINDOW)
		{
   dt1->passa=(char *)NULL;
   mpg = (struct MsgPort *)FindPort("porta1");
   PutMsg(mpg,dt1);
   DeletePort(rp2);
   DeletePort(mp2);
   CloseWindow(Fints2);
   FreeMem(dt1,sizeof(dt1));
   uscita = 1;
   goto end;
                }      
       }

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

