home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / lg / issue29 / HAMILTON / Monitor.java < prev    next >
Encoding:
Java Source  |  1998-01-24  |  7.1 KB  |  149 lines

  1. package Jcd; // -- Listing-6 -- Monitor.java//   1
  2.                                             //   2
  3. import java.lang.*;                         //   3
  4. import java.util.*;                         //   4
  5. import Jcd.Drive;                           //   5
  6.                                             //   6
  7. class Monitor 
  8.   extends Observable implements Runnable {  //   7
  9.                                             //   8
  10.   // Updates once a second - calls client   //   9
  11.   // object's update methods so they can 
  12.   // reflect the updates.                   //  10
  13.                                             //  11
  14.   // The following public status info is 
  15.   // available for the client to use.
  16.                                             //  13
  17.   public Drive cdPlayer;                    //  14
  18.   public int status = Drive.STATUS_INVALID; //  15
  19.   public int currentTrack = 0;              //  16
  20.   public int currentIndex = 0;              //  17
  21.   public int currentAddress = 0;            //  18
  22.   public int trackStartAddress = 0;         //  19
  23.   public int trackElapsed = 0;              //  20
  24.                                             //  21
  25.   public boolean cdChanged = false;         //  22
  26.   public boolean trackChanged = false;      //  23
  27.                                             //  24
  28.   public int cdEndAddress = -1;             //  25
  29.   public int numberOfTracks = 0;            //  26
  30.     // Info origin [1], [0] is total for CD.// 27
  31.   public int trackAddress[] = {0};          //  28
  32.   public int trackEndAddress[] = {0};       //  29
  33.   public int trackLength[] = {0};           //  30
  34.                                             //  31
  35.   public String cddbID = "00000000";        //  32
  36.                                             //  33
  37.   protected int lastTrack = 0;              //  34
  38.   protected Thread updateThread = null;     //  35
  39.                                             //  36
  40.   public Monitor(Drive cdPlayer) {          //  37
  41.     this.cdPlayer = cdPlayer;               //  38
  42.   }                                         //  39
  43.                                             //  40
  44.   public void run() {                       //  41
  45.     for (;;) {                              //  42
  46.       int sleep = 1000;                     //  43
  47.       synchronized (cdPlayer) {             //  44
  48.         updateCdInfo();                     //  45
  49.         setChanged();                       //  46
  50.          // Force notify to do its thing.
  51.         notifyObservers();                  //  47
  52.       }                                     //  48
  53.       try {                                 //  49
  54.         Thread.sleep(sleep);                //  50
  55.       }                                     //  51
  56.       catch (InterruptedException e) {      //  52
  57.       }                                     //  53
  58.     }                                       //  54
  59.   }                                         //  55
  60.                                             //  56
  61.   public void start() {                     //  57
  62.                                             //  58
  63.     if (updateThread == null) {             //  59
  64.       System.out.println("Starting thread");//  60
  65.       updateThread = new Thread(this);      //  61
  66.              // Higher priority for console access
  67.       updateThread.setPriority(6);          //  62
  68.       updateThread.start();                 //  63
  69.     }                                       //  64
  70.   }                                         //  65
  71.                                             //  66
  72.   public void stop() {                      //  67
  73.     if (updateThread != null &&             //  68
  74.         updateThread.isAlive()) 
  75.       updateThread.stop();
  76.     updateThread = null;                    //  69
  77.   }                                         //  70
  78.                                             //  71
  79.   protected void updateCdInfo()             //  72
  80.   {                                         //  73
  81.     try {                                   //  74
  82.       cdChanged = false;                    //  75
  83.       trackChanged = false;                 //  76
  84.       status = cdPlayer.status();           //  77
  85.       if (status != Drive.STATUS_INVALID) { //  78
  86.         int cdEnd = cdPlayer.cdEndAddress();//  79
  87.                                             //  80
  88.         if (cdEnd != cdEndAddress) {        //  81
  89.           // Assumes we never get two cd's the  82
  90.           // exact same length in a row! Breaks 83
  91.           // if we do!          
  92.           cdEndAddress = cdEnd;             //  84
  93.           cdChanged = true;                 //  85
  94.           trackChanged = true;              //  86
  95.                                             //  87
  96.           cddbID = cdPlayer.cddbID();       //  88
  97.           numberOfTracks =                  //  89
  98.             cdPlayer.numberOfTracks();   
  99.                                             //  90
  100.           trackAddress= new int[numberOfTracks+1];
  101.           trackEndAddress =                 //  92
  102.             new int[numberOfTracks+1];     
  103.           trackLength = new int[numberOfTracks+1];
  104.                                             //  94
  105.           for (int i=1; i<=numberOfTracks; i++) {
  106.             trackAddress[i] =               //  96
  107.               cdPlayer.trackAddress(i);      
  108.             trackEndAddress[i] =            //  97
  109.               cdPlayer.trackEndAddress(i);  
  110.             trackLength[i] =                //  98
  111.               cdPlayer.trackLength(i);    
  112.           }                                 //  99
  113.           trackAddress[0] =                 // 100
  114.             cdPlayer.trackAddress(Drive.LEAD_OUT);
  115.           trackEndAddress[0] =              // 101
  116.             cdPlayer.trackEndAddress(
  117.               Drive.LEAD_OUT);
  118.           trackLength[0] =                  // 102
  119.             cdPlayer.trackLength(Drive.LEAD_OUT); 
  120.         }                                   // 103
  121.                                             // 104
  122.         currentTrack = cdPlayer.currentTrack();
  123.         if (currentTrack > numberOfTracks) {// 106 
  124.           // Fishy, probably at end of CD.  // 107
  125.           // Prevent subscript out of bounds.
  126.           currentTrack = numberOfTracks;   
  127.         }                                   // 108
  128.         if (currentTrack==0){ // Just in case. 109
  129.           currentTrack = 1;                 // 110
  130.         }                                   // 111
  131.         currentIndex = cdPlayer.currentIndex();
  132.         currentAddress= cdPlayer.currentAddress();
  133.         trackStartAddress =                 // 114  
  134.           cdPlayer.trackAddress(currentTrack);  
  135.         trackElapsed =                      // 115 
  136.           currentAddress - trackStartAddress;
  137.         if (trackElapsed<0) trackElapsed=0; // 116
  138.         if (currentTrack != lastTrack) {    // 117
  139.           trackChanged = true;              // 118
  140.           lastTrack = currentTrack;         // 119
  141.         }                                   // 120
  142.       }                                     // 121
  143.     }                                       // 122
  144.     catch (DriveException e) {              // 123
  145.       System.out.println("Monitor Exception "+e);
  146.     }                                       // 125
  147.   }                                         // 126
  148. }                                           // 127
  149.