001    package stringtransformers;
002    
003    import java.util.Vector;
004    import java.util.Enumeration;
005    import cs101.io.connection.InputConnection;
006    import cs101.io.connection.OutputConnection;
007    import cs101.io.connection.ConnectionRejectedException;
008    import cs101.io.Console;
009    
010    public class Broadcaster extends StringTransformer
011    {
012      private Vector outVector = new Vector();
013    
014      public Broadcaster ()
015      {
016        super();    
017      }
018    
019      //  public synchronized void addOutputConnection( OutputConnection out ) 
020    
021      // note the above was because Broadcaster's Vector might be changed
022      // while the act method is stepping through the enumerations, but
023      // caused a deadlock.  Is this a problem?
024    
025      public void addOutputConnection( OutputConnection out ) 
026           throws ConnectionRejectedException
027      {
028    
029        Console.print ( this + " adding ");
030        if ( this.outVector.isEmpty() )
031          {
032            this.outVector.addElement( this.visualizer.interceptOutput( out ) );
033            Console.print ( "first " );
034          }
035        else
036          {
037            this.outVector.addElement( out );
038            Console.print ( "subsequent " );
039          }
040        Console.println( "output connection");
041      }
042    
043      //  public synchronized void act () 
044      public void act () 
045      {
046        
047        if ( ( this.in != null ) && ( ! this.outVector.isEmpty() ) )
048          {
049            String string = this.transform( this.in.readInput() );
050    
051            Enumeration outs = this.outVector.elements();
052    
053            while ( outs.hasMoreElements() )
054              {
055                out = (OutputConnection) outs.nextElement();
056                out.writeOutput( string );
057              }
058          }
059      }
060    
061      public String transform( String input )
062      {
063        return input;
064      }
065    
066    }
067