//@author: j.n.magee 11/11/96

import java.awt.*;
import java.applet.*;

/********************************************************/

class Turnstile implements Runnable {

    private Channel chan_;
    private Character signal = new Character('#');

    public Turnstile (Channel c) {
        chan_=c;
    }

    public void run() {
          while(true)  {
            for(int i=0;i < 59;++i) DisplayThread.rotate();
            chan_.out(signal);
            DisplayThread.rotate();
          }
    }
}

/********************************************************/

class Count implements Runnable {
    private Channel east_;
    private Channel west_;
    private Counter count_;

    public Count(Channel east, Channel west, Counter count) {
        east_=east;
        west_=west;
        count_=count;
    }

    public void run() {
         while(true) {
            for(int i=0;i<15;i++) DisplayThread.rotate();
            east_.in();
            count_.inc();
            for(int i=0;i<15;i++) DisplayThread.rotate();
            west_.in();
            count_.inc();
        }
    }
}

 /********************************************************/



public class GardenMsg extends Applet {

    ThreadPanel thread1_;
    ThreadPanel thread2_;
    ThreadPanel thread3_;

    public void init() {
        setLayout(new BorderLayout());
        //counter
        TextCanvas t = new TextCanvas("Count");
        Counter c = new DisplayCounter(t,0);
        //channels
        ChannelCanvas e = new ChannelCanvas("East",false);
        ChannelCanvas w = new ChannelCanvas("West",true);
        Channel east = new DisplayChannel(e);
        Channel west = new DisplayChannel(w);

        Panel center = new Panel();
        center.add(w);
        center.add(t);
        center.add(e);
        add("Center",center);
        //threads
        Panel p = new Panel();
        p.add(thread3_=new ThreadPanel("West Gate",Color.blue,new Turnstile(west)));
        p.add(thread2_=new ThreadPanel("Counter",Color.yellow,new Count(east,west,c)));
        p.add(thread1_=new ThreadPanel("East Gate",Color.blue,new Turnstile(east)));
        add("South",p);
    }

    public void start() {
        super.start();
    }

    public void stop() {
        thread1_.passivate();
        thread2_.passivate();
        thread3_.passivate();
    }

    public void destroy() {
        thread1_.stop();
        thread2_.stop();
        thread3_.stop();
    }
}

