home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / Station.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  6.3 KB  |  204 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Station.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import java.io.*;
  49. import java.net.*;
  50. import java.util.Vector;
  51.  
  52.  
  53. /** The Station creates a DJ thread, 
  54.     then listens on a particular port and accepts incoming connections,
  55.     dispatching each one to its own Transmitter thread.
  56. */
  57. public class Station extends Thread {
  58.  
  59.     public static final String    kPropPort  = "port",
  60.                                 kPropName  = "name",
  61.                                 kPropGenre = "genre",
  62.                                 kPropDJ    = "dj",            // DJ class
  63.                                 kPropHost  = "host",        // (human) host of this station
  64.                                 kPropHostURL = "host-url",
  65.                                 kPropHomePage = "home-page";
  66.                                 
  67.     public static final int        kDefaultPort = 8000;
  68.     
  69.  
  70.     /** Creates a Station and starts its thread waiting for incoming connections.
  71.         @param  props  The Properties read from the station's configuration file. */
  72.     public Station( RadioProperties props ) throws IOException {
  73.         super("Station");
  74.         fProps = props;
  75.         
  76.         fPort = props.getIntProperty(kPropPort,kDefaultPort);
  77.         setName("Station "+getStationName());
  78.                 
  79.         fDJ = (DJ) props.getObjectProperty(kPropDJ);
  80.         if( fDJ == null )
  81.             fDJ = new FileDJ();
  82.         fDJ.setStation(this);
  83.         
  84.         start();
  85.     }
  86.     
  87.     
  88.     public void run( ) {
  89.         fDJ.start();
  90.         
  91.         ServerSocket socket = null;
  92.         try{
  93.             socket = new ServerSocket(fPort);
  94.             
  95.             LOG(1,"*** Up and running! Address is <"+getAddress().getHostName()+":"+getPort()+">");
  96.             while(true) {
  97.                 Socket inSocket = socket.accept();        // Blocks until client connects
  98.                 LOG(1,"Station received an incoming connection from "
  99.                             +inSocket.getInetAddress().getHostName()
  100.                             +" -- now "+(getTransmitterCount()+1)+" listeners");
  101.                 
  102.                 Transmitter trans = new Transmitter(fDJ,inSocket);
  103.                 fTransmitters.addElement(trans);
  104.                 fMaxTransmitters = Math.max(fMaxTransmitters, fTransmitters.size());
  105.                 trans.start();
  106.             }
  107.         }catch( IOException x ) {
  108.             System.err.println("Station died with exception:");
  109.             x.printStackTrace(System.err);
  110.         } finally {
  111.             if( socket != null ) {
  112.                 try{
  113.                     socket.close();
  114.                 }catch( IOException x ) {
  115.                 }
  116.             }
  117.         }
  118.     }
  119.     
  120.     
  121.     void transmitterStopped( Transmitter trans ) {
  122.         fTransmitters.removeElement(trans);
  123.         LOG(1,"Down to "+getTransmitterCount()+" listeners, from max of "+getMaxTransmitterCount());
  124.     }
  125.     
  126.     
  127.     // ACCESSORS:
  128.     
  129.     
  130.     /** Returns the Station's Properties object. */
  131.     public RadioProperties getProperties( ) {
  132.         return fProps;
  133.     }
  134.     
  135.     
  136.     /** Returns the name of the channel being served by this Station.
  137.         (The method getName is from Thread and returns the name of the thread.) */
  138.     public String getStationName( ) {
  139.         return fProps.getProperty(kPropName);
  140.     }
  141.     
  142.     
  143.     /** Returns the genre of the music being served by this Station. */
  144.     public String getGenre( ) {
  145.         return fProps.getProperty(kPropGenre);
  146.     }
  147.     
  148.     
  149.     /** Returns the address of this Station. */
  150.     public InetAddress getAddress( ) {
  151.         try{
  152.             return InetAddress.getLocalHost();
  153.         }catch( UnknownHostException x ) {
  154.             return null;
  155.         }
  156.     }
  157.     
  158.     
  159.     /** Returns the port number this Station is listening on. */
  160.     public int getPort( ) {
  161.         return fPort;
  162.     }
  163.     
  164.     
  165.     /** Returns the current number of active Transmitters. */
  166.     public int getTransmitterCount( ) {
  167.         return fTransmitters.size();
  168.     }
  169.     
  170.     
  171.     /** Returns the maximum number of active Transmitters
  172.         during the lifespan of this Station. */
  173.     public int getMaxTransmitterCount( ) {
  174.         return fMaxTransmitters;
  175.     }
  176.     
  177.     
  178.     /** Returns an array of all active Transmitters of this Station. */
  179.     public Transmitter[] getTransmitters( ) {
  180.         synchronized(fTransmitters) {
  181.             int n = fTransmitters.size();
  182.             Transmitter[] trans = new Transmitter[n];
  183.             fTransmitters.copyInto(trans);
  184.             return trans;
  185.         }
  186.     }
  187.     
  188.     
  189.     private void LOG( int level, String msg ) {
  190.         Radio.LOG(level,getName(),msg);
  191.     }
  192.     
  193.  
  194.     // INSTANCE DATA:
  195.     
  196.     
  197.     private RadioProperties fProps;
  198.     private DJ fDJ;
  199.     private int fPort;
  200.     private Vector fTransmitters = new Vector();
  201.     private int fMaxTransmitters;
  202.     
  203. }
  204.