Channels Next: NrpMachine Up: The SugarCubes Tool Box Previous: Introduction

Channels

The Channel  class defines unbounded fifo files.

class Channel
{
  protected Vector fifo = new Vector();

  public void put(Object obj){ fifo.addElement(obj); }

  public Object get(){ 
    Object res = fifo.firstElement(); 
    fifo.removeElementAt(0);
    return res;
  }

  public boolean isEmpty() { return fifo.isEmpty(); }
}

ChannelEnv  associates channels to names. The newChannel method always return a new channel with a unique name.

public class ChannelEnv
{   
  private Hashtable channelEnv  = new Hashtable();
  private int localNum = 0;

  public Channel getChannel(String name)
  {
     Channel c = (Channel)channelEnv.get(name);
     if (c==null){
       c = new Channel();
       channelEnv.put(name,c);
     }
     return c;
  }

  /* Strange name, is'nt it ? */
  public Channel newChannel(){
    return getChannel("@@@local@@@" + (localNum++));
  }
}